File tree Expand file tree Collapse file tree 4 files changed +91
-1
lines changed Expand file tree Collapse file tree 4 files changed +91
-1
lines changed Original file line number Diff line number Diff line change 257
257
<Compile Include =" SafeComWrappers\MSForms\WindowState.cs" />
258
258
<Compile Include =" SafeComWrappers\SafeComWrapper.cs" />
259
259
<Compile Include =" SafeComWrappers\VBA\VBE.cs" />
260
+ <Compile Include =" Utility\DisposalActionContainer.cs" />
260
261
<Compile Include =" WindowsApi\ChildWindowFinder.cs" />
261
262
<Compile Include =" WindowsApi\CodePaneSubclass.cs" />
262
263
<Compile Include =" WindowsApi\DesignerWindowSubclass.cs" />
Original file line number Diff line number Diff line change
1
+ using System ;
2
+
3
+ namespace Rubberduck . VBEditor . Utility
4
+ {
5
+ public sealed class DisposalActionContainer < T > : IDisposable
6
+ {
7
+ public T Value { get ; }
8
+ private readonly Action _disposalAction ;
9
+
10
+ public DisposalActionContainer ( T value , Action disposalAction )
11
+ {
12
+ Value = value ;
13
+ _disposalAction = disposalAction ;
14
+ }
15
+
16
+ private bool _isDisposed = false ;
17
+ private readonly object _disposalLockObject = new object ( ) ;
18
+ public void Dispose ( )
19
+ {
20
+ lock ( _disposalLockObject )
21
+ {
22
+ if ( _isDisposed )
23
+ {
24
+ return ;
25
+ }
26
+ _isDisposed = true ;
27
+ }
28
+
29
+ _disposalAction . Invoke ( ) ;
30
+ }
31
+ }
32
+
33
+ public static class DisposalActionContainer
34
+ {
35
+ public static DisposalActionContainer < T > Create < T > ( T value , Action disposalAction )
36
+ {
37
+ return new DisposalActionContainer < T > ( value , disposalAction ) ;
38
+ }
39
+ }
40
+ }
Original file line number Diff line number Diff line change 108
108
<Compile Include =" Mocks\MockExtentions.cs" />
109
109
<Compile Include =" Inspections\DefTypeStatementInspectionTests.cs" />
110
110
<Compile Include =" Inspections\InspectionProviderTests.cs" />
111
+ <Compile Include =" VBEditor\Utility\DisposalActionContainerTests.cs" />
111
112
<Compile Include =" VBEditor\ComSafeManagerTests.cs" />
112
113
<Compile Include =" VBEditor\ProjectsRepositoryTests.cs" />
113
114
<Compile Include =" VBEditor\ReferenceEqualityComparerTests.cs" />
346
347
<Name >Rubberduck.VBEditor</Name >
347
348
</ProjectReference >
348
349
</ItemGroup >
349
- <ItemGroup />
350
+ <ItemGroup >
351
+ <Folder Include =" Utility\" />
352
+ </ItemGroup >
350
353
<ItemGroup >
351
354
<None Include =" IntegrationTests\FakeTests.bas" />
352
355
<None Include =" IntegrationTests\StubTests.bas" />
Original file line number Diff line number Diff line change
1
+ using NUnit . Framework ;
2
+ using Rubberduck . VBEditor . Utility ;
3
+
4
+ namespace RubberduckTests . VBEditor . Utility
5
+ {
6
+ [ TestFixture ( ) ]
7
+ public class DisposalActionContainerTests
8
+ {
9
+ [ Test ( ) ]
10
+ public void ValueReturnsValuePassedIn ( )
11
+ {
12
+ var testValue = 42 ;
13
+ var dac = DisposalActionContainer . Create ( testValue , ( ) => { } ) ;
14
+ var returnedValue = dac . Value ;
15
+
16
+ Assert . AreEqual ( testValue , returnedValue ) ;
17
+ }
18
+
19
+ [ Test ( ) ]
20
+ public void FirstDisposeTriggersActionPassedIn ( )
21
+ {
22
+ var useCount = 0 ;
23
+ var dac = DisposalActionContainer . Create ( 42 , ( ) => useCount ++ ) ;
24
+ dac . Dispose ( ) ;
25
+ var expectedUseCount = 1 ;
26
+
27
+ Assert . AreEqual ( expectedUseCount , useCount ) ;
28
+ }
29
+
30
+ [ Test ( ) ]
31
+ public void MultipleCallsOfDisposeTriggerTheActionPassedInOnce ( )
32
+ {
33
+ var useCount = 0 ;
34
+ var dac = DisposalActionContainer . Create ( 42 , ( ) => useCount ++ ) ;
35
+ dac . Dispose ( ) ;
36
+ dac . Dispose ( ) ;
37
+ dac . Dispose ( ) ;
38
+ dac . Dispose ( ) ;
39
+ dac . Dispose ( ) ;
40
+ dac . Dispose ( ) ;
41
+ var expectedUseCount = 1 ;
42
+
43
+ Assert . AreEqual ( expectedUseCount , useCount ) ;
44
+ }
45
+ }
46
+ }
You can’t perform that action at this time.
0 commit comments