完善断言

develop
bicijinlian 7 years ago
parent 81b141418a
commit 495b9dbf9e

@ -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()
@ -1093,9 +1129,84 @@ namespace xUnitStudy.WebApi.Test
#region Throws #region Throws
[Fact] [Fact]
public void Throw_Exception_Test() public void Throws_Func_Test()
{ {
Assert.ThrowsAsync<ArgumentNullException>(()=> { throw new ArgumentNullException("message"); }); //方法不推荐使用:用泛型版本替代
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()
{
var result = await Assert.ThrowsAnyAsync<FormatException>(() => ThrowAndReturnFormatExceptionAsync());
} }
[Fact] [Fact]
@ -1120,6 +1231,176 @@ 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()
{ {
@ -1127,6 +1408,234 @@ namespace xUnitStudy.WebApi.Test
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()
{ {

Loading…
Cancel
Save