Skip to content

Commit

Permalink
Merge pull request #791 from nunit/RemoveDuplicates
Browse files Browse the repository at this point in the history
Remove duplicates
  • Loading branch information
OsirisTerje authored Oct 23, 2020
2 parents 33ea912 + 3543ab0 commit 5354334
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 154 deletions.
1 change: 1 addition & 0 deletions NUnit3TestAdapter.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.gitignore = .gitignore
.runsettings = .runsettings
appveyor.yml = appveyor.yml
azure-pipelines.yml = azure-pipelines.yml
build.cake = build.cake
build.cmd = build.cmd
build.ps1 = build.ps1
Expand Down
38 changes: 18 additions & 20 deletions src/NUnitTestAdapter/Execution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ public virtual bool Run(TestFilter filter, DiscoveryConverter discovery, NUnit3T
protected NUnitTestFilterBuilder CreateTestFilterBuilder()
=> new NUnitTestFilterBuilder(NUnitEngineAdapter.GetService<ITestFilterService>(), Settings);
protected ITestConverterCommon CreateConverter(DiscoveryConverter discovery) => Settings.DiscoveryMethod == DiscoveryMethod.Current ? discovery.TestConverter : discovery.TestConverterForXml;

protected TestFilter CheckFilter(IDiscoveryConverter discovery)
{
TestFilter filter;
if (discovery.NoOfLoadedTestCasesAboveLimit)
{
TestLog.Debug("Setting filter to empty due to number of testcases");
filter = TestFilter.Empty;
}
else
{
var filterBuilder = CreateTestFilterBuilder();
filter = filterBuilder.FilterByList(discovery.LoadedTestCases);
}
return filter;
}
}

public class IdeExecution : Execution
Expand All @@ -87,16 +103,7 @@ public override TestFilter CheckFilterInCurrentMode(TestFilter filter, IDiscover
return filter;
if (filter.IsEmpty())
return filter;
if (discovery.NoOfLoadedTestCasesAboveLimit)
{
TestLog.Debug("Setting filter to empty due to number of testcases");
filter = TestFilter.Empty;
}
else
{
var filterBuilder = CreateTestFilterBuilder();
filter = filterBuilder.FilterByList(discovery.LoadedTestCases);
}
filter = CheckFilter(discovery);
return filter;
}
}
Expand Down Expand Up @@ -151,16 +158,7 @@ public override TestFilter CheckFilterInCurrentMode(TestFilter filter, IDiscover
return filter;
if ((VsTestFilter == null || VsTestFilter.IsEmpty) && filter != TestFilter.Empty)
{
if (discovery.NoOfLoadedTestCasesAboveLimit)
{
TestLog.Debug("Setting filter to empty due to number of testcases");
filter = TestFilter.Empty;
}
else
{
var filterBuilder = CreateTestFilterBuilder();
filter = filterBuilder.FilterByList(discovery.LoadedTestCases);
}
filter = CheckFilter(discovery);
}
else if (VsTestFilter != null && !VsTestFilter.IsEmpty && !Settings.UseNUnitFilter)
{
Expand Down
68 changes: 28 additions & 40 deletions src/NUnitTestAdapter/TraitsFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// 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;
Expand Down Expand Up @@ -68,26 +69,7 @@ public static void AddTraitsFromTestNode(this TestCase testCase, NUnitDiscoveryT
// Reading ancestor properties of a test-case node. And adding to the cache.
while (ancestor != null && !string.IsNullOrEmpty(key))
{
if (traitsCache.ContainsKey(key))
{
categoryList.AddRange(traitsCache[key].Traits.Where(o => o.Name == NunitTestCategoryLabel).Select(prop => prop.Value).ToList());

if (traitsCache[key].Explicit)
testCase.SetPropertyValue(CategoryList.NUnitExplicitProperty, true);

var traitsList = traitsCache[key].Traits.Where(o => o.Name != NunitTestCategoryLabel).ToList();
if (traitsList.Count > 0)
testCase.Traits.AddRange(traitsList);
}
else
{
categoryList.ProcessTestCaseProperties(ancestor, true, key, traitsCache);
// Adding entry to dictionary, so that we will not make SelectNodes call again.
if (categoryList.LastNodeListCount == 0 && !traitsCache.ContainsKey(key))
{
traitsCache[key] = new CachedTestCaseInfo();
}
}
AddingToCache(testCase, traitsCache, key, categoryList, ancestor, categoryList.ProcessTestCaseProperties);
ancestor = ancestor.Parent;
key = ancestor?.Id;
}
Expand All @@ -97,6 +79,31 @@ public static void AddTraitsFromTestNode(this TestCase testCase, NUnitDiscoveryT
categoryList.UpdateCategoriesToVs();
}

private static void AddingToCache<T>(TestCase testCase, IDictionary<string, CachedTestCaseInfo> traitsCache, string key, CategoryList categoryList, T ancestor, Func<T, bool, string, IDictionary<string, CachedTestCaseInfo>, IEnumerable<string>> processTestCaseProperties)
{
if (traitsCache.ContainsKey(key))
{
categoryList.AddRange(traitsCache[key].Traits.Where(o => o.Name == NunitTestCategoryLabel)
.Select(prop => prop.Value).ToList());

if (traitsCache[key].Explicit)
testCase.SetPropertyValue(CategoryList.NUnitExplicitProperty, true);

var traitsList = traitsCache[key].Traits.Where(o => o.Name != NunitTestCategoryLabel).ToList();
if (traitsList.Count > 0)
testCase.Traits.AddRange(traitsList);
}
else
{
processTestCaseProperties(ancestor, true, key, traitsCache);
// Adding entry to dictionary, so that we will not make SelectNodes call again.
if (categoryList.LastNodeListCount == 0 && !traitsCache.ContainsKey(key))
{
traitsCache[key] = new CachedTestCaseInfo();
}
}
}

public static void AddTraitsFromXmlTestNode(this TestCase testCase, NUnitEventTestCase testNCase,
IDictionary<string, CachedTestCaseInfo> traitsCache, ITestLogger logger, IAdapterSettings adapterSettings)
{
Expand All @@ -106,26 +113,7 @@ public static void AddTraitsFromXmlTestNode(this TestCase testCase, NUnitEventTe
// Reading ancestor properties of a test-case node. And adding to the cache.
while (ancestor != null && key != null)
{
if (traitsCache.ContainsKey(key))
{
categoryList.AddRange(traitsCache[key].Traits.Where(o => o.Name == NunitTestCategoryLabel).Select(prop => prop.Value).ToList());

if (traitsCache[key].Explicit)
testCase.SetPropertyValue(CategoryList.NUnitExplicitProperty, true);

var traitsList = traitsCache[key].Traits.Where(o => o.Name != NunitTestCategoryLabel).ToList();
if (traitsList.Count > 0)
testCase.Traits.AddRange(traitsList);
}
else
{
categoryList.ProcessTestCaseProperties(ancestor, true, key, traitsCache);
// Adding entry to dictionary, so that we will not make SelectNodes call again.
if (categoryList.LastNodeListCount == 0 && !traitsCache.ContainsKey(key))
{
traitsCache[key] = new CachedTestCaseInfo();
}
}
AddingToCache(testCase, traitsCache, key, categoryList, ancestor, categoryList.ProcessTestCaseProperties);
ancestor = ancestor.Parent;
key = ancestor?.Id;
}
Expand Down
20 changes: 0 additions & 20 deletions src/NUnitTestAdapter/time.puml

This file was deleted.

85 changes: 85 additions & 0 deletions src/NUnitTestAdapterTests/NUnitEventListenerOutputTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Collections.Generic;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using NSubstitute;
using NUnit.Framework;
using NUnit.VisualStudio.TestAdapter.Dump;
using NUnit.VisualStudio.TestAdapter.NUnitEngine;

namespace NUnit.VisualStudio.TestAdapter.Tests
{
/// <summary>
/// These tests ensure correct console output, which is what we send to the "recorder".
/// </summary>
public class NUnitEventListenerOutputTests
{
private ITestExecutionRecorder recorder;
private ITestConverterCommon converter;
private IDumpXml dumpxml;
private IAdapterSettings settings;
private INUnit3TestExecutor executor;


private const string TestOutputProgress =
@"<test-output stream='Progress' testid='0-1001' testname='Something.TestClass.Whatever'><![CDATA[Whatever
]]></test-output>";

private const string TestOutputOut =
@"<test-output stream='Out' testid='0-1001' testname='Something.TestClass.Whatever'><![CDATA[Whatever
]]></test-output>";

private const string TestOutputError =
@"<test-output stream='Error' testid='0-1001' testname='Something.TestClass.Whatever'><![CDATA[Whatever
]]></test-output>";

private const string BlankTestOutput =
@"<test-output stream='Progress' testid='0-1001' testname='Something.TestClass.Whatever'><![CDATA[ ]]></test-output>";

private const string TestFinish =
@"<test-case id='0-1001' name='Test1' fullname='UnitTests.Test1' methodname='Test1' classname='UnitTests' runstate='Runnable' seed='108294034' result='Passed' start-time='2018-10-15 09:41:24Z' end-time='2018-10-15 09:41:24Z' duration='0.000203' asserts='0' parentId='0-1000' />";

[SetUp]
public void Setup()
{
recorder = Substitute.For<IFrameworkHandle>();
converter = Substitute.For<ITestConverterCommon>();
dumpxml = Substitute.For<IDumpXml>();
settings = Substitute.For<IAdapterSettings>();
executor = Substitute.For<INUnit3TestExecutor>();
executor.Settings.Returns(settings);
executor.FrameworkHandle.Returns(recorder);
}

[Test]
public void ThatNormalTestOutputIsOutput()
{
var sut = new NUnitEventListener(converter, executor);
sut.OnTestEvent(TestOutputProgress);
sut.OnTestEvent(TestFinish);

recorder.Received().SendMessage(Arg.Any<TestMessageLevel>(), Arg.Is<string>(x => x.StartsWith("Whatever")));
converter.Received().GetVsTestResults(Arg.Any<NUnitTestEventTestCase>(), Arg.Is<ICollection<INUnitTestEventTestOutput>>(x => x.Count == 1));
}

[Test]
public void ThatNormalTestOutputIsError()
{
var sut = new NUnitEventListener(converter, executor);
sut.OnTestEvent(TestOutputError);
sut.OnTestEvent(TestFinish);

recorder.Received().SendMessage(Arg.Any<TestMessageLevel>(), Arg.Is<string>(x => x.StartsWith("Whatever")));
converter.Received().GetVsTestResults(Arg.Any<NUnitTestEventTestCase>(), Arg.Is<ICollection<INUnitTestEventTestOutput>>(x => x.Count == 1));
}

[Test]
public void ThatTestOutputWithOnlyWhiteSpaceIsNotOutput()
{
var sut = new NUnitEventListener(converter, executor);

sut.OnTestEvent(BlankTestOutput);

recorder.DidNotReceive().SendMessage(Arg.Any<TestMessageLevel>(), Arg.Any<string>());
}
}
}
73 changes: 0 additions & 73 deletions src/NUnitTestAdapterTests/NUnitEventListenerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,77 +194,4 @@ private void VerifyTestResult(VSTestResult ourResult)

#endregion
}

/// <summary>
/// These tests ensure correct console output, which is what we send to the "recorder".
/// </summary>
public class NUnitEventListenerOutputTests
{
private ITestExecutionRecorder recorder;
private ITestConverterCommon converter;
private IAdapterSettings settings;
private INUnit3TestExecutor executor;


private const string TestOutputProgress =
@"<test-output stream='Progress' testid='0-1001' testname='Something.TestClass.Whatever'><![CDATA[Whatever
]]></test-output>";

private const string TestOutputOut =
@"<test-output stream='Out' testid='0-1001' testname='Something.TestClass.Whatever'><![CDATA[Whatever
]]></test-output>";

private const string TestOutputError =
@"<test-output stream='Error' testid='0-1001' testname='Something.TestClass.Whatever'><![CDATA[Whatever
]]></test-output>";

private const string BlankTestOutput =
@"<test-output stream='Progress' testid='0-1001' testname='Something.TestClass.Whatever'><![CDATA[ ]]></test-output>";

private const string TestFinish =
@"<test-case id='0-1001' name='Test1' fullname='UnitTests.Test1' methodname='Test1' classname='UnitTests' runstate='Runnable' seed='108294034' result='Passed' start-time='2018-10-15 09:41:24Z' end-time='2018-10-15 09:41:24Z' duration='0.000203' asserts='0' parentId='0-1000' />";

[SetUp]
public void Setup()
{
recorder = Substitute.For<IFrameworkHandle>();
converter = Substitute.For<ITestConverterCommon>();
settings = Substitute.For<IAdapterSettings>();
executor = Substitute.For<INUnit3TestExecutor>();
executor.Settings.Returns(settings);
executor.FrameworkHandle.Returns(recorder);
}

[Test]
public void ThatNormalTestOutputIsOutput()
{
var sut = new NUnitEventListener(converter, executor);
sut.OnTestEvent(TestOutputProgress);
sut.OnTestEvent(TestFinish);

recorder.Received().SendMessage(Arg.Any<TestMessageLevel>(), Arg.Is<string>(x => x.StartsWith("Whatever")));
converter.Received().GetVsTestResults(Arg.Any<NUnitTestEventTestCase>(), Arg.Is<ICollection<INUnitTestEventTestOutput>>(x => x.Count == 1));
}

[Test]
public void ThatNormalTestOutputIsError()
{
var sut = new NUnitEventListener(converter, executor);
sut.OnTestEvent(TestOutputError);
sut.OnTestEvent(TestFinish);

recorder.Received().SendMessage(Arg.Any<TestMessageLevel>(), Arg.Is<string>(x => x.StartsWith("Whatever")));
converter.Received().GetVsTestResults(Arg.Any<NUnitTestEventTestCase>(), Arg.Is<ICollection<INUnitTestEventTestOutput>>(x => x.Count == 1));
}

[Test]
public void ThatTestOutputWithOnlyWhiteSpaceIsNotOutput()
{
var sut = new NUnitEventListener(converter, executor);

sut.OnTestEvent(BlankTestOutput);

recorder.DidNotReceive().SendMessage(Arg.Any<TestMessageLevel>(), Arg.Any<string>());
}
}
}
3 changes: 2 additions & 1 deletion src/NUnitTestAdapterTests/TraitsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using NUnit.Framework;
using NUnit.VisualStudio.TestAdapter.NUnitEngine;
using NUnit.VisualStudio.TestAdapter.Tests.Fakes;
// ReSharper disable StringLiteralTypo

namespace NUnit.VisualStudio.TestAdapter.Tests
{
Expand Down Expand Up @@ -516,7 +517,7 @@ public void ThatTestCaseHasTraits()
Assert.That(testcaselist.Count, Is.EqualTo(3), "Wrong number of testcases found");
var testcasesWithCategories = testcaselist.Where(o => o.GetCategories()?.FirstOrDefault() != null).ToList();
Assert.That(testcasesWithCategories, Is.Not.Null, "Didn't find the testcases");
Assert.That(testcasesWithCategories.Count(), Is.EqualTo(1), "Wrong number of testcases with categories, should be only 1");
Assert.That(testcasesWithCategories.Count, Is.EqualTo(1), "Wrong number of testcases with categories, should be only 1");
var tc = testcasesWithCategories.FirstOrDefault();
VerifyCategoriesOnly(tc, 1, "simple");
Assert.That(tc.GetCategories().First(), Is.EqualTo("Single"));
Expand Down

0 comments on commit 5354334

Please sign in to comment.