using System; using System.Linq; using Developpez.Dotnet.Linq; using NUnit.Framework; namespace Developpez.Dotnet.Tests.Linq { [TestFixture] public class LinqHelperTests { #region Dummy properties and methods public int Foo { get; set; } public int Bar(int x) { return x; } public void Baz(int x) { } #endregion [Test] public void Test_GetPropertyName() { string expected = "Foo"; string actual = LinqHelper.GetPropertyName(() => Foo); Assert.AreEqual(actual, expected); Assert.Throws( () => actual = LinqHelper.GetPropertyName(() => Bar(42))); } [Test] public void Test_GetMethodName() { string expected = "Bar"; string actual = LinqHelper.GetMethodName(() => Bar(42)); Assert.AreEqual(actual, expected); expected = "Baz"; actual = LinqHelper.GetMethodName(() => Baz(42)); Assert.AreEqual(actual, expected); Assert.Throws( () => actual = LinqHelper.GetMethodName(() => Foo)); } [Test] public void Test_Generate() { var fibonacci = LinqHelper.Generate( new { n = 1, value = 0, next = 1 }, s => s.n <= 8, s => new { n = s.n + 1, value = s.next, next = s.value + s.next }, s => s.value); var expected = new[] { 0, 1, 1, 2, 3, 5, 8, 13 }; var actual = fibonacci.ToArray(); Assert.That(actual, Is.EquivalentTo(expected)); } } }