Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit dafdf9f

Browse files
committed
[Touch.Client] Add API to exclude tests based on categories.
The NUnitLite API to create test filters is very rudimentary (either subclass TestFilter, or have NUnitLite create the filter based on an xml string), so we can at least make it easy for consumers to exclude tests based on categories, which we need to be able to do for our tests anyway.
1 parent e6fb7d7 commit dafdf9f

9 files changed

Lines changed: 77 additions & 0 deletions
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
using NUnit.Framework.Internal;
5+
using NUnit.Framework.Interfaces;
6+
7+
namespace MonoTouch.NUnit.UI {
8+
class ExcludeCategoryFilter : TestFilter {
9+
public HashSet<string> ExcludedCategories { get; private set; }
10+
11+
public ExcludeCategoryFilter (IEnumerable<string> categories)
12+
{
13+
ExcludedCategories = new HashSet<string> (categories);
14+
}
15+
16+
public override TNode AddToXml (TNode parentNode, bool recursive)
17+
{
18+
throw new NotImplementedException ();
19+
}
20+
21+
public override bool Match (ITest test)
22+
{
23+
var categories = test.Properties ["Category"];
24+
if (categories != null) {
25+
foreach (string cat in categories) {
26+
if (ExcludedCategories.Contains (cat))
27+
return false;
28+
}
29+
}
30+
31+
if (test.Parent != null)
32+
return Match (test.Parent);
33+
34+
return true;
35+
}
36+
37+
public override bool Pass (ITest test)
38+
{
39+
return Match (test);
40+
}
41+
}
42+
}

NUnitLite/TouchRunner/TouchRunner.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using System.Collections;
2424
using System.Collections.Generic;
2525
using System.Net.Sockets;
26+
using System.Linq;
2627
using System.Reflection;
2728
using System.Runtime.InteropServices;
2829
using System.Threading;
@@ -84,6 +85,8 @@ public ITestFilter Filter {
8485
set { filter = value; }
8586
}
8687

88+
public HashSet<string> ExcludedCategories { get; set; }
89+
8790
public bool TerminateAfterExecution {
8891
get { return TouchOptions.Current.TerminateAfterExecution && !connection_failure; }
8992
set { TouchOptions.Current.TerminateAfterExecution = value; }
@@ -591,6 +594,10 @@ public void Run (Test test)
591594

592595
#if NUNITLITE_NUGET
593596
var filter = new MatchTestFilter { MatchTest = test };
597+
if (this.filter != null)
598+
filter.AndFilters.Add (this.filter);
599+
if (ExcludedCategories != null)
600+
filter.AndFilters.Add (new ExcludeCategoryFilter (ExcludedCategories));
594601
foreach (var runner in runners)
595602
runner.Run (this, filter);
596603

@@ -878,6 +885,7 @@ protected override void ExecuteOnMainThread (Action action)
878885
// A filter that matches a specific test
879886
class MatchTestFilter : TestFilter {
880887
public ITest MatchTest;
888+
public List<ITestFilter> AndFilters = new List<ITestFilter> ();
881889

882890
#if NUNITLITE_NUGET
883891
public override TNode AddToXml (TNode parentNode, bool recursive)
@@ -888,6 +896,12 @@ public override TNode AddToXml (TNode parentNode, bool recursive)
888896

889897
public override bool Match (ITest test)
890898
{
899+
if (AndFilters != null) {
900+
// If any of the And filters returns false, then return false too.
901+
if (AndFilters.Any ((v) => !v.Pass (test)))
902+
return false;
903+
}
904+
891905
return IsMatch (test, MatchTest);
892906
}
893907

Touch.Client/dotnet/iOS/Touch.Client-iOS.dotnet.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
<AssemblyName>Touch.Client</AssemblyName>
1010
</PropertyGroup>
1111
<ItemGroup>
12+
<Compile Include="..\..\NUnitLite\TouchRunner\ExcludedCategoryFilter.cs">
13+
<Link>ExcludedCategoryFilter.cs</Link>
14+
</Compile>
1215
<Compile Include="..\..\NUnitLite\TouchRunner\HttpTextWriter.cs">
1316
<Link>HttpTextWriter.cs</Link>
1417
</Compile>

Touch.Client/dotnet/tvOS/Touch.Client-tvOS.dotnet.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
<AssemblyName>Touch.Client</AssemblyName>
1010
</PropertyGroup>
1111
<ItemGroup>
12+
<Compile Include="..\..\NUnitLite\TouchRunner\ExcludedCategoryFilter.cs">
13+
<Link>ExcludedCategoryFilter.cs</Link>
14+
</Compile>
1215
<Compile Include="..\..\NUnitLite\TouchRunner\HttpTextWriter.cs">
1316
<Link>HttpTextWriter.cs</Link>
1417
</Compile>

Touch.Client/iOS/Touch.Client-iOS.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
<Folder Include="Resources\" />
4242
</ItemGroup>
4343
<ItemGroup>
44+
<Compile Include="..\..\NUnitLite\TouchRunner\ExcludedCategoryFilter.cs">
45+
<Link>ExcludedCategoryFilter.cs</Link>
46+
</Compile>
4447
<Compile Include="..\..\NUnitLite\TouchRunner\HttpTextWriter.cs">
4548
<Link>HttpTextWriter.cs</Link>
4649
</Compile>

Touch.Client/macOS/full/Touch.Client-macOS-full.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
<Folder Include="Resources\" />
4242
</ItemGroup>
4343
<ItemGroup>
44+
<Compile Include="..\..\..\NUnitLite\TouchRunner\ExcludedCategoryFilter.cs">
45+
<Link>ExcludedCategoryFilter.cs</Link>
46+
</Compile>
4447
<Compile Include="..\..\..\NUnitLite\TouchRunner\HttpTextWriter.cs">
4548
<Link>HttpTextWriter.cs</Link>
4649
</Compile>

Touch.Client/macOS/mobile/Touch.Client-macOS-mobile.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
<Folder Include="Resources\" />
4242
</ItemGroup>
4343
<ItemGroup>
44+
<Compile Include="..\..\..\NUnitLite\TouchRunner\ExcludedCategoryFilter.cs">
45+
<Link>ExcludedCategoryFilter.cs</Link>
46+
</Compile>
4447
<Compile Include="..\..\..\NUnitLite\TouchRunner\HttpTextWriter.cs">
4548
<Link>HttpTextWriter.cs</Link>
4649
</Compile>

Touch.Client/tvOS/Touch.Client-tvOS.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
<Folder Include="Resources\" />
4242
</ItemGroup>
4343
<ItemGroup>
44+
<Compile Include="..\..\NUnitLite\TouchRunner\ExcludedCategoryFilter.cs">
45+
<Link>ExcludedCategoryFilter.cs</Link>
46+
</Compile>
4447
<Compile Include="..\..\NUnitLite\TouchRunner\HttpTextWriter.cs">
4548
<Link>HttpTextWriter.cs</Link>
4649
</Compile>

Touch.Client/watchOS/Touch.Client-watchOS.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
<Folder Include="Resources\" />
4242
</ItemGroup>
4343
<ItemGroup>
44+
<Compile Include="..\..\NUnitLite\TouchRunner\ExcludedCategoryFilter.cs">
45+
<Link>ExcludedCategoryFilter.cs</Link>
46+
</Compile>
4447
<Compile Include="..\..\NUnitLite\TouchRunner\HttpTextWriter.cs">
4548
<Link>HttpTextWriter.cs</Link>
4649
</Compile>

0 commit comments

Comments
 (0)