diff --git a/LinqStudy.Test/LinqStudy.Test.csproj b/LinqStudy.Test/LinqStudy.Test.csproj index de4eab1..eb3dddd 100644 --- a/LinqStudy.Test/LinqStudy.Test.csproj +++ b/LinqStudy.Test/LinqStudy.Test.csproj @@ -7,6 +7,7 @@ + @@ -19,7 +20,6 @@ - diff --git a/LinqStudy.Test/LinqToObject/LinqTest.cs b/LinqStudy.Test/LinqToObject/LinqTest.cs new file mode 100644 index 0000000..b955451 --- /dev/null +++ b/LinqStudy.Test/LinqToObject/LinqTest.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using System.Linq.Expressions; +using FluentAssertions; + +using Xunit; + +namespace LinqStudy.Test.LinqToObject +{ + /// + /// 基本项测试 + /// 异常、空值等项 + /// + public class LinqTest + { + [Fact] + public void Null_Test() + { + List person = null; + + //对Linq操作符而言,基本上数据源为Null时,将引发异常。 + Assert.ThrowsAny(() => + { + var query = person.Where(p => p == null); + }); + } + + [Fact] + public void Count_0_Test() + { + List person = new List(); + + //数据源为没有任何内容项时,即 Count=0,不会引发异常。 + Action action = () => + { + //查不到任何数据,不返回null,而是返回 Count=0的对象。 + person.Where(p => p == null).ToList(); + }; + + action.Should().NotThrow(); + } + } +} diff --git a/LinqStudy.Test/LinqToObject/TakeTest.cs b/LinqStudy.Test/LinqToObject/TakeTest.cs new file mode 100644 index 0000000..faee9ce --- /dev/null +++ b/LinqStudy.Test/LinqToObject/TakeTest.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using System.Linq.Expressions; + +using Xunit; + +namespace LinqStudy.Test.LinqToObject +{ + public class TakeTest + { + [Fact] + public void TakeWhile_Test() + { + List customers = new List { + new Person { Id=1,Name="woody1"}, + new Person { Id=2,Name="woody2"}, + new Person { Id=3,Name="woody3"}, + new Person { Id=4,Name="woody1"} + }; + + var person = customers.TakeWhile(c => c.Name.StartsWith("woody1")); + + //因为匹配第二项时,结果是False,程序返回,不再进行下次迭代,所以返回集合中只有第一项。 + Assert.Equal(customers.FindAll(q => q.Id == 1), person); } + + [Fact] + public void Test() + { + List customers = new List { + new Person { Id=1,Name="woody1"}, + new Person { Id=2,Name="woody2"}, + new Person { Id=3,Name="woody3"}, + new Person { Id=4,Name="woody1"} + }; + + var person = customers.TakeWhile(c => c.Name.StartsWith("woody2")); + + //因为匹配第一项时,结果是False,程序直接返回,不再进行下次迭代,所以返回空集合。 + Assert.Empty(person); + } + } +}