OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library ElementTest; | |
6 import 'package:unittest/unittest.dart'; | |
7 import 'package:unittest/html_config.dart'; | |
8 import 'dart:collection'; | 5 import 'dart:collection'; |
9 import 'dart:html'; | 6 import 'dart:html'; |
10 | 7 |
| 8 import 'package:minitest/minitest.dart'; |
| 9 |
11 Element makeElement() => new Element.tag('div'); | 10 Element makeElement() => new Element.tag('div'); |
12 | 11 |
13 Element makeElementWithClasses() => | 12 Element makeElementWithClasses() => |
14 new Element.html('<div class="foo bar baz"></div>'); | 13 new Element.html('<div class="foo bar baz"></div>'); |
15 | 14 |
16 Set<String> makeClassSet() => makeElementWithClasses().classes; | 15 CssClassSet makeClassSet() => makeElementWithClasses().classes; |
17 | 16 |
18 Element makeListElement() => | 17 Element makeListElement() => |
19 new Element.html('<ul class="foo bar baz">' | 18 new Element.html('<ul class="foo bar baz">' |
20 '<li class="quux qux">' | 19 '<li class="quux qux">' |
21 '<li class="meta">' | 20 '<li class="meta">' |
22 '<li class="classy lassy">' | 21 '<li class="classy lassy">' |
23 '<li class="qux lassy">' | 22 '<li class="qux lassy">' |
24 '</ul>'); | 23 '</ul>'); |
25 | 24 |
26 Element listElement; | 25 Element listElement; |
(...skipping 13 matching lines...) Expand all Loading... |
40 | 39 |
41 /// Returns a canonical string for Set<String> and lists of Element's classes. | 40 /// Returns a canonical string for Set<String> and lists of Element's classes. |
42 String view(var e) { | 41 String view(var e) { |
43 if (e is Set) return '${e.toList()..sort()}'; | 42 if (e is Set) return '${e.toList()..sort()}'; |
44 if (e is Element) return view(e.classes); | 43 if (e is Element) return view(e.classes); |
45 if (e is Iterable) return '${e.map(view).toList()}'; | 44 if (e is Iterable) return '${e.map(view).toList()}'; |
46 throw new ArgumentError('Cannot make canonical view string for: $e}'); | 45 throw new ArgumentError('Cannot make canonical view string for: $e}'); |
47 } | 46 } |
48 | 47 |
49 main() { | 48 main() { |
50 useHtmlConfiguration(); | |
51 | |
52 Set<String> extractClasses(Element el) { | 49 Set<String> extractClasses(Element el) { |
53 final match = new RegExp('class="([^"]+)"').firstMatch(el.outerHtml); | 50 final match = new RegExp('class="([^"]+)"').firstMatch(el.outerHtml); |
54 return new LinkedHashSet.from(match[1].split(' ')); | 51 return new LinkedHashSet.from(match[1].split(' ')); |
55 } | 52 } |
56 | 53 |
57 | |
58 test('affects the "class" attribute', () { | 54 test('affects the "class" attribute', () { |
59 final el = makeElementWithClasses(); | 55 final el = makeElementWithClasses(); |
60 el.classes.add('qux'); | 56 el.classes.add('qux'); |
61 expect(extractClasses(el), orderedEquals(['foo', 'bar', 'baz', 'qux'])); | 57 expect(extractClasses(el), equals(['foo', 'bar', 'baz', 'qux'])); |
62 }); | 58 }); |
63 | 59 |
64 test('is affected by the "class" attribute', () { | 60 test('is affected by the "class" attribute', () { |
65 final el = makeElementWithClasses(); | 61 final el = makeElementWithClasses(); |
66 el.attributes['class'] = 'foo qux'; | 62 el.attributes['class'] = 'foo qux'; |
67 expect(el.classes, orderedEquals(['foo', 'qux'])); | 63 expect(el.classes, equals(['foo', 'qux'])); |
68 }); | 64 }); |
69 | 65 |
70 test('classes=', () { | 66 test('classes=', () { |
71 final el = makeElementWithClasses(); | 67 final el = makeElementWithClasses(); |
72 el.classes = ['foo', 'qux']; | 68 el.classes = ['foo', 'qux']; |
73 expect(el.classes, orderedEquals(['foo', 'qux'])); | 69 expect(el.classes, equals(['foo', 'qux'])); |
74 expect(extractClasses(el), orderedEquals(['foo', 'qux'])); | 70 expect(extractClasses(el), equals(['foo', 'qux'])); |
75 }); | 71 }); |
76 | 72 |
77 test('toString', () { | 73 test('toString', () { |
78 expect(makeClassSet().toString().split(' '), | 74 expect(makeClassSet().toString().split(' '), |
79 orderedEquals(['foo', 'bar', 'baz'])); | 75 equals(['foo', 'bar', 'baz'])); |
80 expect(makeElement().classes.toString(), ''); | 76 expect(makeElement().classes.toString(), ''); |
81 }); | 77 }); |
82 | 78 |
83 test('forEach', () { | 79 test('forEach', () { |
84 final classes = <String>[]; | 80 final classes = <String>[]; |
85 makeClassSet().forEach(classes.add); | 81 makeClassSet().forEach(classes.add); |
86 expect(classes, orderedEquals(['foo', 'bar', 'baz'])); | 82 expect(classes, equals(['foo', 'bar', 'baz'])); |
87 }); | 83 }); |
88 | 84 |
89 test('iterator', () { | 85 test('iterator', () { |
90 final classes = <String>[]; | 86 final classes = <String>[]; |
91 for (var el in makeClassSet()) { | 87 for (var el in makeClassSet()) { |
92 classes.add(el); | 88 classes.add(el); |
93 } | 89 } |
94 expect(classes, orderedEquals(['foo', 'bar', 'baz'])); | 90 expect(classes, equals(['foo', 'bar', 'baz'])); |
95 }); | 91 }); |
96 | 92 |
97 test('map', () { | 93 test('map', () { |
98 expect(makeClassSet().map((c) => c.toUpperCase()).toList(), | 94 expect(makeClassSet().map((c) => c.toUpperCase()).toList(), |
99 orderedEquals(['FOO', 'BAR', 'BAZ'])); | 95 equals(['FOO', 'BAR', 'BAZ'])); |
100 }); | 96 }); |
101 | 97 |
102 test('where', () { | 98 test('where', () { |
103 expect(makeClassSet().where((c) => c.contains('a')).toList(), | 99 expect(makeClassSet().where((c) => c.contains('a')).toList(), |
104 orderedEquals(['bar', 'baz'])); | 100 equals(['bar', 'baz'])); |
105 }); | 101 }); |
106 | 102 |
107 test('every', () { | 103 test('every', () { |
108 expect(makeClassSet().every((c) => c is String), isTrue); | 104 expect(makeClassSet().every((c) => c is String), isTrue); |
109 expect(makeClassSet().every((c) => c.contains('a')), isFalse); | 105 expect(makeClassSet().every((c) => c.contains('a')), isFalse); |
110 }); | 106 }); |
111 | 107 |
112 test('any', () { | 108 test('any', () { |
113 expect(makeClassSet().any((c) => c.contains('a')), isTrue); | 109 expect(makeClassSet().any((c) => c.contains('a')), isTrue); |
114 expect(makeClassSet().any((c) => c is num), isFalse); | 110 expect(makeClassSet().any((c) => c is num), isFalse); |
(...skipping 20 matching lines...) Expand all Loading... |
135 final classes = makeClassSet(); | 131 final classes = makeClassSet(); |
136 expect(classes.contains(1), isFalse); | 132 expect(classes.contains(1), isFalse); |
137 expect(() => classes.contains(''), throws); | 133 expect(() => classes.contains(''), throws); |
138 expect(() => classes.contains('foo bar'), throws); | 134 expect(() => classes.contains('foo bar'), throws); |
139 }); | 135 }); |
140 | 136 |
141 test('add', () { | 137 test('add', () { |
142 final classes = makeClassSet(); | 138 final classes = makeClassSet(); |
143 var added = classes.add('qux'); | 139 var added = classes.add('qux'); |
144 expect(added, isTrue); | 140 expect(added, isTrue); |
145 expect(classes, orderedEquals(['foo', 'bar', 'baz', 'qux'])); | 141 expect(classes, equals(['foo', 'bar', 'baz', 'qux'])); |
146 | 142 |
147 added = classes.add('qux'); | 143 added = classes.add('qux'); |
148 expect(added, isFalse); | 144 expect(added, isFalse); |
149 final list = new List.from(classes); | 145 final list = new List.from(classes); |
150 list.sort((a, b) => a.compareTo(b)); | 146 list.sort((a, b) => a.compareTo(b)); |
151 expect(list, orderedEquals(['bar', 'baz', 'foo', 'qux']), | 147 expect(list, equals(['bar', 'baz', 'foo', 'qux']), |
152 reason: "The class set shouldn't have duplicate elements."); | 148 reason: "The class set shouldn't have duplicate elements."); |
153 }); | 149 }); |
154 | 150 |
155 test('add-bad', () { | 151 test('add-bad', () { |
156 final classes = makeClassSet(); | 152 final classes = makeClassSet(); |
157 expect(() => classes.add(''), throws); | 153 expect(() => classes.add(''), throws); |
158 expect(() => classes.add('foo bar'), throws); | 154 expect(() => classes.add('foo bar'), throws); |
159 }); | 155 }); |
160 | 156 |
161 test('remove', () { | 157 test('remove', () { |
162 final classes = makeClassSet(); | 158 final classes = makeClassSet(); |
163 classes.remove('bar'); | 159 classes.remove('bar'); |
164 expect(classes, orderedEquals(['foo', 'baz'])); | 160 expect(classes, equals(['foo', 'baz'])); |
165 classes.remove('qux'); | 161 classes.remove('qux'); |
166 expect(classes, orderedEquals(['foo', 'baz'])); | 162 expect(classes, equals(['foo', 'baz'])); |
167 }); | 163 }); |
168 | 164 |
169 test('remove-bad', () { | 165 test('remove-bad', () { |
170 final classes = makeClassSet(); | 166 final classes = makeClassSet(); |
171 expect(() => classes.remove(''), throws); | 167 expect(() => classes.remove(''), throws); |
172 expect(() => classes.remove('foo bar'), throws); | 168 expect(() => classes.remove('foo bar'), throws); |
173 }); | 169 }); |
174 | 170 |
175 test('toggle', () { | 171 test('toggle', () { |
176 final classes = makeClassSet(); | 172 final classes = makeClassSet(); |
177 classes.toggle('bar'); | 173 classes.toggle('bar'); |
178 expect(classes, orderedEquals(['foo', 'baz'])); | 174 expect(classes, equals(['foo', 'baz'])); |
179 classes.toggle('qux'); | 175 classes.toggle('qux'); |
180 expect(classes, orderedEquals(['foo', 'baz', 'qux'])); | 176 expect(classes, equals(['foo', 'baz', 'qux'])); |
181 | 177 |
182 classes.toggle('qux', true); | 178 classes.toggle('qux', true); |
183 expect(classes, orderedEquals(['foo', 'baz', 'qux'])); | 179 expect(classes, equals(['foo', 'baz', 'qux'])); |
184 classes.toggle('qux', false); | 180 classes.toggle('qux', false); |
185 expect(classes, orderedEquals(['foo', 'baz'])); | 181 expect(classes, equals(['foo', 'baz'])); |
186 classes.toggle('qux', false); | 182 classes.toggle('qux', false); |
187 expect(classes, orderedEquals(['foo', 'baz'])); | 183 expect(classes, equals(['foo', 'baz'])); |
188 classes.toggle('qux', true); | 184 classes.toggle('qux', true); |
189 expect(classes, orderedEquals(['foo', 'baz', 'qux'])); | 185 expect(classes, equals(['foo', 'baz', 'qux'])); |
190 }); | 186 }); |
191 | 187 |
192 test('toggle-bad', () { | 188 test('toggle-bad', () { |
193 final classes = makeClassSet(); | 189 final classes = makeClassSet(); |
194 expect(() => classes.toggle(''), throws); | 190 expect(() => classes.toggle(''), throws); |
195 expect(() => classes.toggle('', true), throws); | 191 expect(() => classes.toggle('', true), throws); |
196 expect(() => classes.toggle('', false), throws); | 192 expect(() => classes.toggle('', false), throws); |
197 expect(() => classes.toggle('foo bar'), throws); | 193 expect(() => classes.toggle('foo bar'), throws); |
198 expect(() => classes.toggle('foo bar', true), throws); | 194 expect(() => classes.toggle('foo bar', true), throws); |
199 expect(() => classes.toggle('foo bar', false), throws); | 195 expect(() => classes.toggle('foo bar', false), throws); |
200 }); | 196 }); |
201 | 197 |
202 test('addAll', () { | 198 test('addAll', () { |
203 final classes = makeClassSet(); | 199 final classes = makeClassSet(); |
204 classes.addAll(['bar', 'qux', 'bip']); | 200 classes.addAll(['bar', 'qux', 'bip']); |
205 expect(classes, orderedEquals(['foo', 'bar', 'baz', 'qux', 'bip'])); | 201 expect(classes, equals(['foo', 'bar', 'baz', 'qux', 'bip'])); |
206 }); | 202 }); |
207 | 203 |
208 test('removeAll', () { | 204 test('removeAll', () { |
209 final classes = makeClassSet(); | 205 final classes = makeClassSet(); |
210 classes.removeAll(['bar', 'baz', 'qux']); | 206 classes.removeAll(['bar', 'baz', 'qux']); |
211 expect(classes, orderedEquals(['foo'])); | 207 expect(classes, equals(['foo'])); |
212 }); | 208 }); |
213 | 209 |
214 test('toggleAll', () { | 210 test('toggleAll', () { |
215 final classes = makeClassSet(); | 211 final classes = makeClassSet(); |
216 classes.toggleAll(['bar', 'foo']); | 212 classes.toggleAll(['bar', 'foo']); |
217 expect(classes, orderedEquals(['baz'])); | 213 expect(classes, equals(['baz'])); |
218 classes.toggleAll(['qux', 'quux']); | 214 classes.toggleAll(['qux', 'quux']); |
219 expect(classes, orderedEquals(['baz', 'qux', 'quux'])); | 215 expect(classes, equals(['baz', 'qux', 'quux'])); |
220 classes.toggleAll(['bar', 'foo'], true); | 216 classes.toggleAll(['bar', 'foo'], true); |
221 expect(classes, orderedEquals(['baz', 'qux', 'quux', 'bar', 'foo'])); | 217 expect(classes, equals(['baz', 'qux', 'quux', 'bar', 'foo'])); |
222 classes.toggleAll(['baz', 'quux'], false); | 218 classes.toggleAll(['baz', 'quux'], false); |
223 expect(classes, orderedEquals(['qux','bar', 'foo'])); | 219 expect(classes, equals(['qux','bar', 'foo'])); |
224 }); | 220 }); |
225 | 221 |
226 test('retainAll', () { | 222 test('retainAll', () { |
227 final classes = makeClassSet(); | 223 final classes = makeClassSet(); |
228 classes.retainAll(['bar', 'baz', 'qux']); | 224 classes.retainAll(['bar', 'baz', 'qux']); |
229 expect(classes, orderedEquals(['bar', 'baz'])); | 225 expect(classes, equals(['bar', 'baz'])); |
230 }); | 226 }); |
231 | 227 |
232 test('removeWhere', () { | 228 test('removeWhere', () { |
233 final classes = makeClassSet(); | 229 final classes = makeClassSet(); |
234 classes.removeWhere((s) => s.startsWith('b')); | 230 classes.removeWhere((s) => s.startsWith('b')); |
235 expect(classes, orderedEquals(['foo'])); | 231 expect(classes, equals(['foo'])); |
236 }); | 232 }); |
237 | 233 |
238 test('retainWhere', () { | 234 test('retainWhere', () { |
239 final classes = makeClassSet(); | 235 final classes = makeClassSet(); |
240 classes.retainWhere((s) => s.startsWith('b')); | 236 classes.retainWhere((s) => s.startsWith('b')); |
241 expect(classes, orderedEquals(['bar', 'baz'])); | 237 expect(classes, equals(['bar', 'baz'])); |
242 }); | 238 }); |
243 | 239 |
244 test('containsAll', () { | 240 test('containsAll', () { |
245 final classes = makeClassSet(); | 241 final classes = makeClassSet(); |
246 expect(classes.containsAll(['foo', 'baz']), isTrue); | 242 expect(classes.containsAll(['foo', 'baz']), isTrue); |
247 expect(classes.containsAll(['foo', 'qux']), isFalse); | 243 expect(classes.containsAll(['foo', 'qux']), isFalse); |
248 expect(classes.containsAll(['foo', 'baz'].toSet()), isTrue); | 244 expect(classes.containsAll(['foo', 'baz'].toSet()), isTrue); |
249 expect(classes.containsAll(['foo', 'qux'].toSet()), isFalse); | 245 expect(classes.containsAll(['foo', 'qux'].toSet()), isFalse); |
250 }); | 246 }); |
251 | 247 |
252 test('intersection', () { | 248 test('intersection', () { |
253 final classes = makeClassSet(); | 249 final classes = makeClassSet(); |
254 expect(classes.intersection(['foo', 'qux', 'baz'].toSet()), | 250 expect(classes.intersection(['foo', 'qux', 'baz'].toSet()), |
255 unorderedEquals(['foo', 'baz'])); | 251 unorderedEquals(['foo', 'baz'])); |
256 }); | 252 }); |
257 | 253 |
258 test('clear', () { | 254 test('clear', () { |
259 final classes = makeClassSet(); | 255 final classes = makeClassSet(); |
260 classes.clear(); | 256 classes.clear(); |
261 expect(classes, equals([])); | 257 expect(classes, equals([])); |
262 }); | 258 }); |
263 | 259 |
264 test('order', () { | 260 test('order', () { |
265 var classes = makeClassSet(); | 261 var classes = makeClassSet(); |
266 classes.add('aardvark'); | 262 classes.add('aardvark'); |
267 expect(classes, orderedEquals(['foo', 'bar', 'baz', 'aardvark'])); | 263 expect(classes, equals(['foo', 'bar', 'baz', 'aardvark'])); |
268 classes.toggle('baz'); | 264 classes.toggle('baz'); |
269 expect(classes, orderedEquals(['foo', 'bar', 'aardvark'])); | 265 expect(classes, equals(['foo', 'bar', 'aardvark'])); |
270 classes.toggle('baz'); | 266 classes.toggle('baz'); |
271 expect(classes, orderedEquals(['foo', 'bar', 'aardvark', 'baz'])); | 267 expect(classes, equals(['foo', 'bar', 'aardvark', 'baz'])); |
272 }); | 268 }); |
273 | 269 |
274 tearDown(listElementTearDown); | 270 tearDown(listElementTearDown); |
275 | 271 |
276 test('list_view', () { | 272 test('list_view', () { |
277 // Test that the 'view' helper function is behaving. | 273 // Test that the 'view' helper function is behaving. |
278 var elements = listElementSetup(); | 274 var elements = listElementSetup(); |
279 expect(view(elements.classes), '[classy, lassy, meta, quux, qux]'); | 275 expect(view(elements.classes), '[classy, lassy, meta, quux, qux]'); |
280 expect(view(elements), | 276 expect(view(elements), |
281 '[[quux, qux], [meta], [classy, lassy], [lassy, qux]]'); | 277 '[[quux, qux], [meta], [classy, lassy], [lassy, qux]]'); |
282 }); | 278 }); |
283 | 279 |
284 test('listClasses=', () { | 280 test('listClasses=', () { |
285 var elements = listElementSetup(); | 281 var elements = listElementSetup(); |
286 elements.classes = ['foo', 'qux']; | 282 elements.classes = ['foo', 'qux']; |
287 elements = document.queryAll('li'); | 283 elements = document.queryAll('li'); |
288 for (Element e in elements) { | 284 for (Element e in elements) { |
289 expect(e.classes, orderedEquals(['foo', 'qux'])); | 285 expect(e.classes, equals(['foo', 'qux'])); |
290 expect(extractClasses(e), orderedEquals(['foo', 'qux'])); | 286 expect(extractClasses(e), equals(['foo', 'qux'])); |
291 } | 287 } |
292 | 288 |
293 elements.classes = []; | 289 elements.classes = []; |
294 expect(view(elements.classes), '[]'); | 290 expect(view(elements.classes), '[]'); |
295 expect(view(elements), '[[], [], [], []]'); | 291 expect(view(elements), '[[], [], [], []]'); |
296 }); | 292 }); |
297 | 293 |
298 test('listMap', () { | 294 test('listMap', () { |
299 var elements = listElementSetup(); | 295 var elements = listElementSetup(); |
300 expect(elements.classes.map((c) => c.toUpperCase()).toList(), | 296 expect(elements.classes.map((c) => c.toUpperCase()).toList(), |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 expect(view(elements.classes), '[quux, qux]'); | 384 expect(view(elements.classes), '[quux, qux]'); |
389 expect(view(elements), '[[quux, qux], [], [], [qux]]'); | 385 expect(view(elements), '[[quux, qux], [], [], [qux]]'); |
390 }); | 386 }); |
391 | 387 |
392 test('listContainsAll', () { | 388 test('listContainsAll', () { |
393 var elements = listElementSetup(); | 389 var elements = listElementSetup(); |
394 expect(elements.classes.containsAll(['qux', 'meta', 'mornin']), isFalse); | 390 expect(elements.classes.containsAll(['qux', 'meta', 'mornin']), isFalse); |
395 expect(elements.classes.containsAll(['qux', 'lassy', 'classy']), isTrue); | 391 expect(elements.classes.containsAll(['qux', 'lassy', 'classy']), isTrue); |
396 }); | 392 }); |
397 } | 393 } |
OLD | NEW |