2
2
using System . Collections . Generic ;
3
3
using System . Diagnostics . CodeAnalysis ;
4
4
using System . Linq ;
5
+ using System . Threading . Tasks ;
5
6
using Moq ;
6
7
using NUnit . Framework ;
7
8
using Rubberduck . Parsing . UIContext ;
@@ -47,8 +48,17 @@ internal class MockedTestEngine : IDisposable
47
48
48
49
private MockedTestEngine ( )
49
50
{
50
- Dispatcher . Setup ( d => d . InvokeAsync ( It . IsAny < Action > ( ) ) ) . Callback ( ( Action action ) => action . Invoke ( ) ) . Verifiable ( ) ;
51
-
51
+ Dispatcher . Setup ( d => d . InvokeAsync ( It . IsAny < Action > ( ) ) )
52
+ . Callback ( ( Action action ) => action . Invoke ( ) )
53
+ . Verifiable ( ) ;
54
+ Dispatcher . Setup ( d => d . StartTask ( It . IsAny < Action > ( ) , It . IsAny < TaskCreationOptions > ( ) ) )
55
+ . Returns ( ( Action action , TaskCreationOptions options ) =>
56
+ {
57
+ action . Invoke ( ) ;
58
+ return Task . CompletedTask ;
59
+ } )
60
+ . Verifiable ( ) ;
61
+
52
62
TypeLib . Setup ( tlm => tlm . Dispose ( ) ) . Verifiable ( ) ;
53
63
WrapperProvider . Setup ( p => p . TypeLibWrapperFromProject ( It . IsAny < string > ( ) ) ) . Returns ( TypeLib . Object ) . Verifiable ( ) ;
54
64
@@ -64,7 +74,7 @@ public MockedTestEngine(string testModuleCode) : this()
64
74
65
75
Vbe = builder . Build ( ) ;
66
76
ParserState = MockParser . Create ( Vbe . Object ) . State ;
67
- TestEngine = new TestEngine ( ParserState , _fakesFactory . Object , VbeInteraction . Object , WrapperProvider . Object , Dispatcher . Object , Vbe . Object ) ;
77
+ TestEngine = new SynchronouslySuspendingTestEngine ( ParserState , _fakesFactory . Object , VbeInteraction . Object , WrapperProvider . Object , Dispatcher . Object , Vbe . Object ) ;
68
78
}
69
79
70
80
public MockedTestEngine ( IReadOnlyList < string > moduleNames , IReadOnlyList < int > methodCounts ) : this ( )
@@ -87,7 +97,7 @@ public MockedTestEngine(IReadOnlyList<string> moduleNames, IReadOnlyList<int> me
87
97
project . AddProjectToVbeBuilder ( ) ;
88
98
Vbe = builder . Build ( ) ;
89
99
ParserState = MockParser . Create ( Vbe . Object ) . State ;
90
- TestEngine = new TestEngine ( ParserState , _fakesFactory . Object , VbeInteraction . Object , WrapperProvider . Object , Dispatcher . Object , Vbe . Object ) ;
100
+ TestEngine = new SynchronouslySuspendingTestEngine ( ParserState , _fakesFactory . Object , VbeInteraction . Object , WrapperProvider . Object , Dispatcher . Object , Vbe . Object ) ;
91
101
}
92
102
93
103
public MockedTestEngine ( int testMethodCount )
@@ -246,5 +256,32 @@ Public Sub TestCleanup()
246
256
'this method runs after every test in the module.
247
257
End Sub
248
258
" ;
259
+
260
+ private class SynchronouslySuspendingTestEngine : TestEngine
261
+ {
262
+ private readonly RubberduckParserState _state ;
263
+
264
+ public SynchronouslySuspendingTestEngine (
265
+ RubberduckParserState state ,
266
+ IFakesFactory fakesFactory ,
267
+ IVBEInteraction declarationRunner ,
268
+ ITypeLibWrapperProvider wrapperProvider ,
269
+ IUiDispatcher uiDispatcher ,
270
+ IVBE vbe )
271
+ : base ( state , fakesFactory , declarationRunner , wrapperProvider , uiDispatcher , vbe )
272
+ {
273
+ _state = state ;
274
+ }
275
+
276
+ protected override void RunInternal ( IEnumerable < TestMethod > tests )
277
+ {
278
+ if ( ! CanRun )
279
+ {
280
+ return ;
281
+ }
282
+ //We have to do this on the same thread here to guarantee that the actions runs before the assert in the unit tests is called.
283
+ _state . OnSuspendParser ( this , AllowedRunStates , ( ) => RunWhileSuspended ( tests ) ) ;
284
+ }
285
+ }
249
286
}
250
287
}
0 commit comments