4040using NUnit . Framework . Api ;
4141using NUnit . Framework . Internal ;
4242using 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 ;
4349using 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
4558namespace 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