Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update test method matching to support generic classes #1768

Merged
merged 2 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/OmniSharp.DotNetTest/VSTestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,32 @@ bool isInRequestedMethods(TestCase testCase)
}

testName = testName.Trim();

// Discovered tests in generic classes come back in the form `Namespace.GenericClass<TParam>.TestName`
// however requested test names are sent from the IDE in the form of `Namespace.GenericClass`1.TestName`
// to compensate we format each part of the discovered test name to match what the IDE would send.
testName = string.Join(".", testName.Split('.').Select(FormatAsMetadata));

return hashset.Contains(testName, StringComparer.Ordinal);
};

static string FormatAsMetadata(string name)
{
if (!name.EndsWith(">"))
{
return name;
}

var genericParamStart = name.IndexOf('<');
if (genericParamStart < 0)
{
return name;
}

var genericParams = name.Substring(genericParamStart, name.Length - genericParamStart - 1);
var paramCount = genericParams.Split(',').Length;
return $"{name.Substring(0, genericParamStart)}`{paramCount}";
}
}

private TestCase[] DiscoverTests(string[] methodNames, string runSettings, string targetFrameworkVersion)
Expand Down
13 changes: 12 additions & 1 deletion test-assets/test-projects/NUnitTestProject/TestProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void CheckStandardOutput()
{
int a = 1, b = 1;
Console.WriteLine($"a = {a}, b = {b}");
Assert.AreEqual(a,b);
Assert.AreEqual(a, b);
}

public void UtilityFunction()
Expand All @@ -52,4 +52,15 @@ public void UtilityFunction()

private static int[] _items = new int[1] { 1 };
}

[TestFixture(typeof(int))]
[TestFixture(typeof(double))]
public class GenericTest<T>
{
[Test]
public void TypedTest()
{
Assert.NotNull(default(T));
}
}
}
12 changes: 12 additions & 0 deletions tests/OmniSharp.DotNetTest.Tests/RunTestFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ public async Task RunNunitStandardOutputIsReturned()
Assert.NotEmpty(response.Results[0].StandardOutput);
}

[Fact]
public async Task RunNunitTypedTestRunsTwice()
{
var response = await RunDotNetTestAsync(
NUnitTestProject,
methodName: "Main.Test.GenericTest`1.TypedTest",
testFramework: "nunit",
shouldPass: true);

Assert.Equal(2, response.Results.Length);
}

[Fact]
public async Task RunMSTestTest()
{
Expand Down
1 change: 1 addition & 0 deletions tests/OmniSharp.DotNetTest.Tests/TestDiscoveryFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public TestDiscoveryFacts(ITestOutputHelper output)
[InlineData(NUnitTestProject, TestProgram, 28, 20, true, nunit, NUnitTestMethod, "Main.Test.MainTest.SourceDataDrivenTest")]
[InlineData(NUnitTestProject, TestProgram, 34, 20, true, nunit, NUnitTestMethod, "Main.Test.MainTest.FailingTest")]
[InlineData(NUnitTestProject, TestProgram, 47, 20, false, "", "", "")]
[InlineData(NUnitTestProject, TestProgram, 60, 20, true, nunit, NUnitTestMethod, "Main.Test.GenericTest`1.TypedTest")]
public async Task FindTestMethods(string projectName, string fileName, int line, int column, bool expectToFind, string expectedTestFramework, string expectedFeatureName, string expectedMethodName)
{
using (var testProject = await this._testAssets.GetTestProjectAsync(projectName))
Expand Down