|
|
|
@ -1,7 +1,9 @@
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Xunit;
|
|
|
|
|
using xUnitStudy.Model;
|
|
|
|
@ -21,8 +23,6 @@ namespace xUnitStudy.WebApi.Test
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Equal
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Equal_String_Test()
|
|
|
|
@ -607,6 +607,7 @@ namespace xUnitStudy.WebApi.Test
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Null|Empty
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Empty_Test()
|
|
|
|
|
{
|
|
|
|
@ -651,7 +652,9 @@ namespace xUnitStudy.WebApi.Test
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(new List<string>() { "first", "second" });
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Type|Assignable
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IsType_Test()
|
|
|
|
|
{
|
|
|
|
@ -689,8 +692,356 @@ namespace xUnitStudy.WebApi.Test
|
|
|
|
|
{
|
|
|
|
|
//可以从指定的类型派生
|
|
|
|
|
Assert.IsAssignableFrom<object>(22);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Person person = new Person() { Id = 1, FirstName = "first", LastName = "last" };
|
|
|
|
|
Assert. //接口派生
|
|
|
|
|
IsAssignableFrom<IPerson>(person);
|
|
|
|
|
|
|
|
|
|
Teacher teacher = new Teacher() { Id = 1, FirstName = "first", LastName = "last", Grade = 2 };
|
|
|
|
|
Assert. //类继承
|
|
|
|
|
IsAssignableFrom<Person>(teacher);
|
|
|
|
|
|
|
|
|
|
Assert //接口派生
|
|
|
|
|
.IsAssignableFrom<IPerson>(teacher);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert //万源归宗
|
|
|
|
|
.IsAssignableFrom<object>(teacher);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 断言非派生、继承关系
|
|
|
|
|
/// Assert.False(obj is Type)替代
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IsNotAssignableFrom_Test()
|
|
|
|
|
{
|
|
|
|
|
// Assert.False(obj is Type) 替代
|
|
|
|
|
|
|
|
|
|
//可以从指定的类型派生
|
|
|
|
|
Assert.False(88 is String);
|
|
|
|
|
Assert.False(new object() is int);
|
|
|
|
|
|
|
|
|
|
Person person = new Person() { Id = 1, FirstName = "first", LastName = "last" };
|
|
|
|
|
Teacher teacher = new Teacher() { Id = 2, FirstName = "first", LastName = "last", Grade = 2 };
|
|
|
|
|
IPerson person2 = new Person() { Id = 3, FirstName = "first", LastName = "last" };
|
|
|
|
|
|
|
|
|
|
Assert.False(person is Teacher);
|
|
|
|
|
Assert.False(person2 is Teacher);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Contains For String
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Contains_String_Test()
|
|
|
|
|
{
|
|
|
|
|
Assert.Contains("hello", "hello,world.");
|
|
|
|
|
Assert.Contains("llo", "hello,world.");
|
|
|
|
|
Assert.Contains("world", "hello,world.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Contains_String_Comparison_Ordinal_Test()
|
|
|
|
|
{
|
|
|
|
|
//StringComparison.Ordinal 二进制比较:最快,最严格
|
|
|
|
|
//进行非语言(non-linguistic)上的比较,对两个字符串进行byte级别的比较,比较结果严格而准确,性能非常好。
|
|
|
|
|
Assert.Contains("hello", "hello,world", StringComparison.Ordinal);
|
|
|
|
|
Assert.Contains("llo", "hello,world", StringComparison.Ordinal);
|
|
|
|
|
Assert.Contains("orld", "hello,world", StringComparison.Ordinal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Contains_String_Comparison_OrdinalIgnoreCase_Test()
|
|
|
|
|
{
|
|
|
|
|
//StringComparison.OrdinalIgnoreCase与StringComparison.Ordinal相同,只是不区分大小写
|
|
|
|
|
Assert.Contains("hello", "Hello,World", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
Assert.Contains("llo", "Hello,World", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
Assert.Contains("orld", "Hello,World", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Contains_String_Comparison_CurrentCulture_Test()
|
|
|
|
|
{
|
|
|
|
|
//StringComparison.CurrentCulture 默认的比较方式:使用区域敏感排序规则和当前区域比较字符串
|
|
|
|
|
//在当前的区域信息下进行比较,这是String.Compare在没有指定StringComparison的时候默认的比较方式
|
|
|
|
|
|
|
|
|
|
Assert.Contains("", "", StringComparison.CurrentCulture);
|
|
|
|
|
Assert.Contains("orl", "hello,world", StringComparison.CurrentCulture);
|
|
|
|
|
|
|
|
|
|
//设置特定区域
|
|
|
|
|
string s1 = "encyclopædia";
|
|
|
|
|
string s2 = "encyclopaedia";
|
|
|
|
|
|
|
|
|
|
//当前的区域信息是美国:s1与s2是相等的。
|
|
|
|
|
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
|
|
|
|
|
Assert.Contains(s1, s2, StringComparison.CurrentCulture);
|
|
|
|
|
|
|
|
|
|
//当前的区域信息是se-SE,s1与s2是相等的
|
|
|
|
|
Thread.CurrentThread.CurrentCulture = new CultureInfo("se-SE");
|
|
|
|
|
Assert.DoesNotContain(s1, s2, StringComparison.CurrentCulture);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Contains_String_Comparison_CurrentCultureIgnoreCase_Test()
|
|
|
|
|
{
|
|
|
|
|
//用法同:StringComparison.CurrentCulture,只是不区分大小写
|
|
|
|
|
|
|
|
|
|
Assert.Contains("A", "aBB", StringComparison.CurrentCultureIgnoreCase);
|
|
|
|
|
Assert.Contains("worl", "Hello,World", StringComparison.CurrentCultureIgnoreCase);
|
|
|
|
|
|
|
|
|
|
//设置特定区域
|
|
|
|
|
string s1 = "Encyclopædia";
|
|
|
|
|
string s2 = "encyclopaediA";
|
|
|
|
|
|
|
|
|
|
//当前的区域信息是美国:s1与s2是相等的。
|
|
|
|
|
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
|
|
|
|
|
Assert.Contains(s1, s2, StringComparison.CurrentCultureIgnoreCase);
|
|
|
|
|
|
|
|
|
|
//当前的区域信息是se-SE,s1与s2是相等的
|
|
|
|
|
Thread.CurrentThread.CurrentCulture = new CultureInfo("se-SE");
|
|
|
|
|
Assert.DoesNotContain(s1, s2, StringComparison.CurrentCultureIgnoreCase);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Contains_String_Comparison_InvariantCulture_Test()
|
|
|
|
|
{
|
|
|
|
|
//StringComparison.InvariantCulture:使用区域敏感排序规则和固定区域比较字符串
|
|
|
|
|
//在任何系统中(不同的culture)比较都将得到相同的结果,他是使用CultureInfo.InvariantCulture的静态成员CompareInfo来进行比较操作的.
|
|
|
|
|
//StringComparison.InvariantCultureIgnoreCase与StringComparison.InvariantCulture,只是忽略大小写
|
|
|
|
|
|
|
|
|
|
Assert.Contains("ping", "ping", StringComparison.InvariantCulture);
|
|
|
|
|
Assert.Contains("p", "ping", StringComparison.InvariantCulture);
|
|
|
|
|
Assert.Contains("in", "ping", StringComparison.InvariantCulture);
|
|
|
|
|
Assert.Contains("ing", "ping", StringComparison.InvariantCulture);
|
|
|
|
|
|
|
|
|
|
//区域无关性
|
|
|
|
|
string s1 = "encyclopædia";
|
|
|
|
|
string s2 = "encyclopaedia";
|
|
|
|
|
|
|
|
|
|
Assert.Contains(s1, s2, StringComparison.InvariantCulture);
|
|
|
|
|
|
|
|
|
|
//设置区为美国:无效
|
|
|
|
|
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
|
|
|
|
|
Assert.Contains(s1, s2, StringComparison.InvariantCulture);
|
|
|
|
|
|
|
|
|
|
//设置区为se-SE:无效
|
|
|
|
|
Thread.CurrentThread.CurrentCulture = new CultureInfo("se-SE");
|
|
|
|
|
Assert.Contains(s1, s2, StringComparison.InvariantCulture);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Contains_String_Comparison_InvariantCultureIgnoreCase_Test()
|
|
|
|
|
{
|
|
|
|
|
//StringComparison.InvariantCultureIgnoreCase与StringComparison.InvariantCulture,只是忽略大小写
|
|
|
|
|
|
|
|
|
|
Assert.Contains("ping", "PinG", StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
Assert.Contains("p", "PinG", StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
Assert.Contains("in", "PinG", StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
Assert.Contains("ing", "PinG", StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
|
|
|
|
|
//区域无关性
|
|
|
|
|
string s1 = "Encyclopædia";
|
|
|
|
|
string s2 = "encyclopaediA";
|
|
|
|
|
|
|
|
|
|
Assert.Contains(s1, s2, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
|
|
|
|
|
//设置区为美国:无效
|
|
|
|
|
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
|
|
|
|
|
Assert.Contains(s1, s2, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
|
|
|
|
|
//设置区为se-SE:无效
|
|
|
|
|
Thread.CurrentThread.CurrentCulture = new CultureInfo("se-SE");
|
|
|
|
|
Assert.Contains(s1, s2, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Contains Collection
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Contains_Generic_In_IEnumerable_Test()
|
|
|
|
|
{
|
|
|
|
|
//字符数组
|
|
|
|
|
var stringArray = new List<string>() { "first", "second", "third", "four" };
|
|
|
|
|
Assert.Contains<string>("first", stringArray);
|
|
|
|
|
Assert //可以简化写法
|
|
|
|
|
.Contains("second", stringArray);
|
|
|
|
|
|
|
|
|
|
//int 数组
|
|
|
|
|
var intArray = new List<int>() { 1, 2, 3, 4, 5 };
|
|
|
|
|
Assert.Contains<int>(2, intArray);
|
|
|
|
|
Assert //可以简化写法
|
|
|
|
|
.Contains(3, intArray);
|
|
|
|
|
|
|
|
|
|
//类组
|
|
|
|
|
|
|
|
|
|
//内部各项都相等,但实例(引用)不同,则不包含
|
|
|
|
|
var classArray = new List<Person>()
|
|
|
|
|
{
|
|
|
|
|
new Person(){ Id=1, FirstName="first1", LastName="last1"},
|
|
|
|
|
new Person(){ Id=2, FirstName="first2", LastName="last2"},
|
|
|
|
|
new Person(){ Id=3, FirstName="first3", LastName="last3"},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Person person1 = new Person() { Id = 1, FirstName = "first1", LastName = "last1" };
|
|
|
|
|
Person person2 = new Person() { Id = 2, FirstName = "first2", LastName = "last2" };
|
|
|
|
|
Person person3 = new Person() { Id = 3, FirstName = "first3", LastName = "last3" };
|
|
|
|
|
|
|
|
|
|
Assert.DoesNotContain<Person>(person1, classArray);
|
|
|
|
|
Assert //简化写法
|
|
|
|
|
.DoesNotContain(person2, classArray);
|
|
|
|
|
Assert //简化写法
|
|
|
|
|
.DoesNotContain(person3, classArray);
|
|
|
|
|
|
|
|
|
|
//相同的引用才包含
|
|
|
|
|
classArray.Add(person1);
|
|
|
|
|
classArray.Add(person2);
|
|
|
|
|
classArray.Add(person3);
|
|
|
|
|
|
|
|
|
|
Assert.Contains(person1, classArray);
|
|
|
|
|
Assert.Contains(person2, classArray);
|
|
|
|
|
Assert.Contains(person3, classArray);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Contains_Generic_In_IEnumerable_IEqualityComparer_Test()
|
|
|
|
|
{
|
|
|
|
|
//以 IEqualityComparer 比较器为断言依据
|
|
|
|
|
|
|
|
|
|
Person person1 = new Person() { Id = 1, FirstName = "first1", LastName = "last1" };
|
|
|
|
|
Person person2 = new Person() { Id = 2, FirstName = "first2", LastName = "last2" };
|
|
|
|
|
Person person3 = new Person() { Id = 3, FirstName = "first3", LastName = "last3" };
|
|
|
|
|
|
|
|
|
|
var classArray = new List<Person>()
|
|
|
|
|
{
|
|
|
|
|
new Person() { Id = 1, FirstName = "ab", LastName = "cd" },
|
|
|
|
|
new Person() { Id = 2, FirstName = "ef", LastName = "g" },
|
|
|
|
|
new Person() { Id = 3, FirstName = "s", LastName = "b" },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//自定义比较器
|
|
|
|
|
IEqualityComparer<Person> equalityComparer = new Person();
|
|
|
|
|
|
|
|
|
|
Assert.Contains(person1, classArray, equalityComparer);
|
|
|
|
|
Assert.Contains(person2, classArray, equalityComparer);
|
|
|
|
|
Assert.Contains(person3, classArray, equalityComparer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Contains_Generic_IEnumerable_Filter_Test()
|
|
|
|
|
{
|
|
|
|
|
Person person1 = new Person() { Id = 1, FirstName = "first1", LastName = "last1" };
|
|
|
|
|
Person person2 = new Person() { Id = 2, FirstName = "first2", LastName = "last2" };
|
|
|
|
|
Person person3 = new Person() { Id = 3, FirstName = "first3", LastName = "last3" };
|
|
|
|
|
|
|
|
|
|
var classArray = new List<Person>()
|
|
|
|
|
{
|
|
|
|
|
person1,
|
|
|
|
|
person2,
|
|
|
|
|
person3,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//Predicate<T> 委托(入参T,返回 bool),作为筛选条件
|
|
|
|
|
Assert.Contains<Person>(classArray, person => person.Id == 1);
|
|
|
|
|
Assert //简写为
|
|
|
|
|
.Contains<Person>(classArray, person => person.FirstName == "first1");
|
|
|
|
|
Assert //简写为
|
|
|
|
|
.Contains<Person>(classArray, person => person.Id == 3 && person.LastName=="last3");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Contains_Generic_In_IDictionary_Test()
|
|
|
|
|
{
|
|
|
|
|
//字符串
|
|
|
|
|
KeyValuePair<string, string> sring_kv1 = new KeyValuePair<string, string>("key1", "first");
|
|
|
|
|
KeyValuePair<string, string> string_kv2 = new KeyValuePair<string, string>("key2", "second");
|
|
|
|
|
KeyValuePair<string, string> string_kv3 = new KeyValuePair<string, string>("key3", "third");
|
|
|
|
|
|
|
|
|
|
IDictionary<string, string> stringDic = new Dictionary<string, string>();
|
|
|
|
|
stringDic.Add("key1", "first");
|
|
|
|
|
stringDic.Add("key2", "second");
|
|
|
|
|
stringDic.Add("key3", "third");
|
|
|
|
|
|
|
|
|
|
Assert.Contains<string,string>("key1", stringDic);
|
|
|
|
|
Assert.Contains<KeyValuePair<string, string>>(sring_kv1, stringDic);
|
|
|
|
|
|
|
|
|
|
//数字
|
|
|
|
|
KeyValuePair<string, int> int_kv1 = new KeyValuePair<string, int>("key1", 1);
|
|
|
|
|
KeyValuePair<string, int> int_kv2 = new KeyValuePair<string, int>("key2", 2);
|
|
|
|
|
KeyValuePair<string, int> int_kv3 = new KeyValuePair<string, int>("key3", 3);
|
|
|
|
|
|
|
|
|
|
IDictionary<string, int> intDic = new Dictionary<string, int>();
|
|
|
|
|
intDic.Add("key1", 1);
|
|
|
|
|
intDic.Add("key2", 2);
|
|
|
|
|
intDic.Add("key3", 3);
|
|
|
|
|
|
|
|
|
|
Assert.Contains<string,int>("key1", intDic);
|
|
|
|
|
Assert.Contains<KeyValuePair<string, int>>(int_kv1, intDic);
|
|
|
|
|
|
|
|
|
|
//类
|
|
|
|
|
Person person1 = new Person() { Id=1,FirstName="first1",LastName="last1"};
|
|
|
|
|
Person person2 = new Person() { Id=2,FirstName="first2",LastName="last2"};
|
|
|
|
|
Person person3 = new Person() { Id=3,FirstName="first3",LastName="last3"};
|
|
|
|
|
|
|
|
|
|
KeyValuePair<string, Person> class_kv1 = new KeyValuePair<string, Person>("key1", person1);
|
|
|
|
|
KeyValuePair<string, Person> class_kv2 = new KeyValuePair<string, Person>("key2", person2);
|
|
|
|
|
KeyValuePair<string, Person> class_kv3 = new KeyValuePair<string, Person>("key3", person3);
|
|
|
|
|
|
|
|
|
|
IDictionary<string, Person> classDic = new Dictionary<string, Person>();
|
|
|
|
|
classDic.Add("key1", person1);
|
|
|
|
|
classDic.Add("key2", person2);
|
|
|
|
|
classDic.Add("key3", person3);
|
|
|
|
|
|
|
|
|
|
Assert.Contains<string,Person>("key1", classDic);
|
|
|
|
|
Assert.Contains<KeyValuePair<string,Person>>(class_kv1,classDic);
|
|
|
|
|
Assert.Contains(class_kv2,classDic);
|
|
|
|
|
Assert.Contains(class_kv3,classDic);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Contains_Generic_In_IReadOnlyDictionary_Test()
|
|
|
|
|
{
|
|
|
|
|
//类似 IDictionary
|
|
|
|
|
|
|
|
|
|
//字符串
|
|
|
|
|
KeyValuePair<string, string> sring_kv1 = new KeyValuePair<string, string>("key1", "first");
|
|
|
|
|
KeyValuePair<string, string> string_kv2 = new KeyValuePair<string, string>("key2", "second");
|
|
|
|
|
KeyValuePair<string, string> string_kv3 = new KeyValuePair<string, string>("key3", "third");
|
|
|
|
|
|
|
|
|
|
IDictionary<string, string> stringDic = new Dictionary<string, string>();
|
|
|
|
|
stringDic.Add("key1", "first");
|
|
|
|
|
stringDic.Add("key2", "second");
|
|
|
|
|
stringDic.Add("key3", "third");
|
|
|
|
|
|
|
|
|
|
IReadOnlyDictionary<string,string> stringReadOnlyDic = stringDic as IReadOnlyDictionary<string,string>;
|
|
|
|
|
|
|
|
|
|
Assert.Contains<string, string>("key1", stringReadOnlyDic);
|
|
|
|
|
Assert.Contains<KeyValuePair<string, string>>(sring_kv1, stringReadOnlyDic);
|
|
|
|
|
|
|
|
|
|
//类
|
|
|
|
|
Person person1 = new Person() { Id = 1, FirstName = "first1", LastName = "last1" };
|
|
|
|
|
Person person2 = new Person() { Id = 2, FirstName = "first2", LastName = "last2" };
|
|
|
|
|
Person person3 = new Person() { Id = 3, FirstName = "first3", LastName = "last3" };
|
|
|
|
|
|
|
|
|
|
KeyValuePair<string, Person> class_kv1 = new KeyValuePair<string, Person>("key1", person1);
|
|
|
|
|
KeyValuePair<string, Person> class_kv2 = new KeyValuePair<string, Person>("key2", person2);
|
|
|
|
|
KeyValuePair<string, Person> class_kv3 = new KeyValuePair<string, Person>("key3", person3);
|
|
|
|
|
|
|
|
|
|
IDictionary<string, Person> classDic = new Dictionary<string, Person>();
|
|
|
|
|
classDic.Add("key1", person1);
|
|
|
|
|
classDic.Add("key2", person2);
|
|
|
|
|
classDic.Add("key3", person3);
|
|
|
|
|
|
|
|
|
|
IReadOnlyDictionary<string,Person> classReadOnlyDic = classDic as IReadOnlyDictionary<string,Person>;
|
|
|
|
|
|
|
|
|
|
Assert.Contains<string, Person>("key1", classReadOnlyDic);
|
|
|
|
|
Assert.Contains<KeyValuePair<string, Person>>(class_kv1, classReadOnlyDic);
|
|
|
|
|
Assert.Contains(class_kv2, classReadOnlyDic);
|
|
|
|
|
Assert.Contains(class_kv3,classReadOnlyDic);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void All_Test()
|
|
|
|
|
{
|
|
|
|
|