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('NodeTest'); | 5 #library('NodeTest'); |
6 #import('../../pkg/unittest/unittest.dart'); | 6 #import('../../pkg/unittest/unittest.dart'); |
7 #import('../../pkg/unittest/html_config.dart'); | 7 #import('../../pkg/unittest/html_config.dart'); |
8 #import('dart:html'); | 8 #import('dart:html'); |
9 | 9 |
10 Node makeNode() => new Element.tag('div'); | 10 Node makeNode() => new Element.tag('div'); |
11 Node makeNodeWithChildren() => | 11 Node makeNodeWithChildren() => |
12 new Element.html("<div>Foo<br/><!--baz--></div>"); | 12 new Element.html("<div>Foo<br/><!--baz--></div>"); |
13 | 13 |
14 main() { | 14 main() { |
15 useHtmlConfiguration(); | 15 useHtmlConfiguration(); |
16 | 16 |
| 17 var isText = predicate((x) => x is Text, 'is a Text'); |
| 18 var isComment = predicate((x) => x is Comment, 'is a Comment'); |
| 19 var isBRElement = predicate((x) => x is BRElement, 'is a BRElement'); |
| 20 var isHRElement = predicate((x) => x is HRElement, 'is a HRElement'); |
| 21 var isNodeList = predicate((x) => x is List<Node>, 'is a List<Node>'); |
| 22 var isImageElement = |
| 23 predicate((x) => x is ImageElement, 'is an ImageElement'); |
| 24 var isInputElement = |
| 25 predicate((x) => x is InputElement, 'is an InputElement'); |
| 26 |
17 test('replaceWith', () { | 27 test('replaceWith', () { |
18 final node = makeNodeWithChildren(); | 28 final node = makeNodeWithChildren(); |
19 final subnode = node.nodes[1]; | 29 final subnode = node.nodes[1]; |
20 final out = subnode.replaceWith(new Text('Bar')); | 30 final out = subnode.replaceWith(new Text('Bar')); |
21 Expect.equals(subnode, out, '#replaceWith should be chainable'); | 31 expect(out, equals(subnode), reason: '#replaceWith should be chainable'); |
22 Expect.equals(3, node.nodes.length); | 32 expect(node.nodes.length, 3); |
23 Expect.isTrue(node.nodes[0] is Text); | 33 expect(node.nodes[0], isText); |
24 Expect.equals('Foo', node.nodes[0].text); | 34 expect(node.nodes[0].text, 'Foo'); |
25 Expect.isTrue(node.nodes[1] is Text); | 35 expect(node.nodes[1], isText); |
26 Expect.equals('Bar', node.nodes[1].text); | 36 expect(node.nodes[1].text, 'Bar'); |
27 Expect.isTrue(node.nodes[2] is Comment); | 37 expect(node.nodes[2], isComment); |
28 }); | 38 }); |
29 | 39 |
30 test('remove', () { | 40 test('remove', () { |
31 final node = makeNodeWithChildren(); | 41 final node = makeNodeWithChildren(); |
32 final subnode = node.nodes[1]; | 42 final subnode = node.nodes[1]; |
33 final out = subnode.remove(); | 43 final out = subnode.remove(); |
34 Expect.isNull(out); | 44 expect(out, isNull); |
35 Expect.equals(2, node.nodes.length); | 45 expect(node.nodes.length, 2); |
36 Expect.isTrue(node.nodes[0] is Text); | 46 expect(node.nodes[0], isText); |
37 Expect.isTrue(node.nodes[1] is Comment); | 47 expect(node.nodes[1], isComment); |
38 }); | 48 }); |
39 | 49 |
40 test('contains', () { | 50 test('contains', () { |
41 final Node node = new Element.html("<div>Foo<span>Bar</span></div>"); | 51 final Node node = new Element.html("<div>Foo<span>Bar</span></div>"); |
42 Expect.isTrue(node.contains(node.nodes.first)); | 52 expect(node.contains(node.nodes.first), isTrue); |
43 Expect.isTrue(node.contains(node.nodes[1].nodes.first)); | 53 expect(node.contains(node.nodes[1].nodes.first), isTrue); |
44 Expect.isFalse(node.contains(new Text('Foo'))); | 54 expect(node.contains(new Text('Foo')), isFalse); |
45 }); | 55 }); |
46 | 56 |
47 group('nodes', () { | 57 group('nodes', () { |
48 test('is a NodeList', () { | 58 test('is a NodeList', () { |
49 Expect.isTrue(makeNodeWithChildren().nodes is List<Node>); | 59 expect(makeNodeWithChildren().nodes, isNodeList); |
50 }); | 60 }); |
51 | 61 |
52 test('first', () { | 62 test('first', () { |
53 var node = makeNodeWithChildren(); | 63 var node = makeNodeWithChildren(); |
54 Expect.isTrue(node.nodes.first is Text); | 64 expect(node.nodes.first, isText); |
55 }); | 65 }); |
56 | 66 |
57 test('last', () { | 67 test('last', () { |
58 var node = makeNodeWithChildren(); | 68 var node = makeNodeWithChildren(); |
59 Expect.isTrue(node.nodes.last is Comment); | 69 expect(node.nodes.last, isComment); |
60 }); | 70 }); |
61 | 71 |
62 test('forEach', () { | 72 test('forEach', () { |
63 var nodes = []; | 73 var nodes = []; |
64 var node = makeNodeWithChildren(); | 74 var node = makeNodeWithChildren(); |
65 node.nodes.forEach((n) => nodes.add(n)); | 75 node.nodes.forEach((n) => nodes.add(n)); |
66 Expect.isTrue(nodes[0] is Text); | 76 expect(nodes[0], isText); |
67 Expect.isTrue(nodes[1] is BRElement); | 77 expect(nodes[1], isBRElement); |
68 Expect.isTrue(nodes[2] is Comment); | 78 expect(nodes[2], isComment); |
69 }); | 79 }); |
70 | 80 |
71 test('filter', () { | 81 test('filter', () { |
72 var filtered = makeNodeWithChildren().nodes.filter((n) => n is BRElement); | 82 var filtered = makeNodeWithChildren().nodes.filter((n) => n is BRElement); |
73 Expect.equals(1, filtered.length); | 83 expect(filtered.length, 1); |
74 Expect.isTrue(filtered[0] is BRElement); | 84 expect(filtered[0], isBRElement); |
75 Expect.isTrue(filtered is List<Node>); | 85 expect(filtered, isNodeList); |
76 }); | 86 }); |
77 | 87 |
78 test('every', () { | 88 test('every', () { |
79 var node = makeNodeWithChildren(); | 89 var node = makeNodeWithChildren(); |
80 Expect.isTrue(node.nodes.every((n) => n is Node)); | 90 expect(node.nodes.every((n) => n is Node), isTrue); |
81 Expect.isFalse(node.nodes.every((n) => n is Comment)); | 91 expect(node.nodes.every((n) => n is Comment), isFalse); |
82 }); | 92 }); |
83 | 93 |
84 test('some', () { | 94 test('some', () { |
85 var node = makeNodeWithChildren(); | 95 var node = makeNodeWithChildren(); |
86 Expect.isTrue(node.nodes.some((n) => n is Comment)); | 96 expect(node.nodes.some((n) => n is Comment), isTrue); |
87 Expect.isFalse(node.nodes.some((n) => n is SVGElement)); | 97 expect(node.nodes.some((n) => n is SVGElement), isFalse); |
88 }); | 98 }); |
89 | 99 |
90 test('isEmpty', () { | 100 test('isEmpty', () { |
91 Expect.isTrue(makeNode().nodes.isEmpty); | 101 expect(makeNode().nodes.isEmpty, isTrue); |
92 Expect.isFalse(makeNodeWithChildren().nodes.isEmpty); | 102 expect(makeNodeWithChildren().nodes.isEmpty, isFalse); |
93 }); | 103 }); |
94 | 104 |
95 test('length', () { | 105 test('length', () { |
96 Expect.equals(0, makeNode().nodes.length); | 106 expect(makeNode().nodes.length, 0); |
97 Expect.equals(3, makeNodeWithChildren().nodes.length); | 107 expect(makeNodeWithChildren().nodes.length, 3); |
98 }); | 108 }); |
99 | 109 |
100 test('[]', () { | 110 test('[]', () { |
101 var node = makeNodeWithChildren(); | 111 var node = makeNodeWithChildren(); |
102 Expect.isTrue(node.nodes[0] is Text); | 112 expect(node.nodes[0], isText); |
103 Expect.isTrue(node.nodes[1] is BRElement); | 113 expect(node.nodes[1], isBRElement); |
104 Expect.isTrue(node.nodes[2] is Comment); | 114 expect(node.nodes[2], isComment); |
105 }); | 115 }); |
106 | 116 |
107 test('[]=', () { | 117 test('[]=', () { |
108 var node = makeNodeWithChildren(); | 118 var node = makeNodeWithChildren(); |
109 node.nodes[1] = new Element.tag('hr'); | 119 node.nodes[1] = new Element.tag('hr'); |
110 Expect.isTrue(node.nodes[0] is Text); | 120 expect(node.nodes[0], isText); |
111 Expect.isTrue(node.nodes[1] is HRElement); | 121 expect(node.nodes[1], isHRElement); |
112 Expect.isTrue(node.nodes[2] is Comment); | 122 expect(node.nodes[2], isComment); |
113 }); | 123 }); |
114 | 124 |
115 test('add', () { | 125 test('add', () { |
116 var node = makeNode(); | 126 var node = makeNode(); |
117 node.nodes.add(new Element.tag('hr')); | 127 node.nodes.add(new Element.tag('hr')); |
118 Expect.isTrue(node.nodes.last is HRElement); | 128 expect(node.nodes.last, isHRElement); |
119 }); | 129 }); |
120 | 130 |
121 test('addLast', () { | 131 test('addLast', () { |
122 var node = makeNode(); | 132 var node = makeNode(); |
123 node.nodes.addLast(new Element.tag('hr')); | 133 node.nodes.addLast(new Element.tag('hr')); |
124 Expect.isTrue(node.nodes.last is HRElement); | 134 expect(node.nodes.last, isHRElement); |
125 }); | 135 }); |
126 | 136 |
127 test('iterator', () { | 137 test('iterator', () { |
128 var nodes = []; | 138 var nodes = []; |
129 var node = makeNodeWithChildren(); | 139 var node = makeNodeWithChildren(); |
130 for (var subnode in node.nodes) { | 140 for (var subnode in node.nodes) { |
131 nodes.add(subnode); | 141 nodes.add(subnode); |
132 } | 142 } |
133 Expect.isTrue(nodes[0] is Text); | 143 expect(nodes[0], isText); |
134 Expect.isTrue(nodes[1] is BRElement); | 144 expect(nodes[1], isBRElement); |
135 Expect.isTrue(nodes[2] is Comment); | 145 expect(nodes[2], isComment); |
136 }); | 146 }); |
137 | 147 |
138 test('addAll', () { | 148 test('addAll', () { |
139 var node = makeNodeWithChildren(); | 149 var node = makeNodeWithChildren(); |
140 node.nodes.addAll([ | 150 node.nodes.addAll([ |
141 new Element.tag('hr'), | 151 new Element.tag('hr'), |
142 new Element.tag('img'), | 152 new Element.tag('img'), |
143 new Element.tag('input') | 153 new Element.tag('input') |
144 ]); | 154 ]); |
145 Expect.isTrue(node.nodes[0] is Text); | 155 expect(node.nodes[0], isText); |
146 Expect.isTrue(node.nodes[1] is BRElement); | 156 expect(node.nodes[1], isBRElement); |
147 Expect.isTrue(node.nodes[2] is Comment); | 157 expect(node.nodes[2], isComment); |
148 Expect.isTrue(node.nodes[3] is HRElement); | 158 expect(node.nodes[3], isHRElement); |
149 Expect.isTrue(node.nodes[4] is ImageElement); | 159 expect(node.nodes[4], isImageElement); |
150 Expect.isTrue(node.nodes[5] is InputElement); | 160 expect(node.nodes[5], isInputElement); |
151 }); | 161 }); |
152 | 162 |
153 test('clear', () { | 163 test('clear', () { |
154 var node = makeNodeWithChildren(); | 164 var node = makeNodeWithChildren(); |
155 node.nodes.clear(); | 165 node.nodes.clear(); |
156 Expect.listEquals([], node.nodes); | 166 expect(node.nodes, []); |
157 }); | 167 }); |
158 | 168 |
159 test('removeLast', () { | 169 test('removeLast', () { |
160 var node = makeNodeWithChildren(); | 170 var node = makeNodeWithChildren(); |
161 Expect.isTrue(node.nodes.removeLast() is Comment); | 171 expect(node.nodes.removeLast(), isComment); |
162 Expect.equals(2, node.nodes.length); | 172 expect(node.nodes.length, 2); |
163 Expect.isTrue(node.nodes.removeLast() is BRElement); | 173 expect(node.nodes.removeLast(), isBRElement); |
164 Expect.equals(1, node.nodes.length); | 174 expect(node.nodes.length, 1); |
165 }); | 175 }); |
166 | 176 |
167 test('getRange', () { | 177 test('getRange', () { |
168 var node = makeNodeWithChildren(); | 178 var node = makeNodeWithChildren(); |
169 Expect.isTrue(node.nodes.getRange(1, 2) is List<Node>); | 179 expect(node.nodes.getRange(1, 2), isNodeList); |
170 }); | 180 }); |
171 }); | 181 }); |
172 | 182 |
173 group('_NodeList', () { | 183 group('_NodeList', () { |
174 List<Node> makeNodeList() => | 184 List<Node> makeNodeList() => |
175 makeNodeWithChildren().nodes.filter((_) => true); | 185 makeNodeWithChildren().nodes.filter((_) => true); |
176 | 186 |
177 test('first', () { | 187 test('first', () { |
178 var nodes = makeNodeList(); | 188 var nodes = makeNodeList(); |
179 Expect.isTrue(nodes.first is Text); | 189 expect(nodes.first, isText); |
180 }); | 190 }); |
181 | 191 |
182 test('filter', () { | 192 test('filter', () { |
183 var filtered = makeNodeList().filter((n) => n is BRElement); | 193 var filtered = makeNodeList().filter((n) => n is BRElement); |
184 Expect.equals(1, filtered.length); | 194 expect(filtered.length, 1); |
185 Expect.isTrue(filtered[0] is BRElement); | 195 expect(filtered[0], isBRElement); |
186 Expect.isTrue(filtered is List<Node>); | 196 expect(filtered, isNodeList); |
187 }); | 197 }); |
188 | 198 |
189 test('getRange', () { | 199 test('getRange', () { |
190 var range = makeNodeList().getRange(1, 2); | 200 var range = makeNodeList().getRange(1, 2); |
191 Expect.isTrue(range is List<Node>); | 201 expect(range, isNodeList); |
192 Expect.isTrue(range[0] is BRElement); | 202 expect(range[0], isBRElement); |
193 Expect.isTrue(range[1] is Comment); | 203 expect(range[1], isComment); |
194 }); | 204 }); |
195 }); | 205 }); |
196 } | 206 } |
OLD | NEW |