Add tests of filter conversion

This commit is contained in:
Charlie Poole 2019-06-15 17:56:01 +01:00
Родитель f8bcb25c59
Коммит bba43d0b30
3 изменённых файлов: 90 добавлений и 7 удалений

Просмотреть файл

@ -160,7 +160,17 @@ namespace NUnit.Engine.Drivers
get { return string.IsNullOrEmpty(ID) ? "1" : ID + "-1";}
}
private static ITestFilter CreateNUnit2TestFilter(string filterXml)
// Constants are public so they can be used by tests
public const string NO_FILTER_ELEMENT_MESSAGE = "Invalid filter passed to NUnit V2 driver: no filter element at top level";
public const string NO_REGULAR_EXPRESSIONS_MESSAGE = "Filters with regular expressions are only supported when running NUnit 3 tests";
public const string NO_ID_FILTER_MESSAGE = "Filtering on id is only valid when running NUnit 3 tests";
public const string NO_NAME_FILTER_MESSAGE = "Filtering on name is only valid when running NUnit 3 tests";
public const string NO_CLASS_FILTER_MESSAGE = "Filtering on class is only valid when running NUnit 3 tests";
public const string NO_METHOD_FILTER_MESSAGE = "Filtering on method is only valid when running NUnit 3 tests";
public const string NO_PROPERTY_FILTER_MESSAGE = "Filtering on property value is only valid when running NUnit 3 tests";
public const string INVALID_FILTER_MESSAGE = "Invalid filter passed to the NUnit V2 driver: {0} is not a known filter type";
public static ITestFilter CreateNUnit2TestFilter(string filterXml)
{
if (string.IsNullOrEmpty(filterXml))
return Core.TestFilter.Empty;
@ -169,7 +179,7 @@ namespace NUnit.Engine.Drivers
doc.LoadXml(filterXml);
var topNode = doc.FirstChild;
if (topNode.Name != "filter")
throw new Exception("Invalid filter passed to NUnit V2 driver: no filter element at top level");
throw new NUnitEngineException(NO_FILTER_ELEMENT_MESSAGE);
ITestFilter filter;
switch (topNode.ChildNodes.Count)
@ -215,26 +225,29 @@ namespace NUnit.Engine.Drivers
case "test":
if (xmlNode.Attributes["re"] != null)
throw new NUnitEngineException("Filters with regular expressions are only supported when running NUnit 3 tests");
throw new NUnitEngineException(NO_REGULAR_EXPRESSIONS_MESSAGE);
return new Core.Filters.SimpleNameFilter(xmlNode.InnerText);
case "cat":
if (xmlNode.Attributes["re"] != null)
throw new NUnitEngineException("Filters with regular expressions are only supported when running NUnit 3 tests");
throw new NUnitEngineException(NO_REGULAR_EXPRESSIONS_MESSAGE);
var catFilter = new Core.Filters.CategoryFilter();
foreach (string cat in xmlNode.InnerText.Split(COMMA))
catFilter.AddCategory(cat);
return catFilter;
case "id":
throw new NUnitEngineException(NO_ID_FILTER_MESSAGE);
case "name":
throw new NUnitEngineException(NO_NAME_FILTER_MESSAGE);
case "class":
throw new NUnitEngineException(NO_CLASS_FILTER_MESSAGE);
case "method":
throw new NUnitEngineException(string.Format("Filtering on {0} is only valid when running NUnit 3 tests", xmlNode.Name));
throw new NUnitEngineException(NO_METHOD_FILTER_MESSAGE);
case "prop":
throw new NUnitEngineException("Filtering on a property value is only valid when running NUnit 3 tests");
throw new NUnitEngineException(NO_PROPERTY_FILTER_MESSAGE);
default:
throw new NUnitEngineException(string.Format("Invalid filter passed to the NUnit V2 driver: {0} is not a known filter type", xmlNode.Name));
throw new NUnitEngineException(string.Format(INVALID_FILTER_MESSAGE, xmlNode.Name));
}
}
}

Просмотреть файл

@ -0,0 +1,69 @@
// ***********************************************************************
// Copyright (c) 2019 Charlie Poole
//
// 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.Text;
using NUnit.Framework;
namespace NUnit.Engine.Drivers.Tests
{
public class FilterConverterTests
{
[TestCase("<filter></filter>")]
[TestCase("<filter/>")]
[TestCase("")]
[TestCase(null)]
public void EmptyFilter(string input)
{
Assert.That(NUnit2FrameworkDriver.CreateNUnit2TestFilter(input).IsEmpty);
}
[TestCase("<filter><test>Some.Test.Name</test></filter>", typeof(Core.Filters.SimpleNameFilter))]
[TestCase("<filter><test>Some.Test.Name,Another.Test.Name</test></filter>", typeof(Core.Filters.SimpleNameFilter))]
[TestCase("<filter><cat>Urgent</cat></filter>", typeof(Core.Filters.CategoryFilter))]
[TestCase("<filter><cat>A,B,C</cat></filter>", typeof(Core.Filters.CategoryFilter))]
[TestCase("<filter><cat>A</cat><cat>B></cat></filter>", typeof(Core.Filters.AndFilter))]
[TestCase("<filter><and><cat>A</cat><cat>B></cat></and></filter>", typeof(Core.Filters.AndFilter))]
[TestCase("<filter><or><cat>A</cat><cat>B></cat></or></filter>", typeof(Core.Filters.OrFilter))]
[TestCase("<filter><not><cat>A</cat></not></filter>", typeof(Core.Filters.NotFilter))]
public void CorrectFilterType(string input, Type type)
{
var filter = NUnit2FrameworkDriver.CreateNUnit2TestFilter(input);
Assert.That(filter, Is.TypeOf(type));
}
[TestCase("<notafilter/>", NUnit2FrameworkDriver.NO_FILTER_ELEMENT_MESSAGE)]
[TestCase("<filter><test re='1'>Some.Test.*</test></filter>", NUnit2FrameworkDriver.NO_REGULAR_EXPRESSIONS_MESSAGE)]
[TestCase("<filter><id>123-456</id></filter>", NUnit2FrameworkDriver.NO_ID_FILTER_MESSAGE)]
[TestCase("<filter><name>NAME</name></filter>", NUnit2FrameworkDriver.NO_NAME_FILTER_MESSAGE)]
[TestCase("<filter><class>CLASSNAME</class></filter>", NUnit2FrameworkDriver.NO_CLASS_FILTER_MESSAGE)]
[TestCase("<filter><method>METHOD</method></filter>", NUnit2FrameworkDriver.NO_METHOD_FILTER_MESSAGE)]
[TestCase("<filter><prop name='NAME'>VALUE</prop></filter>", NUnit2FrameworkDriver.NO_PROPERTY_FILTER_MESSAGE)]
public void NUnitEngineExceptionTests(string input, string message)
{
var ex = Assert.Throws<NUnitEngineException>(() => NUnit2FrameworkDriver.CreateNUnit2TestFilter(input));
Assert.That(ex.Message, Is.EqualTo(message));
}
}
}

Просмотреть файл

@ -58,6 +58,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FilterConverterTests.cs" />
<Compile Include="NUnit2FrameworkDriverTests.cs" />
<Compile Include="TestEventAdapterTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />