From 495b9dbf9e6b3b0503ddedf97ca085a7bd47e735 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Sat, 4 Aug 2018 22:47:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=96=AD=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xUnitStudy.WebApi.Test/UseAssertTest.cs | 557 +++++++++++++++++++++++- 1 file changed, 533 insertions(+), 24 deletions(-) diff --git a/xUnitStudy.WebApi.Test/UseAssertTest.cs b/xUnitStudy.WebApi.Test/UseAssertTest.cs index 4bf601f..2d8c40a 100644 --- a/xUnitStudy.WebApi.Test/UseAssertTest.cs +++ b/xUnitStudy.WebApi.Test/UseAssertTest.cs @@ -262,7 +262,7 @@ namespace xUnitStudy.WebApi.Test //没有小数位,舍入到整数 Assert.NotEqual(16d, 160d, precision: 0); } - + [Fact] public void NotEqual_Decimal_Test() { @@ -354,7 +354,7 @@ namespace xUnitStudy.WebApi.Test //对实现IEnumerable接口的对象进行 Equal比较 //形如:NotEqual(IEnumerable expected, IEnumerable actual) - List expected = new List() { "first", "second","third" }; + List expected = new List() { "first", "second", "third" }; List actual = new List() { "first", "second" }; Assert.NotEqual>(expected, actual); @@ -393,6 +393,42 @@ namespace xUnitStudy.WebApi.Test } #endregion + #region StrictEqual + [Fact] + public void StrictEqual_Test() + { + //使用类型的默认比较器验证两个对象是否严格相等,用法类似Equal + //不支持自定义IEqualityComparer + Assert.StrictEqual(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 + //不支持自定义IEqualityComparer + Assert.NotStrictEqual(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 [Fact] public void Same_String_Test() @@ -517,7 +553,7 @@ namespace xUnitStudy.WebApi.Test { Assert.False(false); Assert.False("ping" == "ack"); - Assert.False(3 == 1+1); + Assert.False(3 == 1 + 1); Assert.False("ping" == "ping" && 3 == 1 + 1); } @@ -603,7 +639,7 @@ namespace xUnitStudy.WebApi.Test Assert.True(double.IsPositiveInfinity(1 / 0d)); Assert.True(double.IsNegativeInfinity(-1 / 0d)); - + } #endregion @@ -733,7 +769,7 @@ namespace xUnitStudy.WebApi.Test #endregion #region Contains For String - [Fact] + [Fact] public void Contains_String_Test() { Assert.Contains("hello", "hello,world."); @@ -946,7 +982,7 @@ namespace xUnitStudy.WebApi.Test Assert //简写为 .Contains(classArray, person => person.FirstName == "first1"); Assert //简写为 - .Contains(classArray, person => person.Id == 3 && person.LastName=="last3"); + .Contains(classArray, person => person.Id == 3 && person.LastName == "last3"); } [Fact] @@ -962,7 +998,7 @@ namespace xUnitStudy.WebApi.Test stringDic.Add("key2", "second"); stringDic.Add("key3", "third"); - Assert.Contains("key1", stringDic); + Assert.Contains("key1", stringDic); Assert.Contains>(sring_kv1, stringDic); //数字 @@ -975,13 +1011,13 @@ namespace xUnitStudy.WebApi.Test intDic.Add("key2", 2); intDic.Add("key3", 3); - Assert.Contains("key1", intDic); + Assert.Contains("key1", intDic); Assert.Contains>(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"}; + 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 class_kv1 = new KeyValuePair("key1", person1); KeyValuePair class_kv2 = new KeyValuePair("key2", person2); @@ -992,10 +1028,10 @@ namespace xUnitStudy.WebApi.Test classDic.Add("key2", person2); classDic.Add("key3", person3); - Assert.Contains("key1", classDic); - Assert.Contains>(class_kv1,classDic); - Assert.Contains(class_kv2,classDic); - Assert.Contains(class_kv3,classDic); + Assert.Contains("key1", classDic); + Assert.Contains>(class_kv1, classDic); + Assert.Contains(class_kv2, classDic); + Assert.Contains(class_kv3, classDic); } [Fact] @@ -1013,7 +1049,7 @@ namespace xUnitStudy.WebApi.Test stringDic.Add("key2", "second"); stringDic.Add("key3", "third"); - IReadOnlyDictionary stringReadOnlyDic = stringDic as IReadOnlyDictionary; + IReadOnlyDictionary stringReadOnlyDic = stringDic as IReadOnlyDictionary; Assert.Contains("key1", stringReadOnlyDic); Assert.Contains>(sring_kv1, stringReadOnlyDic); @@ -1032,12 +1068,12 @@ namespace xUnitStudy.WebApi.Test classDic.Add("key2", person2); classDic.Add("key3", person3); - IReadOnlyDictionary classReadOnlyDic = classDic as IReadOnlyDictionary; + IReadOnlyDictionary classReadOnlyDic = classDic as IReadOnlyDictionary; Assert.Contains("key1", classReadOnlyDic); Assert.Contains>(class_kv1, classReadOnlyDic); Assert.Contains(class_kv2, classReadOnlyDic); - Assert.Contains(class_kv3,classReadOnlyDic); + Assert.Contains(class_kv3, classReadOnlyDic); } #endregion @@ -1059,9 +1095,9 @@ namespace xUnitStudy.WebApi.Test { IComparer comparer = new Person(); - Person person1 = new Person() { Id = 1}; - Person person2 = new Person() { Id = 2}; - Person person3 = new Person() { Id = 3}; + Person person1 = new Person() { Id = 1 }; + Person person2 = new Person() { Id = 2 }; + Person person3 = new Person() { Id = 3 }; Assert.InRange(person2, person1, person3, comparer); } @@ -1093,9 +1129,84 @@ namespace xUnitStudy.WebApi.Test #region Throws [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(() => ThrowAndReturnFormatException()); + } + + [Fact] + public void Throws_Generic_Action_Test() + { + var result = Assert.Throws(() => ThrowFormatException()); + } + + [Fact] + public void Throws_Generic_ParamName_Func_Test() + { + var paramName = "userName"; + var result = Assert.Throws(paramName, () => ThrowAndReturnArgumentNullException(paramName)); + } + + [Fact] + public void Throws_Generic_ParamName_Action_Test() + { + var paramName = "userName"; + var result = Assert.Throws(paramName, () => ThrowArgumentNullException(paramName)); + } + + [Fact] + public async Task Throws_Generic_Func_Async_Test() + { + var reslut = await Assert.ThrowsAsync(() => ThrowAndReturnFormatExceptionAsync()); + } + + [Fact] + public async Task Throws_Generic_ParamName_Async_Test() + { + var paramName = "userName"; + var reslut = await Assert.ThrowsAsync(paramName, () => ThrowAndReturnArgumentNullExceptionAsync(paramName)); + } + + [Fact] + public void ThrowsAny_Func_Test() + { + var result = Assert.ThrowsAny(() => ThrowAndReturnFormatException()); + } + + /// + /// 断言异常或派生类异常 + /// + [Fact] + public void ThrowsAny_Action_Test() { - Assert.ThrowsAsync(()=> { throw new ArgumentNullException("message"); }); + var result = Assert.ThrowsAny(() => ThrowFormatException()); + } + + [Fact] + public async Task ThrowsAny_Func_Async_Test() + { + var result = await Assert.ThrowsAnyAsync(() => ThrowAndReturnFormatExceptionAsync()); } [Fact] @@ -1120,13 +1231,411 @@ namespace xUnitStudy.WebApi.Test } #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 stringArray = new List() { "first" }; + Assert.Single(stringArray); + + List decimalArray = new List() { 5.66M }; + Assert.Single(decimalArray); + + List classArray = new List() { new Person() { Id = 2 } }; + Assert.Single(classArray); + } + + /// + /// 集合中,指定项没有重复项 + /// + [Fact] + public void Single_Collection_Expected_Test() + { + //集合中,指定的项只有一个,即是指定项在集合中没有重复项 + //不被指定的项,可以有多个 + + List stringArray = new List() { "first", "second", "second" }; + + Assert //如果换成:List stringArray = new List() { "first","second","first" } + //因为"first"有两项,则断言失败 + .Single(stringArray, "first"); + + List decimalArray = new List() { 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 classArray = new List() { person1, person2 }; + Assert.Single(classArray, person1); + + List classArray2 = new List() { person1, person2, person3 }; + Assert.True(true, "person2和person3是相同的项,所以 Assert.Single(classArray, person2) 会失败"); + } + + [Fact] + public void Single_Generic_Test() + { + //只有一项的集合(推荐简化写法) + + List stringArray = new List() { "first" }; + Assert.Single(stringArray); + + List decimalArray = new List() { 5.66M }; + Assert.Single(decimalArray); + + List classArray = new List() { new Person() { Id = 2 } }; + Assert.Single(classArray); + } + + [Fact] + public void Single_Generic_Predicate_Test() + { + //集合中,指定表达式的项只有一个,即是Predicate<>筛选结果在集合中没有重复项 + //不被指定的项,可以有多个 + + List stringArray = new List() { "first", "second", "second" }; + + Assert //如果换成:List stringArray = new List() { "first","second","first" } + //因为"first"有两项,则断言失败 + .Single(stringArray, s => s.StartsWith("f")); + + List decimalArray = new List() { 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 classArray = new List() { person1, person2, person3 }; + + Assert.Single(classArray, p => p.Id == 1); + + //Assert.Single(classArray, p => p.Id == 2),则失败 + } + + #endregion + [Fact] public void All_Test() { - List items = new List() { "abc","ac","ad","adddeddd"}; + List items = new List() { "abc", "ac", "ad", "adddeddd" }; Assert.All(items, f => f.StartsWith("a")); } + /// + /// 相当于集合的自定义断言,实用意义不大 + /// (Action参数中自定义代码) + /// 看源代码,可能是集合类断言的基类 + /// + [Fact] + public void Collection_Test() + { + //验证集合是否包含给定元素数量,满足元素检查器提供的条件。 + + List list = new List() { "first", "second" }; + //参数Action[] elementInspectors,元素检查器,依次检查每个元素。 + //元素检查器的总数必须与集合中元素的数量完全匹配 + + //看源代码:每个Action执行时,均不抛出异常,则断言通过 + Action[] actions = new Action[2] + { + new Action (s=> {} ), + new Action (s=>{}), + }; + + Assert.Collection(list, actions); + } + + #region ISet(集合) + /// + /// 子集断言 + /// ISet是集合接口:包含不重复元素,实际集合的交、并、子集等运算 + /// 实现了ISet接口的集合:HashSet和SortedSet + /// + [Fact] + public void Subset_Test() + { + //"真实值"是"期望值"的子集 + + HashSet hset = new HashSet() { "first", "second", "third" }; + HashSet sub1 = new HashSet() { "first", "second" }; + HashSet sub2 = new HashSet() { "second", "third" }; + + //expectedSuperset 期望超集,真实值是子集 + Assert.Subset(expectedSuperset: hset, actual: sub1); + Assert.Subset(hset, sub2); + + //自己是自己子集 + Assert.Subset(hset, hset); + } + + /// + /// 超集断言 + /// + [Fact] + public void Superset_Test() + { + //"真实值"是"期望值"的超集 + + HashSet hset = new HashSet() { "first", "second", "third" }; + HashSet sub1 = new HashSet() { "first", "second" }; + HashSet sub2 = new HashSet() { "second", "third" }; + + //expectedSuperset 期望超集,真实值是子集 + Assert.Superset(sub1, hset); + Assert.Superset(sub2, hset); + + //自己是自己子的超集 + Assert.Superset(hset, hset); + } + + /// + /// 真子集断言 + /// + [Fact] + public void ProperSubset_Test() + { + //"真实值"是"期望值"的真子集 + + HashSet hset = new HashSet() { "first", "second", "third" }; + HashSet sub1 = new HashSet() { "first", "second" }; + HashSet sub2 = new HashSet() { "second", "third" }; + + //expectedSuperset 期望超集,真实值是子集 + Assert.ProperSubset(expectedSuperset: hset, actual: sub1); + Assert.ProperSubset(hset, sub2); + + //自己不是自己真子集 + //Assert.ProperSubset(hset, hset); + } + + /// + /// 真超集断言 + /// + [Fact] + public void ProperSuperset_Test() + { + //"真实值"是"期望值"的真超集 + + HashSet hset = new HashSet() { "first", "second", "third" }; + HashSet sub1 = new HashSet() { "first", "second" }; + HashSet sub2 = new HashSet() { "second", "third" }; + + //expectedSuperset 期望超集,真实值是子集 + Assert.ProperSuperset(sub1, hset); + Assert.ProperSuperset(sub2, hset); + + //自己不是自己子的真超集 + //Assert.ProperSuperset(hset, hset); + } + #endregion + + #region 事件(暂未实现) + + /// + /// 属性更改事件 + /// + [Fact] + public void PropertyChanged_Test() + { + + } + + /// + /// 属性更改事件(异步) + /// + [Fact] + public void PropertyChangedAsync_Test() + { + + } + + /// + /// 断言 特定事件被触发 + /// + [Fact] + public void Raises_Test() + { + + } + + /// + /// 断言 事件或派生事件 被触发 + /// + [Fact] + public void RaisesAny_Test() + { + + } + + /// + /// 断言 特定事件被触发(异步) + /// + [Fact] + public void RaisesAsync_Test() + { + + } + + /// + /// 断言 事件或派生事件 被触发(异步) + /// + [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 ThrowAndReturnArgumentNullExceptionAsync(string paramName) + { + Func 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 func = () => + { + int.Parse("ping"); + return new FormatException(); + }; + + await Task.Run(() => func()); + } + #endregion + #region 清理 public void Dispose() {