Skip to content

Commit

Permalink
Moved cache into TraitsFeature as instance state
Browse files Browse the repository at this point in the history
  • Loading branch information
jnm2 committed Feb 21, 2018
1 parent e530b0c commit d9f9001
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
7 changes: 3 additions & 4 deletions src/NUnitTestAdapter/TestConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ public sealed class TestConverter : IDisposable
private readonly NavigationDataProvider _navigationDataProvider;
private readonly bool _collectSourceInformation;

internal TraitsProvider TraitsProvider { get; } = new TraitsProvider();

public TestConverter(ITestLogger logger, string sourceAssembly, bool collectSourceInformation)
{
_logger = logger;
_sourceAssembly = sourceAssembly;
_vsTestCaseMap = new Dictionary<string, TestCase>();
_collectSourceInformation = collectSourceInformation;
TraitsCache = new Dictionary<string, TestTraitInfo>();

if (_collectSourceInformation)
{
Expand All @@ -59,8 +60,6 @@ public void Dispose()
_navigationDataProvider?.Dispose();
}

public IDictionary<string, TestTraitInfo> TraitsCache { get; }

#region Public Methods
/// <summary>
/// Converts an NUnit test into a TestCase for Visual Studio,
Expand Down Expand Up @@ -161,7 +160,7 @@ private TestCase MakeTestCaseFromXmlNode(XmlNode testNode)
}
}

testCase.AddTraitsFromTestNode(testNode, TraitsCache);
TraitsProvider.GetTraitInfo(testNode).ApplyTo(testCase);

return testCase;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,21 @@

namespace NUnit.VisualStudio.TestAdapter
{
public static class TraitsFeature
public sealed class TraitsProvider
{
public static void AddTraitsFromTestNode(this TestCase testCase, XmlNode testNode, IDictionary<string, TestTraitInfo> traitsCache)
{
var combinedTraitInfo = BuildTestTraitInfo(traitsCache, testNode);
private readonly Dictionary<string, TestTraitInfo> cachedInfoByTestId = new Dictionary<string, TestTraitInfo>();

combinedTraitInfo.ApplyTo(testCase);
}
internal IDictionary<string, TestTraitInfo> CachedInfoByTestId => cachedInfoByTestId;

private static TestTraitInfo BuildTestTraitInfo(IDictionary<string, TestTraitInfo> traitsCache, XmlNode testNode)
public TestTraitInfo GetTraitInfo(XmlNode testNode)
{
var currentNodeInfo = ParseNodeTraits(testNode);

var parentId = testNode.ParentNode?.Attributes?["id"]?.Value;
if (parentId == null) return currentNodeInfo;

if (!traitsCache.TryGetValue(parentId, out var combinedAncestorInfo))
traitsCache.Add(parentId, combinedAncestorInfo = BuildTestTraitInfo(traitsCache, testNode.ParentNode));
if (!cachedInfoByTestId.TryGetValue(parentId, out var combinedAncestorInfo))
cachedInfoByTestId.Add(parentId, combinedAncestorInfo = GetTraitInfo(testNode.ParentNode));

return TestTraitInfo.Combine(combinedAncestorInfo, currentNodeInfo);
}
Expand Down
4 changes: 2 additions & 2 deletions src/NUnitTestAdapterTests/TestConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void CanMakeTestCaseFromTestWithCache()

CheckTestCase(testCase);

CheckNodeProperties(testConverter.TraitsCache, "121", categories: new[] { "super" });
CheckNodeProperties(testConverter.TraitsProvider.CachedInfoByTestId, "121", categories: new[] { "super" });
}

[Test]
Expand All @@ -80,7 +80,7 @@ public void CanMakeTestCaseShouldBuildTraitsCache()
var testCase = testConverter.ConvertTestCase(node);
}

var traitsCache = testConverter.TraitsCache;
var traitsCache = testConverter.TraitsProvider.CachedInfoByTestId;

// There are 12 ids in the TestXml2, but will be storing only ancestor properties.
// Not the leaf node test-case properties.
Expand Down

0 comments on commit d9f9001

Please sign in to comment.