| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 import "dart:collection"; | 5 import "dart:collection"; |
| 6 import "package:collection/iterable_zip.dart"; | 6 |
| 7 import "package:test/test.dart"; | 7 import "package:test/test.dart"; |
| 8 | 8 |
| 9 import "package:collection/collection.dart"; |
| 10 |
| 9 /// Iterable like [base] except that it throws when value equals [errorValue]. | 11 /// Iterable like [base] except that it throws when value equals [errorValue]. |
| 10 Iterable iterError(Iterable base, int errorValue) { | 12 Iterable iterError(Iterable base, int errorValue) { |
| 11 return base.map((x) => x == errorValue ? throw "BAD" : x); | 13 return base.map((x) => x == errorValue ? throw "BAD" : x); |
| 12 } | 14 } |
| 13 | 15 |
| 14 main() { | 16 main() { |
| 15 test("Basic", () { | 17 test("Basic", () { |
| 16 expect(new IterableZip([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), | 18 expect( |
| 17 equals([[1, 4, 7], [2, 5, 8], [3, 6, 9]])); | 19 new IterableZip([ |
| 20 [1, 2, 3], |
| 21 [4, 5, 6], |
| 22 [7, 8, 9] |
| 23 ]), |
| 24 equals([ |
| 25 [1, 4, 7], |
| 26 [2, 5, 8], |
| 27 [3, 6, 9] |
| 28 ])); |
| 18 }); | 29 }); |
| 19 | 30 |
| 20 test("Uneven length 1", () { | 31 test("Uneven length 1", () { |
| 21 expect(new IterableZip([[1, 2, 3, 99, 100], [4, 5, 6], [7, 8, 9]]), | 32 expect( |
| 22 equals([[1, 4, 7], [2, 5, 8], [3, 6, 9]])); | 33 new IterableZip([ |
| 34 [1, 2, 3, 99, 100], |
| 35 [4, 5, 6], |
| 36 [7, 8, 9] |
| 37 ]), |
| 38 equals([ |
| 39 [1, 4, 7], |
| 40 [2, 5, 8], |
| 41 [3, 6, 9] |
| 42 ])); |
| 23 }); | 43 }); |
| 24 | 44 |
| 25 test("Uneven length 2", () { | 45 test("Uneven length 2", () { |
| 26 expect(new IterableZip([[1, 2, 3], [4, 5, 6, 99, 100], [7, 8, 9]]), | 46 expect( |
| 27 equals([[1, 4, 7], [2, 5, 8], [3, 6, 9]])); | 47 new IterableZip([ |
| 48 [1, 2, 3], |
| 49 [4, 5, 6, 99, 100], |
| 50 [7, 8, 9] |
| 51 ]), |
| 52 equals([ |
| 53 [1, 4, 7], |
| 54 [2, 5, 8], |
| 55 [3, 6, 9] |
| 56 ])); |
| 28 }); | 57 }); |
| 29 | 58 |
| 30 test("Uneven length 3", () { | 59 test("Uneven length 3", () { |
| 31 expect(new IterableZip([[1, 2, 3], [4, 5, 6], [7, 8, 9, 99, 100]]), | 60 expect( |
| 32 equals([[1, 4, 7], [2, 5, 8], [3, 6, 9]])); | 61 new IterableZip([ |
| 62 [1, 2, 3], |
| 63 [4, 5, 6], |
| 64 [7, 8, 9, 99, 100] |
| 65 ]), |
| 66 equals([ |
| 67 [1, 4, 7], |
| 68 [2, 5, 8], |
| 69 [3, 6, 9] |
| 70 ])); |
| 33 }); | 71 }); |
| 34 | 72 |
| 35 test("Uneven length 3", () { | 73 test("Uneven length 3", () { |
| 36 expect(new IterableZip([[1, 2, 3, 98], [4, 5, 6], [7, 8, 9, 99, 100]]), | 74 expect( |
| 37 equals([[1, 4, 7], [2, 5, 8], [3, 6, 9]])); | 75 new IterableZip([ |
| 76 [1, 2, 3, 98], |
| 77 [4, 5, 6], |
| 78 [7, 8, 9, 99, 100] |
| 79 ]), |
| 80 equals([ |
| 81 [1, 4, 7], |
| 82 [2, 5, 8], |
| 83 [3, 6, 9] |
| 84 ])); |
| 38 }); | 85 }); |
| 39 | 86 |
| 40 test("Empty 1", () { | 87 test("Empty 1", () { |
| 41 expect(new IterableZip([[], [4, 5, 6], [7, 8, 9]]), equals([])); | 88 expect( |
| 89 new IterableZip([ |
| 90 [], |
| 91 [4, 5, 6], |
| 92 [7, 8, 9] |
| 93 ]), |
| 94 equals([])); |
| 42 }); | 95 }); |
| 43 | 96 |
| 44 test("Empty 2", () { | 97 test("Empty 2", () { |
| 45 expect(new IterableZip([[1, 2, 3], [], [7, 8, 9]]), equals([])); | 98 expect( |
| 99 new IterableZip([ |
| 100 [1, 2, 3], |
| 101 [], |
| 102 [7, 8, 9] |
| 103 ]), |
| 104 equals([])); |
| 46 }); | 105 }); |
| 47 | 106 |
| 48 test("Empty 3", () { | 107 test("Empty 3", () { |
| 49 expect(new IterableZip([[1, 2, 3], [4, 5, 6], []]), equals([])); | 108 expect( |
| 109 new IterableZip([ |
| 110 [1, 2, 3], |
| 111 [4, 5, 6], |
| 112 [] |
| 113 ]), |
| 114 equals([])); |
| 50 }); | 115 }); |
| 51 | 116 |
| 52 test("Empty source", () { | 117 test("Empty source", () { |
| 53 expect(new IterableZip([]), equals([])); | 118 expect(new IterableZip([]), equals([])); |
| 54 }); | 119 }); |
| 55 | 120 |
| 56 test("Single Source", () { | 121 test("Single Source", () { |
| 57 expect(new IterableZip([[1, 2, 3]]), equals([[1], [2], [3]])); | 122 expect( |
| 123 new IterableZip([ |
| 124 [1, 2, 3] |
| 125 ]), |
| 126 equals([ |
| 127 [1], |
| 128 [2], |
| 129 [3] |
| 130 ])); |
| 58 }); | 131 }); |
| 59 | 132 |
| 60 test("Not-lists", () { | 133 test("Not-lists", () { |
| 61 // Use other iterables than list literals. | 134 // Use other iterables than list literals. |
| 62 Iterable it1 = [1, 2, 3, 4, 5, 6].where((x) => x < 4); | 135 Iterable it1 = [1, 2, 3, 4, 5, 6].where((x) => x < 4); |
| 63 Set it2 = new LinkedHashSet()..add(4)..add(5)..add(6); | 136 Set it2 = new LinkedHashSet()..add(4)..add(5)..add(6); |
| 64 Iterable it3 = (new LinkedHashMap()..[7] = 0 ..[8] = 0 ..[9] = 0).keys; | 137 Iterable it3 = (new LinkedHashMap() |
| 138 ..[7] = 0 |
| 139 ..[8] = 0 |
| 140 ..[9] = 0) |
| 141 .keys; |
| 65 Iterable<Iterable> allIts = | 142 Iterable<Iterable> allIts = |
| 66 new Iterable.generate(3, (i) => [it1, it2, it3][i]); | 143 new Iterable.generate(3, (i) => [it1, it2, it3][i]); |
| 67 expect(new IterableZip(allIts), | 144 expect( |
| 68 equals([[1, 4, 7], [2, 5, 8], [3, 6, 9]])); | 145 new IterableZip(allIts), |
| 146 equals([ |
| 147 [1, 4, 7], |
| 148 [2, 5, 8], |
| 149 [3, 6, 9] |
| 150 ])); |
| 69 }); | 151 }); |
| 70 | 152 |
| 71 test("Error 1", () { | 153 test("Error 1", () { |
| 72 expect(() => new IterableZip([iterError([1, 2, 3], 2), | 154 expect( |
| 73 [4, 5, 6], | 155 () => new IterableZip([ |
| 74 [7, 8, 9]]).toList(), | 156 iterError([1, 2, 3], 2), |
| 75 throwsA(equals("BAD"))); | 157 [4, 5, 6], |
| 158 [7, 8, 9] |
| 159 ]).toList(), |
| 160 throwsA(equals("BAD"))); |
| 76 }); | 161 }); |
| 77 | 162 |
| 78 test("Error 2", () { | 163 test("Error 2", () { |
| 79 expect(() => new IterableZip([[1, 2, 3], | 164 expect( |
| 80 iterError([4, 5, 6], 5), | 165 () => new IterableZip([ |
| 81 [7, 8, 9]]).toList(), | 166 [1, 2, 3], |
| 82 throwsA(equals("BAD"))); | 167 iterError([4, 5, 6], 5), |
| 168 [7, 8, 9] |
| 169 ]).toList(), |
| 170 throwsA(equals("BAD"))); |
| 83 }); | 171 }); |
| 84 | 172 |
| 85 test("Error 3", () { | 173 test("Error 3", () { |
| 86 expect(() => new IterableZip([[1, 2, 3], | 174 expect( |
| 87 [4, 5, 6], | 175 () => new IterableZip([ |
| 88 iterError([7, 8, 9], 8)]).toList(), | 176 [1, 2, 3], |
| 89 throwsA(equals("BAD"))); | 177 [4, 5, 6], |
| 178 iterError([7, 8, 9], 8) |
| 179 ]).toList(), |
| 180 throwsA(equals("BAD"))); |
| 90 }); | 181 }); |
| 91 | 182 |
| 92 test("Error at end", () { | 183 test("Error at end", () { |
| 93 expect(() => new IterableZip([[1, 2, 3], | 184 expect( |
| 94 iterError([4, 5, 6], 6), | 185 () => new IterableZip([ |
| 95 [7, 8, 9]]).toList(), | 186 [1, 2, 3], |
| 96 throwsA(equals("BAD"))); | 187 iterError([4, 5, 6], 6), |
| 188 [7, 8, 9] |
| 189 ]).toList(), |
| 190 throwsA(equals("BAD"))); |
| 97 }); | 191 }); |
| 98 | 192 |
| 99 test("Error before first end", () { | 193 test("Error before first end", () { |
| 100 expect(() => new IterableZip([iterError([1, 2, 3, 4], 4), | 194 expect( |
| 101 [4, 5, 6], | 195 () => new IterableZip([ |
| 102 [7, 8, 9]]).toList(), | 196 iterError([1, 2, 3, 4], 4), |
| 103 throwsA(equals("BAD"))); | 197 [4, 5, 6], |
| 198 [7, 8, 9] |
| 199 ]).toList(), |
| 200 throwsA(equals("BAD"))); |
| 104 }); | 201 }); |
| 105 | 202 |
| 106 test("Error after first end", () { | 203 test("Error after first end", () { |
| 107 expect(new IterableZip([[1, 2, 3], | 204 expect( |
| 108 [4, 5, 6], | 205 new IterableZip([ |
| 109 iterError([7, 8, 9, 10], 10)]), | 206 [1, 2, 3], |
| 110 equals([[1, 4, 7], [2, 5, 8], [3, 6, 9]])); | 207 [4, 5, 6], |
| 208 iterError([7, 8, 9, 10], 10) |
| 209 ]), |
| 210 equals([ |
| 211 [1, 4, 7], |
| 212 [2, 5, 8], |
| 213 [3, 6, 9] |
| 214 ])); |
| 111 }); | 215 }); |
| 112 } | 216 } |
| OLD | NEW |