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; | 5 library ElementTest; |
6 import 'package:unittest/unittest.dart'; | 6 import 'package:unittest/unittest.dart'; |
7 import 'package:unittest/html_config.dart'; | 7 import 'package:unittest/html_config.dart'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 import 'dart:html'; | 9 import 'dart:html'; |
10 | 10 |
11 main() { | 11 Element makeElement() => new Element.tag('div'); |
12 useHtmlConfiguration(); | |
13 | 12 |
14 Element makeElement() => new Element.tag('div'); | 13 Element makeElementWithClasses() => |
15 | |
16 Element makeElementWithChildren() => | |
17 new Element.html("<div><br/><img/><input/></div>"); | |
18 | |
19 Element makeElementWithClasses() => | |
20 new Element.html('<div class="foo bar baz"></div>'); | 14 new Element.html('<div class="foo bar baz"></div>'); |
21 | 15 |
22 Element makeListElement() => | 16 Set<String> makeClassSet() => makeElementWithClasses().classes; |
| 17 |
| 18 Element makeListElement() => |
23 new Element.html('<ul class="foo bar baz">' | 19 new Element.html('<ul class="foo bar baz">' |
24 '<li class="quux qux">' | 20 '<li class="quux qux">' |
25 '<li class="meta">' | 21 '<li class="meta">' |
26 '<li class="classy lassy">' | 22 '<li class="classy lassy">' |
27 '<li class="qux lassy">' | 23 '<li class="qux lassy">' |
28 '</ul>'); | 24 '</ul>'); |
29 | 25 |
30 Set<String> makeClassSet() => makeElementWithClasses().classes; | 26 Element listElement; |
| 27 |
| 28 ElementList<Element> listElementSetup() { |
| 29 listElement = makeListElement(); |
| 30 document.documentElement.children.add(listElement); |
| 31 return document.querySelectorAll('li'); |
| 32 } |
| 33 |
| 34 void listElementTearDown() { |
| 35 if (listElement != null) { |
| 36 document.documentElement.children.remove(listElement); |
| 37 listElement = null; |
| 38 } |
| 39 } |
| 40 |
| 41 /// Returns a canonical string for Set<String> and lists of Element's classes. |
| 42 String view(var e) { |
| 43 if (e is Set) return '${e.toList()..sort()}'; |
| 44 if (e is Element) return view(e.classes); |
| 45 if (e is Iterable) return '${e.map(view).toList()}'; |
| 46 throw new ArgumentError('Cannot make canonical view string for: $e}'); |
| 47 } |
| 48 |
| 49 main() { |
| 50 useHtmlConfiguration(); |
31 | 51 |
32 Set<String> extractClasses(Element el) { | 52 Set<String> extractClasses(Element el) { |
33 final match = new RegExp('class="([^"]+)"').firstMatch(el.outerHtml); | 53 final match = new RegExp('class="([^"]+)"').firstMatch(el.outerHtml); |
34 return new LinkedHashSet.from(match[1].split(' ')); | 54 return new LinkedHashSet.from(match[1].split(' ')); |
35 } | 55 } |
36 | 56 |
37 /// Returns a canonical string for Set<String> and lists of Element's classes. | |
38 String view(var e) { | |
39 if (e is Set) return '${e.toList()..sort()}'; | |
40 if (e is Element) return view(e.classes); | |
41 if (e is Iterable) return '${e.map(view).toList()}'; | |
42 throw new ArgumentError('Cannot make canonical view string for: $e}'); | |
43 } | |
44 | 57 |
45 test('affects the "class" attribute', () { | 58 test('affects the "class" attribute', () { |
46 final el = makeElementWithClasses(); | 59 final el = makeElementWithClasses(); |
47 el.classes.add('qux'); | 60 el.classes.add('qux'); |
48 expect(extractClasses(el), orderedEquals(['foo', 'bar', 'baz', 'qux'])); | 61 expect(extractClasses(el), orderedEquals(['foo', 'bar', 'baz', 'qux'])); |
49 }); | 62 }); |
50 | 63 |
51 test('is affected by the "class" attribute', () { | 64 test('is affected by the "class" attribute', () { |
52 final el = makeElementWithClasses(); | 65 final el = makeElementWithClasses(); |
53 el.attributes['class'] = 'foo qux'; | 66 el.attributes['class'] = 'foo qux'; |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 test('order', () { | 233 test('order', () { |
221 var classes = makeClassSet(); | 234 var classes = makeClassSet(); |
222 classes.add('aardvark'); | 235 classes.add('aardvark'); |
223 expect(classes, orderedEquals(['foo', 'bar', 'baz', 'aardvark'])); | 236 expect(classes, orderedEquals(['foo', 'bar', 'baz', 'aardvark'])); |
224 classes.toggle('baz'); | 237 classes.toggle('baz'); |
225 expect(classes, orderedEquals(['foo', 'bar', 'aardvark'])); | 238 expect(classes, orderedEquals(['foo', 'bar', 'aardvark'])); |
226 classes.toggle('baz'); | 239 classes.toggle('baz'); |
227 expect(classes, orderedEquals(['foo', 'bar', 'aardvark', 'baz'])); | 240 expect(classes, orderedEquals(['foo', 'bar', 'aardvark', 'baz'])); |
228 }); | 241 }); |
229 | 242 |
230 | 243 tearDown(listElementTearDown); |
231 Element listElement; | |
232 | |
233 ElementList<Element> listElementSetup() { | |
234 listElement = makeListElement(); | |
235 document.documentElement.children.add(listElement); | |
236 return document.querySelectorAll('li'); | |
237 } | |
238 | |
239 tearDown(() { | |
240 if (listElement != null) { | |
241 document.documentElement.children.remove(listElement); | |
242 listElement = null; | |
243 } | |
244 }); | |
245 | 244 |
246 test('list_view', () { | 245 test('list_view', () { |
247 // Test that the 'view' helper function is behaving. | 246 // Test that the 'view' helper function is behaving. |
248 var elements = listElementSetup(); | 247 var elements = listElementSetup(); |
249 expect(view(elements.classes), '[classy, lassy, meta, quux, qux]'); | 248 expect(view(elements.classes), '[classy, lassy, meta, quux, qux]'); |
250 expect(view(elements), | 249 expect(view(elements), |
251 '[[quux, qux], [meta], [classy, lassy], [lassy, qux]]'); | 250 '[[quux, qux], [meta], [classy, lassy], [lassy, qux]]'); |
252 }); | 251 }); |
253 | 252 |
254 test('listClasses=', () { | 253 test('listClasses=', () { |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 expect(view(elements.classes), '[quux, qux]'); | 357 expect(view(elements.classes), '[quux, qux]'); |
359 expect(view(elements), '[[quux, qux], [], [], [qux]]'); | 358 expect(view(elements), '[[quux, qux], [], [], [qux]]'); |
360 }); | 359 }); |
361 | 360 |
362 test('listContainsAll', () { | 361 test('listContainsAll', () { |
363 var elements = listElementSetup(); | 362 var elements = listElementSetup(); |
364 expect(elements.classes.containsAll(['qux', 'meta', 'mornin']), isFalse); | 363 expect(elements.classes.containsAll(['qux', 'meta', 'mornin']), isFalse); |
365 expect(elements.classes.containsAll(['qux', 'lassy', 'classy']), isTrue); | 364 expect(elements.classes.containsAll(['qux', 'lassy', 'classy']), isTrue); |
366 }); | 365 }); |
367 } | 366 } |
OLD | NEW |