From dafdf9f44f8fada4e47e2abd0c95c9e494377a8c Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Fri, 17 Jul 2020 19:17:29 +0200 Subject: [PATCH] [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. --- .../TouchRunner/ExcludedCategoryFilter.cs | 42 +++++++++++++++++++ NUnitLite/TouchRunner/TouchRunner.cs | 14 +++++++ .../dotnet/iOS/Touch.Client-iOS.dotnet.csproj | 3 ++ .../tvOS/Touch.Client-tvOS.dotnet.csproj | 3 ++ Touch.Client/iOS/Touch.Client-iOS.csproj | 3 ++ .../macOS/full/Touch.Client-macOS-full.csproj | 3 ++ .../mobile/Touch.Client-macOS-mobile.csproj | 3 ++ Touch.Client/tvOS/Touch.Client-tvOS.csproj | 3 ++ .../watchOS/Touch.Client-watchOS.csproj | 3 ++ 9 files changed, 77 insertions(+) create mode 100644 NUnitLite/TouchRunner/ExcludedCategoryFilter.cs diff --git a/NUnitLite/TouchRunner/ExcludedCategoryFilter.cs b/NUnitLite/TouchRunner/ExcludedCategoryFilter.cs new file mode 100644 index 0000000..1c0b6c3 --- /dev/null +++ b/NUnitLite/TouchRunner/ExcludedCategoryFilter.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; + +using NUnit.Framework.Internal; +using NUnit.Framework.Interfaces; + +namespace MonoTouch.NUnit.UI { + class ExcludeCategoryFilter : TestFilter { + public HashSet ExcludedCategories { get; private set; } + + public ExcludeCategoryFilter (IEnumerable categories) + { + ExcludedCategories = new HashSet (categories); + } + + public override TNode AddToXml (TNode parentNode, bool recursive) + { + throw new NotImplementedException (); + } + + public override bool Match (ITest test) + { + var categories = test.Properties ["Category"]; + if (categories != null) { + foreach (string cat in categories) { + if (ExcludedCategories.Contains (cat)) + return false; + } + } + + if (test.Parent != null) + return Match (test.Parent); + + return true; + } + + public override bool Pass (ITest test) + { + return Match (test); + } + } +} diff --git a/NUnitLite/TouchRunner/TouchRunner.cs b/NUnitLite/TouchRunner/TouchRunner.cs index bce8578..71376c9 100644 --- a/NUnitLite/TouchRunner/TouchRunner.cs +++ b/NUnitLite/TouchRunner/TouchRunner.cs @@ -23,6 +23,7 @@ using System.Collections; using System.Collections.Generic; using System.Net.Sockets; +using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Threading; @@ -84,6 +85,8 @@ public ITestFilter Filter { set { filter = value; } } + public HashSet ExcludedCategories { get; set; } + public bool TerminateAfterExecution { get { return TouchOptions.Current.TerminateAfterExecution && !connection_failure; } set { TouchOptions.Current.TerminateAfterExecution = value; } @@ -591,6 +594,10 @@ public void Run (Test test) #if NUNITLITE_NUGET var filter = new MatchTestFilter { MatchTest = test }; + if (this.filter != null) + filter.AndFilters.Add (this.filter); + if (ExcludedCategories != null) + filter.AndFilters.Add (new ExcludeCategoryFilter (ExcludedCategories)); foreach (var runner in runners) runner.Run (this, filter); @@ -878,6 +885,7 @@ protected override void ExecuteOnMainThread (Action action) // A filter that matches a specific test class MatchTestFilter : TestFilter { public ITest MatchTest; + public List AndFilters = new List (); #if NUNITLITE_NUGET public override TNode AddToXml (TNode parentNode, bool recursive) @@ -888,6 +896,12 @@ public override TNode AddToXml (TNode parentNode, bool recursive) public override bool Match (ITest test) { + if (AndFilters != null) { + // If any of the And filters returns false, then return false too. + if (AndFilters.Any ((v) => !v.Pass (test))) + return false; + } + return IsMatch (test, MatchTest); } diff --git a/Touch.Client/dotnet/iOS/Touch.Client-iOS.dotnet.csproj b/Touch.Client/dotnet/iOS/Touch.Client-iOS.dotnet.csproj index edb5975..aa2aaca 100644 --- a/Touch.Client/dotnet/iOS/Touch.Client-iOS.dotnet.csproj +++ b/Touch.Client/dotnet/iOS/Touch.Client-iOS.dotnet.csproj @@ -9,6 +9,9 @@ Touch.Client + + ExcludedCategoryFilter.cs + HttpTextWriter.cs diff --git a/Touch.Client/dotnet/tvOS/Touch.Client-tvOS.dotnet.csproj b/Touch.Client/dotnet/tvOS/Touch.Client-tvOS.dotnet.csproj index 7863ee1..9555159 100644 --- a/Touch.Client/dotnet/tvOS/Touch.Client-tvOS.dotnet.csproj +++ b/Touch.Client/dotnet/tvOS/Touch.Client-tvOS.dotnet.csproj @@ -9,6 +9,9 @@ Touch.Client + + ExcludedCategoryFilter.cs + HttpTextWriter.cs diff --git a/Touch.Client/iOS/Touch.Client-iOS.csproj b/Touch.Client/iOS/Touch.Client-iOS.csproj index 032c217..26b6bbd 100644 --- a/Touch.Client/iOS/Touch.Client-iOS.csproj +++ b/Touch.Client/iOS/Touch.Client-iOS.csproj @@ -41,6 +41,9 @@ + + ExcludedCategoryFilter.cs + HttpTextWriter.cs diff --git a/Touch.Client/macOS/full/Touch.Client-macOS-full.csproj b/Touch.Client/macOS/full/Touch.Client-macOS-full.csproj index 9ddd82f..08ba058 100644 --- a/Touch.Client/macOS/full/Touch.Client-macOS-full.csproj +++ b/Touch.Client/macOS/full/Touch.Client-macOS-full.csproj @@ -41,6 +41,9 @@ + + ExcludedCategoryFilter.cs + HttpTextWriter.cs diff --git a/Touch.Client/macOS/mobile/Touch.Client-macOS-mobile.csproj b/Touch.Client/macOS/mobile/Touch.Client-macOS-mobile.csproj index 0a79a9d..aa4e296 100644 --- a/Touch.Client/macOS/mobile/Touch.Client-macOS-mobile.csproj +++ b/Touch.Client/macOS/mobile/Touch.Client-macOS-mobile.csproj @@ -41,6 +41,9 @@ + + ExcludedCategoryFilter.cs + HttpTextWriter.cs diff --git a/Touch.Client/tvOS/Touch.Client-tvOS.csproj b/Touch.Client/tvOS/Touch.Client-tvOS.csproj index 399c47f..6146c6e 100644 --- a/Touch.Client/tvOS/Touch.Client-tvOS.csproj +++ b/Touch.Client/tvOS/Touch.Client-tvOS.csproj @@ -41,6 +41,9 @@ + + ExcludedCategoryFilter.cs + HttpTextWriter.cs diff --git a/Touch.Client/watchOS/Touch.Client-watchOS.csproj b/Touch.Client/watchOS/Touch.Client-watchOS.csproj index 05d8356..0441c46 100644 --- a/Touch.Client/watchOS/Touch.Client-watchOS.csproj +++ b/Touch.Client/watchOS/Touch.Client-watchOS.csproj @@ -41,6 +41,9 @@ + + ExcludedCategoryFilter.cs + HttpTextWriter.cs