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

Make it possible to filter by TestCategory again #453

Merged
merged 3 commits into from
Feb 18, 2018
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ static TfsTestFilter()
SupportedPropertiesCache = new Dictionary<string, TestProperty>(StringComparer.OrdinalIgnoreCase);
SupportedPropertiesCache["FullyQualifiedName"] = TestCaseProperties.FullyQualifiedName;
SupportedPropertiesCache["Name"] = TestCaseProperties.DisplayName;
SupportedPropertiesCache["TestCategory"] = CategoryList.NUnitTestCategoryProperty;
// Initialize the trait cache
var priorityTrait = new NTrait("Priority", "");
var categoryTrait = new NTrait("Category", "");
Expand Down
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 All @@ -20,9 +20,17 @@
// 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;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using NSubstitute;
using NUnit.Framework;
using NUnit.VisualStudio.TestAdapter.Tests.Fakes;

namespace NUnit.VisualStudio.TestAdapter.Tests
{
Expand Down Expand Up @@ -106,5 +114,72 @@ public void PropertyValueProviderCategoryFail()
var obj = TfsTestFilter.PropertyValueProvider(tc, "Garbage");
Assert.Null(obj);
}


[TestCase("CategoryThatMatchesNothing", new string[0])]
[TestCase("AsmCat", new[] { "nUnitClassLibrary.Class1.nUnitTest", "nUnitClassLibrary.ClassD.dNunitTest", "nUnitClassLibrary.ClassD.nUnitTest", "nUnitClassLibrary.NestedClasses.NC11", "nUnitClassLibrary.NestedClasses+NestedClass2.NC21" })]
[TestCase("BaseClass", new[] { "nUnitClassLibrary.Class1.nUnitTest", "nUnitClassLibrary.ClassD.dNunitTest", "nUnitClassLibrary.ClassD.nUnitTest" })]
[TestCase("Base", new[] { "nUnitClassLibrary.Class1.nUnitTest", "nUnitClassLibrary.ClassD.nUnitTest" })]
[TestCase("DerivedClass", new[] { "nUnitClassLibrary.ClassD.dNunitTest", "nUnitClassLibrary.ClassD.nUnitTest" })]
[TestCase("Derived", new[] { "nUnitClassLibrary.ClassD.dNunitTest" })]
[TestCase("NS1", new[] { "nUnitClassLibrary.NestedClasses.NC11" })]
[TestCase("NS11", new[] { "nUnitClassLibrary.NestedClasses.NC11" })]
[TestCase("NS2", new[] { "nUnitClassLibrary.NestedClasses+NestedClass2.NC21" })]
[TestCase("NS21", new[] { "nUnitClassLibrary.NestedClasses+NestedClass2.NC21" })]
public static void CanFilterConvertedTestsByCategory(string category, IReadOnlyCollection<string> expectedMatchingTestNames)
{
var filter = CreateFilter(TestDoubleFilterExpression.AnyIsEqualTo("TestCategory", category));

var matchingTestCases = filter.CheckFilter(GetConvertedTestCases());

Assert.That(matchingTestCases.Select(t => t.FullyQualifiedName), Is.EquivalentTo(expectedMatchingTestNames));
}

private static IReadOnlyCollection<TestCase> GetConvertedTestCases()
{
var testConverter = new TestConverter(
new TestLogger(new MessageLoggerStub()),
FakeTestData.AssemblyPath,
collectSourceInformation: true);

return FakeTestData.GetTestNodes()
.Cast<XmlNode>()
.Select(testConverter.ConvertTestCase)
.ToList();
}

private static TfsTestFilter CreateFilter(ITestCaseFilterExpression filterExpression)
{
var context = Substitute.For<IRunContext>();
context.GetTestCaseFilter(null, null).ReturnsForAnyArgs(filterExpression);
return new TfsTestFilter(context);
}

private sealed class TestDoubleFilterExpression : ITestCaseFilterExpression
{
private readonly Func<Func<string, object>, bool> predicate;

public TestDoubleFilterExpression(string testCaseFilterValue, Func<Func<string, object>, bool> predicate)
{
TestCaseFilterValue = testCaseFilterValue;
this.predicate = predicate;
}

public string TestCaseFilterValue { get; }

public bool MatchTestCase(TestCase testCase, Func<string, object> propertyValueProvider)
{
return predicate.Invoke(propertyValueProvider);
}

public static TestDoubleFilterExpression AnyIsEqualTo(string propertyName, object value)
{
return new TestDoubleFilterExpression($"{propertyName}={value}", propertyValueProvider =>
{
var list = propertyValueProvider.Invoke(propertyName) as IEnumerable;
return list == null ? false : list.Cast<object>().Contains(value);
});
}
}
}
}