iter_test.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.dom.iterTest');
  15. goog.setTestOnly('goog.dom.iterTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.NodeType');
  18. goog.require('goog.dom.iter.AncestorIterator');
  19. goog.require('goog.dom.iter.ChildIterator');
  20. goog.require('goog.dom.iter.SiblingIterator');
  21. goog.require('goog.testing.dom');
  22. goog.require('goog.testing.jsunit');
  23. var test;
  24. var br;
  25. function setUpPage() {
  26. test = goog.dom.getElement('test');
  27. br = goog.dom.getElement('br');
  28. }
  29. function testNextSibling() {
  30. goog.testing.dom.assertNodesMatch(
  31. new goog.dom.iter.SiblingIterator(test.firstChild), ['#br', 'def']);
  32. }
  33. function testNextSiblingInclusive() {
  34. goog.testing.dom.assertNodesMatch(
  35. new goog.dom.iter.SiblingIterator(test.firstChild, true),
  36. ['abc', '#br', 'def']);
  37. }
  38. function testPreviousSibling() {
  39. goog.testing.dom.assertNodesMatch(
  40. new goog.dom.iter.SiblingIterator(test.lastChild, false, true),
  41. ['#br', 'abc']);
  42. }
  43. function testPreviousSiblingInclusive() {
  44. goog.testing.dom.assertNodesMatch(
  45. new goog.dom.iter.SiblingIterator(test.lastChild, true, true),
  46. ['def', '#br', 'abc']);
  47. }
  48. function testChildIterator() {
  49. goog.testing.dom.assertNodesMatch(
  50. new goog.dom.iter.ChildIterator(test), ['abc', '#br', 'def']);
  51. }
  52. function testChildIteratorIndex() {
  53. goog.testing.dom.assertNodesMatch(
  54. new goog.dom.iter.ChildIterator(test, false, 1), ['#br', 'def']);
  55. }
  56. function testChildIteratorReverse() {
  57. goog.testing.dom.assertNodesMatch(
  58. new goog.dom.iter.ChildIterator(test, true), ['def', '#br', 'abc']);
  59. }
  60. function testEmptyChildIteratorReverse() {
  61. goog.testing.dom.assertNodesMatch(
  62. new goog.dom.iter.ChildIterator(br, true), []);
  63. }
  64. function testChildIteratorIndexReverse() {
  65. goog.testing.dom.assertNodesMatch(
  66. new goog.dom.iter.ChildIterator(test, true, 1), ['#br', 'abc']);
  67. }
  68. function testAncestorIterator() {
  69. goog.testing.dom.assertNodesMatch(
  70. new goog.dom.iter.AncestorIterator(br),
  71. ['#test', '#body', '#html', goog.dom.NodeType.DOCUMENT]);
  72. }
  73. function testAncestorIteratorInclusive() {
  74. goog.testing.dom.assertNodesMatch(
  75. new goog.dom.iter.AncestorIterator(br, true),
  76. ['#br', '#test', '#body', '#html', goog.dom.NodeType.DOCUMENT]);
  77. }