@@ -4,26 +4,34 @@ namespace Boxed.Mapping.Test
44 using System . Collections . Generic ;
55 using System . Collections . ObjectModel ;
66 using System . Linq ;
7+ using System . Threading ;
78 using System . Threading . Tasks ;
89 using Xunit ;
910
10- public class AsyncMapperTest
11+ public class AsyncMapperTest : Disposable
1112 {
13+ private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource ( ) ;
14+
1215 [ Fact ]
1316 public Task MapAsync_Null_ThrowsArgumentNullExceptionAsync ( )
1417 {
1518 var mapper = new AsyncMapper ( ) ;
1619
17- return Assert . ThrowsAsync < ArgumentNullException > ( "source" , ( ) => mapper . MapAsync ( null ) ) ;
20+ return Assert . ThrowsAsync < ArgumentNullException > (
21+ "source" ,
22+ ( ) => mapper . MapAsync ( null , this . cancellationTokenSource . Token ) ) ;
1823 }
1924
2025 [ Fact ]
2126 public async Task MapAsync_ToNewObject_MappedAsync ( )
2227 {
2328 var mapper = new AsyncMapper ( ) ;
2429
25- var to = await mapper . MapAsync ( new MapFrom ( ) { Property = 1 } ) . ConfigureAwait ( false ) ;
30+ var to = await mapper
31+ . MapAsync ( new MapFrom ( ) { Property = 1 } , this . cancellationTokenSource . Token )
32+ . ConfigureAwait ( false ) ;
2633
34+ Assert . Equal ( this . cancellationTokenSource . Token , mapper . CancellationToken ) ;
2735 Assert . Equal ( 1 , to . Property ) ;
2836 }
2937
@@ -32,7 +40,9 @@ public async Task MapArrayAsync_Empty_MappedAsync()
3240 {
3341 var mapper = new AsyncMapper ( ) ;
3442
35- var to = await mapper . MapArrayAsync ( Array . Empty < MapFrom > ( ) ) . ConfigureAwait ( false ) ;
43+ var to = await mapper
44+ . MapArrayAsync ( Array . Empty < MapFrom > ( ) , this . cancellationTokenSource . Token )
45+ . ConfigureAwait ( false ) ;
3646
3747 Assert . IsType < MapTo [ ] > ( to ) ;
3848 Assert . Empty ( to ) ;
@@ -49,9 +59,11 @@ public async Task MapArrayAsync_ToNewObject_MappedAsync()
4959 {
5060 new MapFrom ( ) { Property = 1 } ,
5161 new MapFrom ( ) { Property = 2 } ,
52- } )
62+ } ,
63+ this . cancellationTokenSource . Token )
5364 . ConfigureAwait ( false ) ;
5465
66+ Assert . Equal ( this . cancellationTokenSource . Token , mapper . CancellationToken ) ;
5567 Assert . IsType < MapTo [ ] > ( to ) ;
5668 Assert . Equal ( 2 , to . Length ) ;
5769 Assert . Equal ( 1 , to [ 0 ] . Property ) ;
@@ -63,7 +75,9 @@ public async Task MapTypedCollectionAsync_Empty_MappedAsync()
6375 {
6476 var mapper = new AsyncMapper ( ) ;
6577
66- var to = await mapper . MapCollectionAsync ( Array . Empty < MapFrom > ( ) , new List < MapTo > ( ) ) . ConfigureAwait ( false ) ;
78+ var to = await mapper
79+ . MapCollectionAsync ( Array . Empty < MapFrom > ( ) , new List < MapTo > ( ) , this . cancellationTokenSource . Token )
80+ . ConfigureAwait ( false ) ;
6781
6882 Assert . IsType < List < MapTo > > ( to ) ;
6983 Assert . Empty ( to ) ;
@@ -81,9 +95,11 @@ public async Task MapTypedCollectionAsync_ToNewObject_MappedAsync()
8195 new MapFrom ( ) { Property = 1 } ,
8296 new MapFrom ( ) { Property = 2 } ,
8397 } ,
84- new List < MapTo > ( ) )
98+ new List < MapTo > ( ) ,
99+ this . cancellationTokenSource . Token )
85100 . ConfigureAwait ( false ) ;
86101
102+ Assert . Equal ( this . cancellationTokenSource . Token , mapper . CancellationToken ) ;
87103 Assert . IsType < List < MapTo > > ( to ) ;
88104 Assert . Equal ( 2 , to . Count ) ;
89105 Assert . Equal ( 1 , to [ 0 ] . Property ) ;
@@ -95,7 +111,9 @@ public async Task MapCollectionAsync_Empty_MappedAsync()
95111 {
96112 var mapper = new AsyncMapper ( ) ;
97113
98- var to = await mapper . MapCollectionAsync ( Array . Empty < MapFrom > ( ) ) . ConfigureAwait ( false ) ;
114+ var to = await mapper
115+ . MapCollectionAsync ( Array . Empty < MapFrom > ( ) , this . cancellationTokenSource . Token )
116+ . ConfigureAwait ( false ) ;
99117
100118 Assert . IsType < Collection < MapTo > > ( to ) ;
101119 Assert . Empty ( to ) ;
@@ -112,9 +130,11 @@ public async Task MapCollectionAsync_ToNewObject_MappedAsync()
112130 {
113131 new MapFrom ( ) { Property = 1 } ,
114132 new MapFrom ( ) { Property = 2 } ,
115- } )
133+ } ,
134+ this . cancellationTokenSource . Token )
116135 . ConfigureAwait ( false ) ;
117136
137+ Assert . Equal ( this . cancellationTokenSource . Token , mapper . CancellationToken ) ;
118138 Assert . IsType < Collection < MapTo > > ( to ) ;
119139 Assert . Equal ( 2 , to . Count ) ;
120140 Assert . Equal ( 1 , to [ 0 ] . Property ) ;
@@ -126,7 +146,9 @@ public async Task MapListAsync_Empty_MappedAsync()
126146 {
127147 var mapper = new AsyncMapper ( ) ;
128148
129- var to = await mapper . MapListAsync ( Array . Empty < MapFrom > ( ) ) . ConfigureAwait ( false ) ;
149+ var to = await mapper
150+ . MapListAsync ( Array . Empty < MapFrom > ( ) , this . cancellationTokenSource . Token )
151+ . ConfigureAwait ( false ) ;
130152
131153 Assert . IsType < List < MapTo > > ( to ) ;
132154 Assert . Empty ( to ) ;
@@ -143,9 +165,11 @@ public async Task MapListAsync_ToNewObject_MappedAsync()
143165 {
144166 new MapFrom ( ) { Property = 1 } ,
145167 new MapFrom ( ) { Property = 2 } ,
146- } )
168+ } ,
169+ this . cancellationTokenSource . Token )
147170 . ConfigureAwait ( false ) ;
148171
172+ Assert . Equal ( this . cancellationTokenSource . Token , mapper . CancellationToken ) ;
149173 Assert . IsType < List < MapTo > > ( to ) ;
150174 Assert . Equal ( 2 , to . Count ) ;
151175 Assert . Equal ( 1 , to [ 0 ] . Property ) ;
@@ -158,7 +182,7 @@ public async Task MapObservableCollectionAsync_Empty_MappedAsync()
158182 var mapper = new AsyncMapper ( ) ;
159183
160184 var to = await mapper
161- . MapObservableCollectionAsync ( Array . Empty < MapFrom > ( ) )
185+ . MapObservableCollectionAsync ( Array . Empty < MapFrom > ( ) , this . cancellationTokenSource . Token )
162186 . ConfigureAwait ( false ) ;
163187
164188 Assert . IsType < ObservableCollection < MapTo > > ( to ) ;
@@ -176,17 +200,19 @@ public async Task MapObservableCollectionAsync_ToNewObject_MappedAsync()
176200 {
177201 new MapFrom ( ) { Property = 1 } ,
178202 new MapFrom ( ) { Property = 2 } ,
179- } )
203+ } ,
204+ this . cancellationTokenSource . Token )
180205 . ConfigureAwait ( false ) ;
181206
207+ Assert . Equal ( this . cancellationTokenSource . Token , mapper . CancellationToken ) ;
182208 Assert . IsType < ObservableCollection < MapTo > > ( to ) ;
183209 Assert . Equal ( 2 , to . Count ) ;
184210 Assert . Equal ( 1 , to [ 0 ] . Property ) ;
185211 Assert . Equal ( 2 , to [ 1 ] . Property ) ;
186212 }
187213
188214 [ Fact ]
189- public async Task MapAsyncEnumerable_ToNewObject_MappedAsync ( )
215+ public async Task MapEnumerableAsync_ToNewObject_MappedAsync ( )
190216 {
191217 var mapper = new AsyncMapper ( ) ;
192218 var source = new TestAsyncEnumerable < MapFrom > (
@@ -196,13 +222,16 @@ public async Task MapAsyncEnumerable_ToNewObject_MappedAsync()
196222 new MapFrom ( ) { Property = 2 } ,
197223 } ) ;
198224
199- var to = mapper . MapEnumerableAsync ( source ) ;
225+ var to = mapper . MapEnumerableAsync ( source , this . cancellationTokenSource . Token ) ;
200226
201227 var list = await to . ToListAsync ( ) . ConfigureAwait ( false ) ;
228+ Assert . Equal ( this . cancellationTokenSource . Token , mapper . CancellationToken ) ;
202229 Assert . IsType < List < MapTo > > ( list ) ;
203230 Assert . Equal ( 2 , list . Count ) ;
204231 Assert . Equal ( 1 , list [ 0 ] . Property ) ;
205232 Assert . Equal ( 2 , list [ 1 ] . Property ) ;
206233 }
234+
235+ protected override void DisposeManaged ( ) => this . cancellationTokenSource . Dispose ( ) ;
207236 }
208237}
0 commit comments