-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathIterableTest.kt
More file actions
532 lines (465 loc) · 18.6 KB
/
Copy pathIterableTest.kt
File metadata and controls
532 lines (465 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
package test.assertk.assertions
import assertk.all
import assertk.assertThat
import assertk.assertions.*
import assertk.assertions.support.fail
import test.assertk.opentestPackageName
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFails
class IterableTest {
//region contains
@Test fun contains_element_present_passes() {
assertThat(listOf(1, 2) as Iterable<Int>).contains(2)
}
@Test fun contains_element_missing_fails() {
val error = assertFails {
assertThat(emptyList<Any?>() as Iterable<Any?>).contains(1)
}
assertEquals("expected to contain:<1> but was:<[]>", error.message)
}
//endregion
//region doesNotContain
@Test fun doesNotContain_element_missing_passes() {
assertThat(emptyList<Any?>() as Iterable<Any?>).doesNotContain(1)
}
@Test fun doesNotContain_element_present_fails() {
val error = assertFails {
assertThat(listOf(1, 2) as Iterable<Int>).doesNotContain(2)
}
assertEquals("expected to not contain:<2> but was:<[1, 2]>", error.message)
}
//endregion
//region containsNone
@Test fun containsNone_missing_elements_passes() {
assertThat(emptyList<Any?>() as Iterable<Any?>).containsNone(1)
}
@Test fun containsNone_present_element_fails() {
val error = assertFails {
assertThat(listOf(1, 2) as Iterable<Int>).containsNone(2, 3)
}
assertEquals(
"""expected to contain none of:<[2, 3]> but was:<[1, 2]>
| elements not expected:<[2]>
""".trimMargin(), error.message
)
}
//region
//region containsAll
@Test fun containsAll_all_elements_passes() {
assertThat(listOf(1, 2) as Iterable<Int>).containsAll(2, 1)
}
@Test fun containsAll_some_elements_fails() {
val error = assertFails {
assertThat(listOf(1) as Iterable<Int>).containsAll(1, 2)
}
assertEquals(
"""expected to contain all:<[1, 2]> but was:<[1]>
| elements not found:<[2]>
""".trimMargin(), error.message
)
}
//endregion
//region containsOnly
@Test fun containsOnly_only_elements_passes() {
assertThat(listOf(1, 2) as Iterable<Int>).containsOnly(2, 1)
}
@Test fun containsOnly_duplicate_elements_passes() {
assertThat(listOf(1, 2, 2) as Iterable<Int>).containsOnly(2, 1)
}
@Test fun containsOnly_duplicate_elements_passes2() {
assertThat(listOf(1, 2) as Iterable<Int>).containsOnly(2, 2, 1)
}
@Test fun containsOnly_more_elements_fails() {
val error = assertFails {
assertThat(listOf(1, 2, 3) as Iterable<Int>).containsOnly(2, 1)
}
assertEquals(
"""expected to contain only:<[2, 1]> but was:<[1, 2, 3]>
| extra elements found:<[3]>
""".trimMargin(), error.message
)
}
@Test fun containsOnly_less_elements_fails() {
val error = assertFails {
assertThat(listOf(1, 2, 3) as Iterable<Int>).containsOnly(2, 1, 3, 4)
}
assertEquals(
"""expected to contain only:<[2, 1, 3, 4]> but was:<[1, 2, 3]>
| elements not found:<[4]>
""".trimMargin(),
error.message
)
}
@Test fun containsOnly_different_elements_fails() {
val error = assertFails {
assertThat(listOf(1) as Iterable<Int>).containsOnly(2)
}
assertEquals(
"""expected to contain only:<[2]> but was:<[1]>
| elements not found:<[2]>
| extra elements found:<[1]>
""".trimMargin(),
error.message
)
}
//endregion
//region containsExactlyInAnyOrder
@Test fun containsExactlyInAnyOrder_only_elements_passes() {
assertThat(listOf(1, 2) as Iterable<Int>).containsExactlyInAnyOrder(2, 1)
}
@Test fun containsExactlyInAnyOrder_only_elements_passes2() {
assertThat(listOf(1, 2, 1) as Iterable<Int>).containsExactlyInAnyOrder(2, 1, 1)
}
@Test fun containsExactlyInAnyOrder_duplicate_elements_fails() {
val error = assertFails {
assertThat(listOf(1, 2, 2) as Iterable<Int>).containsExactlyInAnyOrder(2, 1)
}
assertEquals(
"""expected to contain exactly in any order:<[2, 1]> but was:<[1, 2, 2]>
| extra elements found:<[2]>
""".trimMargin(), error.message
)
}
@Test fun containsExactlyInAnyOrder_duplicate_elements_fails2() {
val error = assertFails {
assertThat(listOf(1, 2) as Iterable<Int>).containsExactlyInAnyOrder(2, 2, 1)
}
assertEquals(
"""expected to contain exactly in any order:<[2, 2, 1]> but was:<[1, 2]>
| elements not found:<[2]>
""".trimMargin(), error.message
)
}
@Test fun containsExactlyInAnyOrder_more_elements_fails() {
val error = assertFails {
assertThat(listOf(1, 2, 3) as Iterable<Int>).containsExactlyInAnyOrder(2, 1)
}
assertEquals(
"""expected to contain exactly in any order:<[2, 1]> but was:<[1, 2, 3]>
| extra elements found:<[3]>
""".trimMargin(), error.message
)
}
@Test fun containsExactlyInAnyOrder_less_elements_fails() {
val error = assertFails {
assertThat(listOf(1, 2, 3) as Iterable<Int>).containsExactlyInAnyOrder(2, 1, 3, 4)
}
assertEquals(
"""expected to contain exactly in any order:<[2, 1, 3, 4]> but was:<[1, 2, 3]>
| elements not found:<[4]>
""".trimMargin(),
error.message
)
}
@Test fun containsExactlyInAnyOrder_different_elements_fails() {
val error = assertFails {
assertThat(listOf(1) as Iterable<Int>).containsExactlyInAnyOrder(2)
}
assertEquals(
"""expected to contain exactly in any order:<[2]> but was:<[1]>
| elements not found:<[2]>
| extra elements found:<[1]>
""".trimMargin(),
error.message
)
}
//endregion
//region each
@Test fun each_empty_list_passes() {
assertThat(emptyList<Int>() as Iterable<Int>).each { it.isEqualTo(1) }
}
@Test fun each_content_passes() {
assertThat(listOf(1, 2) as Iterable<Int>).each { it.isGreaterThan(0) }
}
@Test fun each_non_matching_content_fails() {
val error = assertFails {
assertThat(listOf(1, 2, 3) as Iterable<Int>).each { it.isLessThan(2) }
}
assertEquals(
"""The following assertions failed (2 failures)
|${"\t"}${opentestPackageName}AssertionFailedError: expected [[1]] to be less than:<2> but was:<2> ([1, 2, 3])
|${"\t"}${opentestPackageName}AssertionFailedError: expected [[2]] to be less than:<2> but was:<3> ([1, 2, 3])
""".trimMargin().lines(), error.message!!.lines()
)
}
//endregion
//region none
@Test fun none_empty_list_passes() {
assertThat(emptyList<Int>() as Iterable<Int>).none { it.isEqualTo(1) }
}
@Test fun none_matching_content_fails() {
val error = assertFails {
assertThat(listOf(1, 2) as Iterable<Int>).none { it.isGreaterThan(0) }
}
assertEquals(
"expected none to pass", error.message
)
}
@Test fun each_non_matching_content_passes() {
assertThat(listOf(1, 2, 3) as Iterable<Int>).none { it.isLessThan(2) }
}
//endregion
//region atLeast
@Test fun atLeast_too_many_failures_fails() {
val error = assertFails {
assertThat(listOf(1, 2, 3) as Iterable<Int>).atLeast(2) { it.isGreaterThan(2) }
}
assertEquals(
"""expected to pass at least 2 times (2 failures)
|${"\t"}${opentestPackageName}AssertionFailedError: expected [[0]] to be greater than:<2> but was:<1> ([1, 2, 3])
|${"\t"}${opentestPackageName}AssertionFailedError: expected [[1]] to be greater than:<2> but was:<2> ([1, 2, 3])
""".trimMargin().lines(), error.message!!.lines()
)
}
@Test fun atLeast_no_failures_passes() {
assertThat(listOf(1, 2, 3) as Iterable<Int>).atLeast(2) { it.isGreaterThan(0) }
}
@Test fun atLeast_less_than_times_failures_passes() {
assertThat(listOf(1, 2, 3) as Iterable<Int>).atLeast(2) { it.isGreaterThan(1) }
}
@Test fun atLeast_works_in_a_soft_assert_context() {
assertThat(listOf(1, 2, 3) as Iterable<Int>).all { atLeast(2) { it.isGreaterThan(1) } }
}
//endregion
//region atMost
@Test fun atMost_more_than_times_passed_fails() {
val error = assertFails {
assertThat(listOf(1, 2, 3) as Iterable<Int>).atMost(2) { it.isGreaterThan(0) }
}
assertEquals(
"""expected to pass at most 2 times""".trimMargin(), error.message
)
}
@Test fun atMost_exactly_times_passed_passes() {
assertThat(listOf(1, 2, 3) as Iterable<Int>).atMost(2) { it.isGreaterThan(1) }
}
@Test fun atMost_less_than_times_passed_passes() {
assertThat(listOf(1, 2) as Iterable<Int>).atMost(2) { it.isGreaterThan(1) }
}
//endregion
//region exactly
@Test fun exactly_too_few_passes_fails() {
val error = assertFails {
assertThat(listOf(1, 2, 3) as Iterable<Int>).exactly(2) { it.isGreaterThan(2) }
}
assertEquals(
"""expected to pass exactly 2 times (2 failures)
|${"\t"}${opentestPackageName}AssertionFailedError: expected [[0]] to be greater than:<2> but was:<1> ([1, 2, 3])
|${"\t"}${opentestPackageName}AssertionFailedError: expected [[1]] to be greater than:<2> but was:<2> ([1, 2, 3])
""".trimMargin().lines(), error.message!!.lines()
)
}
@Test fun exactly_too_many_passes_fails() {
val error = assertFails {
assertThat(listOf(5, 4, 3) as Iterable<Int>).exactly(2) { it.isGreaterThan(2) }
}
assertEquals(
"""expected to pass exactly 2 times""".trimMargin(), error.message
)
}
@Test fun exactly_too_few_inside_all_fails() {
val error = assertFails {
assertThat(listOf(5, 4, 3) as Iterable<Int>).all {
exactly(2) { it.isGreaterThan(2) }
}
}
assertEquals(
"""expected to pass exactly 2 times""".trimMargin(), error.message
)
}
@Test fun exactly_times_passed_passes() {
assertThat(listOf(0, 1, 2) as Iterable<Int>).exactly(2) { it.isGreaterThan(0) }
}
//endregion
//region any
@Test fun any_passes_if_one_item_passes() {
assertThat(listOf(1, 2) as Iterable<Int>).any { it.isGreaterThan(1) }
}
@Test fun any_fails_if_all_fail() {
val error = assertFails {
assertThat(listOf(1, 2)).any { it.isGreaterThan(3) }
}
assertEquals(
"""expected any item to pass (2 failures)
|${"\t"}${opentestPackageName}AssertionFailedError: expected [[0]] to be greater than:<3> but was:<1> ([1, 2])
|${"\t"}${opentestPackageName}AssertionFailedError: expected [[1]] to be greater than:<3> but was:<2> ([1, 2])
""".trimMargin().lines(), error.message!!.lines()
)
}
@Test fun any_multiple_assertions_fail() {
assertFails {
assertThat(listOf("one")).any {
it.isEqualTo("two")
it.isEqualTo("two")
}
}
}
@Test fun any_multiple_items_fail() {
val error = assertFails {
assertThat(listOf(1, 2, 3)).any {
it.isEqualTo(4)
}
}
assertEquals(
"""expected any item to pass (3 failures)
|${"\t"}${opentestPackageName}AssertionFailedError: expected [[0]]:<[4]> but was:<[1]> ([1, 2, 3])
|${"\t"}${opentestPackageName}AssertionFailedError: expected [[1]]:<[4]> but was:<[2]> ([1, 2, 3])
|${"\t"}${opentestPackageName}AssertionFailedError: expected [[2]]:<[4]> but was:<[3]> ([1, 2, 3])
""".trimMargin().lines(), error.message!!.lines()
)
}
@Test fun any_with_exception_still_passes() {
var count = 0
assertThat(listOf("one", "two")).any {
if (count == 1) {
throw Exception()
}
count++
}
}
//endregion
//region isEmpty
@Test fun empty_iterable_passes_is_empty() {
val empty: List<Int> = emptyList()
assertThat(empty as Iterable<Int>).isEmpty()
}
@Test fun non_empty_iterable_fails_is_empty() {
val nonEmpty: List<Int> = listOf(1)
val error = assertFails {
assertThat(nonEmpty as Iterable<Int>).isEmpty()
}
assertEquals("expected to be empty but was:<[1]>", error.message)
}
//endregion
//region isNotEmpty
@Test fun non_empty_iterable_passes_is_not_empty() {
val nonEmpty: List<Int> = listOf(1)
assertThat(nonEmpty as Iterable<Int>).isNotEmpty()
}
@Test fun empty_iterable_fails_is_not_empty() {
val empty: List<Int> = emptyList()
val error = assertFails {
assertThat(empty as Iterable<Int>).isNotEmpty()
}
assertEquals("expected to not be empty", error.message)
}
//endregion
//region extracting
@Test fun single_extracting_function_passes() {
assertThat(listOf("one", "two") as Iterable<String>).extracting { it.length }.containsExactly(3, 3)
}
@Test fun single_extracting_function_fails() {
val error = assertFails {
assertThat(listOf("one", "two") as Iterable<String>).extracting { it.length }.containsExactly(2, 2)
}
assertEquals(
"""expected to contain exactly:<[2, 2]> but was:<[3, 3]>
| at index:0 expected:<2>
| at index:0 unexpected:<3>
| at index:1 expected:<2>
| at index:1 unexpected:<3> (["one", "two"])""".trimMargin(), error.message
)
}
@Test fun pair_extracting_function_passes() {
assertThat(listOf(Thing("one", 1, listOf('1')), Thing("two", 2, listOf('2'))) as Iterable<Thing>)
.extracting(Thing::one, Thing::two)
.containsExactly("one" to 1, "two" to 2)
}
@Test fun pair_extracting_function_fails() {
val error = assertFails {
assertThat(listOf(Thing("one", 1, listOf('1')), Thing("two", 2, listOf('2'))) as Iterable<Thing>)
.extracting(Thing::one, Thing::two)
.containsExactly("one" to 2, "two" to 1)
}
assertEquals(
"""expected to contain exactly:<[("one", 2), ("two", 1)]> but was:<[("one", 1), ("two", 2)]>
| at index:0 expected:<("one", 2)>
| at index:0 unexpected:<("one", 1)>
| at index:1 expected:<("two", 1)>
| at index:1 unexpected:<("two", 2)> ([Thing(one=one, two=1, three=[1]), Thing(one=two, two=2, three=[2])])""".trimMargin(),
error.message
)
}
@Test fun triple_extracting_function_passes() {
assertThat(listOf(Thing("one", 1, listOf('1')), Thing("two", 2, listOf('2'))) as Iterable<Thing>)
.extracting(Thing::one, Thing::two, Thing::three)
.containsExactly(Triple("one", 1, listOf('1')), Triple("two", 2, listOf('2')))
}
@Test fun triple_extracting_function_fails() {
val error = assertFails {
assertThat(listOf(Thing("one", 1, listOf('1')), Thing("two", 2, listOf('2'))) as Iterable<Thing>)
.extracting(Thing::one, Thing::two, Thing::three)
.containsExactly(Triple("one", 1, listOf('2')), Triple("two", 2, listOf('3')))
}
assertEquals(
"""expected to contain exactly:<[("one", 1, ['2']), ("two", 2, ['3'])]> but was:<[("one", 1, ['1']), ("two", 2, ['2'])]>
| at index:0 expected:<("one", 1, ['2'])>
| at index:0 unexpected:<("one", 1, ['1'])>
| at index:1 expected:<("two", 2, ['3'])>
| at index:1 unexpected:<("two", 2, ['2'])> ([Thing(one=one, two=1, three=[1]), Thing(one=two, two=2, three=[2])])""".trimMargin(),
error.message
)
}
//region extracting
//region flatExtracting
@Test fun flat_extracting_function_passes() {
val thing = Thing("one", 2, listOf('A', 'B'))
assertThat(listOf(thing) as Iterable<Thing>).flatExtracting { it.three }.containsExactly('A', 'B')
}
@Test fun flat_extracting_function_fails() {
val thing = Thing("one", 2, listOf('A', 'B'))
val error = assertFails {
assertThat(listOf(thing) as Iterable<Thing>).flatExtracting { it.three }.containsExactly('C', 'D')
}
assertEquals(
"""expected to contain exactly:<['C', 'D']> but was:<['A', 'B']>
| at index:0 expected:<'C'>
| at index:0 unexpected:<'A'>
| at index:1 expected:<'D'>
| at index:1 unexpected:<'B'> ([Thing(one=one, two=2, three=[A, B])])""".trimMargin(), error.message
)
}
//region flatExtracting
//region first
@Test fun first_element_present_match_passes() {
assertThat(listOf(1, 2)).first().isEqualTo(1)
}
@Test fun first_element_present_mismatch_fails() {
val error = assertFails {
assertThat(listOf(1, 2)).first().isEqualTo(2)
}
assertEquals("expected [first]:<[2]> but was:<[1]> ([1, 2])", error.message)
}
@Test fun first_element_missing_fails() {
val error = assertFails {
assertThat(emptyList<Any?>()).first().isEqualTo(1)
}
assertEquals("expected to not be empty", error.message)
}
//endregion
//region first
@Test fun single_single_element_match_passes() {
assertThat(listOf(1)).single().isEqualTo(1)
}
@Test fun single_single_element_mismatch_fails() {
val error = assertFails {
assertThat(listOf(1)).single().isEqualTo(2)
}
assertEquals("expected [single]:<[2]> but was:<[1]> ([1])", error.message)
}
@Test fun single_no_element_fails() {
val error = assertFails {
assertThat(emptyList<Any?>()).single().isEqualTo(1)
}
assertEquals("expected to have single element but was empty", error.message)
}
@Test fun single_multiple_fails() {
val error = assertFails {
assertThat(listOf(1, 2)).single().isEqualTo(1)
}
assertEquals("expected to have single element but has 2: <[1, 2]>", error.message)
}
//endregion
data class Thing(val one: String, val two: Int, val three: List<Char>)
}