|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 3 | + |
| 4 | +namespace FluentAssertions.Analyzers.Tests.Tips |
| 5 | +{ |
| 6 | + [TestClass] |
| 7 | + public class NumericTests |
| 8 | + { |
| 9 | + [AssertionDataTestMethod] |
| 10 | + [AssertionDiagnostic("actual.Should().BeGreaterThan(0{0});")] |
| 11 | + [AssertionDiagnostic("actual.Should().BeGreaterThan(0{0}).ToString();")] |
| 12 | + [Implemented] |
| 13 | + public void NumericShouldBePositive_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<NumericShouldBePositiveAnalyzer>(assertion); |
| 14 | + |
| 15 | + [AssertionDataTestMethod] |
| 16 | + [AssertionCodeFix( |
| 17 | + oldAssertion: "actual.Should().BeGreaterThan(0{0});", |
| 18 | + newAssertion: "actual.Should().BePositive({0});")] |
| 19 | + [AssertionCodeFix( |
| 20 | + oldAssertion: "actual.Should().BeGreaterThan(0{0}).ToString();", |
| 21 | + newAssertion: "actual.Should().BePositive({0}).ToString();")] |
| 22 | + [Implemented] |
| 23 | + public void NumericShouldBePositive_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<NumericShouldBePositiveCodeFix, NumericShouldBePositiveAnalyzer>(oldAssertion, newAssertion); |
| 24 | + |
| 25 | + [AssertionDataTestMethod] |
| 26 | + [AssertionDiagnostic("actual.Should().BeLessThan(0{0});")] |
| 27 | + [AssertionDiagnostic("actual.Should().BeLessThan(0{0}).ToString();")] |
| 28 | + [Implemented] |
| 29 | + public void NumericShouldBeNegative_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<NumericShouldBeNegativeAnalyzer>(assertion); |
| 30 | + |
| 31 | + [AssertionDataTestMethod] |
| 32 | + [AssertionCodeFix( |
| 33 | + oldAssertion: "actual.Should().BeLessThan(0{0});", |
| 34 | + newAssertion: "actual.Should().BeNegative({0});")] |
| 35 | + [AssertionCodeFix( |
| 36 | + oldAssertion: "actual.Should().BeLessThan(0{0}).ToString();", |
| 37 | + newAssertion: "actual.Should().BeNegative({0}).ToString();")] |
| 38 | + [Implemented] |
| 39 | + public void NumericShouldBeNegative_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<NumericShouldBeNegativeCodeFix, NumericShouldBeNegativeAnalyzer>(oldAssertion, newAssertion); |
| 40 | + |
| 41 | + [AssertionDataTestMethod] |
| 42 | + [AssertionDiagnostic("actual.Should().BeGreaterOrEqualTo(lower{0}).And.BeLessOrEqualTo(upper);")] |
| 43 | + [AssertionDiagnostic("actual.Should().BeGreaterOrEqualTo(lower).And.BeLessOrEqualTo(upper{0});")] |
| 44 | + [AssertionDiagnostic("actual.Should().BeGreaterOrEqualTo(lower{0}).And.BeLessOrEqualTo(upper{0});")] |
| 45 | + [AssertionDiagnostic("actual.Should().BeLessOrEqualTo(upper{0}).And.BeGreaterOrEqualTo(lower);")] |
| 46 | + [AssertionDiagnostic("actual.Should().BeLessOrEqualTo(upper).And.BeGreaterOrEqualTo(lower{0});")] |
| 47 | + [AssertionDiagnostic("actual.Should().BeLessOrEqualTo(upper{0}).And.BeGreaterOrEqualTo(lower{0});")] |
| 48 | + [Implemented] |
| 49 | + public void NumericShouldBeInRange_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<NumericShouldBeInRangeAnalyzer>(assertion); |
| 50 | + |
| 51 | + [AssertionDataTestMethod] |
| 52 | + [AssertionCodeFix( |
| 53 | + oldAssertion: "actual.Should().BeGreaterOrEqualTo(lower{0}).And.BeLessOrEqualTo(upper);", |
| 54 | + newAssertion: "actual.Should().BeInRange(lower, upper{0});")] |
| 55 | + [AssertionCodeFix( |
| 56 | + oldAssertion: "actual.Should().BeGreaterOrEqualTo(lower).And.BeLessOrEqualTo(upper{0});", |
| 57 | + newAssertion: "actual.Should().BeInRange(lower, upper{0});")] |
| 58 | + [AssertionCodeFix( |
| 59 | + oldAssertion: "actual.Should().BeLessOrEqualTo(upper{0}).And.BeGreaterOrEqualTo(lower);", |
| 60 | + newAssertion: "actual.Should().BeInRange(lower, upper{0});")] |
| 61 | + [AssertionCodeFix( |
| 62 | + oldAssertion: "actual.Should().BeLessOrEqualTo(upper).And.BeGreaterOrEqualTo(lower{0});", |
| 63 | + newAssertion: "actual.Should().BeInRange(lower, upper{0});")] |
| 64 | + [NotImplemented] |
| 65 | + public void NumericShouldBeInRange_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<NumericShouldBeApproximatelyCodeFix, NumericShouldBeInRangeAnalyzer>(oldAssertion, newAssertion); |
| 66 | + |
| 67 | + [AssertionDataTestMethod] |
| 68 | + [AssertionDiagnostic("Math.Abs(expected - actual).Should().BeLessOrEqualTo(delta{0});")] |
| 69 | + [Implemented] |
| 70 | + public void NumericShouldBeApproximately_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<NumericShouldBeApproximatelyAnalyzer>(assertion); |
| 71 | + |
| 72 | + [AssertionDataTestMethod] |
| 73 | + [AssertionCodeFix( |
| 74 | + oldAssertion: "Math.Abs(expected - actual).Should().BeLessOrEqualTo(delta{0});", |
| 75 | + newAssertion: "actual.Should().BeApproximately(expected, delta{0});")] |
| 76 | + [Implemented] |
| 77 | + public void NumericShouldBeApproximately_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<NumericShouldBeApproximatelyCodeFix, NumericShouldBeApproximatelyAnalyzer>(oldAssertion, newAssertion); |
| 78 | + |
| 79 | + private void VerifyCSharpDiagnostic<TDiagnosticAnalyzer>(string sourceAssertion) where TDiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer, new() |
| 80 | + { |
| 81 | + var source = GenerateCode.NumericAssertion(sourceAssertion); |
| 82 | + |
| 83 | + var type = typeof(TDiagnosticAnalyzer); |
| 84 | + var diagnosticId = (string)type.GetField("DiagnosticId").GetValue(null); |
| 85 | + var message = (string)type.GetField("Message").GetValue(null); |
| 86 | + |
| 87 | + DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source, new DiagnosticResult |
| 88 | + { |
| 89 | + Id = diagnosticId, |
| 90 | + Message = message, |
| 91 | + Locations = new DiagnosticResultLocation[] |
| 92 | + { |
| 93 | + new DiagnosticResultLocation("Test0.cs", 10, 13) |
| 94 | + }, |
| 95 | + Severity = DiagnosticSeverity.Info |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + private void VerifyCSharpFix<TCodeFixProvider, TDiagnosticAnalyzer>(string oldSourceAssertion, string newSourceAssertion) |
| 100 | + where TCodeFixProvider : Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider, new() |
| 101 | + where TDiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer, new() |
| 102 | + { |
| 103 | + var oldSource = GenerateCode.NumericAssertion(oldSourceAssertion); |
| 104 | + var newSource = GenerateCode.NumericAssertion(newSourceAssertion); |
| 105 | + |
| 106 | + DiagnosticVerifier.VerifyCSharpFix<TCodeFixProvider, TDiagnosticAnalyzer>(oldSource, newSource); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments