Skip to content

Commit

Permalink
[Touch.Client] Add a client that uses MonoTouch.Dialog and NUnitLite …
Browse files Browse the repository at this point in the history
…from NuGet. (#63)

* [TouchRunner] Add an extension method to get a result's duration.

* [TouchRunner] A TestSuite's name can't be empty in recent versions of NUnit.

* [TouchRunner] Use usings.

* [Touch.Client] Add a client that uses MonoTouch.Dialog and NUnitLite from NuGet.
  • Loading branch information
rolfbjarne committed May 14, 2020
1 parent fbf9f30 commit 705964d
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 13 deletions.
4 changes: 4 additions & 0 deletions NUnitLite/TouchRunner/NUnitOutputTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
using System.IO;
using System.Text;

#if NUNITLITE_NUGET
using NUnitLite;
#else
using NUnitLite.Runner;
#endif
using MonoTouch.NUnit.UI;

namespace MonoTouch.NUnit {
Expand Down
10 changes: 9 additions & 1 deletion NUnitLite/TouchRunner/TestCaseElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@

using NUnit.Framework;
using NUnit.Framework.Internal;
#if NUNITLITE_NUGET
using NUnit.Framework.Interfaces;
#else
using NUnit.Framework.Api;
#endif

namespace MonoTouch.NUnit.UI {

Expand All @@ -43,12 +47,16 @@ public TestCaseElement (TestMethod testCase, TouchRunner runner)
return;

var suite = (testCase.Parent as TestSuite);
#if NUNITLITE_NUGET
Run ();
#else
var context = TestExecutionContext.CurrentContext;
context.TestObject = Reflect.Construct (testCase.Method.ReflectedType, null);

suite.GetOneTimeSetUpCommand ().Execute (context);
Run ();
suite.GetOneTimeTearDownCommand ().Execute (context);
#endif

Runner.CloseWriter ();
// display more details on (any) failure (but not when ignored)
Expand Down Expand Up @@ -87,7 +95,7 @@ public override void Update ()
int counter = Result.AssertCount;
Value = String.Format ("{0} {1} ms for {2} assertion{3}",
Result.IsInconclusive () ? "Inconclusive." : "Success!",
Result.Duration.TotalMilliseconds, counter,
Result.GetDuration ().TotalMilliseconds, counter,
counter == 1 ? String.Empty : "s");
DetailColor = DarkGreen;
} else if (Result.IsFailure ()) {
Expand Down
4 changes: 4 additions & 0 deletions NUnitLite/TouchRunner/TestElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
using MonoTouch.Dialog;

using NUnit.Framework.Internal;
#if NUNITLITE_NUGET
using NUnit.Framework.Interfaces;
#else
using NUnit.Framework.Api;
#endif

namespace MonoTouch.NUnit.UI {

Expand Down
24 changes: 24 additions & 0 deletions NUnitLite/TouchRunner/TestRocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@
// limitations under the License.
//

using System;
using System.IO;

using NUnit.Framework;
using NUnit.Framework.Internal;
#if NUNITLITE_NUGET
using NUnitLite;
using NUnit.Framework.Interfaces;
#else
using NUnit.Framework.Api;
#endif

namespace MonoTouch.NUnit {

Expand Down Expand Up @@ -58,5 +66,21 @@ static public string GetMessage (this TestResult result)
return m;
return m.Substring (m.IndexOf (" : ") + 3);
}

static public TimeSpan GetDuration (this TestResult result)
{
#if NUNITLITE_NUGET
return TimeSpan.FromSeconds (result.Duration);
#else
return result.Duration;
#endif
}

#if NUNITLITE_NUGET
static public void WriteResultFile (this NUnitLite.OutputWriter @this, ITestResult result, TextWriter writer)
{
@this.WriteResultFile (result, writer, null, null);
}
#endif
}
}
2 changes: 1 addition & 1 deletion NUnitLite/TouchRunner/TestSuiteElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public override void Update ()
StringBuilder sb = new StringBuilder ();
if (failure == 0) {
DetailColor = DarkGreen;
sb.Append ("Success! ").Append (Result.Duration.TotalMilliseconds).Append (" ms for ").Append (positive).Append (" test");
sb.Append ("Success! ").Append (Result.GetDuration ().TotalMilliseconds).Append (" ms for ").Append (positive).Append (" test");
if (positive > 1)
sb.Append ('s');
} else {
Expand Down
146 changes: 135 additions & 11 deletions NUnitLite/TouchRunner/TouchRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,24 @@
using NUnit.Framework.Api;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
#if NUNITLITE_NUGET
using NUnitLite;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal.Execution;
#else
using NUnitLite.Runner;
using NUnit.Framework.Internal.WorkItems;
#endif

#if NUNITLITE_NUGET
using SettingsDictionary = System.Collections.Generic.IDictionary<string, object>;
#else
using SettingsDictionary = System.Collections.IDictionary;
#endif

namespace MonoTouch.NUnit.UI {
public abstract class BaseTouchRunner : ITestListener {
TestSuite suite = new TestSuite (String.Empty);
TestSuite suite = new TestSuite ("TestSuite");
ITestFilter filter = TestFilter.Empty;
bool connection_failure;

Expand Down Expand Up @@ -119,7 +132,7 @@ protected virtual void ExecuteOnMainThread (Action action)
public void LoadSync ()
{
foreach (Assembly assembly in assemblies)
Load (assembly, fixtures == null ? null : new Dictionary<string, IList<string>> () { { "LOAD", fixtures } });
Load (assembly);
assemblies.Clear ();
}

Expand Down Expand Up @@ -275,13 +288,21 @@ public bool OpenWriter (string message)
break;
}
if (options.EnableXml) {
NUnitLite.Runner.OutputWriter formatter;
OutputWriter formatter;
switch (options.XmlVersion) {
case XmlVersion.NUnitV3:
formatter = new NUnitLite.Runner.NUnit3XmlOutputWriter (DateTime.UtcNow);
#if NUNITLITE_NUGET
formatter = new NUnit3XmlOutputWriter ();
#else
formatter = new NUnit3XmlOutputWriter (DateTime.UtcNow);
#endif
break;
default:
formatter = new NUnitLite.Runner.NUnit2XmlOutputWriter (DateTime.UtcNow);
#if NUNITLITE_NUGET
formatter = new NUnit2XmlOutputWriter ();
#else
formatter = new NUnit2XmlOutputWriter (DateTime.UtcNow);
#endif
break;
}
Writer = new NUnitOutputTextWriter (
Expand Down Expand Up @@ -388,7 +409,7 @@ public virtual void TestFinished (ITestResult r)

string name = result.Test.Name;
if (!String.IsNullOrEmpty (name))
Writer.WriteLine ("{0} : {1} ms", name, result.Duration.TotalMilliseconds);
Writer.WriteLine ("{0} : {1} ms", name, result.GetDuration ().TotalMilliseconds);
} else {
if (result.IsSuccess ()) {
Writer.Write ("\t[PASS] ");
Expand All @@ -405,7 +426,11 @@ public virtual void TestFinished (ITestResult r)
} else {
Writer.Write ("\t[INFO] ");
}
#if NUNITLITE_NUGET
Writer.Write (result.Test.FullName);
#else
Writer.Write (result.Test.FixtureType.Name);
#endif
Writer.Write (".");
Writer.Write (result.Test.Name);

Expand All @@ -424,18 +449,55 @@ public virtual void TestFinished (ITestResult r)
}
}

Dictionary<string, object> default_settings = new Dictionary<string, object> () {
#if NUNITLITE_NUGET
{ "RunOnMainThread", true },
#endif
};

SettingsDictionary CreateSettings (SettingsDictionary settings)
{
if (fixtures == null && (settings == null || settings.Count == 0))
return default_settings;

var dict = new Dictionary<string, object> (default_settings);

if (settings != null) {
foreach (var key in settings.Keys)
dict [key?.ToString ()] = settings [key];
}

if (fixtures != null)
dict ["LOAD"] = fixtures;

return dict;
}

#if NUNITLITE_NUGET
NUnitTestAssemblyRunner runner = new NUnitTestAssemblyRunner (new DefaultTestAssemblyBuilder ());

public bool Load (string assemblyName, IDictionary<string, object> settings = null)
{
return AddSuite ((TestSuite) runner.Load (assemblyName, CreateSettings (settings)));
}

public bool Load (Assembly assembly, IDictionary<string, object> settings = null)
{
return AddSuite ((TestSuite) runner.Load (assembly, CreateSettings (settings)));
}
#else
NUnitLiteTestAssemblyBuilder builder = new NUnitLiteTestAssemblyBuilder ();
Dictionary<string, object> empty = new Dictionary<string, object> ();

public bool Load (string assemblyName, IDictionary settings)
public bool Load (string assemblyName, SettingsDictionary settings = null)
{
return AddSuite (builder.Build (assemblyName, settings ?? empty));
return AddSuite (builder.Build (assemblyName, CreateSettings (settings)));
}

public bool Load (Assembly assembly, IDictionary settings)
public bool Load (Assembly assembly, SettingsDictionary settings = null)
{
return AddSuite (builder.Build (assembly, settings ?? empty));
return AddSuite (builder.Build (assembly, CreateSettings (settings)));
}
#endif

bool AddSuite (TestSuite ts)
{
Expand All @@ -453,12 +515,34 @@ public TestResult Run (Test test)
InconclusiveCount = 0;

Result = null;

#if NUNITLITE_NUGET
runner.Run (this, new MatchTestFilter { MatchTest = test });

// The TestResult we get back from the runner is for the top-most test suite,
// which isn't necessarily the test that we ran. So look for the TestResult
// for the test we ran.
ITestResult find_result (ITestResult tr)
{
if (tr.Test == test)
return tr;
foreach (var child in tr.Children) {
var r = find_result (child);
if (r != null)
return r;
}
return null;
}

Result = (TestResult) (find_result (runner.Result) ?? runner.Result);
#else
TestExecutionContext current = TestExecutionContext.CurrentContext;
current.WorkDirectory = Environment.CurrentDirectory;
current.Listener = this;
WorkItem wi = test.CreateWorkItem (filter, new FinallyDelegate ());
wi.Execute (current);
Result = wi.Result;
#endif
return Result;
}

Expand All @@ -471,6 +555,13 @@ public ITest LoadedTest {
public void TestOutput (TestOutput testOutput)
{
}

#if NUNITLITE_NUGET
public void SendMessage (TestMessage message)
{
Writer.WriteLine (message.ToString ());
}
#endif
}

#if __WATCHOS__
Expand Down Expand Up @@ -696,4 +787,37 @@ protected override void ExecuteOnMainThread (Action action)
}
}
#endif

// A filter that matches a specific test
class MatchTestFilter : TestFilter {
public ITest MatchTest;

#if NUNITLITE_NUGET
public override TNode AddToXml (TNode parentNode, bool recursive)
{
throw new NotImplementedException ();
}
#endif

public override bool Match (ITest test)
{
return IsMatch (test, MatchTest);
}

static bool IsMatch (ITest test, ITest match)
{
if (test == match)
return true;

if (match.HasChildren) {
foreach (var child in match.Tests) {
if (IsMatch (test, child))
return true;
}
}

return false;

}
}
}
Loading

0 comments on commit 705964d

Please sign in to comment.