Skip to content

Commit

Permalink
Added more unit tests to cover the case we cannot detect errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
manfred-brands committed Jan 28, 2023
1 parent 1aa644d commit 261d71a
Showing 1 changed file with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Linq;
using Gu.Roslyn.Asserts;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
Expand Down Expand Up @@ -553,7 +552,7 @@ public void AnalyzeWhenNumberOfParametersOfTestIsLessThanProvidedByTestCaseSourc
{
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@"
[TestFixture]
public class AnalyzeWhenNumberOfParametersDoesNotMatchNoParametersExpected
public class AnalyzeWhenNumberOfParametersOfTestIsLessThanProvidedByTestCaseSource
{
[TestCaseSource(↓nameof(TestData), new object[] { 3 })]
public void ShortName()
Expand Down Expand Up @@ -581,7 +580,7 @@ public void AnalyzeWhenNumberOfParametersOfTestIsMoreThanProvidedByTestCaseSourc
{
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@"
[TestFixture]
public class AnalyzeWhenNumberOfParametersDoesNotMatchNoParametersExpected
public class AnalyzeWhenNumberOfParametersOfTestIsMoreThanProvidedByTestCaseSource
{
[TestCaseSource(↓nameof(TestData))]
public void ShortName(int x, int y)
Expand All @@ -608,7 +607,7 @@ public void AnalyzeWhenParameterTypeOfTestDiffersFromTestCaseSource()
{
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@"
[TestFixture]
public class AnalyzeWhenNumberOfParametersDoesNotMatchNoParametersExpected
public class AnalyzeWhenParameterTypeOfTestDiffersFromTestCaseSource
{
[TestCaseSource(↓nameof(TestData))]
public void ShortName(string message)
Expand All @@ -631,14 +630,14 @@ static IEnumerable<int> TestData()
}

[Test]
[Explicit("The code is wrong, but it is too complext for the analyzer to detect this.")]
public void AnalyzeWhenNumberOfParametersOfTestIsNotEvidentFromTestSource()
{
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@"
[TestFixture]
public class AnalyzeWhenNumberOfParametersDoesNotMatchNoParametersExpected
public class AnalyzeWhenNumberOfParametersOfTestIsNotEvidentFromTestSource
{
[TestCaseSource(↓nameof(TestData))]
[Explicit(""The code is wrong, but it is too complext for the analyzer to detect this."")]
[TestCaseSource(nameof(TestData))]
public void ShortName(int n)
{
Assert.That(n, Is.GreaterThanOrEqualTo(0));
Expand All @@ -651,10 +650,54 @@ static IEnumerable<object> TestData()
}
}", additionalUsings: "using System.Collections.Generic;");

var expectedDiagnostic = ExpectedDiagnostic
.Create(AnalyzerIdentifiers.TestCaseSourceMismatchInNumberOfTestMethodParameters)
.WithMessage("The TestCaseSource provides '>0' parameter(s), but the Test method expects '1' parameter(s)");
RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, testCode);
// The TestCaseSource provides '>0' parameter(s), but the Test method expects '1' parameter(s)
// Analyzing the actual code inside the TestCaseSource method is beyond the scope of the analyzer.
RoslynAssert.Valid(analyzer, testCode);
}

[TestCase("IEnumerable", "object", "System.Collections")]
[TestCase("IEnumerable<object>", "object", "System.Collections.Generic")]
[TestCase("IEnumerable<TestCaseData>", "TestCaseData", "System.Collections.Generic")]
[TestCase("IEnumerable<int>", "int", "System.Collections.Generic")]
public void NoIssueIsRaisedWhenOneParameterIsExpectedAndTypeCannotBeDetermined(string enumerableType, string testCaseType, string collections)
{
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing($@"
using {collections};
public class NoIssueIsRaisedWhenOneParameterIsExpectedAndTypeCannotBeDetermined
{{
[TestCaseSource(nameof(TestData))]
public void ShortName(int n)
{{
Assert.That(n, Is.GreaterThanOrEqualTo(0));
}}
public static {enumerableType} TestData() => Array.Empty<{testCaseType}>();
}}");

RoslynAssert.Valid(analyzer, testCode);
}

[TestCase("IEnumerable", "object", "System.Collections")]
[TestCase("IEnumerable<object>", "object", "System.Collections.Generic")]
[TestCase("IEnumerable<TestCaseData>", "TestCaseData", "System.Collections.Generic")]
public void NoIssueIsRaisedWhenMultipleParameterAreExpectedAndTypeCannotBeDetermined(string enumerableType, string testCaseType, string collections)
{
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing($@"
using {collections};
public class NoIssueIsRaisedWhenMultipleParameterAreExpectedAndTypeCannotBeDetermined
{{
[TestCaseSource(nameof(TestData))]
public void ShortName(int first, int second)
{{
Assert.That(first, Is.LessThan(second));
}}
public static {enumerableType} TestData() => Array.Empty<{testCaseType}>();
}}");

RoslynAssert.Valid(analyzer, testCode);
}
}
}

0 comments on commit 261d71a

Please sign in to comment.