Skip to content

Commit

Permalink
Merge pull request #229 from csoltenborn/#227_FixHandlingOfNamespaces
Browse files Browse the repository at this point in the history
bugfix: source locations for tests contained in namespaces are found again (#227)
  • Loading branch information
csoltenborn authored Jun 28, 2018
2 parents 9970eaf + 6e73471 commit b7a072c
Show file tree
Hide file tree
Showing 20 changed files with 247 additions and 20 deletions.
1 change: 1 addition & 0 deletions GoogleTestAdapter/Core.Tests/Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Compile Include="Helpers\UtilsTests.cs" />
<Compile Include="Runners\CommandLineGeneratorTests.cs" />
<Compile Include="Runners\SequentialTestRunnerTests.cs" />
<Compile Include="TestCases\TestCaseResolverTests.cs" />
<Compile Include="TestCases\TestCaseFactoryTests.cs" />
<Compile Include="TestCases\ListTestsParserTests.cs" />
<Compile Include="TestResults\StreamingStandardOutputTestResultParserTests.cs" />
Expand Down
4 changes: 4 additions & 0 deletions GoogleTestAdapter/Core.Tests/GoogleTestDiscovererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ private void AssertFindsTest(string fullyQualifiedName, Regex displayNameRegex)
var discoverer = new GoogleTestDiscoverer(TestEnvironment.Logger, TestEnvironment.Options);
IList<TestCase> tests = discoverer.GetTestsFromExecutable(TestResources.Tests_DebugX86);

MockLogger.Verify(l => l.LogError(It.IsAny<string>()), Times.Never);
MockLogger.Verify(l => l.DebugError(It.IsAny<string>()), Times.Never);
tests.Should().NotBeEmpty();

TestCase testCase = tests.Single(t => t.FullyQualifiedName == fullyQualifiedName);
testCase.DisplayName.Should().MatchRegex(displayNameRegex.ToString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,12 @@ private void AssertFindsTestWithTraits(string displayName, Trait[] traits)
GoogleTestDiscoverer discoverer = new GoogleTestDiscoverer(TestEnvironment.Logger, TestEnvironment.Options);
List<TestCase> tests = discoverer.GetTestsFromExecutable(SampleTestToUse).ToList();

MockLogger.Verify(l => l.LogError(It.IsAny<string>()), Times.Never);
MockLogger.Verify(l => l.DebugError(It.IsAny<string>()), Times.Never);
tests.Should().NotBeEmpty();

TestCase testCase = tests.Find(tc => tc.Traits.Count == traits.Length && tc.DisplayName.StartsWith(displayName));
testCase.Should().NotBeNull($"Test not found: {displayName}, {traits.Length}");
testCase.Should().NotBeNull($"Test should exist: {displayName}, {traits.Length}");

foreach (Trait trait in traits)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void ExecuteProcessBlocking_SampleTests()

exitCode.Should().Be(1);
output.Should().Contain(s => s.Contains("TestMath.AddPasses"));
output.Count.Should().Be(533);
output.Count.Should().Be(563);
}

}
Expand Down
87 changes: 87 additions & 0 deletions GoogleTestAdapter/Core.Tests/TestCases/TestCaseResolverTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System.Linq;
using FluentAssertions;
using GoogleTestAdapter.DiaResolver;
using GoogleTestAdapter.Helpers;
using GoogleTestAdapter.Tests.Common;
using GoogleTestAdapter.Tests.Common.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using static GoogleTestAdapter.Tests.Common.TestMetadata.TestCategories;

namespace GoogleTestAdapter.TestCases
{

[TestClass]
public class TestCaseResolverTests : TestsBase
{
private FakeLogger _fakeLogger;

[TestInitialize]
public void Setup()
{
_fakeLogger = new FakeLogger(() => true, false);
}

[TestMethod]
[TestCategory(Integration)]
public void FindTestCaseLocation_Namespace_Named_LocationIsFound()
{
AssertCorrectTestLocationIsFound("Namespace_Named", 9);
}

[TestMethod]
[TestCategory(Integration)]
public void FindTestCaseLocation_Namespace_Named_Named_LocationIsFound()
{
AssertCorrectTestLocationIsFound("Namespace_Named_Named", 16);
}

[TestMethod]
[TestCategory(Integration)]
public void FindTestCaseLocation_Namespace_Named_Anon_LocationIsFound()
{
AssertCorrectTestLocationIsFound("Namespace_Named_Anon", 25);
}

[TestMethod]
[TestCategory(Integration)]
public void FindTestCaseLocation_Namespace_Anon_LocationIsFound()
{
AssertCorrectTestLocationIsFound("Namespace_Anon", 35);
}

[TestMethod]
[TestCategory(Integration)]
public void FindTestCaseLocation_Namespace_Anon_Anon_LocationIsFound()
{
AssertCorrectTestLocationIsFound("Namespace_Anon_Anon", 42);
}

[TestMethod]
[TestCategory(Integration)]
public void FindTestCaseLocation_Namespace_Anon_Named_LocationIsFound()
{
AssertCorrectTestLocationIsFound("Namespace_Anon_Named", 51);
}

private void AssertCorrectTestLocationIsFound(string suite, uint line)
{
var descriptor = new TestCaseDescriptor(
suite,
"Test",
$"{suite}.Test",
$"{suite}.Test",
TestCaseDescriptor.TestTypes.Simple);
var signatures = new MethodSignatureCreator().GetTestMethodSignatures(descriptor);
var resolver = new TestCaseResolver(TestResources.Tests_ReleaseX64, "", "".Yield(),
new DefaultDiaResolverFactory(), true, _fakeLogger);

var testCaseLocation = resolver.FindTestCaseLocation(signatures.ToList());

_fakeLogger.Errors.Should().BeEmpty();
testCaseLocation.Should().NotBeNull();
testCaseLocation.Sourcefile.Should().EndWithEquivalent(@"sampletests\tests\namespacetests.cpp");
testCaseLocation.Line.Should().Be(line);
}
}

}
4 changes: 2 additions & 2 deletions GoogleTestAdapter/Core/TestCases/MethodSignatureCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace GoogleTestAdapter.TestCases
{

internal class MethodSignatureCreator
public class MethodSignatureCreator
{

internal IEnumerable<string> GetTestMethodSignatures(TestCaseDescriptor descriptor)
public IEnumerable<string> GetTestMethodSignatures(TestCaseDescriptor descriptor)
{
switch (descriptor.TestType)
{
Expand Down
2 changes: 1 addition & 1 deletion GoogleTestAdapter/Core/TestCases/TestCaseDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public enum TestTypes { Simple, Parameterized, TypeParameterized }
public string DisplayName { get; }
public TestTypes TestType { get; }

internal TestCaseDescriptor(string suite, string name, string fullyQualifiedName, string displayName, TestTypes testType)
public TestCaseDescriptor(string suite, string name, string fullyQualifiedName, string displayName, TestTypes testType)
{
Suite = suite;
Name = name;
Expand Down
6 changes: 3 additions & 3 deletions GoogleTestAdapter/Core/TestCases/TestCaseLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace GoogleTestAdapter.TestCases
{

internal class TestCaseLocation : SourceFileLocation
public class TestCaseLocation : SourceFileLocation
{
internal List<Trait> Traits { get; } = new List<Trait>();
public List<Trait> Traits { get; } = new List<Trait>();

internal TestCaseLocation(string symbol, string sourceFile, uint line) : base(symbol, sourceFile, line)
public TestCaseLocation(string symbol, string sourceFile, uint line) : base(symbol, sourceFile, line)
{
}
}
Expand Down
10 changes: 5 additions & 5 deletions GoogleTestAdapter/Core/TestCases/TestCaseResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace GoogleTestAdapter.TestCases
{

internal class TestCaseResolver
public class TestCaseResolver
{
// see GTA_Traits.h
private const string TraitSeparator = "__GTA__";
Expand All @@ -29,7 +29,7 @@ internal class TestCaseResolver
private bool _loadedSymbolsFromAdditionalPdbs;
private bool _loadedSymbolsFromImports;

internal TestCaseResolver(string executable, string pathExtension, IEnumerable<string> additionalPdbs, IDiaResolverFactory diaResolverFactory, bool parseSymbolInformation, ILogger logger)
public TestCaseResolver(string executable, string pathExtension, IEnumerable<string> additionalPdbs, IDiaResolverFactory diaResolverFactory, bool parseSymbolInformation, ILogger logger)
{
_executable = executable;
_pathExtension = pathExtension;
Expand All @@ -48,7 +48,7 @@ internal TestCaseResolver(string executable, string pathExtension, IEnumerable<s
}
}

internal TestCaseLocation FindTestCaseLocation(List<string> testMethodSignatures)
public TestCaseLocation FindTestCaseLocation(List<string> testMethodSignatures)
{
TestCaseLocation result = DoFindTestCaseLocation(testMethodSignatures);
if (result == null && !_loadedSymbolsFromAdditionalPdbs)
Expand Down Expand Up @@ -124,15 +124,15 @@ private void AddSymbolsFromBinary(string binary, string pdb)
}
catch (Exception e)
{
_logger.DebugError($"Exception while resolving test locations and traits in {binary}\n{e}");
_logger.DebugError($"Exception while resolving test locations and traits in '{binary}':{Environment.NewLine}{e}");
}
}
}

private TestCaseLocation DoFindTestCaseLocation(List<string> testMethodSignatures)
{
return _allTestMethodSymbols
.Where(nsfl => testMethodSignatures.Any(tms => Regex.IsMatch(nsfl.Symbol, $"^{tms}"))) // Regex instead of == because nsfl might contain namespace
.Where(nsfl => testMethodSignatures.Any(tms => Regex.IsMatch(nsfl.Symbol, $@"^(((\w+)|(`anonymous namespace'))::)*{tms}"))) // Regex instead of == because nsfl might contain namespace
.Select(nsfl => ToTestCaseLocation(nsfl, _allTraitSymbols))
.FirstOrDefault(); // we need to force immediate query execution, otherwise our session object will already be released
}
Expand Down
10 changes: 8 additions & 2 deletions GoogleTestAdapter/TestAdapter.Tests/TestExecutorTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public virtual void RunTests_WithNonexistingSetupBatch_LogsError()
{
MockOptions.Setup(o => o.BatchForTestSetup).Returns("some_nonexisting_file");

RunAndVerifyTests(TestResources.DllTests_ReleaseX86, 1, 1, 0);
RunAndVerifyTests(TestResources.DllTests_ReleaseX86, 1, 1, 0, checkNoErrorsLogged: false);

MockLogger.Verify(l => l.LogError(
It.Is<string>(s => s.Contains(PreparingTestRunner.TestSetup.ToLower()))),
Expand Down Expand Up @@ -254,11 +254,17 @@ public virtual void RunTests_WithoutPathExtension_ExecutionFails()
}
}

protected void RunAndVerifyTests(string executable, int nrOfPassedTests, int nrOfFailedTests, int nrOfUnexecutedTests, int nrOfSkippedTests = 0)
protected void RunAndVerifyTests(string executable, int nrOfPassedTests, int nrOfFailedTests, int nrOfUnexecutedTests, int nrOfSkippedTests = 0, bool checkNoErrorsLogged = true)
{
TestExecutor executor = new TestExecutor(TestEnvironment.Logger, TestEnvironment.Options);
executor.RunTests(executable.Yield(), MockRunContext.Object, MockFrameworkHandle.Object);

if (checkNoErrorsLogged)
{
MockLogger.Verify(l => l.LogError(It.IsAny<string>()), Times.Never);
MockLogger.Verify(l => l.DebugError(It.IsAny<string>()), Times.Never);
}

CheckMockInvocations(nrOfPassedTests, nrOfFailedTests, nrOfUnexecutedTests, nrOfSkippedTests);
}

Expand Down
6 changes: 3 additions & 3 deletions GoogleTestAdapter/Tests.Common/TestResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public static class TestResources
public const string Tests_DebugX64 = SampleTestsBuildDir + @"Debug-x64\Tests_gta.exe";
public const string Tests_ReleaseX64 = SampleTestsBuildDir + @"Release-x64\Tests_gta.exe";
public const string Tests_ReleaseX64_Output = TestdataDir + @"Tests_gta_exe_output.txt";
public const int NrOfTests = 101;
public const int NrOfPassingTests = 47;
public const int NrOfTests = 107;
public const int NrOfPassingTests = 53;
public const int NrOfFailingTests = 54;
public const int NrOfGtest170CompatibleTests = 97;
public const int NrOfGtest170CompatibleTests = 103;

public static readonly string LoadTests_ReleaseX86 = Path.Combine(SampleTestsBuildDir, @"Release\LoadTests_gta.exe");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ OutputHandling.OneLine
abcd.t
bbcd.t
bcd.t
Namespace_Named.Test
Namespace_Named_Named.Test
Namespace_Named_Anon.Test
Namespace_Anon.Test
Namespace_Anon_Anon.Test
Namespace_Anon_Named.Test
MessageParserTests.SimpleAssert
MessageParserTests.SimpleExpect
MessageParserTests.ExpectAndAssert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ TheFixture.Crash
TheFixture.AddPassesWithTraits
TheFixture.AddPassesWithTraits2
TheFixture.AddPassesWithTraits3
Namespace_Named.Test
Namespace_Named_Named.Test
Namespace_Named_Anon.Test
Namespace_Anon.Test
Namespace_Anon_Anon.Test
Namespace_Anon_Named.Test
MessageParserTests.SimpleAssert
MessageParserTests.SimpleExpect
MessageParserTests.ExpectAndAssert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ at --&gt;TestMethod Outer in $(Directory)\sampletests\tests\scopedtracestests.cp
</ErrorInfo>
</Output>
</UnitTestResult>
<UnitTestResult testName="Namespace_Anon.Test" outcome="Passed" />
<UnitTestResult testName="Namespace_Anon_Anon.Test" outcome="Passed" />
<UnitTestResult testName="Namespace_Anon_Named.Test" outcome="Passed" />
<UnitTestResult testName="Namespace_Named.Test" outcome="Passed" />
<UnitTestResult testName="Namespace_Named_Anon.Test" outcome="Passed" />
<UnitTestResult testName="Namespace_Named_Named.Test" outcome="Passed" />
<UnitTestResult testName="OutputHandling.ManyLinesWithNewlines" outcome="Failed">
<Output>
<ErrorInfo>
Expand Down Expand Up @@ -707,6 +713,24 @@ Which is: "\xE4\xF6\xFC\xDF\xC4\xD6\xDC"</Message>
<UnitTest name="MessageParserTests.TwoScopedTracesInTestMethod" storage="$(Directory)\sampletests\${ConfigurationName}\tests_1.7.0_gta.exe">
<TestMethod name="MessageParserTests.TwoScopedTracesInTestMethod" className="MessageParserTests" adapterTypeName="executor://googletestrunner/v1" codeBase="$(Directory)\SampleTests\${ConfigurationName}\Tests_1.7.0_gta.exe" />
</UnitTest>
<UnitTest name="Namespace_Anon.Test" storage="$(Directory)\sampletests\${ConfigurationName}\tests_1.7.0_gta.exe">
<TestMethod name="Namespace_Anon.Test" className="Namespace_Anon" adapterTypeName="executor://googletestrunner/v1" codeBase="$(Directory)\SampleTests\${ConfigurationName}\Tests_1.7.0_gta.exe" />
</UnitTest>
<UnitTest name="Namespace_Anon_Anon.Test" storage="$(Directory)\sampletests\${ConfigurationName}\tests_1.7.0_gta.exe">
<TestMethod name="Namespace_Anon_Anon.Test" className="Namespace_Anon_Anon" adapterTypeName="executor://googletestrunner/v1" codeBase="$(Directory)\SampleTests\${ConfigurationName}\Tests_1.7.0_gta.exe" />
</UnitTest>
<UnitTest name="Namespace_Anon_Named.Test" storage="$(Directory)\sampletests\${ConfigurationName}\tests_1.7.0_gta.exe">
<TestMethod name="Namespace_Anon_Named.Test" className="Namespace_Anon_Named" adapterTypeName="executor://googletestrunner/v1" codeBase="$(Directory)\SampleTests\${ConfigurationName}\Tests_1.7.0_gta.exe" />
</UnitTest>
<UnitTest name="Namespace_Named.Test" storage="$(Directory)\sampletests\${ConfigurationName}\tests_1.7.0_gta.exe">
<TestMethod name="Namespace_Named.Test" className="Namespace_Named" adapterTypeName="executor://googletestrunner/v1" codeBase="$(Directory)\SampleTests\${ConfigurationName}\Tests_1.7.0_gta.exe" />
</UnitTest>
<UnitTest name="Namespace_Named_Anon.Test" storage="$(Directory)\sampletests\${ConfigurationName}\tests_1.7.0_gta.exe">
<TestMethod name="Namespace_Named_Anon.Test" className="Namespace_Named_Anon" adapterTypeName="executor://googletestrunner/v1" codeBase="$(Directory)\SampleTests\${ConfigurationName}\Tests_1.7.0_gta.exe" />
</UnitTest>
<UnitTest name="Namespace_Named_Named.Test" storage="$(Directory)\sampletests\${ConfigurationName}\tests_1.7.0_gta.exe">
<TestMethod name="Namespace_Named_Named.Test" className="Namespace_Named_Named" adapterTypeName="executor://googletestrunner/v1" codeBase="$(Directory)\SampleTests\${ConfigurationName}\Tests_1.7.0_gta.exe" />
</UnitTest>
<UnitTest name="OutputHandling.ManyLinesWithNewlines" storage="$(Directory)\sampletests\${ConfigurationName}\tests_1.7.0_gta.exe">
<TestMethod name="OutputHandling.ManyLinesWithNewlines" className="OutputHandling" adapterTypeName="executor://googletestrunner/v1" codeBase="$(Directory)\SampleTests\${ConfigurationName}\Tests_1.7.0_gta.exe" />
</UnitTest>
Expand Down Expand Up @@ -889,7 +913,7 @@ Which is: "\xE4\xF6\xFC\xDF\xC4\xD6\xDC"</Message>
</UnitTest>
</TestDefinitions>
<ResultSummary outcome="Failed">
<Counters total="97" executed="97" passed="44" failed="53" error="0" timeout="0" aborted="0" inconclusive="0" passedButRunAborted="0" notRunnable="0" notExecuted="0" disconnected="0" warning="0" completed="0" inProgress="0" pending="0" />
<Counters total="103" executed="103" passed="50" failed="53" error="0" timeout="0" aborted="0" inconclusive="0" passedButRunAborted="0" notRunnable="0" notExecuted="0" disconnected="0" warning="0" completed="0" inProgress="0" pending="0" />
<Output />
</ResultSummary>
</TestRun>
Loading

0 comments on commit b7a072c

Please sign in to comment.