| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:svg' as svg; | 7 import 'dart:svg' as svg; |
| 11 | 8 |
| 9 import 'package:minitest/minitest.dart'; |
| 10 |
| 12 // Test for `querySelectorAll(xxx).classes.op()` where the query returns mixed | 11 // Test for `querySelectorAll(xxx).classes.op()` where the query returns mixed |
| 13 // Html and Svg elements. | 12 // Html and Svg elements. |
| 14 | 13 |
| 15 Element makeElementsContainer() { | 14 Element makeElementsContainer() { |
| 16 var e = new Element.html( | 15 var e = new Element.html( |
| 17 '<ul class="yes foo">' | 16 '<ul class="yes foo">' |
| 18 '<li class="yes quux qux">' | 17 '<li class="yes quux qux">' |
| 19 '</ul>'); | 18 '</ul>'); |
| 20 final svgContent = r""" | 19 final svgContent = r""" |
| 21 <svg version="1.1"> | 20 <svg version="1.1"> |
| (...skipping 24 matching lines...) Expand all Loading... |
| 46 | 45 |
| 47 /// Returns a canonical string for Set<String> and lists of Element's classes. | 46 /// Returns a canonical string for Set<String> and lists of Element's classes. |
| 48 String view(var e) { | 47 String view(var e) { |
| 49 if (e is Set) return '${e.toList()..sort()}'; | 48 if (e is Set) return '${e.toList()..sort()}'; |
| 50 if (e is Element) return view(e.classes); | 49 if (e is Element) return view(e.classes); |
| 51 if (e is Iterable) return '${e.map(view).toList()}'; | 50 if (e is Iterable) return '${e.map(view).toList()}'; |
| 52 throw new ArgumentError('Cannot make canonical view string for: $e}'); | 51 throw new ArgumentError('Cannot make canonical view string for: $e}'); |
| 53 } | 52 } |
| 54 | 53 |
| 55 main() { | 54 main() { |
| 56 useHtmlConfiguration(); | |
| 57 | |
| 58 Set<String> extractClasses(Element el) { | 55 Set<String> extractClasses(Element el) { |
| 59 final match = new RegExp('class="([^"]+)"').firstMatch(el.outerHtml); | 56 final match = new RegExp('class="([^"]+)"').firstMatch(el.outerHtml); |
| 60 return new LinkedHashSet.from(match[1].split(' ')); | 57 return new LinkedHashSet.from(match[1].split(' ')); |
| 61 } | 58 } |
| 62 | 59 |
| 63 tearDown(elementsTearDown); | 60 tearDown(elementsTearDown); |
| 64 | 61 |
| 65 test('list_view', () { | 62 test('list_view', () { |
| 66 // Test that the 'view' helper function is behaving. | 63 // Test that the 'view' helper function is behaving. |
| 67 var elements = elementsSetup(); | 64 var elements = elementsSetup(); |
| 68 expect(view(elements.classes), '[classy, foo, quux, qux, yes]'); | 65 expect(view(elements.classes), '[classy, foo, quux, qux, yes]'); |
| 69 expect(view(elements), | 66 expect(view(elements), |
| 70 '[[foo, yes], [quux, qux, yes], [qux, yes], [classy, yes]]'); | 67 '[[foo, yes], [quux, qux, yes], [qux, yes], [classy, yes]]'); |
| 71 }); | 68 }); |
| 72 | 69 |
| 73 test('listClasses=', () { | 70 test('listClasses=', () { |
| 74 var elements = elementsSetup(); | 71 var elements = elementsSetup(); |
| 75 | 72 |
| 76 elements.classes = ['foo', 'qux']; | 73 elements.classes = ['foo', 'qux']; |
| 77 expect(view(elements.classes), '[foo, qux]'); | 74 expect(view(elements.classes), '[foo, qux]'); |
| 78 expect(view(elements), '[[foo, qux], [foo, qux], [foo, qux], [foo, qux]]'); | 75 expect(view(elements), '[[foo, qux], [foo, qux], [foo, qux], [foo, qux]]'); |
| 79 | 76 |
| 80 var elements2 = document.querySelectorAll('.qux'); | 77 var elements2 = document.querySelectorAll('.qux'); |
| 81 expect(view(elements2.classes), '[foo, qux]'); | 78 expect(view(elements2.classes), '[foo, qux]'); |
| 82 expect(view(elements2), '[[foo, qux], [foo, qux], [foo, qux], [foo, qux]]'); | 79 expect(view(elements2), '[[foo, qux], [foo, qux], [foo, qux], [foo, qux]]'); |
| 83 | 80 |
| 84 for (Element e in elements2) { | 81 for (Element e in elements2) { |
| 85 expect(e.classes, orderedEquals(['foo', 'qux'])); | 82 expect(e.classes, equals(['foo', 'qux'])); |
| 86 expect(extractClasses(e), orderedEquals(['foo', 'qux'])); | 83 expect(extractClasses(e), equals(['foo', 'qux'])); |
| 87 } | 84 } |
| 88 | 85 |
| 89 elements.classes = []; | 86 elements.classes = []; |
| 90 expect(view(elements2.classes), '[]'); | 87 expect(view(elements2.classes), '[]'); |
| 91 expect(view(elements2), '[[], [], [], []]'); | 88 expect(view(elements2), '[[], [], [], []]'); |
| 92 }); | 89 }); |
| 93 | 90 |
| 94 test('listMap', () { | 91 test('listMap', () { |
| 95 var elements = elementsSetup(); | 92 var elements = elementsSetup(); |
| 96 expect(elements.classes.map((c) => c.toUpperCase()).toList(), | 93 expect(elements.classes.map((c) => c.toUpperCase()).toList(), |
| 97 unorderedEquals(['YES', 'FOO', 'QUX', 'QUUX', 'CLASSY'])); | 94 unorderedEquals(['YES', 'FOO', 'QUX', 'QUUX', 'CLASSY'])); |
| 98 }); | 95 }); |
| 99 | 96 |
| 100 test('listContains', () { | 97 test('listContains', () { |
| 101 var elements = elementsSetup(); | 98 var elements = elementsSetup(); |
| 102 expect(elements.classes.contains('classy'), isTrue); | 99 expect(elements.classes.contains('classy'), isTrue); |
| 103 expect(elements.classes.contains('troll'), isFalse); | 100 expect(elements.classes.contains('troll'), isFalse); |
| 104 }); | 101 }); |
| 105 | 102 |
| 106 | |
| 107 test('listAdd', () { | 103 test('listAdd', () { |
| 108 var elements = elementsSetup(); | 104 var elements = elementsSetup(); |
| 109 var added = elements.classes.add('lassie'); | 105 var added = elements.classes.add('lassie'); |
| 110 expect(added, isNull); | 106 expect(added, isNull); |
| 111 | 107 |
| 112 expect(view(elements.classes), '[classy, foo, lassie, quux, qux, yes]'); | 108 expect(view(elements.classes), '[classy, foo, lassie, quux, qux, yes]'); |
| 113 expect(view(elements), | 109 expect(view(elements), |
| 114 '[[foo, lassie, yes], [lassie, quux, qux, yes], ' | 110 '[[foo, lassie, yes], [lassie, quux, qux, yes], ' |
| 115 '[lassie, qux, yes], [classy, lassie, yes]]'); | 111 '[lassie, qux, yes], [classy, lassie, yes]]'); |
| 116 }); | 112 }); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 expect(view(elements), | 187 expect(view(elements), |
| 192 '[[], [quux, qux], [qux], []]'); | 188 '[[], [quux, qux], [qux], []]'); |
| 193 }); | 189 }); |
| 194 | 190 |
| 195 test('listContainsAll', () { | 191 test('listContainsAll', () { |
| 196 var elements = elementsSetup(); | 192 var elements = elementsSetup(); |
| 197 expect(elements.classes.containsAll(['qux', 'mornin']), isFalse); | 193 expect(elements.classes.containsAll(['qux', 'mornin']), isFalse); |
| 198 expect(elements.classes.containsAll(['qux', 'classy']), isTrue); | 194 expect(elements.classes.containsAll(['qux', 'classy']), isTrue); |
| 199 }); | 195 }); |
| 200 } | 196 } |
| OLD | NEW |