| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /// Tests wrapper utilities. | |
| 6 | |
| 7 import "dart:collection"; | |
| 8 import "package:collection_helpers/all.dart"; | |
| 9 import "package:unittest/unittest.dart"; | |
| 10 | |
| 11 // Test that any member access/call on the wrapper object is equal to | |
| 12 // an expected access on the wrapped object. | |
| 13 // This is implemented by capturing accesses using noSuchMethod and comparing | |
| 14 // them to expected accesses captured previously. | |
| 15 | |
| 16 // Compare two Invocations for having equal type and arguments. | |
| 17 void testInvocations(Invocation i1, Invocation i2) { | |
| 18 String name = "${i1.memberName}"; | |
| 19 expect(i1.isGetter, equals(i2.isGetter), reason: name); | |
| 20 expect(i1.isSetter, equals(i2.isSetter), reason: name); | |
| 21 expect(i1.memberName, equals(i2.memberName), reason: name); | |
| 22 expect(i1.positionalArguments, equals(i2.positionalArguments), reason: name); | |
| 23 expect(i1.namedArguments, equals(i2.namedArguments), reason: name); | |
| 24 } | |
| 25 | |
| 26 /** | |
| 27 * Utility class to record a member access and a member access on a wrapped | |
| 28 * object, and compare them for equality. | |
| 29 */ | |
| 30 abstract class Expector { | |
| 31 getWrappedObject(action(Invocation i)); | |
| 32 // Hack to test assignment ([]=) because it doesn't return the result | |
| 33 // of the member call. Instead use (expect..[4]=5).equal[4]=5 where | |
| 34 // you would normally use expect[4].equals[4] for non-assignments. | |
| 35 var equals; | |
| 36 | |
| 37 noSuchMethod(Invocation m) => new _Equals(equals = getWrappedObject((m2) { | |
| 38 testInvocations(m, m2); | |
| 39 })); | |
| 40 } | |
| 41 | |
| 42 // An object with a field called "equals", only introduced into the | |
| 43 // flow to allow writing expect.xxx.equals.xxx. | |
| 44 class _Equals { | |
| 45 final equals; | |
| 46 _Equals(this.equals); | |
| 47 } | |
| 48 | |
| 49 // Parameterization of noSuchMethod. | |
| 50 class NSM { | |
| 51 Function _action; | |
| 52 NSM(this._action); | |
| 53 noSuchMethod(Invocation i) => _action(i); | |
| 54 } | |
| 55 | |
| 56 // LikeNSM, but has types Iterable, Set and List to allow it as | |
| 57 // argument to DelegatingIterable/Set/List. | |
| 58 class IterableNSM extends NSM implements Iterable, Set, List, Queue { | |
| 59 IterableNSM(action(Invocation i)) : super(action); | |
| 60 noSuchMethod(Invocation i) => super.noSuchMethod(i); // Silence warnings | |
| 61 } | |
| 62 | |
| 63 // Expector that wraps in DelegatingIterable. | |
| 64 class IterableExpector extends Expector { | |
| 65 getWrappedObject(void action(Invocation i)) { | |
| 66 return new DelegatingIterable(new IterableNSM(action)); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 // Expector that wraps in DelegatingList. | |
| 71 class ListExpector extends Expector { | |
| 72 getWrappedObject(void action(Invocation i)) { | |
| 73 return new DelegatingList(new IterableNSM(action)); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 // Expector that wraps in DelegatingSet. | |
| 78 class SetExpector extends Expector { | |
| 79 getWrappedObject(void action(Invocation i)) { | |
| 80 return new DelegatingSet(new IterableNSM(action)); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 // Expector that wraps in DelegatingSet. | |
| 85 class QueueExpector extends Expector { | |
| 86 getWrappedObject(void action(Invocation i)) { | |
| 87 return new DelegatingQueue(new IterableNSM(action)); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 // Like NSM but implements Map to allow as argument for DelegatingMap. | |
| 92 class MapNSM extends NSM implements Map { | |
| 93 MapNSM(action(Invocation i)) : super(action); | |
| 94 noSuchMethod(Invocation i) => super.noSuchMethod(i); | |
| 95 } | |
| 96 | |
| 97 // Expector that wraps in DelegatingMap. | |
| 98 class MapExpector extends Expector { | |
| 99 getWrappedObject(void action(Invocation i)) { | |
| 100 return new DelegatingMap(new MapNSM(action)); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 // Utility values to use as arguments in calls. | |
| 105 func0() {} | |
| 106 func1(x) {} | |
| 107 func2(x, y) {} | |
| 108 var val = new Object(); | |
| 109 | |
| 110 void main() { | |
| 111 testIterable(var expect) { | |
| 112 expect.any(func1).equals.any(func1); | |
| 113 expect.contains(val).equals.contains(val); | |
| 114 expect.elementAt(0).equals.elementAt(0); | |
| 115 expect.every(func1).equals.every(func1); | |
| 116 expect.expand(func1).equals.expand(func1); | |
| 117 expect.first.equals.first; | |
| 118 // Default values of the Iterable interface will be added in the | |
| 119 // second call to firstWhere, so we must record them in our | |
| 120 // expectation (which doesn't have the interface implementat or | |
| 121 // its default values). | |
| 122 expect.firstWhere(func1, orElse: null).equals.firstWhere(func1); | |
| 123 expect.firstWhere(func1, orElse: func0).equals. | |
| 124 firstWhere(func1, orElse: func0); | |
| 125 expect.fold(null, func2).equals.fold(null, func2); | |
| 126 expect.forEach(func1).equals.forEach(func1); | |
| 127 expect.isEmpty.equals.isEmpty; | |
| 128 expect.isNotEmpty.equals.isNotEmpty; | |
| 129 expect.iterator.equals.iterator; | |
| 130 expect.join('').equals.join(); | |
| 131 expect.join("X").equals.join("X"); | |
| 132 expect.last.equals.last; | |
| 133 expect.lastWhere(func1, orElse: null).equals.lastWhere(func1); | |
| 134 expect.lastWhere(func1, orElse: func0).equals. | |
| 135 lastWhere(func1, orElse: func0); | |
| 136 expect.length.equals.length; | |
| 137 expect.map(func1).equals.map(func1); | |
| 138 expect.reduce(func2).equals.reduce(func2); | |
| 139 expect.single.equals.single; | |
| 140 expect.singleWhere(func1).equals.singleWhere(func1); | |
| 141 expect.skip(5).equals.skip(5); | |
| 142 expect.skipWhile(func1).equals.skipWhile(func1); | |
| 143 expect.take(5).equals.take(5); | |
| 144 expect.takeWhile(func1).equals.takeWhile(func1); | |
| 145 expect.toList(growable: true).equals.toList(); | |
| 146 expect.toList(growable: true).equals.toList(growable: true); | |
| 147 expect.toList(growable: false).equals.toList(growable: false); | |
| 148 expect.toSet().equals.toSet(); | |
| 149 expect.where(func1).equals.where(func1); | |
| 150 } | |
| 151 | |
| 152 void testList(var expect) { | |
| 153 testIterable(expect); | |
| 154 | |
| 155 expect[4].equals[4]; | |
| 156 (expect..[4] = 5).equals[4] = 5; | |
| 157 | |
| 158 expect.add(val).equals.add(val); | |
| 159 expect.addAll([val]).equals.addAll([val]); | |
| 160 expect.asMap().equals.asMap(); | |
| 161 expect.clear().equals.clear(); | |
| 162 expect.fillRange(4, 5, null).equals.fillRange(4, 5); | |
| 163 expect.fillRange(4, 5, val).equals.fillRange(4, 5, val); | |
| 164 expect.getRange(4, 5).equals.getRange(4, 5); | |
| 165 expect.indexOf(val, 0).equals.indexOf(val); | |
| 166 expect.indexOf(val, 4).equals.indexOf(val, 4); | |
| 167 expect.insert(4, val).equals.insert(4, val); | |
| 168 expect.insertAll(4, [val]).equals.insertAll(4, [val]); | |
| 169 expect.lastIndexOf(val, null).equals.lastIndexOf(val); | |
| 170 expect.lastIndexOf(val, 4).equals.lastIndexOf(val, 4); | |
| 171 (expect..length = 4).equals.length = 4; | |
| 172 expect.remove(val).equals.remove(val); | |
| 173 expect.removeAt(4).equals.removeAt(4); | |
| 174 expect.removeLast().equals.removeLast(); | |
| 175 expect.removeRange(4, 5).equals.removeRange(4, 5); | |
| 176 expect.removeWhere(func1).equals.removeWhere(func1); | |
| 177 expect.replaceRange(4, 5, [val]).equals.replaceRange(4, 5, [val]); | |
| 178 expect.retainWhere(func1).equals.retainWhere(func1); | |
| 179 expect.reversed.equals.reversed; | |
| 180 expect.setAll(4, [val]).equals.setAll(4, [val]); | |
| 181 expect.setRange(4, 5, [val], 0).equals.setRange(4, 5, [val]); | |
| 182 expect.setRange(4, 5, [val], 3).equals.setRange(4, 5, [val], 3); | |
| 183 expect.sort(null).equals.sort(); | |
| 184 expect.sort(func2).equals.sort(func2); | |
| 185 expect.sublist(4, null).equals.sublist(4); | |
| 186 expect.sublist(4, 5).equals.sublist(4, 5); | |
| 187 } | |
| 188 | |
| 189 void testSet(var expect) { | |
| 190 testIterable(expect); | |
| 191 Set set = new Set(); | |
| 192 expect.add(val).equals.add(val); | |
| 193 expect.addAll([val]).equals.addAll([val]); | |
| 194 expect.clear().equals.clear(); | |
| 195 expect.containsAll([val]).equals.containsAll([val]); | |
| 196 expect.difference(set).equals.difference(set); | |
| 197 expect.intersection(set).equals.intersection(set); | |
| 198 expect.remove(val).equals.remove(val); | |
| 199 expect.removeAll([val]).equals.removeAll([val]); | |
| 200 expect.removeWhere(func1).equals.removeWhere(func1); | |
| 201 expect.retainAll([val]).equals.retainAll([val]); | |
| 202 expect.retainWhere(func1).equals.retainWhere(func1); | |
| 203 expect.union(set).equals.union(set); | |
| 204 } | |
| 205 | |
| 206 void testQueue(var expect) { | |
| 207 testIterable(expect); | |
| 208 expect.add(val).equals.add(val); | |
| 209 expect.addAll([val]).equals.addAll([val]); | |
| 210 expect.addFirst(val).equals.addFirst(val); | |
| 211 expect.addLast(val).equals.addLast(val); | |
| 212 expect.clear().equals.clear(); | |
| 213 expect.remove(val).equals.remove(val); | |
| 214 expect.removeFirst().equals.removeFirst(); | |
| 215 expect.removeLast().equals.removeLast(); | |
| 216 } | |
| 217 | |
| 218 void testMap(var expect) { | |
| 219 Map map = new Map(); | |
| 220 expect[val].equals[val]; | |
| 221 (expect..[val] = val).equals[val] = val; | |
| 222 expect.addAll(map).equals.addAll(map); | |
| 223 expect.clear().equals.clear(); | |
| 224 expect.containsKey(val).equals.containsKey(val); | |
| 225 expect.containsValue(val).equals.containsValue(val); | |
| 226 expect.forEach(func2).equals.forEach(func2); | |
| 227 expect.isEmpty.equals.isEmpty; | |
| 228 expect.isNotEmpty.equals.isNotEmpty; | |
| 229 expect.keys.equals.keys; | |
| 230 expect.length.equals.length; | |
| 231 expect.putIfAbsent(val, func0).equals.putIfAbsent(val, func0); | |
| 232 expect.remove(val).equals.remove(val); | |
| 233 expect.values.equals.values; | |
| 234 } | |
| 235 | |
| 236 test("Iterable", () { | |
| 237 testIterable(new IterableExpector()); | |
| 238 }); | |
| 239 | |
| 240 test("List", () { | |
| 241 testList(new ListExpector()); | |
| 242 }); | |
| 243 | |
| 244 test("Set", () { | |
| 245 testSet(new SetExpector()); | |
| 246 }); | |
| 247 | |
| 248 test("Queue", () { | |
| 249 testQueue(new QueueExpector()); | |
| 250 }); | |
| 251 | |
| 252 test("Map", () { | |
| 253 testMap(new MapExpector()); | |
| 254 }); | |
| 255 } | |
| OLD | NEW |