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

Refactor TraitsFeature #457

Closed
wants to merge 4 commits into from
Closed
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
89 changes: 0 additions & 89 deletions src/NUnitTestAdapter/CategoryList.cs

This file was deleted.

37 changes: 37 additions & 0 deletions src/NUnitTestAdapter/NUnitTestCaseProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// ***********************************************************************
// Copyright (c) 2018 Charlie Poole, Terje Sandstrom
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

using Microsoft.VisualStudio.TestPlatform.ObjectModel;

namespace NUnit.VisualStudio.TestAdapter
{
internal static class NUnitTestCaseProperties
{
public static readonly TestProperty TestCategory = TestProperty.Register(
id: "NUnit.TestCategory",
label: "TestCategory",
valueType: typeof(string[]),
attributes: TestPropertyAttributes.Hidden | TestPropertyAttributes.Trait,
owner: typeof(TestCase));
}
}
11 changes: 5 additions & 6 deletions src/NUnitTestAdapter/TestConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Expand Down 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, List<Trait>>();

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

public IDictionary<string, List<Trait>> 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,_logger);
TraitsProvider.GetTraitInfo(testNode).ApplyTo(testCase);

return testCase;
}
Expand Down
97 changes: 97 additions & 0 deletions src/NUnitTestAdapter/TestTraitInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// ***********************************************************************
// Copyright (c) 2013-2018 Charlie Poole, Terje Sandstrom
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;

namespace NUnit.VisualStudio.TestAdapter
{
public struct TestTraitInfo
{
public Trait[] Traits { get; }
public string[] Categories { get; }

public TestTraitInfo(Trait[] traits, string[] categories)
{
Traits = traits;
Categories = categories;
}

public void ApplyTo(TestCase testCase)
{
if (testCase == null) throw new ArgumentNullException(nameof(testCase));

if (Traits != null) testCase.Traits.AddRange(Traits);
if (Categories != null) testCase.SetPropertyValue(NUnitTestCaseProperties.TestCategory, Categories);
}

public static TestTraitInfo FromTestCase(TestCase testCase)
{
if (testCase == null) throw new ArgumentNullException(nameof(testCase));

return new TestTraitInfo(
testCase.Traits.ToArray(),
(string[])testCase.GetPropertyValue(NUnitTestCaseProperties.TestCategory));
}

public static TestTraitInfo Combine(TestTraitInfo inherited, TestTraitInfo current)
{
return new TestTraitInfo(
CombineTraits(inherited.Traits, current.Traits),
CombineCategories(inherited.Categories, current.Categories));
}

private static Trait[] CombineTraits(Trait[] inherited, Trait[] current)
{
if (inherited == null) return current;
if (current == null) return inherited;
if (inherited.Length == 0) return current;
if (current.Length == 0) return inherited;

var combined = new Trait[inherited.Length + current.Length];
inherited.CopyTo(combined, 0);
current.CopyTo(combined, inherited.Length);

return combined;
}

private static string[] CombineCategories(string[] inherited, string[] current)
{
if (inherited == null) return current;
if (current == null) return inherited;
if (inherited.Length == 0) return current;
if (current.Length == 0) return inherited;

var combined = new List<string>(inherited.Length + current.Length);
combined.AddRange(inherited);

foreach (var category in current)
if (!combined.Contains(category))
combined.Add(category);

return combined.ToArray();
}
}
}
24 changes: 12 additions & 12 deletions src/NUnitTestAdapter/TfsTestFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public class TfsTestFilter : ITfsTestFilter

///</summary>
private static readonly Dictionary<string, TestProperty> SupportedPropertiesCache;
private static readonly Dictionary<string, NTrait> SupportedTraitCache;
private static readonly Dictionary<NTrait, TestProperty> TraitPropertyMap;
private static readonly Dictionary<string, Trait> SupportedTraitCache;
private static readonly Dictionary<Trait, TestProperty> TraitPropertyMap;
private static readonly List<string> SupportedProperties;

static TfsTestFilter()
Expand All @@ -59,15 +59,15 @@ static TfsTestFilter()
SupportedPropertiesCache = new Dictionary<string, TestProperty>(StringComparer.OrdinalIgnoreCase);
SupportedPropertiesCache["FullyQualifiedName"] = TestCaseProperties.FullyQualifiedName;
SupportedPropertiesCache["Name"] = TestCaseProperties.DisplayName;
SupportedPropertiesCache["TestCategory"] = CategoryList.NUnitTestCategoryProperty;
SupportedPropertiesCache["TestCategory"] = NUnitTestCaseProperties.TestCategory;
// Initialize the trait cache
var priorityTrait = new NTrait("Priority", "");
var categoryTrait = new NTrait("Category", "");
SupportedTraitCache = new Dictionary<string, NTrait>(StringComparer.OrdinalIgnoreCase);
var priorityTrait = new Trait("Priority", "");
var categoryTrait = new Trait("Category", "");
SupportedTraitCache = new Dictionary<string, Trait>(StringComparer.OrdinalIgnoreCase);
SupportedTraitCache["Priority"] = priorityTrait;
SupportedTraitCache["TestCategory"] = categoryTrait;
// Initialize the trait property map, since TFS doesnt know about traits
TraitPropertyMap = new Dictionary<NTrait, TestProperty>(new NTraitNameComparer());
TraitPropertyMap = new Dictionary<Trait, TestProperty>(new TraitNameComparer());
var priorityProperty = TestProperty.Find("Priority") ??
TestProperty.Register("Priority", "Priority", typeof(string), typeof(TestCase));
TraitPropertyMap[priorityTrait] = priorityProperty;
Expand Down Expand Up @@ -189,9 +189,9 @@ public static TestProperty PropertyProvider(string propertyName)
return null;
}

public static NTrait TraitProvider(string traitName)
public static Trait TraitProvider(string traitName)
{
NTrait testTrait;
Trait testTrait;
SupportedTraitCache.TryGetValue(traitName, out testTrait);
return testTrait;
}
Expand All @@ -200,15 +200,15 @@ public static NTrait TraitProvider(string traitName)

}

public class NTraitNameComparer : IEqualityComparer<NTrait>
public class TraitNameComparer : IEqualityComparer<Trait>
{

public bool Equals(NTrait n, NTrait y)
public bool Equals(Trait n, Trait y)
{
return n.Name == y.Name;
}

public int GetHashCode(NTrait obj)
public int GetHashCode(Trait obj)
{
return obj.Name.GetHashCode();
}
Expand Down
Loading