Skip to content

Commit 0543068

Browse files
authored
Merge pull request #1768 from JoeRobich/generic-test-classes
Update test method matching to support generic classes
2 parents 08017b2 + c02acf4 commit 0543068

4 files changed

Lines changed: 49 additions & 1 deletion

File tree

src/OmniSharp.DotNetTest/VSTestManager.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,32 @@ bool isInRequestedMethods(TestCase testCase)
334334
}
335335

336336
testName = testName.Trim();
337+
338+
// Discovered tests in generic classes come back in the form `Namespace.GenericClass<TParam>.TestName`
339+
// however requested test names are sent from the IDE in the form of `Namespace.GenericClass`1.TestName`
340+
// to compensate we format each part of the discovered test name to match what the IDE would send.
341+
testName = string.Join(".", testName.Split('.').Select(FormatAsMetadata));
342+
337343
return hashset.Contains(testName, StringComparer.Ordinal);
338344
};
345+
346+
static string FormatAsMetadata(string name)
347+
{
348+
if (!name.EndsWith(">"))
349+
{
350+
return name;
351+
}
352+
353+
var genericParamStart = name.IndexOf('<');
354+
if (genericParamStart < 0)
355+
{
356+
return name;
357+
}
358+
359+
var genericParams = name.Substring(genericParamStart, name.Length - genericParamStart - 1);
360+
var paramCount = genericParams.Split(',').Length;
361+
return $"{name.Substring(0, genericParamStart)}`{paramCount}";
362+
}
339363
}
340364

341365
private TestCase[] DiscoverTests(string[] methodNames, string runSettings, string targetFrameworkVersion)

test-assets/test-projects/NUnitTestProject/TestProgram.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void CheckStandardOutput()
4242
{
4343
int a = 1, b = 1;
4444
Console.WriteLine($"a = {a}, b = {b}");
45-
Assert.AreEqual(a,b);
45+
Assert.AreEqual(a, b);
4646
}
4747

4848
public void UtilityFunction()
@@ -52,4 +52,15 @@ public void UtilityFunction()
5252

5353
private static int[] _items = new int[1] { 1 };
5454
}
55+
56+
[TestFixture(typeof(int))]
57+
[TestFixture(typeof(double))]
58+
public class GenericTest<T>
59+
{
60+
[Test]
61+
public void TypedTest()
62+
{
63+
Assert.NotNull(default(T));
64+
}
65+
}
5566
}

tests/OmniSharp.DotNetTest.Tests/RunTestFacts.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ public async Task RunNunitStandardOutputIsReturned()
167167
Assert.NotEmpty(response.Results[0].StandardOutput);
168168
}
169169

170+
[Fact]
171+
public async Task RunNunitTypedTestRunsTwice()
172+
{
173+
var response = await RunDotNetTestAsync(
174+
NUnitTestProject,
175+
methodName: "Main.Test.GenericTest`1.TypedTest",
176+
testFramework: "nunit",
177+
shouldPass: true);
178+
179+
Assert.Equal(2, response.Results.Length);
180+
}
181+
170182
[Fact]
171183
public async Task RunMSTestTest()
172184
{

tests/OmniSharp.DotNetTest.Tests/TestDiscoveryFacts.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public TestDiscoveryFacts(ITestOutputHelper output)
4444
[InlineData(NUnitTestProject, TestProgram, 28, 20, true, nunit, NUnitTestMethod, "Main.Test.MainTest.SourceDataDrivenTest")]
4545
[InlineData(NUnitTestProject, TestProgram, 34, 20, true, nunit, NUnitTestMethod, "Main.Test.MainTest.FailingTest")]
4646
[InlineData(NUnitTestProject, TestProgram, 47, 20, false, "", "", "")]
47+
[InlineData(NUnitTestProject, TestProgram, 60, 20, true, nunit, NUnitTestMethod, "Main.Test.GenericTest`1.TypedTest")]
4748
public async Task FindTestMethods(string projectName, string fileName, int line, int column, bool expectToFind, string expectedTestFramework, string expectedFeatureName, string expectedMethodName)
4849
{
4950
using (var testProject = await this._testAssets.GetTestProjectAsync(projectName))

0 commit comments

Comments
 (0)