33using System . Threading ;
44using System . Threading . Tasks ;
55using AutoMoq ;
6+ using FineCodeCoverage . Core . Initialization ;
67using FineCodeCoverage . Core . Utilities ;
78using FineCodeCoverage . Engine ;
89using FineCodeCoverage . Engine . Model ;
1516
1617namespace Test
1718{
19+
20+ internal class TestOperationStateInvocationManager_Tests
21+ {
22+ private AutoMoqer mocker ;
23+ private TestOperationStateInvocationManager testOperationStateInvocationManager ;
24+
25+ [ SetUp ]
26+ public void SetUp ( )
27+ {
28+ mocker = new AutoMoqer ( ) ;
29+ testOperationStateInvocationManager = mocker . Create < TestOperationStateInvocationManager > ( ) ;
30+ }
31+
32+ [ Test ]
33+ public void Should_Return_True_When_Initialized_And_TestExecutionStarting ( )
34+ {
35+ mocker . GetMock < IInitializeStatusProvider > ( ) . Setup ( initializeStatusProvider => initializeStatusProvider . InitializeStatus ) . Returns ( InitializeStatus . Initialized ) ;
36+ Assert . That ( testOperationStateInvocationManager . CanInvoke ( TestOperationStates . TestExecutionStarting ) , Is . True ) ;
37+ }
38+
39+ [ Test ]
40+ public void Should_Return_False_When_Not_Initialized_And_TestExecutionStarting ( )
41+ {
42+ mocker . GetMock < IInitializeStatusProvider > ( ) . Setup ( initializeStatusProvider => initializeStatusProvider . InitializeStatus ) . Returns ( InitializeStatus . Initializing ) ;
43+ Assert . That ( testOperationStateInvocationManager . CanInvoke ( TestOperationStates . TestExecutionStarting ) , Is . False ) ;
44+ }
45+
46+ [ TestCase ( true ) ]
47+ [ TestCase ( false ) ]
48+ public void Should_Return_True_For_All_Other_States_If_Was_Initialized_When_TestExecutionStarting ( bool initializedWhenStarting )
49+ {
50+ var startingInitializeStatus = initializedWhenStarting ? InitializeStatus . Initialized : InitializeStatus . Initializing ;
51+ mocker . GetMock < IInitializeStatusProvider > ( ) . Setup ( initializeStatusProvider => initializeStatusProvider . InitializeStatus ) . Returns ( startingInitializeStatus ) ;
52+ testOperationStateInvocationManager . CanInvoke ( TestOperationStates . TestExecutionStarting ) ;
53+ Assert . That ( testOperationStateInvocationManager . CanInvoke ( TestOperationStates . TestExecutionCancelAndFinished ) , Is . EqualTo ( initializedWhenStarting ) ) ;
54+ }
55+
56+ [ TestCase ( TestOperationStates . TestExecutionStarting ) ]
57+ [ TestCase ( TestOperationStates . TestExecutionFinished ) ]
58+ public void Should_Log_When_Cannot_Invoke ( TestOperationStates testOperationState )
59+ {
60+ testOperationStateInvocationManager . CanInvoke ( testOperationState ) ;
61+ mocker . Verify < ILogger > ( logger => logger . Log ( $ "Skipping { testOperationState } as FCC not initialized") ) ;
62+ }
63+
64+ }
65+
1866 internal class TestContainerDiscovery_Tests
1967 {
2068 private AutoMoqer mocker ;
@@ -73,42 +121,19 @@ private void SetUpOptions(Action<Mock<IAppOptions>> setupAppOptions)
73121 public void SetUp ( )
74122 {
75123 mocker = new AutoMoqer ( ) ;
76- var mockDisposeAwareTaskRunner = mocker . GetMock < IDisposeAwareTaskRunner > ( ) ;
77- mockDisposeAwareTaskRunner . Setup ( runner => runner . RunAsync ( It . IsAny < Func < Task > > ( ) ) ) . Callback < Func < Task > > ( async taskProvider => await taskProvider ( ) ) ;
78124 testContainerDiscoverer = mocker . Create < TestContainerDiscoverer > ( ) ;
79125 testContainerDiscoverer . RunAsync = ( taskProvider ) =>
80126 {
81127 taskProvider ( ) . Wait ( ) ;
82128 } ;
83- testContainerDiscoverer . initializeTask . Wait ( ) ;
84- }
85-
86- [ Test ]
87- public void It_Should_Initialize_As_Is_The_Entrance ( )
88- {
89- mocker . Verify < IInitializer > ( i => i . InitializeAsync ( It . IsAny < CancellationToken > ( ) ) ) ;
129+ var mockTestOperationStateInvocationManager = mocker . GetMock < ITestOperationStateInvocationManager > ( ) ;
130+ mockTestOperationStateInvocationManager . Setup ( testOperationStateInvocationManager => testOperationStateInvocationManager . CanInvoke ( It . IsAny < TestOperationStates > ( ) ) ) . Returns ( true ) ;
90131 }
91132
92133 [ Test ]
93- public async Task It_Should_Watch_For_Operation_State_Change_Before_Initialize ( )
134+ public void It_Should_Load_The_Package ( )
94135 {
95- List < int > order = new List < int > ( ) ;
96- mocker = new AutoMoqer ( ) ;
97- var mockDisposeAwareTaskRunner = mocker . GetMock < IDisposeAwareTaskRunner > ( ) ;
98- mockDisposeAwareTaskRunner . Setup ( runner => runner . RunAsync ( It . IsAny < Func < Task > > ( ) ) ) . Callback < Func < Task > > ( async taskProvider => await taskProvider ( ) ) ;
99- var mockOperationState = mocker . GetMock < IOperationState > ( ) ;
100- mockOperationState . SetupAdd ( o => o . StateChanged += It . IsAny < EventHandler < OperationStateChangedEventArgs > > ( ) ) . Callback ( ( ) =>
101- {
102- order . Add ( 1 ) ;
103- } ) ;
104- var mockInitializer = mocker . GetMock < IInitializer > ( ) ;
105- mockInitializer . Setup ( i => i . InitializeAsync ( It . IsAny < CancellationToken > ( ) ) ) . Callback ( ( ) =>
106- {
107- order . Add ( 2 ) ;
108- } ) ;
109- var testContainerDiscoverer = mocker . Create < TestContainerDiscoverer > ( ) ;
110- await testContainerDiscoverer . initializeTask ;
111- Assert . AreEqual ( new List < int > { 1 , 2 } , order ) ;
136+ mocker . Verify < IPackageLoader > ( packageLoader => packageLoader . LoadPackageAsync ( It . IsAny < CancellationToken > ( ) ) ) ;
112137 }
113138
114139 [ Test ]
@@ -348,5 +373,23 @@ public void Should_Handle_Any_Exception_In_OperationState_Changed_Handler_Loggin
348373 RaiseTestExecutionCancelling ( ) ;
349374 mocker . Verify < ILogger > ( logger => logger . Log ( "Error processing unit test events" , exception ) ) ;
350375 }
376+
377+ [ TestCase ( true ) ]
378+ [ TestCase ( false ) ]
379+ public void Should_Not_Handle_OperationState_Changes_When_The_testOperationStateInvocationManager_Cannot_Invoke ( bool canInvoke )
380+ {
381+ var invoked = false ;
382+ testContainerDiscoverer . testOperationStateChangeHandlers = new Dictionary < TestOperationStates , Func < IOperation , Task > >
383+ {
384+ { TestOperationStates . TestExecutionCanceling , ( _ ) => { invoked = true ; return Task . CompletedTask ; } }
385+ } ;
386+ var mockTestOperationStateInvocationManager = mocker . GetMock < ITestOperationStateInvocationManager > ( ) ;
387+ mockTestOperationStateInvocationManager . Setup ( testOperationStateInvocationManager => testOperationStateInvocationManager . CanInvoke ( It . IsAny < TestOperationStates > ( ) ) ) . Returns ( canInvoke ) ;
388+
389+ RaiseTestExecutionCancelling ( ) ;
390+ Assert . That ( invoked , Is . EqualTo ( canInvoke ) ) ;
391+
392+
393+ }
351394 }
352395}
0 commit comments