Skip to content

Commit

Permalink
Merge pull request #467 from nunit/jnm2/explicit_trait
Browse files Browse the repository at this point in the history
Adds Explicit as a trait
  • Loading branch information
OsirisTerje authored Mar 3, 2018
2 parents 3249f72 + 1465974 commit 70dcabd
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 5 deletions.
25 changes: 25 additions & 0 deletions src/NUnitTestAdapter/CategoryList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ public class CategoryList
private const string VsTestCategoryLabel = "TestCategory";
internal static readonly TestProperty NUnitTestCategoryProperty = TestProperty.Register(NUnitCategoryName, VsTestCategoryLabel, typeof(string[]), TestPropertyAttributes.Hidden | TestPropertyAttributes.Trait, typeof(TestCase));

private const string ExplicitTraitName = "Explicit";
// The empty string causes the UI we want.
// If it's null, the explicit trait doesn't show up in Test Explorer.
// If it's not empty, it shows up as “Explicit [value]” in Test Explorer.
private const string ExplicitTraitValue = "";

private readonly List<string> categorylist = new List<string>();
private readonly TestCase testCase;

Expand Down Expand Up @@ -47,11 +53,30 @@ public IEnumerable<string> ProcessTestCaseProperties(XmlNode testNode, bool addT
}
}
}

if (testNode.Attributes?["runstate"]?.Value == "Explicit")
{
if (!testCase.Traits.Any(trait => trait.Name == ExplicitTraitName))
{
testCase.Traits.Add(new Trait(ExplicitTraitName, ExplicitTraitValue));

if (addToCache)
AddTraitsToCache(traitsCache, key, ExplicitTraitName, ExplicitTraitValue);
}
}

return categorylist;
}

private static bool IsInternalProperty(string propertyName, string propertyValue)
{
if (propertyName == ExplicitTraitName)
{
// Otherwise the IsNullOrEmpty check does the wrong thing,
// but I'm not sure of the consequences of allowing all empty strings.
return false;
}

// Property names starting with '_' are for internal use only
return String.IsNullOrEmpty(propertyName) || propertyName[0] == '_' || String.IsNullOrEmpty(propertyValue);
}
Expand Down
55 changes: 55 additions & 0 deletions src/NUnitTestAdapterTests/TestCaseUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// ***********************************************************************
// 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;
using System;
using System.Collections.Generic;
using System.Xml;

namespace NUnit.VisualStudio.TestAdapter.Tests
{
/// <summary>
/// Helper methods for testing against test cases.
/// </summary>
internal static class TestCaseUtils
{
/// <summary>
/// Knows how to convert an entire XML fragment.
/// </summary>
public static IReadOnlyList<TestCase> ConvertTestCases(this TestConverter testConverter, string xml)
{
if (testConverter == null) throw new ArgumentNullException(nameof(testConverter));

var fragment = new XmlDocument().CreateDocumentFragment();
fragment.InnerXml = xml;
var testCaseNodes = fragment.SelectNodes("//test-case");

var testCases = new TestCase[testCaseNodes.Count];

for (var i = 0; i < testCases.Length; i++)
testCases[i] = testConverter.ConvertTestCase(testCaseNodes[i]);

return testCases;
}
}
}
62 changes: 57 additions & 5 deletions src/NUnitTestAdapterTests/TraitsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using NSubstitute;
using NUnit.Framework;

using NUnit.VisualStudio.TestAdapter.Tests.Fakes;

namespace NUnit.VisualStudio.TestAdapter.Tests
{

public class TestDataForTraits
{
#region TestXml Data
Expand Down Expand Up @@ -366,8 +365,6 @@ public class TestDataForTraits

}

#if !NETCOREAPP1_0

[Category(nameof(TestTraits))]
public class TestTraits
{
Expand Down Expand Up @@ -539,6 +536,61 @@ private void VerifyCategoriesOnly(TestCase testcase, int expectedCategories, str
});
}

private static IReadOnlyList<TestCase> GetTestCases(string xml)
{
using (var converter = new TestConverter(
new TestLogger(new MessageLoggerStub()),
sourceAssembly: "unused",
collectSourceInformation: false))
{
return converter.ConvertTestCases(xml);
}
}

[Test]
public static void ThatExplicitTestCaseHasExplicitTrait()
{
var testCase = GetTestCases(
@"<test-suite id='1' name='Fixture' fullname='Fixture' classname='Fixture'>
<test-case id='2' name='Test' fullname='Fixture.Test' methodname='Test' classname='Fixture' runstate='Explicit' />
</test-suite>").Single();

Assert.That(testCase.Traits, Has.One.With.Property("Name").EqualTo("Explicit"));
}

[Test]
public static void ThatTestCaseWithExplicitParentHasExplicitTrait()
{
var testCase = GetTestCases(
@"<test-suite id='1' name='Fixture' fullname='Fixture' classname='Fixture' runstate='Explicit'>
<test-case id='2' name='Test' fullname='Fixture.Test' methodname='Test' classname='Fixture'/>
</test-suite>").Single();

Assert.That(testCase.Traits, Has.One.With.Property("Name").EqualTo("Explicit"));
}

[Test]
public static void ThatMultipleChildTestCasesWithExplicitParentHaveExplicitTraits()
{
var testCases = GetTestCases(
@"<test-suite id='1' name='Fixture' fullname='Fixture' classname='Fixture' runstate='Explicit'>
<test-case id='2' name='Test' fullname='Fixture.Test' methodname='Test' classname='Fixture'/>
<test-case id='3' name='Test2' fullname='Fixture.Test2' methodname='Test2' classname='Fixture'/>
</test-suite>");

foreach (var testCase in testCases)
Assert.That(testCase.Traits, Has.One.With.Property("Name").EqualTo("Explicit"));
}

[Test]
public static void ThatExplicitTraitValueIsEmptyString()
{
var testCase = GetTestCases(
@"<test-suite id='1' name='Fixture' fullname='Fixture' classname='Fixture'>
<test-case id='2' name='Test' fullname='Fixture.Test' methodname='Test' classname='Fixture' runstate='Explicit' />
</test-suite>").Single();

Assert.That(testCase.Traits, Has.One.With.Property("Name").EqualTo("Explicit").And.Property("Value").SameAs(string.Empty));
}
}
#endif
}

0 comments on commit 70dcabd

Please sign in to comment.