master
bicijinlian 6 years ago
parent 3e901db9ed
commit 241622c7b8

@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
@ -19,7 +20,6 @@
<Folder Include="ExpressionTree\" />
<Folder Include="LinqBase\" />
<Folder Include="LinqToDataSet\" />
<Folder Include="LinqToObject\" />
<Folder Include="LinqToSQL\" />
<Folder Include="LinqToXml\" />
<Folder Include="Util\" />

@ -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
{
/// <summary>
/// 基本项测试
/// 异常、空值等项
/// </summary>
public class LinqTest
{
[Fact]
public void Null_Test()
{
List<Person> person = null;
//对Linq操作符而言基本上数据源为Null时将引发异常。
Assert.ThrowsAny<ArgumentNullException>(() =>
{
var query = person.Where(p => p == null);
});
}
[Fact]
public void Count_0_Test()
{
List<Person> person = new List<Person>();
//数据源为没有任何内容项时,即 Count=0不会引发异常。
Action action = () =>
{
//查不到任何数据不返回null而是返回 Count=0的对象。
person.Where(p => p == null).ToList();
};
action.Should().NotThrow();
}
}
}

@ -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<Person> customers = new List<Person> {
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<Person> customers = new List<Person> {
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);
}
}
}
Loading…
Cancel
Save