File tree Expand file tree Collapse file tree 4 files changed +96
-1
lines changed Expand file tree Collapse file tree 4 files changed +96
-1
lines changed Original file line number Diff line number Diff line change 262
262
<Compile Include =" SafeComWrappers\MSForms\WindowState.cs" />
263
263
<Compile Include =" SafeComWrappers\SafeComWrapper.cs" />
264
264
<Compile Include =" SafeComWrappers\VBA\VBE.cs" />
265
+ <Compile Include =" Utility\DisposalActionContainer.cs" />
265
266
<Compile Include =" WindowsApi\ChildWindowFinder.cs" />
266
267
<Compile Include =" WindowsApi\CodePaneSubclass.cs" />
267
268
<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 interface IDisposalActionContainer < out T > : IDisposable
6
+ {
7
+ T Value { get ; }
8
+ }
9
+
10
+ internal sealed class DisposalActionContainer < T > : IDisposalActionContainer < T >
11
+ {
12
+ public T Value { get ; }
13
+ private readonly Action _disposalAction ;
14
+
15
+ public DisposalActionContainer ( T value , Action disposalAction )
16
+ {
17
+ Value = value ;
18
+ _disposalAction = disposalAction ;
19
+ }
20
+
21
+ private bool _isDisposed = false ;
22
+ private readonly object _disposalLockObject = new object ( ) ;
23
+ public void Dispose ( )
24
+ {
25
+ lock ( _disposalLockObject )
26
+ {
27
+ if ( _isDisposed )
28
+ {
29
+ return ;
30
+ }
31
+ _isDisposed = true ;
32
+ }
33
+
34
+ _disposalAction . Invoke ( ) ;
35
+ }
36
+ }
37
+
38
+ public static class DisposalActionContainer
39
+ {
40
+ public static IDisposalActionContainer < T > Create < T > ( T value , Action disposalAction )
41
+ {
42
+ return new DisposalActionContainer < T > ( value , disposalAction ) ;
43
+ }
44
+ }
45
+ }
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