es6_test.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2017 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.module('goog.iter.es6Test');
  15. goog.setTestOnly('goog.iter.es6Test');
  16. const testSuite = goog.require('goog.testing.testSuite');
  17. const {ShimIterable} = goog.require('goog.iter.es6');
  18. const {range, toArray} = goog.require('goog.iter');
  19. goog.require('goog.testing.jsunit');
  20. /** @return {!Iterator<number>} */
  21. function* gen() {
  22. yield* [1, 3, 5];
  23. }
  24. /** @return {!ShimIterable<number>} */
  25. function fromEs6Iterator() {
  26. return ShimIterable.of(gen());
  27. }
  28. /** @return {!ShimIterable<number>} */
  29. function fromEs6Iterable() {
  30. return ShimIterable.of({
  31. [Symbol.iterator]() {
  32. return gen();
  33. }
  34. });
  35. }
  36. /** @return {!ShimIterable<number>} */
  37. function fromGoogIterator() {
  38. return ShimIterable.of(range(1, 6, 2));
  39. }
  40. /** @return {!ShimIterable<number>} */
  41. function fromGoogIterable() {
  42. return ShimIterable.of({
  43. __iterator__() {
  44. return range(1, 6, 2);
  45. }
  46. });
  47. }
  48. testSuite({
  49. // Start with ES6
  50. testEs6IterableAsIterable() {
  51. const iter = fromEs6Iterable();
  52. assertArrayEquals([1, 3, 5], toArray(iter));
  53. assertArrayEquals([1, 3, 5], [...iter]);
  54. assertArrayEquals([1, 3, 5], toArray(iter));
  55. assertArrayEquals([1, 3, 5], [...iter]);
  56. },
  57. testEs6IterableToEs6Iterator() {
  58. assertArrayEquals([1, 3, 5], toArray(fromEs6Iterable().toEs6()));
  59. assertArrayEquals([1, 3, 5], [...fromEs6Iterable().toEs6()]);
  60. },
  61. testEs6IterableToGoogIterator() {
  62. assertArrayEquals([1, 3, 5], toArray(fromEs6Iterable().toGoog()));
  63. assertArrayEquals([1, 3, 5], [...fromEs6Iterable().toGoog()]);
  64. },
  65. testEs6IteratorAsIterable() {
  66. assertArrayEquals([1, 3, 5], toArray(fromEs6Iterator()));
  67. assertArrayEquals([1, 3, 5], [...fromEs6Iterator()]);
  68. },
  69. testEs6IteratorToEs6Iterator() {
  70. assertArrayEquals([1, 3, 5], toArray(fromEs6Iterator().toEs6()));
  71. assertArrayEquals([1, 3, 5], [...fromEs6Iterator().toEs6()]);
  72. },
  73. testEs6IteratorToGoogIterator() {
  74. assertArrayEquals([1, 3, 5], toArray(fromEs6Iterator().toGoog()));
  75. assertArrayEquals([1, 3, 5], [...fromEs6Iterator().toGoog()]);
  76. },
  77. // Start with Goog
  78. testGoogIterableAsIterable() {
  79. const iter = fromGoogIterable();
  80. assertArrayEquals([1, 3, 5], toArray(iter));
  81. assertArrayEquals([1, 3, 5], [...iter]);
  82. assertArrayEquals([1, 3, 5], toArray(iter));
  83. assertArrayEquals([1, 3, 5], [...iter]);
  84. },
  85. testGoogIterableToEs6Iterator() {
  86. assertArrayEquals([1, 3, 5], toArray(fromGoogIterable().toEs6()));
  87. assertArrayEquals([1, 3, 5], [...fromGoogIterable().toEs6()]);
  88. },
  89. testGoogIterableToGoogIterator() {
  90. assertArrayEquals([1, 3, 5], toArray(fromGoogIterable().toGoog()));
  91. assertArrayEquals([1, 3, 5], [...fromGoogIterable().toGoog()]);
  92. },
  93. testGoogIteratorAsIterable() {
  94. assertArrayEquals([1, 3, 5], toArray(fromGoogIterator()));
  95. assertArrayEquals([1, 3, 5], [...fromGoogIterator()]);
  96. },
  97. testGoogIteratorToEs6Iterator() {
  98. assertArrayEquals([1, 3, 5], toArray(fromGoogIterator().toEs6()));
  99. assertArrayEquals([1, 3, 5], [...fromGoogIterator().toEs6()]);
  100. },
  101. testGoogIteratorToGoogIterator() {
  102. assertArrayEquals([1, 3, 5], toArray(fromGoogIterator().toGoog()));
  103. assertArrayEquals([1, 3, 5], [...fromGoogIterator().toGoog()]);
  104. },
  105. // Misc tests
  106. testMultipleConversions() {
  107. const iter = fromGoogIterable();
  108. assertArrayEquals([1, 3, 5], [...iter.toEs6()]);
  109. assertArrayEquals([1, 3, 5], [...iter.toGoog()]);
  110. assertArrayEquals([1, 3, 5], [...iter.toGoog().toEs6()]);
  111. assertArrayEquals([1, 3, 5], [...iter.toEs6().toGoog()]);
  112. },
  113. testExhaustedAfterConversionToIterator() {
  114. let iter = fromGoogIterable().toEs6();
  115. assertArrayEquals([1, 3, 5], [...iter.toEs6()]);
  116. assertArrayEquals([], [...iter]);
  117. iter = fromGoogIterable().toEs6();
  118. assertArrayEquals([1, 3, 5], [...iter.toGoog()]);
  119. assertArrayEquals([], [...iter]);
  120. iter = fromGoogIterable().toGoog();
  121. assertArrayEquals([1, 3, 5], [...iter.toEs6()]);
  122. assertArrayEquals([], [...iter]);
  123. iter = fromGoogIterable().toGoog();
  124. assertArrayEquals([1, 3, 5], [...iter.toGoog()]);
  125. assertArrayEquals([], [...iter]);
  126. },
  127. });