diff options
author | 杨宇千 <crupest@outlook.com> | 2019-10-28 23:35:00 +0800 |
---|---|---|
committer | 杨宇千 <crupest@outlook.com> | 2019-10-28 23:35:00 +0800 |
commit | fbaa8cab95a91b887bbd2d108d27c5abb38e4e29 (patch) | |
tree | 97ed1fa767e1492cd3df292247d8fd3e47252882 /Timeline.Tests/Helpers/ParameterInfoAssertions.cs | |
parent | 294807481dd42dc3c660e29630b36462e7bcfaaf (diff) | |
download | timeline-fbaa8cab95a91b887bbd2d108d27c5abb38e4e29.tar.gz timeline-fbaa8cab95a91b887bbd2d108d27c5abb38e4e29.tar.bz2 timeline-fbaa8cab95a91b887bbd2d108d27c5abb38e4e29.zip |
Add UserDetailController unit tests.
Diffstat (limited to 'Timeline.Tests/Helpers/ParameterInfoAssertions.cs')
-rw-r--r-- | Timeline.Tests/Helpers/ParameterInfoAssertions.cs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/Timeline.Tests/Helpers/ParameterInfoAssertions.cs b/Timeline.Tests/Helpers/ParameterInfoAssertions.cs new file mode 100644 index 00000000..e3becee1 --- /dev/null +++ b/Timeline.Tests/Helpers/ParameterInfoAssertions.cs @@ -0,0 +1,63 @@ +using FluentAssertions;
+using FluentAssertions.Execution;
+using FluentAssertions.Formatting;
+using FluentAssertions.Primitives;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Threading.Tasks;
+
+namespace Timeline.Tests.Helpers
+{
+ public class ParameterInfoValueFormatter : IValueFormatter
+ {
+ public bool CanHandle(object value)
+ {
+ return value is ParameterInfo;
+ }
+
+ public string Format(object value, FormattingContext context, FormatChild formatChild)
+ {
+ var param = (ParameterInfo)value;
+ return $"{param.Member.DeclaringType.FullName}.{param.Member.Name}#{param.Name}";
+ }
+ }
+
+ public class ParameterInfoAssertions : ReferenceTypeAssertions<ParameterInfo, ParameterInfoAssertions>
+ {
+ static ParameterInfoAssertions()
+ {
+ Formatter.AddFormatter(new ParameterInfoValueFormatter());
+ }
+
+ public ParameterInfoAssertions(ParameterInfo parameterInfo)
+ {
+ Subject = parameterInfo;
+ }
+
+ protected override string Identifier => "parameter";
+
+ public AndWhichConstraint<ParameterInfoAssertions, TAttribute> BeDecoratedWith<TAttribute>(string because = "", params object[] becauseArgs)
+ where TAttribute : Attribute
+ {
+ var attribute = Subject.GetCustomAttribute<TAttribute>(false);
+
+ Execute.Assertion
+ .BecauseOf(because, becauseArgs)
+ .ForCondition(attribute != null)
+ .FailWith("Expected {0} {1} to be decorated with {2}{reason}, but that attribute was not found.",
+ Identifier, Subject, typeof(TAttribute).FullName);
+
+ return new AndWhichConstraint<ParameterInfoAssertions, TAttribute>(this, attribute);
+ }
+ }
+
+ public static class ParameterInfoAssertionExtensions
+ {
+ public static ParameterInfoAssertions Should(this ParameterInfo parameterInfo)
+ {
+ return new ParameterInfoAssertions(parameterInfo);
+ }
+ }
+}
|