-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathNullConditionalAssertionTests.cs
76 lines (73 loc) · 3.64 KB
/
NullConditionalAssertionTests.cs
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
using FluentAssertions.Analyzers.TestUtils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text;
namespace FluentAssertions.Analyzers.Tests.Tips
{
[TestClass]
public class NullConditionalAssertionTests
{
[DataTestMethod]
[AssertionDiagnostic("actual?.Should().Be(expected{0});")]
[AssertionDiagnostic("actual?.MyProperty.Should().Be(\"test\"{0});")]
[AssertionDiagnostic("actual.MyProperty?.Should().Be(\"test\"{0});")]
[AssertionDiagnostic("(actual.MyProperty)?.Should().Be(\"test\"{0});")]
[AssertionDiagnostic("(actual?.MyProperty)?.Should().Be(\"test\"{0});")]
[AssertionDiagnostic("actual?.MyProperty.Should().Be(actual?.MyProperty{0});")]
[AssertionDiagnostic("actual.MyList?.Where(obj => obj?.ToString() == null).Count().Should().Be(0{0});")]
[Implemented]
public void NullConditionalMayNotExecuteTest(string assertion)
{
DiagnosticVerifier.VerifyDiagnostic(new DiagnosticVerifierArguments()
.WithDiagnosticAnalyzer<FluentAssertionsAnalyzer>()
.WithSources(Code(assertion))
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0)
.WithExpectedDiagnostics(new LegacyDiagnosticResult
{
Id = FluentAssertionsAnalyzer.DiagnosticId,
Message = DiagnosticMetadata.NullConditionalMayNotExecute.Message,
Severity = Microsoft.CodeAnalysis.DiagnosticSeverity.Info, // TODO: change to warning
VisitorName = nameof(DiagnosticMetadata.NullConditionalMayNotExecute),
Locations = new DiagnosticResultLocation[]
{
new DiagnosticResultLocation("Test0.cs", 11, 13)
}
})
);
}
[DataTestMethod]
[AssertionDiagnostic("(actual?.MyProperty).Should().Be(\"test\"{0});")]
[AssertionDiagnostic("actual.MyProperty.Should().Be(actual?.MyProperty{0});")]
[AssertionDiagnostic("actual.MyList.Where(obj => obj?.ToString() == null).Should().HaveCount(6{0});")]
[Implemented]
public void NullConditionalWillStillExecuteTest(string assertion)
{
DiagnosticVerifier.VerifyDiagnostic(new DiagnosticVerifierArguments()
.WithDiagnosticAnalyzer<FluentAssertionsAnalyzer>()
.WithSources(Code(assertion))
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0)
);
}
private static string Code(string assertion) =>
new StringBuilder()
.AppendLine("using System;")
.AppendLine("using System.Collections.Generic;")
.AppendLine("using System.Linq;")
.AppendLine("using FluentAssertions;using FluentAssertions.Extensions;")
.AppendLine("namespace TestNamespace")
.AppendLine("{")
.AppendLine(" class TestClass")
.AppendLine(" {")
.AppendLine(" void TestMethod(MyClass actual, MyClass expected)")
.AppendLine(" {")
.AppendLine($" {assertion}")
.AppendLine(" }")
.AppendLine(" }")
.AppendLine(" class MyClass")
.AppendLine(" {")
.AppendLine(" public string MyProperty { get; set; }")
.AppendLine(" public List<object> MyList { get; set; }")
.AppendLine(" }")
.AppendLine("}")
.ToString();
}
}