1+ using System ;
2+ using System . Collections . Generic ;
3+ using NSubstitute ;
4+ using SystemsRx . Extensions ;
5+ using Xunit ;
6+
7+ namespace SystemsRx . Tests . SystemsRx . Extensions
8+ {
9+ public class IDictionaryExtensionTests
10+ {
11+ [ Fact ]
12+ public void should_work_with_nulls_and_disposables_individually ( )
13+ {
14+ var mockDisposable1 = Substitute . For < IDisposable > ( ) ;
15+ var mockDisposable2 = Substitute . For < IDisposable > ( ) ;
16+
17+ var dictionary = new Dictionary < int , IDisposable >
18+ {
19+ { 1 , null } ,
20+ { 2 , mockDisposable1 } ,
21+ { 3 , null } ,
22+ { 4 , mockDisposable2 }
23+ } ;
24+
25+ dictionary . RemoveAndDispose ( 1 ) ;
26+ dictionary . RemoveAndDispose ( 2 ) ;
27+ dictionary . RemoveAndDispose ( 3 ) ;
28+ dictionary . RemoveAndDispose ( 4 ) ;
29+
30+ Assert . Empty ( dictionary ) ;
31+ mockDisposable1 . Received ( 1 ) . Dispose ( ) ;
32+ mockDisposable2 . Received ( 1 ) . Dispose ( ) ;
33+ }
34+
35+ [ Fact ]
36+ public void should_work_with_nulls_and_disposables ( )
37+ {
38+ var mockDisposable1 = Substitute . For < IDisposable > ( ) ;
39+ var mockDisposable2 = Substitute . For < IDisposable > ( ) ;
40+
41+ var dictionary = new Dictionary < int , IDisposable >
42+ {
43+ { 1 , null } ,
44+ { 2 , mockDisposable1 } ,
45+ { 3 , null } ,
46+ { 4 , mockDisposable2 }
47+ } ;
48+
49+ dictionary . RemoveAndDisposeAll ( ) ;
50+
51+ Assert . Empty ( dictionary ) ;
52+ mockDisposable1 . Received ( 1 ) . Dispose ( ) ;
53+ mockDisposable2 . Received ( 1 ) . Dispose ( ) ;
54+ }
55+
56+ [ Fact ]
57+ public void should_work_with_nulls_and_disposables_in_enumerable ( )
58+ {
59+ var mockDisposable1 = Substitute . For < IDisposable > ( ) ;
60+ var mockDisposable2 = Substitute . For < IDisposable > ( ) ;
61+
62+ var collection = new [ ]
63+ {
64+ null ,
65+ mockDisposable1 ,
66+ null ,
67+ mockDisposable2
68+ } ;
69+
70+ collection . DisposeAll ( ) ;
71+
72+ Assert . Equal ( 4 , collection . Length ) ;
73+ mockDisposable1 . Received ( 1 ) . Dispose ( ) ;
74+ mockDisposable2 . Received ( 1 ) . Dispose ( ) ;
75+ }
76+ }
77+ }
0 commit comments