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

Commit 705964d

Browse files
authored
[Touch.Client] Add a client that uses MonoTouch.Dialog and NUnitLite 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.
1 parent fbf9f30 commit 705964d

File tree

8 files changed

+297
-13
lines changed

8 files changed

+297
-13
lines changed

NUnitLite/TouchRunner/NUnitOutputTextWriter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
using System.IO;
2323
using System.Text;
2424

25+
#if NUNITLITE_NUGET
26+
using NUnitLite;
27+
#else
2528
using NUnitLite.Runner;
29+
#endif
2630
using MonoTouch.NUnit.UI;
2731

2832
namespace MonoTouch.NUnit {

NUnitLite/TouchRunner/TestCaseElement.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727

2828
using NUnit.Framework;
2929
using NUnit.Framework.Internal;
30+
#if NUNITLITE_NUGET
31+
using NUnit.Framework.Interfaces;
32+
#else
3033
using NUnit.Framework.Api;
34+
#endif
3135

3236
namespace MonoTouch.NUnit.UI {
3337

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

4549
var suite = (testCase.Parent as TestSuite);
50+
#if NUNITLITE_NUGET
51+
Run ();
52+
#else
4653
var context = TestExecutionContext.CurrentContext;
4754
context.TestObject = Reflect.Construct (testCase.Method.ReflectedType, null);
4855

4956
suite.GetOneTimeSetUpCommand ().Execute (context);
5057
Run ();
5158
suite.GetOneTimeTearDownCommand ().Execute (context);
59+
#endif
5260

5361
Runner.CloseWriter ();
5462
// display more details on (any) failure (but not when ignored)
@@ -87,7 +95,7 @@ public override void Update ()
8795
int counter = Result.AssertCount;
8896
Value = String.Format ("{0} {1} ms for {2} assertion{3}",
8997
Result.IsInconclusive () ? "Inconclusive." : "Success!",
90-
Result.Duration.TotalMilliseconds, counter,
98+
Result.GetDuration ().TotalMilliseconds, counter,
9199
counter == 1 ? String.Empty : "s");
92100
DetailColor = DarkGreen;
93101
} else if (Result.IsFailure ()) {

NUnitLite/TouchRunner/TestElement.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
using MonoTouch.Dialog;
2626

2727
using NUnit.Framework.Internal;
28+
#if NUNITLITE_NUGET
29+
using NUnit.Framework.Interfaces;
30+
#else
2831
using NUnit.Framework.Api;
32+
#endif
2933

3034
namespace MonoTouch.NUnit.UI {
3135

NUnitLite/TouchRunner/TestRocks.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,17 @@
1818
// limitations under the License.
1919
//
2020

21+
using System;
22+
using System.IO;
23+
2124
using NUnit.Framework;
2225
using NUnit.Framework.Internal;
26+
#if NUNITLITE_NUGET
27+
using NUnitLite;
28+
using NUnit.Framework.Interfaces;
29+
#else
2330
using NUnit.Framework.Api;
31+
#endif
2432

2533
namespace MonoTouch.NUnit {
2634

@@ -58,5 +66,21 @@ static public string GetMessage (this TestResult result)
5866
return m;
5967
return m.Substring (m.IndexOf (" : ") + 3);
6068
}
69+
70+
static public TimeSpan GetDuration (this TestResult result)
71+
{
72+
#if NUNITLITE_NUGET
73+
return TimeSpan.FromSeconds (result.Duration);
74+
#else
75+
return result.Duration;
76+
#endif
77+
}
78+
79+
#if NUNITLITE_NUGET
80+
static public void WriteResultFile (this NUnitLite.OutputWriter @this, ITestResult result, TextWriter writer)
81+
{
82+
@this.WriteResultFile (result, writer, null, null);
83+
}
84+
#endif
6185
}
6286
}

NUnitLite/TouchRunner/TestSuiteElement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public override void Update ()
6767
StringBuilder sb = new StringBuilder ();
6868
if (failure == 0) {
6969
DetailColor = DarkGreen;
70-
sb.Append ("Success! ").Append (Result.Duration.TotalMilliseconds).Append (" ms for ").Append (positive).Append (" test");
70+
sb.Append ("Success! ").Append (Result.GetDuration ().TotalMilliseconds).Append (" ms for ").Append (positive).Append (" test");
7171
if (positive > 1)
7272
sb.Append ('s');
7373
} else {

NUnitLite/TouchRunner/TouchRunner.cs

Lines changed: 135 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,24 @@
4040
using NUnit.Framework.Api;
4141
using NUnit.Framework.Internal;
4242
using NUnit.Framework.Internal.Commands;
43+
#if NUNITLITE_NUGET
44+
using NUnitLite;
45+
using NUnit.Framework.Interfaces;
46+
using NUnit.Framework.Internal.Execution;
47+
#else
48+
using NUnitLite.Runner;
4349
using NUnit.Framework.Internal.WorkItems;
50+
#endif
51+
52+
#if NUNITLITE_NUGET
53+
using SettingsDictionary = System.Collections.Generic.IDictionary<string, object>;
54+
#else
55+
using SettingsDictionary = System.Collections.IDictionary;
56+
#endif
4457

4558
namespace MonoTouch.NUnit.UI {
4659
public abstract class BaseTouchRunner : ITestListener {
47-
TestSuite suite = new TestSuite (String.Empty);
60+
TestSuite suite = new TestSuite ("TestSuite");
4861
ITestFilter filter = TestFilter.Empty;
4962
bool connection_failure;
5063

@@ -119,7 +132,7 @@ protected virtual void ExecuteOnMainThread (Action action)
119132
public void LoadSync ()
120133
{
121134
foreach (Assembly assembly in assemblies)
122-
Load (assembly, fixtures == null ? null : new Dictionary<string, IList<string>> () { { "LOAD", fixtures } });
135+
Load (assembly);
123136
assemblies.Clear ();
124137
}
125138

@@ -275,13 +288,21 @@ public bool OpenWriter (string message)
275288
break;
276289
}
277290
if (options.EnableXml) {
278-
NUnitLite.Runner.OutputWriter formatter;
291+
OutputWriter formatter;
279292
switch (options.XmlVersion) {
280293
case XmlVersion.NUnitV3:
281-
formatter = new NUnitLite.Runner.NUnit3XmlOutputWriter (DateTime.UtcNow);
294+
#if NUNITLITE_NUGET
295+
formatter = new NUnit3XmlOutputWriter ();
296+
#else
297+
formatter = new NUnit3XmlOutputWriter (DateTime.UtcNow);
298+
#endif
282299
break;
283300
default:
284-
formatter = new NUnitLite.Runner.NUnit2XmlOutputWriter (DateTime.UtcNow);
301+
#if NUNITLITE_NUGET
302+
formatter = new NUnit2XmlOutputWriter ();
303+
#else
304+
formatter = new NUnit2XmlOutputWriter (DateTime.UtcNow);
305+
#endif
285306
break;
286307
}
287308
Writer = new NUnitOutputTextWriter (
@@ -388,7 +409,7 @@ public virtual void TestFinished (ITestResult r)
388409

389410
string name = result.Test.Name;
390411
if (!String.IsNullOrEmpty (name))
391-
Writer.WriteLine ("{0} : {1} ms", name, result.Duration.TotalMilliseconds);
412+
Writer.WriteLine ("{0} : {1} ms", name, result.GetDuration ().TotalMilliseconds);
392413
} else {
393414
if (result.IsSuccess ()) {
394415
Writer.Write ("\t[PASS] ");
@@ -405,7 +426,11 @@ public virtual void TestFinished (ITestResult r)
405426
} else {
406427
Writer.Write ("\t[INFO] ");
407428
}
429+
#if NUNITLITE_NUGET
430+
Writer.Write (result.Test.FullName);
431+
#else
408432
Writer.Write (result.Test.FixtureType.Name);
433+
#endif
409434
Writer.Write (".");
410435
Writer.Write (result.Test.Name);
411436

@@ -424,18 +449,55 @@ public virtual void TestFinished (ITestResult r)
424449
}
425450
}
426451

452+
Dictionary<string, object> default_settings = new Dictionary<string, object> () {
453+
#if NUNITLITE_NUGET
454+
{ "RunOnMainThread", true },
455+
#endif
456+
};
457+
458+
SettingsDictionary CreateSettings (SettingsDictionary settings)
459+
{
460+
if (fixtures == null && (settings == null || settings.Count == 0))
461+
return default_settings;
462+
463+
var dict = new Dictionary<string, object> (default_settings);
464+
465+
if (settings != null) {
466+
foreach (var key in settings.Keys)
467+
dict [key?.ToString ()] = settings [key];
468+
}
469+
470+
if (fixtures != null)
471+
dict ["LOAD"] = fixtures;
472+
473+
return dict;
474+
}
475+
476+
#if NUNITLITE_NUGET
477+
NUnitTestAssemblyRunner runner = new NUnitTestAssemblyRunner (new DefaultTestAssemblyBuilder ());
478+
479+
public bool Load (string assemblyName, IDictionary<string, object> settings = null)
480+
{
481+
return AddSuite ((TestSuite) runner.Load (assemblyName, CreateSettings (settings)));
482+
}
483+
484+
public bool Load (Assembly assembly, IDictionary<string, object> settings = null)
485+
{
486+
return AddSuite ((TestSuite) runner.Load (assembly, CreateSettings (settings)));
487+
}
488+
#else
427489
NUnitLiteTestAssemblyBuilder builder = new NUnitLiteTestAssemblyBuilder ();
428-
Dictionary<string, object> empty = new Dictionary<string, object> ();
429490

430-
public bool Load (string assemblyName, IDictionary settings)
491+
public bool Load (string assemblyName, SettingsDictionary settings = null)
431492
{
432-
return AddSuite (builder.Build (assemblyName, settings ?? empty));
493+
return AddSuite (builder.Build (assemblyName, CreateSettings (settings)));
433494
}
434495

435-
public bool Load (Assembly assembly, IDictionary settings)
496+
public bool Load (Assembly assembly, SettingsDictionary settings = null)
436497
{
437-
return AddSuite (builder.Build (assembly, settings ?? empty));
498+
return AddSuite (builder.Build (assembly, CreateSettings (settings)));
438499
}
500+
#endif
439501

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

455517
Result = null;
518+
519+
#if NUNITLITE_NUGET
520+
runner.Run (this, new MatchTestFilter { MatchTest = test });
521+
522+
// The TestResult we get back from the runner is for the top-most test suite,
523+
// which isn't necessarily the test that we ran. So look for the TestResult
524+
// for the test we ran.
525+
ITestResult find_result (ITestResult tr)
526+
{
527+
if (tr.Test == test)
528+
return tr;
529+
foreach (var child in tr.Children) {
530+
var r = find_result (child);
531+
if (r != null)
532+
return r;
533+
}
534+
return null;
535+
}
536+
537+
Result = (TestResult) (find_result (runner.Result) ?? runner.Result);
538+
#else
456539
TestExecutionContext current = TestExecutionContext.CurrentContext;
457540
current.WorkDirectory = Environment.CurrentDirectory;
458541
current.Listener = this;
459542
WorkItem wi = test.CreateWorkItem (filter, new FinallyDelegate ());
460543
wi.Execute (current);
461544
Result = wi.Result;
545+
#endif
462546
return Result;
463547
}
464548

@@ -471,6 +555,13 @@ public ITest LoadedTest {
471555
public void TestOutput (TestOutput testOutput)
472556
{
473557
}
558+
559+
#if NUNITLITE_NUGET
560+
public void SendMessage (TestMessage message)
561+
{
562+
Writer.WriteLine (message.ToString ());
563+
}
564+
#endif
474565
}
475566

476567
#if __WATCHOS__
@@ -696,4 +787,37 @@ protected override void ExecuteOnMainThread (Action action)
696787
}
697788
}
698789
#endif
790+
791+
// A filter that matches a specific test
792+
class MatchTestFilter : TestFilter {
793+
public ITest MatchTest;
794+
795+
#if NUNITLITE_NUGET
796+
public override TNode AddToXml (TNode parentNode, bool recursive)
797+
{
798+
throw new NotImplementedException ();
799+
}
800+
#endif
801+
802+
public override bool Match (ITest test)
803+
{
804+
return IsMatch (test, MatchTest);
805+
}
806+
807+
static bool IsMatch (ITest test, ITest match)
808+
{
809+
if (test == match)
810+
return true;
811+
812+
if (match.HasChildren) {
813+
foreach (var child in match.Tests) {
814+
if (IsMatch (test, child))
815+
return true;
816+
}
817+
}
818+
819+
return false;
820+
821+
}
822+
}
699823
}

0 commit comments

Comments
 (0)