OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library NodeTest; |
| 6 import 'package:unittest/unittest.dart'; |
| 7 import 'package:unittest/html_individual_config.dart'; |
| 8 import 'dart:html'; |
| 9 import 'dart:svg' as svg; |
| 10 |
| 11 Node makeNode() => new Element.tag('div'); |
| 12 Node makeNodeWithChildren() => |
| 13 new Element.html("<div>Foo<br/><!--baz--></div>"); |
| 14 |
| 15 void testUnsupported(String name, void f()) { |
| 16 test(name, () { |
| 17 expect(f, throwsUnsupportedError); |
| 18 }); |
| 19 } |
| 20 |
| 21 main() { |
| 22 useHtmlIndividualConfiguration(); |
| 23 |
| 24 var isText = predicate((x) => x is Text, 'is a Text'); |
| 25 var isComment = predicate((x) => x is Comment, 'is a Comment'); |
| 26 var isBRElement = predicate((x) => x is BRElement, 'is a BRElement'); |
| 27 var isHRElement = predicate((x) => x is HRElement, 'is a HRElement'); |
| 28 var isNodeList = predicate((x) => x is List<Node>, 'is a List<Node>'); |
| 29 var isImageElement = |
| 30 predicate((x) => x is ImageElement, 'is an ImageElement'); |
| 31 var isInputElement = |
| 32 predicate((x) => x is InputElement, 'is an InputElement'); |
| 33 |
| 34 group('functional', () { |
| 35 test('toString', () { |
| 36 final nodes = makeNodeWithChildren(); |
| 37 // TODO(efortuna): Update this test when you have actual toString methods |
| 38 // for the items in the node List as well. |
| 39 expect(nodes.nodes.toString(), "[Foo, br, baz]"); |
| 40 final node = makeNode(); |
| 41 expect(node.nodes.toString(), '[]'); |
| 42 }); |
| 43 |
| 44 test('replaceWith', () { |
| 45 final node = makeNodeWithChildren(); |
| 46 final subnode = node.nodes[1]; |
| 47 final out = subnode.replaceWith(new Text('Bar')); |
| 48 expect(out, equals(subnode), reason: '#replaceWith should be chainable'); |
| 49 expect(node.nodes.length, 3); |
| 50 expect(node.nodes[0], isText); |
| 51 expect(node.nodes[0].text, 'Foo'); |
| 52 expect(node.nodes[1], isText); |
| 53 expect(node.nodes[1].text, 'Bar'); |
| 54 expect(node.nodes[2], isComment); |
| 55 }); |
| 56 |
| 57 test('append', () { |
| 58 var node = makeNode(); |
| 59 node.append(new Element.tag('hr')); |
| 60 expect(node.nodes.last, isHRElement); |
| 61 }); |
| 62 |
| 63 test('remove', () { |
| 64 final node = makeNodeWithChildren(); |
| 65 final subnode = node.nodes[1]; |
| 66 subnode.remove(); |
| 67 expect(node.nodes.length, 2); |
| 68 expect(node.nodes[0], isText); |
| 69 expect(node.nodes[1], isComment); |
| 70 }); |
| 71 |
| 72 test('contains', () { |
| 73 final Node node = |
| 74 new Element.html("<div>Foo<span><div>Bar<div></span></div>"); |
| 75 // IE10 returns false for contains of nodes. |
| 76 //expect(node.contains(node.nodes.first), isTrue); |
| 77 expect(node.contains(node.nodes[1].nodes.first), isTrue); |
| 78 expect(node.contains(new Text('Foo')), isFalse); |
| 79 }); |
| 80 |
| 81 test('insertAllBefore', () { |
| 82 var node = makeNodeWithChildren(); |
| 83 var b = new DivElement(); |
| 84 b.nodes.addAll([ |
| 85 new HRElement(), |
| 86 new ImageElement(), |
| 87 new InputElement() |
| 88 ]); |
| 89 node.insertAllBefore(b.nodes, node.nodes[1]); |
| 90 expect(node.nodes[0], isText); |
| 91 expect(node.nodes[1], isHRElement); |
| 92 expect(node.nodes[2], isImageElement); |
| 93 expect(node.nodes[3], isInputElement); |
| 94 expect(node.nodes[4], isBRElement); |
| 95 expect(node.nodes[5], isComment); |
| 96 |
| 97 var nodes = [ |
| 98 new HRElement(), |
| 99 new ImageElement(), |
| 100 new InputElement() |
| 101 ]; |
| 102 node.insertAllBefore(nodes, node.nodes[5]); |
| 103 |
| 104 expect(node.nodes[0], isText); |
| 105 expect(node.nodes[1], isHRElement); |
| 106 expect(node.nodes[2], isImageElement); |
| 107 expect(node.nodes[3], isInputElement); |
| 108 expect(node.nodes[4], isBRElement); |
| 109 expect(node.nodes[5], isHRElement); |
| 110 expect(node.nodes[6], isImageElement); |
| 111 expect(node.nodes[7], isInputElement); |
| 112 expect(node.nodes[8], isComment); |
| 113 }); |
| 114 }); |
| 115 |
| 116 group('nodes', () { |
| 117 test('is a NodeList', () { |
| 118 expect(makeNodeWithChildren().nodes, isNodeList); |
| 119 }); |
| 120 |
| 121 test('indexer', () { |
| 122 var node = new DivElement(); |
| 123 expect(() { |
| 124 node.nodes[0]; |
| 125 }, throwsRangeError); |
| 126 |
| 127 expect(() { |
| 128 node.nodes[-1]; |
| 129 }, throwsRangeError); |
| 130 }); |
| 131 |
| 132 test('first', () { |
| 133 var node = makeNodeWithChildren(); |
| 134 expect(node.nodes.first, isText); |
| 135 |
| 136 node = new DivElement(); |
| 137 expect(() { |
| 138 node = node.nodes.first; |
| 139 }, throwsStateError); |
| 140 }); |
| 141 |
| 142 test('last', () { |
| 143 var node = makeNodeWithChildren(); |
| 144 expect(node.nodes.last, isComment); |
| 145 }); |
| 146 |
| 147 test('forEach', () { |
| 148 var nodes = []; |
| 149 var node = makeNodeWithChildren(); |
| 150 node.nodes.forEach((n) => nodes.add(n)); |
| 151 expect(nodes[0], isText); |
| 152 expect(nodes[1], isBRElement); |
| 153 expect(nodes[2], isComment); |
| 154 }); |
| 155 |
| 156 test('where', () { |
| 157 var filtered = |
| 158 makeNodeWithChildren().nodes.where((n) => n is BRElement).toList(); |
| 159 expect(filtered.length, 1); |
| 160 expect(filtered[0], isBRElement); |
| 161 expect(filtered, isNodeList); |
| 162 }); |
| 163 |
| 164 test('every', () { |
| 165 var node = makeNodeWithChildren(); |
| 166 expect(node.nodes.every((n) => n is Node), isTrue); |
| 167 expect(node.nodes.every((n) => n is Comment), isFalse); |
| 168 }); |
| 169 |
| 170 test('any', () { |
| 171 var node = makeNodeWithChildren(); |
| 172 expect(node.nodes.any((n) => n is Comment), isTrue); |
| 173 expect(node.nodes.any((n) => n is svg.SvgElement), isFalse); |
| 174 }); |
| 175 |
| 176 test('isEmpty', () { |
| 177 expect(makeNode().nodes.isEmpty, isTrue); |
| 178 expect(makeNodeWithChildren().nodes.isEmpty, isFalse); |
| 179 }); |
| 180 |
| 181 test('length', () { |
| 182 expect(makeNode().nodes.length, 0); |
| 183 expect(makeNodeWithChildren().nodes.length, 3); |
| 184 }); |
| 185 |
| 186 test('[]', () { |
| 187 var node = makeNodeWithChildren(); |
| 188 expect(node.nodes[0], isText); |
| 189 expect(node.nodes[1], isBRElement); |
| 190 expect(node.nodes[2], isComment); |
| 191 }); |
| 192 |
| 193 test('[]=', () { |
| 194 var node = makeNodeWithChildren(); |
| 195 node.nodes[1] = new Element.tag('hr'); |
| 196 expect(node.nodes[0], isText); |
| 197 expect(node.nodes[1], isHRElement); |
| 198 expect(node.nodes[2], isComment); |
| 199 }); |
| 200 |
| 201 test('add', () { |
| 202 var node = makeNode(); |
| 203 node.nodes.add(new Element.tag('hr')); |
| 204 expect(node.nodes.last, isHRElement); |
| 205 }); |
| 206 |
| 207 test('iterator', () { |
| 208 var nodes = []; |
| 209 var node = makeNodeWithChildren(); |
| 210 for (var subnode in node.nodes) { |
| 211 nodes.add(subnode); |
| 212 } |
| 213 expect(nodes[0], isText); |
| 214 expect(nodes[1], isBRElement); |
| 215 expect(nodes[2], isComment); |
| 216 }); |
| 217 |
| 218 test('addAll', () { |
| 219 var node = makeNodeWithChildren(); |
| 220 node.nodes.addAll([ |
| 221 new Element.tag('hr'), |
| 222 new Element.tag('img'), |
| 223 new Element.tag('input') |
| 224 ]); |
| 225 expect(node.nodes[0], isText); |
| 226 expect(node.nodes[1], isBRElement); |
| 227 expect(node.nodes[2], isComment); |
| 228 expect(node.nodes[3], isHRElement); |
| 229 expect(node.nodes[4], isImageElement); |
| 230 expect(node.nodes[5], isInputElement); |
| 231 |
| 232 var a = makeNodeWithChildren(); |
| 233 var b = makeNodeWithChildren(); |
| 234 var childrenLength = a.children.length + b.children.length; |
| 235 var nodesLength = a.nodes.length + b.nodes.length; |
| 236 |
| 237 a.children.addAll(b.children); |
| 238 expect(b.children.length, 0); |
| 239 expect(a.children.length, childrenLength); |
| 240 |
| 241 b.nodes.addAll(a.children); |
| 242 expect(a.children.length, 0); |
| 243 expect(b.children.length, childrenLength); |
| 244 |
| 245 a.nodes.addAll(b.nodes); |
| 246 expect(b.nodes.length, 0); |
| 247 expect(a.nodes.length, nodesLength); |
| 248 }); |
| 249 |
| 250 test('insert', () { |
| 251 var node = new DivElement(); |
| 252 node.nodes.insert(0, new BRElement()); |
| 253 expect(node.nodes[0], isBRElement); |
| 254 node.nodes.insert(0, new HRElement()); |
| 255 expect(node.nodes[0], isHRElement); |
| 256 node.nodes.insert(1, new ImageElement()); |
| 257 expect(node.nodes[1], isImageElement); |
| 258 node.nodes.insert(node.nodes.length, new InputElement()); |
| 259 expect(node.nodes.last, isInputElement); |
| 260 }); |
| 261 |
| 262 test('clear', () { |
| 263 var node = makeNodeWithChildren(); |
| 264 node.nodes.clear(); |
| 265 expect(node.nodes, []); |
| 266 }); |
| 267 |
| 268 test('removeLast', () { |
| 269 var node = makeNodeWithChildren(); |
| 270 expect(node.nodes.removeLast(), isComment); |
| 271 expect(node.nodes.length, 2); |
| 272 expect(node.nodes.removeLast(), isBRElement); |
| 273 expect(node.nodes.length, 1); |
| 274 }); |
| 275 |
| 276 test('getRange', () { |
| 277 var items = makeNodeWithChildren().nodes.getRange(0, 1); |
| 278 expect(items.length, 1); |
| 279 expect(items.first, isText); |
| 280 }); |
| 281 |
| 282 test('sublist', () { |
| 283 var node = makeNodeWithChildren(); |
| 284 expect(node.nodes.sublist(1, 3), isNodeList); |
| 285 }); |
| 286 |
| 287 test('insertAll', () { |
| 288 var node = makeNodeWithChildren(); |
| 289 var b = new DivElement(); |
| 290 b.nodes.addAll([ |
| 291 new HRElement(), |
| 292 new ImageElement(), |
| 293 new InputElement() |
| 294 ]); |
| 295 node.nodes.insertAll(1, b.nodes); |
| 296 expect(node.nodes[0], isText); |
| 297 expect(node.nodes[1], isHRElement); |
| 298 expect(node.nodes[2], isImageElement); |
| 299 expect(node.nodes[3], isInputElement); |
| 300 expect(node.nodes[4], isBRElement); |
| 301 expect(node.nodes[5], isComment); |
| 302 |
| 303 var nodes = [ |
| 304 new HRElement(), |
| 305 new ImageElement(), |
| 306 new InputElement() |
| 307 ]; |
| 308 node.nodes.insertAll(5, nodes); |
| 309 |
| 310 expect(node.nodes[0], isText); |
| 311 expect(node.nodes[1], isHRElement); |
| 312 expect(node.nodes[2], isImageElement); |
| 313 expect(node.nodes[3], isInputElement); |
| 314 expect(node.nodes[4], isBRElement); |
| 315 expect(node.nodes[5], isHRElement); |
| 316 expect(node.nodes[6], isImageElement); |
| 317 expect(node.nodes[7], isInputElement); |
| 318 expect(node.nodes[8], isComment); |
| 319 |
| 320 var d = new DivElement(); |
| 321 var ns = d.nodes; |
| 322 // `insertAll` should work when positioned at end. |
| 323 ns.insertAll(ns.length, [new HRElement()]); |
| 324 expect(ns.length, 1); |
| 325 expect(ns[0], isHRElement); |
| 326 }); |
| 327 |
| 328 testUnsupported('removeRange', () { |
| 329 makeNodeWithChildren().nodes.removeRange(0, 1); |
| 330 }); |
| 331 |
| 332 testUnsupported('replaceRange', () { |
| 333 makeNodeWithChildren().nodes.replaceRange(0, 1, [new InputElement()]); |
| 334 }); |
| 335 |
| 336 testUnsupported('fillRange', () { |
| 337 makeNodeWithChildren().nodes.fillRange(0, 1, null); |
| 338 }); |
| 339 |
| 340 testUnsupported('setAll', () { |
| 341 makeNodeWithChildren().nodes.setAll(0, [new InputElement()]); |
| 342 }); |
| 343 }); |
| 344 |
| 345 group('iterating', () { |
| 346 test('NodeIterator', () { |
| 347 var root = makeNodeWithChildren(); |
| 348 var nodeIterator = new NodeIterator(root, NodeFilter.SHOW_COMMENT); |
| 349 expect(nodeIterator.nextNode(), isComment); |
| 350 expect(nodeIterator.nextNode(), isNull); |
| 351 }); |
| 352 |
| 353 test('TreeWalker', () { |
| 354 var root = makeNodeWithChildren(); |
| 355 var treeWalker = new TreeWalker(root, NodeFilter.SHOW_COMMENT); |
| 356 expect(treeWalker.nextNode(), isComment); |
| 357 expect(treeWalker.nextNode(), isNull); |
| 358 }); |
| 359 }); |
| 360 } |
OLD | NEW |