OLD | NEW |
1 library ElementListTest; | 1 library ElementListTest; |
2 import '../../pkg/unittest/lib/unittest.dart'; | 2 import '../../pkg/unittest/lib/unittest.dart'; |
3 import '../../pkg/unittest/lib/html_config.dart'; | 3 import '../../pkg/unittest/lib/html_config.dart'; |
4 import 'dart:html'; | 4 import 'dart:html'; |
5 | 5 |
6 // Test that List<Element> implements List<T> | 6 // Test that List<Element> implements List<T> |
7 main() { | 7 main() { |
8 Element insertTestDiv() { | 8 Element insertTestDiv() { |
9 Element element = new Element.tag('div'); | 9 Element element = new Element.tag('div'); |
10 element.innerHTML = r""" | 10 element.innerHTML = r""" |
(...skipping 20 matching lines...) Expand all Loading... |
31 document.body.nodes.add(element); | 31 document.body.nodes.add(element); |
32 return element; | 32 return element; |
33 } | 33 } |
34 | 34 |
35 useHtmlConfiguration(); | 35 useHtmlConfiguration(); |
36 | 36 |
37 test('IsList', () { | 37 test('IsList', () { |
38 Element root = insertTestDiv(); | 38 Element root = insertTestDiv(); |
39 | 39 |
40 List<Element> eachChecked = | 40 List<Element> eachChecked = |
41 document.query('#allChecked').elements; | 41 document.query('#allChecked').children; |
42 | 42 |
43 expect(eachChecked, isList); | 43 expect(eachChecked, isList); |
44 | 44 |
45 root.remove(); | 45 root.remove(); |
46 }); | 46 }); |
47 test('Every', () { | 47 test('Every', () { |
48 Element root = insertTestDiv(); | 48 Element root = insertTestDiv(); |
49 | 49 |
50 List<Element> eachChecked = | 50 List<Element> eachChecked = |
51 document.query('#allChecked').elements; | 51 document.query('#allChecked').children; |
52 | 52 |
53 List<Element> someChecked = | 53 List<Element> someChecked = |
54 document.query('#someChecked').elements; | 54 document.query('#someChecked').children; |
55 | 55 |
56 List<Element> noneChecked = | 56 List<Element> noneChecked = |
57 document.query('#noneChecked').elements; | 57 document.query('#noneChecked').children; |
58 | 58 |
59 expect(eachChecked.length, 4); | 59 expect(eachChecked.length, 4); |
60 expect(someChecked.length, 4); | 60 expect(someChecked.length, 4); |
61 expect(noneChecked.length, 4); | 61 expect(noneChecked.length, 4); |
62 | 62 |
63 expect(eachChecked.every((x) => x.checked), isTrue); | 63 expect(eachChecked.every((x) => x.checked), isTrue); |
64 expect(eachChecked.every((x) => !x.checked), isFalse); | 64 expect(eachChecked.every((x) => !x.checked), isFalse); |
65 expect(someChecked.every((x) => x.checked), isFalse); | 65 expect(someChecked.every((x) => x.checked), isFalse); |
66 expect(someChecked.every((x) => !x.checked), isFalse); | 66 expect(someChecked.every((x) => !x.checked), isFalse); |
67 expect(noneChecked.every((x) => x.checked), isFalse); | 67 expect(noneChecked.every((x) => x.checked), isFalse); |
68 expect(noneChecked.every((x) => !x.checked), isTrue); | 68 expect(noneChecked.every((x) => !x.checked), isTrue); |
69 | 69 |
70 root.remove(); | 70 root.remove(); |
71 }); | 71 }); |
72 test('Some', () { | 72 test('Some', () { |
73 Element root = insertTestDiv(); | 73 Element root = insertTestDiv(); |
74 | 74 |
75 List<Element> eachChecked = | 75 List<Element> eachChecked = |
76 document.query('#allChecked').elements; | 76 document.query('#allChecked').children; |
77 | 77 |
78 List<Element> someChecked = | 78 List<Element> someChecked = |
79 document.query('#someChecked').elements; | 79 document.query('#someChecked').children; |
80 | 80 |
81 List<Element> noneChecked = | 81 List<Element> noneChecked = |
82 document.query('#noneChecked').elements; | 82 document.query('#noneChecked').children; |
83 | 83 |
84 expect(eachChecked.length, 4); | 84 expect(eachChecked.length, 4); |
85 expect(someChecked.length, 4); | 85 expect(someChecked.length, 4); |
86 expect(noneChecked.length, 4); | 86 expect(noneChecked.length, 4); |
87 | 87 |
88 expect(eachChecked.some((x) => x.checked), isTrue); | 88 expect(eachChecked.some((x) => x.checked), isTrue); |
89 expect(eachChecked.some((x) => !x.checked), isFalse); | 89 expect(eachChecked.some((x) => !x.checked), isFalse); |
90 expect(someChecked.some((x) => x.checked), isTrue); | 90 expect(someChecked.some((x) => x.checked), isTrue); |
91 expect(someChecked.some((x) => !x.checked), isTrue); | 91 expect(someChecked.some((x) => !x.checked), isTrue); |
92 expect(noneChecked.some((x) => x.checked), isFalse); | 92 expect(noneChecked.some((x) => x.checked), isFalse); |
93 expect(noneChecked.some((x) => !x.checked), isTrue); | 93 expect(noneChecked.some((x) => !x.checked), isTrue); |
94 | 94 |
95 root.remove(); | 95 root.remove(); |
96 }); | 96 }); |
97 test('Filter', () { | 97 test('Filter', () { |
98 Element root = insertTestDiv(); | 98 Element root = insertTestDiv(); |
99 | 99 |
100 List<Element> eachChecked = | 100 List<Element> eachChecked = |
101 document.query('#allChecked').elements; | 101 document.query('#allChecked').children; |
102 | 102 |
103 List<Element> someChecked = | 103 List<Element> someChecked = |
104 document.query('#someChecked').elements; | 104 document.query('#someChecked').children; |
105 | 105 |
106 List<Element> noneChecked = | 106 List<Element> noneChecked = |
107 document.query('#noneChecked').elements; | 107 document.query('#noneChecked').children; |
108 | 108 |
109 expect(eachChecked.length, 4); | 109 expect(eachChecked.length, 4); |
110 expect(someChecked.length, 4); | 110 expect(someChecked.length, 4); |
111 expect(noneChecked.length, 4); | 111 expect(noneChecked.length, 4); |
112 | 112 |
113 expect(eachChecked.filter((x) => x.checked).length, 4); | 113 expect(eachChecked.filter((x) => x.checked).length, 4); |
114 expect(eachChecked.filter((x) => !x.checked).length, 0); | 114 expect(eachChecked.filter((x) => !x.checked).length, 0); |
115 expect(someChecked.filter((x) => x.checked).length, 2); | 115 expect(someChecked.filter((x) => x.checked).length, 2); |
116 expect(someChecked.filter((x) => !x.checked).length, 2); | 116 expect(someChecked.filter((x) => !x.checked).length, 2); |
117 expect(noneChecked.filter((x) => x.checked).length, 0); | 117 expect(noneChecked.filter((x) => x.checked).length, 0); |
118 expect(noneChecked.filter((x) => !x.checked).length, 4); | 118 expect(noneChecked.filter((x) => !x.checked).length, 4); |
119 | 119 |
120 root.remove(); | 120 root.remove(); |
121 }); | 121 }); |
122 test('IsEmpty', () { | 122 test('IsEmpty', () { |
123 Element root = insertTestDiv(); | 123 Element root = insertTestDiv(); |
124 | 124 |
125 List<Element> someChecked = | 125 List<Element> someChecked = |
126 document.query('#someChecked').elements; | 126 document.query('#someChecked').children; |
127 | 127 |
128 List<Element> emptyDiv = | 128 List<Element> emptyDiv = |
129 document.query('#emptyDiv').elements; | 129 document.query('#emptyDiv').children; |
130 | 130 |
131 expect(someChecked.length, 4); | 131 expect(someChecked.length, 4); |
132 expect(emptyDiv.length, 0); | 132 expect(emptyDiv.length, 0); |
133 | 133 |
134 expect(someChecked.isEmpty, isFalse); | 134 expect(someChecked.isEmpty, isFalse); |
135 expect(emptyDiv.isEmpty, isTrue); | 135 expect(emptyDiv.isEmpty, isTrue); |
136 | 136 |
137 root.remove(); | 137 root.remove(); |
138 }); | 138 }); |
139 | 139 |
140 int countWithForEach(collection, predicate) { | 140 int countWithForEach(collection, predicate) { |
141 int count = 0; | 141 int count = 0; |
142 collection.forEach((element) { | 142 collection.forEach((element) { |
143 if (predicate(element)) count++; | 143 if (predicate(element)) count++; |
144 }); | 144 }); |
145 return count; | 145 return count; |
146 } | 146 } |
147 | 147 |
148 test('ForEach', () { | 148 test('ForEach', () { |
149 Element root = insertTestDiv(); | 149 Element root = insertTestDiv(); |
150 | 150 |
151 List<Element> eachChecked = | 151 List<Element> eachChecked = |
152 document.query('#allChecked').elements; | 152 document.query('#allChecked').children; |
153 | 153 |
154 List<Element> someChecked = | 154 List<Element> someChecked = |
155 document.query('#someChecked').elements; | 155 document.query('#someChecked').children; |
156 | 156 |
157 List<Element> noneChecked = | 157 List<Element> noneChecked = |
158 document.query('#noneChecked').elements; | 158 document.query('#noneChecked').children; |
159 | 159 |
160 expect(eachChecked.length, 4); | 160 expect(eachChecked.length, 4); |
161 expect(someChecked.length, 4); | 161 expect(someChecked.length, 4); |
162 expect(noneChecked.length, 4); | 162 expect(noneChecked.length, 4); |
163 | 163 |
164 expect(countWithForEach(eachChecked, (x) => x.checked), 4); | 164 expect(countWithForEach(eachChecked, (x) => x.checked), 4); |
165 expect(countWithForEach(eachChecked, (x) => !x.checked), 0); | 165 expect(countWithForEach(eachChecked, (x) => !x.checked), 0); |
166 expect(countWithForEach(someChecked, (x) => x.checked), 2); | 166 expect(countWithForEach(someChecked, (x) => x.checked), 2); |
167 expect(countWithForEach(someChecked, (x) => !x.checked), 2); | 167 expect(countWithForEach(someChecked, (x) => !x.checked), 2); |
168 expect(countWithForEach(noneChecked, (x) => x.checked), 0); | 168 expect(countWithForEach(noneChecked, (x) => x.checked), 0); |
169 expect(countWithForEach(noneChecked, (x) => !x.checked), 4); | 169 expect(countWithForEach(noneChecked, (x) => !x.checked), 4); |
170 | 170 |
171 root.remove(); | 171 root.remove(); |
172 }); | 172 }); |
173 | 173 |
174 int countWithForLoop(collection, predicate) { | 174 int countWithForLoop(collection, predicate) { |
175 int count = 0; | 175 int count = 0; |
176 for (var element in collection) { | 176 for (var element in collection) { |
177 if (predicate(element)) count++; | 177 if (predicate(element)) count++; |
178 } | 178 } |
179 return count; | 179 return count; |
180 } | 180 } |
181 | 181 |
182 test('ForLoop', () { // Uses iterator. | 182 test('ForLoop', () { // Uses iterator. |
183 Element root = insertTestDiv(); | 183 Element root = insertTestDiv(); |
184 | 184 |
185 List<Element> eachChecked = | 185 List<Element> eachChecked = |
186 document.query('#allChecked').elements; | 186 document.query('#allChecked').children; |
187 | 187 |
188 List<Element> someChecked = | 188 List<Element> someChecked = |
189 document.query('#someChecked').elements; | 189 document.query('#someChecked').children; |
190 | 190 |
191 List<Element> noneChecked = | 191 List<Element> noneChecked = |
192 document.query('#noneChecked').elements; | 192 document.query('#noneChecked').children; |
193 | 193 |
194 expect(eachChecked.length, 4); | 194 expect(eachChecked.length, 4); |
195 expect(someChecked.length, 4); | 195 expect(someChecked.length, 4); |
196 expect(noneChecked.length, 4); | 196 expect(noneChecked.length, 4); |
197 | 197 |
198 expect(countWithForLoop(eachChecked, (x) => x.checked), 4); | 198 expect(countWithForLoop(eachChecked, (x) => x.checked), 4); |
199 expect(countWithForLoop(eachChecked, (x) => !x.checked), 0); | 199 expect(countWithForLoop(eachChecked, (x) => !x.checked), 0); |
200 expect(countWithForLoop(someChecked, (x) => x.checked), 2); | 200 expect(countWithForLoop(someChecked, (x) => x.checked), 2); |
201 expect(countWithForLoop(someChecked, (x) => !x.checked), 2); | 201 expect(countWithForLoop(someChecked, (x) => !x.checked), 2); |
202 expect(countWithForLoop(noneChecked, (x) => x.checked), 0); | 202 expect(countWithForLoop(noneChecked, (x) => x.checked), 0); |
203 expect(countWithForLoop(noneChecked, (x) => !x.checked), 4); | 203 expect(countWithForLoop(noneChecked, (x) => !x.checked), 4); |
204 | 204 |
205 root.remove(); | 205 root.remove(); |
206 }); | 206 }); |
207 test('Last', () { | 207 test('Last', () { |
208 Element root = insertTestDiv(); | 208 Element root = insertTestDiv(); |
209 | 209 |
210 List<Element> someChecked = | 210 List<Element> someChecked = |
211 document.query('#someChecked').elements; | 211 document.query('#someChecked').children; |
212 | 212 |
213 expect(someChecked.length, 4); | 213 expect(someChecked.length, 4); |
214 | 214 |
215 expect(someChecked.last, equals(someChecked[3])); | 215 expect(someChecked.last, equals(someChecked[3])); |
216 | 216 |
217 root.remove(); | 217 root.remove(); |
218 }); | 218 }); |
219 test('IndexOf', () { | 219 test('IndexOf', () { |
220 Element root = insertTestDiv(); | 220 Element root = insertTestDiv(); |
221 | 221 |
222 List<Element> someChecked = | 222 List<Element> someChecked = |
223 document.query('#someChecked').elements; | 223 document.query('#someChecked').children; |
224 | 224 |
225 List<Element> noneChecked = | 225 List<Element> noneChecked = |
226 document.query('#noneChecked').elements; | 226 document.query('#noneChecked').children; |
227 | 227 |
228 expect(someChecked.length, 4); | 228 expect(someChecked.length, 4); |
229 expect(noneChecked.length, 4); | 229 expect(noneChecked.length, 4); |
230 | 230 |
231 expect(someChecked.indexOf(someChecked[0], 0), 0); | 231 expect(someChecked.indexOf(someChecked[0], 0), 0); |
232 expect(someChecked.indexOf(someChecked[1], 0), 1); | 232 expect(someChecked.indexOf(someChecked[1], 0), 1); |
233 expect(someChecked.indexOf(someChecked[2], 0), 2); | 233 expect(someChecked.indexOf(someChecked[2], 0), 2); |
234 expect(someChecked.indexOf(someChecked[3], 0), 3); | 234 expect(someChecked.indexOf(someChecked[3], 0), 3); |
235 | 235 |
236 expect(someChecked.indexOf(someChecked[0], 1), -1); | 236 expect(someChecked.indexOf(someChecked[0], 1), -1); |
237 expect(someChecked.indexOf(someChecked[1], 2), -1); | 237 expect(someChecked.indexOf(someChecked[1], 2), -1); |
238 expect(someChecked.indexOf(someChecked[2], 3), -1); | 238 expect(someChecked.indexOf(someChecked[2], 3), -1); |
239 expect(someChecked.indexOf(someChecked[3], 4), -1); | 239 expect(someChecked.indexOf(someChecked[3], 4), -1); |
240 | 240 |
241 expect(someChecked.indexOf(noneChecked[0], 0), -1); | 241 expect(someChecked.indexOf(noneChecked[0], 0), -1); |
242 expect(noneChecked.indexOf(someChecked[0], 0), -1); | 242 expect(noneChecked.indexOf(someChecked[0], 0), -1); |
243 expect(someChecked.indexOf(noneChecked[1], 0), -1); | 243 expect(someChecked.indexOf(noneChecked[1], 0), -1); |
244 expect(noneChecked.indexOf(someChecked[1], 0), -1); | 244 expect(noneChecked.indexOf(someChecked[1], 0), -1); |
245 | 245 |
246 root.remove(); | 246 root.remove(); |
247 }); | 247 }); |
248 test('LastIndexOf', () { | 248 test('LastIndexOf', () { |
249 Element root = insertTestDiv(); | 249 Element root = insertTestDiv(); |
250 | 250 |
251 List<Element> someChecked = | 251 List<Element> someChecked = |
252 document.query('#someChecked').elements; | 252 document.query('#someChecked').children; |
253 | 253 |
254 List<Element> noneChecked = | 254 List<Element> noneChecked = |
255 document.query('#noneChecked').elements; | 255 document.query('#noneChecked').children; |
256 | 256 |
257 expect(someChecked.length, 4); | 257 expect(someChecked.length, 4); |
258 expect(noneChecked.length, 4); | 258 expect(noneChecked.length, 4); |
259 | 259 |
260 expect(someChecked.lastIndexOf(someChecked[0], 3), 0); | 260 expect(someChecked.lastIndexOf(someChecked[0], 3), 0); |
261 expect(someChecked.lastIndexOf(someChecked[1], 3), 1); | 261 expect(someChecked.lastIndexOf(someChecked[1], 3), 1); |
262 expect(someChecked.lastIndexOf(someChecked[2], 3), 2); | 262 expect(someChecked.lastIndexOf(someChecked[2], 3), 2); |
263 expect(someChecked.lastIndexOf(someChecked[3], 3), 3); | 263 expect(someChecked.lastIndexOf(someChecked[3], 3), 3); |
264 | 264 |
265 expect(someChecked.lastIndexOf(someChecked[0], -1), -1); | 265 expect(someChecked.lastIndexOf(someChecked[0], -1), -1); |
266 expect(someChecked.lastIndexOf(someChecked[1], 0), -1); | 266 expect(someChecked.lastIndexOf(someChecked[1], 0), -1); |
267 expect(someChecked.lastIndexOf(someChecked[2], 1), -1); | 267 expect(someChecked.lastIndexOf(someChecked[2], 1), -1); |
268 expect(someChecked.lastIndexOf(someChecked[3], 2), -1); | 268 expect(someChecked.lastIndexOf(someChecked[3], 2), -1); |
269 | 269 |
270 expect(someChecked.lastIndexOf(noneChecked[0], 3), -1); | 270 expect(someChecked.lastIndexOf(noneChecked[0], 3), -1); |
271 expect(noneChecked.lastIndexOf(someChecked[0], 3), -1); | 271 expect(noneChecked.lastIndexOf(someChecked[0], 3), -1); |
272 expect(someChecked.lastIndexOf(noneChecked[1], 3), -1); | 272 expect(someChecked.lastIndexOf(noneChecked[1], 3), -1); |
273 expect(noneChecked.lastIndexOf(someChecked[1], 3), -1); | 273 expect(noneChecked.lastIndexOf(someChecked[1], 3), -1); |
274 | 274 |
275 root.remove(); | 275 root.remove(); |
276 }); | 276 }); |
277 } | 277 } |
OLD | NEW |