-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListenersListTest.kt
111 lines (90 loc) · 2.26 KB
/
ListenersListTest.kt
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
package com.gazman.signals
import junit.framework.TestCase.*
import org.junit.Before
import org.junit.Test
class ListenersListTest {
private lateinit var list: ListenersList<String>
@Before
fun init() {
list = ListenersList()
list.add("a")
list.add("b")
list.add("c")
}
private fun listToString(): String {
var items = ""
for (item in list) {
// null is valid return value and should be taken into account
if (item != null) {
items += item
}
}
return items
}
@Test
fun add() {
assertEquals("abc", listToString())
}
@Test
fun remove() {
list.remove("b")
assertEquals("ac", listToString())
}
@Test
fun clear() {
list.clear()
assertEquals("", listToString())
}
@Test
fun isNotEmpty() {
assertTrue(list.isNotEmpty())
list.clear()
assertFalse(list.isNotEmpty())
}
@Test
fun removeWhileIterating() {
testIteration("a", "ac") { list.remove("b") }
testIteration("a", "ab") { list.remove("c") }
testIteration("b", "ab") { list.remove("c") }
}
@Test
fun removeRangeWhileIterating() {
testIteration("a", "ac") {
list.remove("a")
list.remove("b")
}
testIteration("b", "ab") {
list.remove("b")
list.remove("c")
}
}
@Test
fun addWhileIterating() {
testIteration("a", "abcd") { list.add("d") }
testIteration("b", "abcd") { list.add("d") }
testIteration("c", "abcd") { list.add("d") }
}
@Test
fun clearWhileIterating() {
testIteration("a", "a") { list.clear() }
}
private fun testIteration(key: String, expected: String, callback: Runnable) {
init()
var items = ""
for (item in list) {
if (item != null) {
items += item
}
if (item == key) {
callback.run()
}
}
assertEquals("key = $key", expected, items)
}
@Test
fun removeAndAddSameItem() {
list.remove("b")
list.add("b")
assertEquals("acb", listToString())
}
}