|
|
@ -354,7 +354,7 @@ namespace xUnitStudy.WebApi.Test
|
|
|
|
//对实现IEnumerable接口的对象进行 Equal比较
|
|
|
|
//对实现IEnumerable接口的对象进行 Equal比较
|
|
|
|
//形如:NotEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual)
|
|
|
|
//形如:NotEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual)
|
|
|
|
|
|
|
|
|
|
|
|
List<string> expected = new List<string>() { "first", "second","third" };
|
|
|
|
List<string> expected = new List<string>() { "first", "second", "third" };
|
|
|
|
List<string> actual = new List<string>() { "first", "second" };
|
|
|
|
List<string> actual = new List<string>() { "first", "second" };
|
|
|
|
|
|
|
|
|
|
|
|
Assert.NotEqual<List<string>>(expected, actual);
|
|
|
|
Assert.NotEqual<List<string>>(expected, actual);
|
|
|
@ -393,6 +393,42 @@ namespace xUnitStudy.WebApi.Test
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region StrictEqual
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void StrictEqual_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//使用类型的默认比较器验证两个对象是否严格相等,用法类似Equal<T>
|
|
|
|
|
|
|
|
//不支持自定义IEqualityComparer<T>
|
|
|
|
|
|
|
|
Assert.StrictEqual<decimal>(1L, 1m);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Person person1 = new Person() { Id = 2 };
|
|
|
|
|
|
|
|
Person person2 = new Person() { Id = 2 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Person person3 = new Person() { Id = 1 };
|
|
|
|
|
|
|
|
IPerson person4 = person3;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.StrictEqual(person3, person4);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void NotStrictEqual_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//使用类型的默认比较器验证两个对象是否严格相等,用法类似NotEqual<T>
|
|
|
|
|
|
|
|
//不支持自定义IEqualityComparer<T>
|
|
|
|
|
|
|
|
Assert.NotStrictEqual<decimal>(2.55M, 3.55M);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Person person1 = new Person() { Id = 2 };
|
|
|
|
|
|
|
|
Person person2 = new Person() { Id = 2 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Person person3 = new Person() { Id = 1 };
|
|
|
|
|
|
|
|
IPerson person4 = person1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.NotStrictEqual(person1, person2);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Same
|
|
|
|
#region Same
|
|
|
|
[Fact]
|
|
|
|
[Fact]
|
|
|
|
public void Same_String_Test()
|
|
|
|
public void Same_String_Test()
|
|
|
@ -517,7 +553,7 @@ namespace xUnitStudy.WebApi.Test
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Assert.False(false);
|
|
|
|
Assert.False(false);
|
|
|
|
Assert.False("ping" == "ack");
|
|
|
|
Assert.False("ping" == "ack");
|
|
|
|
Assert.False(3 == 1+1);
|
|
|
|
Assert.False(3 == 1 + 1);
|
|
|
|
Assert.False("ping" == "ping" && 3 == 1 + 1);
|
|
|
|
Assert.False("ping" == "ping" && 3 == 1 + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -946,7 +982,7 @@ namespace xUnitStudy.WebApi.Test
|
|
|
|
Assert //简写为
|
|
|
|
Assert //简写为
|
|
|
|
.Contains<Person>(classArray, person => person.FirstName == "first1");
|
|
|
|
.Contains<Person>(classArray, person => person.FirstName == "first1");
|
|
|
|
Assert //简写为
|
|
|
|
Assert //简写为
|
|
|
|
.Contains<Person>(classArray, person => person.Id == 3 && person.LastName=="last3");
|
|
|
|
.Contains<Person>(classArray, person => person.Id == 3 && person.LastName == "last3");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
[Fact]
|
|
|
@ -962,7 +998,7 @@ namespace xUnitStudy.WebApi.Test
|
|
|
|
stringDic.Add("key2", "second");
|
|
|
|
stringDic.Add("key2", "second");
|
|
|
|
stringDic.Add("key3", "third");
|
|
|
|
stringDic.Add("key3", "third");
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Contains<string,string>("key1", stringDic);
|
|
|
|
Assert.Contains<string, string>("key1", stringDic);
|
|
|
|
Assert.Contains<KeyValuePair<string, string>>(sring_kv1, stringDic);
|
|
|
|
Assert.Contains<KeyValuePair<string, string>>(sring_kv1, stringDic);
|
|
|
|
|
|
|
|
|
|
|
|
//数字
|
|
|
|
//数字
|
|
|
@ -975,13 +1011,13 @@ namespace xUnitStudy.WebApi.Test
|
|
|
|
intDic.Add("key2", 2);
|
|
|
|
intDic.Add("key2", 2);
|
|
|
|
intDic.Add("key3", 3);
|
|
|
|
intDic.Add("key3", 3);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Contains<string,int>("key1", intDic);
|
|
|
|
Assert.Contains<string, int>("key1", intDic);
|
|
|
|
Assert.Contains<KeyValuePair<string, int>>(int_kv1, intDic);
|
|
|
|
Assert.Contains<KeyValuePair<string, int>>(int_kv1, intDic);
|
|
|
|
|
|
|
|
|
|
|
|
//类
|
|
|
|
//类
|
|
|
|
Person person1 = new Person() { Id=1,FirstName="first1",LastName="last1"};
|
|
|
|
Person person1 = new Person() { Id = 1, FirstName = "first1", LastName = "last1" };
|
|
|
|
Person person2 = new Person() { Id=2,FirstName="first2",LastName="last2"};
|
|
|
|
Person person2 = new Person() { Id = 2, FirstName = "first2", LastName = "last2" };
|
|
|
|
Person person3 = new Person() { Id=3,FirstName="first3",LastName="last3"};
|
|
|
|
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_kv1 = new KeyValuePair<string, Person>("key1", person1);
|
|
|
|
KeyValuePair<string, Person> class_kv2 = new KeyValuePair<string, Person>("key2", person2);
|
|
|
|
KeyValuePair<string, Person> class_kv2 = new KeyValuePair<string, Person>("key2", person2);
|
|
|
@ -992,10 +1028,10 @@ namespace xUnitStudy.WebApi.Test
|
|
|
|
classDic.Add("key2", person2);
|
|
|
|
classDic.Add("key2", person2);
|
|
|
|
classDic.Add("key3", person3);
|
|
|
|
classDic.Add("key3", person3);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Contains<string,Person>("key1", classDic);
|
|
|
|
Assert.Contains<string, Person>("key1", classDic);
|
|
|
|
Assert.Contains<KeyValuePair<string,Person>>(class_kv1,classDic);
|
|
|
|
Assert.Contains<KeyValuePair<string, Person>>(class_kv1, classDic);
|
|
|
|
Assert.Contains(class_kv2,classDic);
|
|
|
|
Assert.Contains(class_kv2, classDic);
|
|
|
|
Assert.Contains(class_kv3,classDic);
|
|
|
|
Assert.Contains(class_kv3, classDic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
[Fact]
|
|
|
@ -1013,7 +1049,7 @@ namespace xUnitStudy.WebApi.Test
|
|
|
|
stringDic.Add("key2", "second");
|
|
|
|
stringDic.Add("key2", "second");
|
|
|
|
stringDic.Add("key3", "third");
|
|
|
|
stringDic.Add("key3", "third");
|
|
|
|
|
|
|
|
|
|
|
|
IReadOnlyDictionary<string,string> stringReadOnlyDic = stringDic as IReadOnlyDictionary<string,string>;
|
|
|
|
IReadOnlyDictionary<string, string> stringReadOnlyDic = stringDic as IReadOnlyDictionary<string, string>;
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Contains<string, string>("key1", stringReadOnlyDic);
|
|
|
|
Assert.Contains<string, string>("key1", stringReadOnlyDic);
|
|
|
|
Assert.Contains<KeyValuePair<string, string>>(sring_kv1, stringReadOnlyDic);
|
|
|
|
Assert.Contains<KeyValuePair<string, string>>(sring_kv1, stringReadOnlyDic);
|
|
|
@ -1032,12 +1068,12 @@ namespace xUnitStudy.WebApi.Test
|
|
|
|
classDic.Add("key2", person2);
|
|
|
|
classDic.Add("key2", person2);
|
|
|
|
classDic.Add("key3", person3);
|
|
|
|
classDic.Add("key3", person3);
|
|
|
|
|
|
|
|
|
|
|
|
IReadOnlyDictionary<string,Person> classReadOnlyDic = classDic as IReadOnlyDictionary<string,Person>;
|
|
|
|
IReadOnlyDictionary<string, Person> classReadOnlyDic = classDic as IReadOnlyDictionary<string, Person>;
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Contains<string, Person>("key1", classReadOnlyDic);
|
|
|
|
Assert.Contains<string, Person>("key1", classReadOnlyDic);
|
|
|
|
Assert.Contains<KeyValuePair<string, Person>>(class_kv1, classReadOnlyDic);
|
|
|
|
Assert.Contains<KeyValuePair<string, Person>>(class_kv1, classReadOnlyDic);
|
|
|
|
Assert.Contains(class_kv2, classReadOnlyDic);
|
|
|
|
Assert.Contains(class_kv2, classReadOnlyDic);
|
|
|
|
Assert.Contains(class_kv3,classReadOnlyDic);
|
|
|
|
Assert.Contains(class_kv3, classReadOnlyDic);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
@ -1059,9 +1095,9 @@ namespace xUnitStudy.WebApi.Test
|
|
|
|
{
|
|
|
|
{
|
|
|
|
IComparer<Person> comparer = new Person();
|
|
|
|
IComparer<Person> comparer = new Person();
|
|
|
|
|
|
|
|
|
|
|
|
Person person1 = new Person() { Id = 1};
|
|
|
|
Person person1 = new Person() { Id = 1 };
|
|
|
|
Person person2 = new Person() { Id = 2};
|
|
|
|
Person person2 = new Person() { Id = 2 };
|
|
|
|
Person person3 = new Person() { Id = 3};
|
|
|
|
Person person3 = new Person() { Id = 3 };
|
|
|
|
|
|
|
|
|
|
|
|
Assert.InRange(person2, person1, person3, comparer);
|
|
|
|
Assert.InRange(person2, person1, person3, comparer);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -1093,9 +1129,84 @@ namespace xUnitStudy.WebApi.Test
|
|
|
|
|
|
|
|
|
|
|
|
#region Throws
|
|
|
|
#region Throws
|
|
|
|
[Fact]
|
|
|
|
[Fact]
|
|
|
|
public void Throw_Exception_Test()
|
|
|
|
public void Throws_Func_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//方法不推荐使用:用泛型版本替代
|
|
|
|
|
|
|
|
var result = Assert.Throws(typeof(ArgumentNullException), () => { ArgumentNullException ex = new ArgumentNullException("userName"); throw ex; return ex; });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Throws_Action_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//方法不推荐使用:用泛型版本替代
|
|
|
|
|
|
|
|
var result = Assert.Throws(typeof(FormatException), () => decimal.Parse("abc"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public async Task Throws_Async_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//方法不推荐使用:用泛型版本替代
|
|
|
|
|
|
|
|
var result = await Assert.ThrowsAsync(typeof(FormatException), () => { return Task.Run(() => { int.Parse("abc"); }); });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Throws_Generic_Func_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var result = Assert.Throws<FormatException>(() => ThrowAndReturnFormatException());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Throws_Generic_Action_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var result = Assert.Throws<FormatException>(() => ThrowFormatException());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Throws_Generic_ParamName_Func_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var paramName = "userName";
|
|
|
|
|
|
|
|
var result = Assert.Throws<ArgumentNullException>(paramName, () => ThrowAndReturnArgumentNullException(paramName));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Throws_Generic_ParamName_Action_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var paramName = "userName";
|
|
|
|
|
|
|
|
var result = Assert.Throws<ArgumentNullException>(paramName, () => ThrowArgumentNullException(paramName));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public async Task Throws_Generic_Func_Async_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var reslut = await Assert.ThrowsAsync<FormatException>(() => ThrowAndReturnFormatExceptionAsync());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public async Task Throws_Generic_ParamName_Async_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var paramName = "userName";
|
|
|
|
|
|
|
|
var reslut = await Assert.ThrowsAsync<ArgumentNullException>(paramName, () => ThrowAndReturnArgumentNullExceptionAsync(paramName));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void ThrowsAny_Func_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var result = Assert.ThrowsAny<FormatException>(() => ThrowAndReturnFormatException());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 断言异常或派生类异常
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void ThrowsAny_Action_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var result = Assert.ThrowsAny<FormatException>(() => ThrowFormatException());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public async Task ThrowsAny_Func_Async_Test()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Assert.ThrowsAsync<ArgumentNullException>(()=> { throw new ArgumentNullException("message"); });
|
|
|
|
var result = await Assert.ThrowsAnyAsync<FormatException>(() => ThrowAndReturnFormatExceptionAsync());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
[Fact]
|
|
|
@ -1120,13 +1231,411 @@ namespace xUnitStudy.WebApi.Test
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Matches
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Matches_String_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//用户名正则:大小写字线、数字、下划线组成,6-16位长度
|
|
|
|
|
|
|
|
var userNameReg = @"^[a-zA-Z]\w{5,15}$";
|
|
|
|
|
|
|
|
Assert.Matches(userNameReg, "bicijinlian");
|
|
|
|
|
|
|
|
Assert.Matches(userNameReg, "wangerxiao_1981");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//email正则
|
|
|
|
|
|
|
|
var emailReg = @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
|
|
|
|
|
|
|
|
Assert.Matches(emailReg, "bicijinlian@163.com");
|
|
|
|
|
|
|
|
Assert.Matches(emailReg, "wangerxiao_1981@126.com");
|
|
|
|
|
|
|
|
Assert.Matches(emailReg, "15601716045@126.com");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void DoesNotMatch_String_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//用户名正则:大小写字线、数字、下划线组成,6-16位长度
|
|
|
|
|
|
|
|
var userNameReg = @"^[a-zA-Z]\w{5,15}$";
|
|
|
|
|
|
|
|
Assert.DoesNotMatch(userNameReg, "abcd0123456789abcdefg");
|
|
|
|
|
|
|
|
Assert.DoesNotMatch(userNameReg, "wang");
|
|
|
|
|
|
|
|
Assert.DoesNotMatch(userNameReg, "bicijinlian$");
|
|
|
|
|
|
|
|
Assert.DoesNotMatch(userNameReg, "wangerxiao_1981@163");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//email正则
|
|
|
|
|
|
|
|
var emailReg = @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
|
|
|
|
|
|
|
|
Assert.DoesNotMatch(emailReg, "bicijinlian");
|
|
|
|
|
|
|
|
Assert.DoesNotMatch(emailReg, "wangerxiao_1981@126");
|
|
|
|
|
|
|
|
Assert.DoesNotMatch(emailReg, "15601716045126.com");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Matches_Regex_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//用户名正则:大小写字线、数字、下划线组成,6-16位长度
|
|
|
|
|
|
|
|
var userNameReg = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z]\w{5,15}$");
|
|
|
|
|
|
|
|
Assert.Matches(userNameReg, "bicijinlian");
|
|
|
|
|
|
|
|
Assert.Matches(userNameReg, "wangerxiao_1981");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//email正则
|
|
|
|
|
|
|
|
var emailReg = new System.Text.RegularExpressions.Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
|
|
|
|
|
|
|
|
Assert.Matches(emailReg, "bicijinlian@163.com");
|
|
|
|
|
|
|
|
Assert.Matches(emailReg, "wangerxiao_1981@126.com");
|
|
|
|
|
|
|
|
Assert.Matches(emailReg, "15601716045@126.com");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void DoesNotMatch_Regex_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//用户名正则:大小写字线、数字、下划线组成,6-16位长度
|
|
|
|
|
|
|
|
var userNameReg = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z]\w{5,15}$");
|
|
|
|
|
|
|
|
Assert.DoesNotMatch(userNameReg, "abcd0123456789abcdefg");
|
|
|
|
|
|
|
|
Assert.DoesNotMatch(userNameReg, "wang");
|
|
|
|
|
|
|
|
Assert.DoesNotMatch(userNameReg, "bicijinlian$");
|
|
|
|
|
|
|
|
Assert.DoesNotMatch(userNameReg, "wangerxiao_1981@163");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//email正则
|
|
|
|
|
|
|
|
var emailReg = new System.Text.RegularExpressions.Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
|
|
|
|
|
|
|
|
Assert.DoesNotMatch(emailReg, "bicijinlian");
|
|
|
|
|
|
|
|
Assert.DoesNotMatch(emailReg, "wangerxiao_1981@126");
|
|
|
|
|
|
|
|
Assert.DoesNotMatch(emailReg, "15601716045126.com");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region StartWith | EndWith
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void StartsWith_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Assert.StartsWith("Start", "StartAndEnd");
|
|
|
|
|
|
|
|
Assert.StartsWith("start", "StartAndEnd", StringComparison.CurrentCultureIgnoreCase);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void EndsWith_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Assert.EndsWith("end", "startAndend");
|
|
|
|
|
|
|
|
Assert.EndsWith("end", "StartAndEnd", StringComparison.CurrentCultureIgnoreCase);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Single
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Single_Collection_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//只有一项的集合
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<string> stringArray = new List<string>() { "first" };
|
|
|
|
|
|
|
|
Assert.Single(stringArray);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<decimal> decimalArray = new List<decimal>() { 5.66M };
|
|
|
|
|
|
|
|
Assert.Single(decimalArray);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<Person> classArray = new List<Person>() { new Person() { Id = 2 } };
|
|
|
|
|
|
|
|
Assert.Single(classArray);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 集合中,指定项没有重复项
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Single_Collection_Expected_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//集合中,指定的项只有一个,即是指定项在集合中没有重复项
|
|
|
|
|
|
|
|
//不被指定的项,可以有多个
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<string> stringArray = new List<string>() { "first", "second", "second" };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert //如果换成:List<string> stringArray = new List<string>() { "first","second","first" }
|
|
|
|
|
|
|
|
//因为"first"有两项,则断言失败
|
|
|
|
|
|
|
|
.Single(stringArray, "first");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<decimal> decimalArray = new List<decimal>() { 5.66M, 7.7M, 8.8M };
|
|
|
|
|
|
|
|
Assert.Single(decimalArray, 5.66M);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Person person1 = new Person() { Id = 1 };
|
|
|
|
|
|
|
|
Person person2 = new Person() { Id = 2 };
|
|
|
|
|
|
|
|
Person person3 = new Person() { Id = 2 };
|
|
|
|
|
|
|
|
List<Person> classArray = new List<Person>() { person1, person2 };
|
|
|
|
|
|
|
|
Assert.Single(classArray, person1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<Person> classArray2 = new List<Person>() { person1, person2, person3 };
|
|
|
|
|
|
|
|
Assert.True(true, "person2和person3是相同的项,所以 Assert.Single(classArray, person2) 会失败");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Single_Generic_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//只有一项的集合(推荐简化写法)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<string> stringArray = new List<string>() { "first" };
|
|
|
|
|
|
|
|
Assert.Single<string>(stringArray);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<decimal> decimalArray = new List<decimal>() { 5.66M };
|
|
|
|
|
|
|
|
Assert.Single<decimal>(decimalArray);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<Person> classArray = new List<Person>() { new Person() { Id = 2 } };
|
|
|
|
|
|
|
|
Assert.Single<Person>(classArray);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Single_Generic_Predicate_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//集合中,指定表达式的项只有一个,即是Predicate<>筛选结果在集合中没有重复项
|
|
|
|
|
|
|
|
//不被指定的项,可以有多个
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<string> stringArray = new List<string>() { "first", "second", "second" };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert //如果换成:List<string> stringArray = new List<string>() { "first","second","first" }
|
|
|
|
|
|
|
|
//因为"first"有两项,则断言失败
|
|
|
|
|
|
|
|
.Single<string>(stringArray, s => s.StartsWith("f"));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<decimal> decimalArray = new List<decimal>() { 5.66M, 7.7M, 8.8M };
|
|
|
|
|
|
|
|
Assert.Single(decimalArray, d => d > 8);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Person person1 = new Person() { Id = 1 };
|
|
|
|
|
|
|
|
Person person2 = new Person() { Id = 2 };
|
|
|
|
|
|
|
|
Person person3 = new Person() { Id = 2 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<Person> classArray = new List<Person>() { person1, person2, person3 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Single(classArray, p => p.Id == 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Assert.Single(classArray, p => p.Id == 2),则失败
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
[Fact]
|
|
|
|
public void All_Test()
|
|
|
|
public void All_Test()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
List<string> items = new List<string>() { "abc","ac","ad","adddeddd"};
|
|
|
|
List<string> items = new List<string>() { "abc", "ac", "ad", "adddeddd" };
|
|
|
|
Assert.All<string>(items, f => f.StartsWith("a"));
|
|
|
|
Assert.All<string>(items, f => f.StartsWith("a"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 相当于集合的自定义断言,实用意义不大
|
|
|
|
|
|
|
|
/// (Action<T>参数中自定义代码)
|
|
|
|
|
|
|
|
/// 看源代码,可能是集合类断言的基类
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Collection_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//验证集合是否包含给定元素数量,满足元素检查器提供的条件。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<string> list = new List<string>() { "first", "second" };
|
|
|
|
|
|
|
|
//参数Action<T>[] elementInspectors,元素检查器,依次检查每个元素。
|
|
|
|
|
|
|
|
//元素检查器的总数必须与集合中元素的数量完全匹配
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//看源代码:每个Action<T>执行时,均不抛出异常,则断言通过
|
|
|
|
|
|
|
|
Action<string>[] actions = new Action<string>[2]
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
new Action<string> (s=> {} ),
|
|
|
|
|
|
|
|
new Action<string> (s=>{}),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Collection(list, actions);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region ISet(集合)
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 子集断言
|
|
|
|
|
|
|
|
/// ISet是集合接口:包含不重复元素,实际集合的交、并、子集等运算
|
|
|
|
|
|
|
|
/// 实现了ISet接口的集合:HashSet和SortedSet
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Subset_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//"真实值"是"期望值"的子集
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HashSet<string> hset = new HashSet<string>() { "first", "second", "third" };
|
|
|
|
|
|
|
|
HashSet<string> sub1 = new HashSet<string>() { "first", "second" };
|
|
|
|
|
|
|
|
HashSet<string> sub2 = new HashSet<string>() { "second", "third" };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//expectedSuperset 期望超集,真实值是子集
|
|
|
|
|
|
|
|
Assert.Subset(expectedSuperset: hset, actual: sub1);
|
|
|
|
|
|
|
|
Assert.Subset(hset, sub2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//自己是自己子集
|
|
|
|
|
|
|
|
Assert.Subset(hset, hset);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 超集断言
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Superset_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//"真实值"是"期望值"的超集
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HashSet<string> hset = new HashSet<string>() { "first", "second", "third" };
|
|
|
|
|
|
|
|
HashSet<string> sub1 = new HashSet<string>() { "first", "second" };
|
|
|
|
|
|
|
|
HashSet<string> sub2 = new HashSet<string>() { "second", "third" };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//expectedSuperset 期望超集,真实值是子集
|
|
|
|
|
|
|
|
Assert.Superset(sub1, hset);
|
|
|
|
|
|
|
|
Assert.Superset(sub2, hset);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//自己是自己子的超集
|
|
|
|
|
|
|
|
Assert.Superset(hset, hset);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 真子集断言
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void ProperSubset_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//"真实值"是"期望值"的真子集
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HashSet<string> hset = new HashSet<string>() { "first", "second", "third" };
|
|
|
|
|
|
|
|
HashSet<string> sub1 = new HashSet<string>() { "first", "second" };
|
|
|
|
|
|
|
|
HashSet<string> sub2 = new HashSet<string>() { "second", "third" };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//expectedSuperset 期望超集,真实值是子集
|
|
|
|
|
|
|
|
Assert.ProperSubset(expectedSuperset: hset, actual: sub1);
|
|
|
|
|
|
|
|
Assert.ProperSubset(hset, sub2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//自己不是自己真子集
|
|
|
|
|
|
|
|
//Assert.ProperSubset(hset, hset);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 真超集断言
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void ProperSuperset_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//"真实值"是"期望值"的真超集
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HashSet<string> hset = new HashSet<string>() { "first", "second", "third" };
|
|
|
|
|
|
|
|
HashSet<string> sub1 = new HashSet<string>() { "first", "second" };
|
|
|
|
|
|
|
|
HashSet<string> sub2 = new HashSet<string>() { "second", "third" };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//expectedSuperset 期望超集,真实值是子集
|
|
|
|
|
|
|
|
Assert.ProperSuperset(sub1, hset);
|
|
|
|
|
|
|
|
Assert.ProperSuperset(sub2, hset);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//自己不是自己子的真超集
|
|
|
|
|
|
|
|
//Assert.ProperSuperset(hset, hset);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 事件(暂未实现)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 属性更改事件
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void PropertyChanged_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 属性更改事件(异步)
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void PropertyChangedAsync_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 断言 特定事件被触发
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Raises_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 断言 事件或派生事件 被触发
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void RaisesAny_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 断言 特定事件被触发(异步)
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void RaisesAsync_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 断言 事件或派生事件 被触发(异步)
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void RaisesAnyAsync_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 私有方法
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ThrowArgumentNullException(string paramName)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ArgumentNullException ex = new ArgumentNullException(paramName, "message");
|
|
|
|
|
|
|
|
throw ex;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ArgumentNullException ThrowAndReturnArgumentNullException(string paramName)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ArgumentNullException ex = new ArgumentNullException(paramName, "message");
|
|
|
|
|
|
|
|
throw ex;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<ArgumentNullException> ThrowAndReturnArgumentNullExceptionAsync(string paramName)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Func<string, ArgumentNullException> func = (para) =>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ArgumentNullException ex = new ArgumentNullException(paramName, "message");
|
|
|
|
|
|
|
|
throw ex;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var reslut = await Task.Run(() => func(paramName));
|
|
|
|
|
|
|
|
return reslut;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ThrowFormatException()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int.Parse("ping");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private FormatException ThrowAndReturnFormatException()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
FormatException exception = null;
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int.Parse("ping");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (FormatException ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
exception = ex;
|
|
|
|
|
|
|
|
throw ex;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return exception;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task ThrowFormatExceptionAsync()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
await Task.Run(() => { int.Parse("ping"); });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task ThrowAndReturnFormatExceptionAsync()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Func<FormatException> func = () =>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int.Parse("ping");
|
|
|
|
|
|
|
|
return new FormatException();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await Task.Run(() => func());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 清理
|
|
|
|
#region 清理
|
|
|
|
public void Dispose()
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
{
|
|
|
|