-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathNumericTests.cs
More file actions
153 lines (136 loc) · 7.97 KB
/
NumericTests.cs
File metadata and controls
153 lines (136 loc) · 7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using FluentAssertions.Analyzers.TestUtils;
using Microsoft.CodeAnalysis;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace FluentAssertions.Analyzers.Tests
{
[TestClass]
public class NumericTests
{
[DataTestMethod]
[AssertionDiagnostic("actual.Should().BeGreaterThan(0{0});")]
[AssertionDiagnostic("actual.Should().BeGreaterThan(0{0}).ToString();")]
[Implemented]
public void NumericShouldBePositive_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.NumericShouldBePositive_ShouldBeGreaterThan);
[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "actual.Should().BeGreaterThan(0{0});",
newAssertion: "actual.Should().BePositive({0});")]
[AssertionCodeFix(
oldAssertion: "actual.Should().BeGreaterThan(0{0}).ToString();",
newAssertion: "actual.Should().BePositive({0}).ToString();")]
[Implemented]
public void NumericShouldBePositive_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix(oldAssertion, newAssertion);
[DataTestMethod]
[AssertionDiagnostic("actual.Should().BeLessThan(0{0});")]
[AssertionDiagnostic("actual.Should().BeLessThan(0{0}).ToString();")]
[Implemented]
public void NumericShouldBeNegative_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.NumericShouldBeNegative_ShouldBeLessThan);
[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "actual.Should().BeLessThan(0{0});",
newAssertion: "actual.Should().BeNegative({0});")]
[AssertionCodeFix(
oldAssertion: "actual.Should().BeLessThan(0{0}).ToString();",
newAssertion: "actual.Should().BeNegative({0}).ToString();")]
[Implemented]
public void NumericShouldBeNegative_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix(oldAssertion, newAssertion);
[DataTestMethod]
[AssertionDiagnostic("actual.Should().BeGreaterOrEqualTo(lower{0}).And.BeLessOrEqualTo(upper);")]
[AssertionDiagnostic("actual.Should().BeGreaterOrEqualTo(lower).And.BeLessOrEqualTo(upper{0});")]
[Implemented]
public void NumericShouldBeInRange_BeGreaterOrEqualToAndBeLessOrEqualTo_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.NumericShouldBeInRange_BeGreaterOrEqualToAndBeLessOrEqualTo);
[DataTestMethod]
[AssertionDiagnostic("actual.Should().BeLessOrEqualTo(upper{0}).And.BeGreaterOrEqualTo(lower);")]
[AssertionDiagnostic("actual.Should().BeLessOrEqualTo(upper).And.BeGreaterOrEqualTo(lower{0});")]
[Implemented]
public void NumericShouldBeInRange_BeLessOrEqualToAndBeGreaterOrEqualTo_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.NumericShouldBeInRange_BeLessOrEqualToAndBeGreaterOrEqualTo);
[DataTestMethod]
[DataRow("actual.Should().BeLessOrEqualTo(upper, \"because reason 1\").And.BeGreaterOrEqualTo(lower, \"because reason 2\");")]
[DataRow("actual.Should().BeLessOrEqualTo(upper, \"because reason 1\").And.BeGreaterOrEqualTo(lower, \"because reason 2\");")]
[Implemented]
public void NumericShouldBeInRange_BeLessOrEqualToAndBeGreaterOrEqualTo_WithMessagesInBothAssertions_TestAnalyzer(string assertion)
{
verifyNoDiagnostic("double");
verifyNoDiagnostic("float");
verifyNoDiagnostic("decimal");
void verifyNoDiagnostic(string type)
{
var source = GenerateCode.NumericAssertion(assertion, type);
DiagnosticVerifier.VerifyDiagnostic(new DiagnosticVerifierArguments()
.WithSources(source)
.WithAllAnalyzers()
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0)
);
}
}
[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "actual.Should().BeGreaterOrEqualTo(lower{0}).And.BeLessOrEqualTo(upper);",
newAssertion: "actual.Should().BeInRange(lower, upper{0});")]
[AssertionCodeFix(
oldAssertion: "actual.Should().BeGreaterOrEqualTo(lower).And.BeLessOrEqualTo(upper{0});",
newAssertion: "actual.Should().BeInRange(lower, upper{0});")]
[AssertionCodeFix(
oldAssertion: "actual.Should().BeLessOrEqualTo(upper{0}).And.BeGreaterOrEqualTo(lower);",
newAssertion: "actual.Should().BeInRange(lower, upper{0});")]
[AssertionCodeFix(
oldAssertion: "actual.Should().BeLessOrEqualTo(upper).And.BeGreaterOrEqualTo(lower{0});",
newAssertion: "actual.Should().BeInRange(lower, upper{0});")]
[NotImplemented]
public void NumericShouldBeInRange_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix(oldAssertion, newAssertion);
[DataTestMethod]
[AssertionDiagnostic("Math.Abs(expected - actual).Should().BeLessOrEqualTo(delta{0});")]
[Implemented]
public void NumericShouldBeApproximately_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.NumericShouldBeApproximately_MathAbsShouldBeLessOrEqualTo);
[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "Math.Abs(expected - actual).Should().BeLessOrEqualTo(delta{0});",
newAssertion: "actual.Should().BeApproximately(expected, delta{0});")]
[Implemented]
public void NumericShouldBeApproximately_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix(oldAssertion, newAssertion);
private void VerifyCSharpDiagnostic(string sourceAssertion, DiagnosticMetadata metadata)
{
VerifyCSharpDiagnostic(sourceAssertion, metadata, "double");
VerifyCSharpDiagnostic(sourceAssertion, metadata, "float");
VerifyCSharpDiagnostic(sourceAssertion, metadata, "decimal");
}
private void VerifyCSharpDiagnostic(string sourceAssertion, DiagnosticMetadata metadata, string numericType)
{
var source = GenerateCode.NumericAssertion(sourceAssertion, numericType);
DiagnosticVerifier.VerifyDiagnostic(new DiagnosticVerifierArguments()
.WithSources(source)
.WithAllAnalyzers()
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0)
.WithExpectedDiagnostics(new LegacyDiagnosticResult
{
Id = FluentAssertionsAnalyzer.DiagnosticId,
Message = metadata.Message,
VisitorName = metadata.Name,
Locations = new DiagnosticResultLocation[]
{
new DiagnosticResultLocation("Test0.cs", 10, 13)
},
Severity = DiagnosticSeverity.Info
})
);
}
private void VerifyCSharpFix(string oldSourceAssertion, string newSourceAssertion)
{
VerifyCSharpFix(oldSourceAssertion, newSourceAssertion, "double");
VerifyCSharpFix(oldSourceAssertion, newSourceAssertion, "float");
VerifyCSharpFix(oldSourceAssertion, newSourceAssertion, "decimal");
}
private void VerifyCSharpFix(string oldSourceAssertion, string newSourceAssertion, string numericType)
{
var oldSource = GenerateCode.NumericAssertion(oldSourceAssertion, numericType);
var newSource = GenerateCode.NumericAssertion(newSourceAssertion, numericType);
DiagnosticVerifier.VerifyFix(new CodeFixVerifierArguments()
.WithCodeFixProvider<FluentAssertionsCodeFixProvider>()
.WithDiagnosticAnalyzer<FluentAssertionsAnalyzer>()
.WithSources(oldSource)
.WithFixedSources(newSource)
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0)
);
}
}
}