Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Side by Side Diff: pkg/observe/test/observable_map_test.dart

Issue 27618002: package:observe fix various api issues (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/observe/test/observable_list_test.dart ('k') | pkg/observe/test/observe_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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:async'; 5 import 'dart:async';
6 import 'package:observe/observe.dart'; 6 import 'package:observe/observe.dart';
7 import 'package:unittest/unittest.dart'; 7 import 'package:unittest/unittest.dart';
8 import 'observe_test_utils.dart'; 8 import 'observe_test_utils.dart';
9 9
10 main() { 10 main() {
11 // TODO(jmesserly): need all standard Map API tests. 11 // TODO(jmesserly): need all standard Map API tests.
12 12
13 StreamSubscription sub; 13 StreamSubscription sub;
14 14
15 sharedTearDown() { 15 sharedTearDown() {
16 if (sub != null) { 16 if (sub != null) {
17 sub.cancel(); 17 sub.cancel();
18 sub = null; 18 sub = null;
19 } 19 }
20 } 20 }
21 21
22 group('observe length', () { 22 group('observe length', () {
23 ObservableMap map; 23 ObservableMap map;
24 List<ChangeRecord> changes; 24 List<ChangeRecord> changes;
25 25
26 setUp(() { 26 setUp(() {
27 map = toObservable({'a': 1, 'b': 2, 'c': 3}); 27 map = toObservable({'a': 1, 'b': 2, 'c': 3});
28 changes = null; 28 changes = null;
29 sub = map.changes.listen((records) { 29 sub = map.changes.listen((records) {
30 changes = records.where((r) => r.changes(#length)).toList(); 30 changes = getPropertyChangeRecords(records, #length);
31 }); 31 });
32 }); 32 });
33 33
34 tearDown(sharedTearDown); 34 tearDown(sharedTearDown);
35 35
36 observeTest('add item changes length', () { 36 observeTest('add item changes length', () {
37 map['d'] = 4; 37 map['d'] = 4;
38 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4}); 38 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4});
39 performMicrotaskCheckpoint(); 39 performMicrotaskCheckpoint();
40 expectChanges(changes, [_lengthChange]); 40 expectChanges(changes, [_lengthChange(map, 3, 4)]);
41 }); 41 });
42 42
43 observeTest('putIfAbsent changes length', () { 43 observeTest('putIfAbsent changes length', () {
44 map.putIfAbsent('d', () => 4); 44 map.putIfAbsent('d', () => 4);
45 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4}); 45 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4});
46 performMicrotaskCheckpoint(); 46 performMicrotaskCheckpoint();
47 expectChanges(changes, [_lengthChange]); 47 expectChanges(changes, [_lengthChange(map, 3, 4)]);
48 }); 48 });
49 49
50 observeTest('remove changes length', () { 50 observeTest('remove changes length', () {
51 map.remove('c'); 51 map.remove('c');
52 map.remove('a'); 52 map.remove('a');
53 expect(map, {'b': 2}); 53 expect(map, {'b': 2});
54 performMicrotaskCheckpoint(); 54 performMicrotaskCheckpoint();
55 expectChanges(changes, [_lengthChange, _lengthChange]); 55 expectChanges(changes, [
56 _lengthChange(map, 3, 2),
57 _lengthChange(map, 2, 1)
58 ]);
56 }); 59 });
57 60
58 observeTest('remove non-existent item does not change length', () { 61 observeTest('remove non-existent item does not change length', () {
59 map.remove('d'); 62 map.remove('d');
60 expect(map, {'a': 1, 'b': 2, 'c': 3}); 63 expect(map, {'a': 1, 'b': 2, 'c': 3});
61 performMicrotaskCheckpoint(); 64 performMicrotaskCheckpoint();
62 expectChanges(changes, null); 65 expectChanges(changes, null);
63 }); 66 });
64 67
65 observeTest('set existing item does not change length', () { 68 observeTest('set existing item does not change length', () {
66 map['c'] = 9000; 69 map['c'] = 9000;
67 expect(map, {'a': 1, 'b': 2, 'c': 9000}); 70 expect(map, {'a': 1, 'b': 2, 'c': 9000});
68 performMicrotaskCheckpoint(); 71 performMicrotaskCheckpoint();
69 expectChanges(changes, []); 72 expectChanges(changes, []);
70 }); 73 });
71 74
72 observeTest('clear changes length', () { 75 observeTest('clear changes length', () {
73 map.clear(); 76 map.clear();
74 expect(map, {}); 77 expect(map, {});
75 performMicrotaskCheckpoint(); 78 performMicrotaskCheckpoint();
76 expectChanges(changes, [_lengthChange]); 79 expectChanges(changes, [_lengthChange(map, 3, 0)]);
77 }); 80 });
78 }); 81 });
79 82
80 group('observe item', () { 83 group('observe item', () {
81 84
82 ObservableMap map; 85 ObservableMap map;
83 List<ChangeRecord> changes; 86 List<ChangeRecord> changes;
84 87
85 setUp(() { 88 setUp(() {
86 map = toObservable({'a': 1, 'b': 2, 'c': 3}); 89 map = toObservable({'a': 1, 'b': 2, 'c': 3});
87 changes = null; 90 changes = null;
88 sub = map.changes.listen((records) { 91 sub = map.changes.listen((records) {
89 changes = records.where((r) => r.changes('b')).toList(); 92 changes = records.where((r) => r is MapChangeRecord && r.key == 'b')
93 .toList();
90 }); 94 });
91 }); 95 });
92 96
93 tearDown(sharedTearDown); 97 tearDown(sharedTearDown);
94 98
95 observeTest('putIfAbsent new item does not change existing item', () { 99 observeTest('putIfAbsent new item does not change existing item', () {
96 map.putIfAbsent('d', () => 4); 100 map.putIfAbsent('d', () => 4);
97 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4}); 101 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4});
98 performMicrotaskCheckpoint(); 102 performMicrotaskCheckpoint();
99 expectChanges(changes, []); 103 expectChanges(changes, []);
100 }); 104 });
101 105
102 observeTest('set item to null', () { 106 observeTest('set item to null', () {
103 map['b'] = null; 107 map['b'] = null;
104 expect(map, {'a': 1, 'b': null, 'c': 3}); 108 expect(map, {'a': 1, 'b': null, 'c': 3});
105 performMicrotaskCheckpoint(); 109 performMicrotaskCheckpoint();
106 expectChanges(changes, [_change('b')]); 110 expectChanges(changes, [_changeKey('b', 2, null)]);
107 }); 111 });
108 112
109 observeTest('set item to value', () { 113 observeTest('set item to value', () {
110 map['b'] = 777; 114 map['b'] = 777;
111 expect(map, {'a': 1, 'b': 777, 'c': 3}); 115 expect(map, {'a': 1, 'b': 777, 'c': 3});
112 performMicrotaskCheckpoint(); 116 performMicrotaskCheckpoint();
113 expectChanges(changes, [_change('b')]); 117 expectChanges(changes, [_changeKey('b', 2, 777)]);
114 }); 118 });
115 119
116 observeTest('putIfAbsent does not change if already there', () { 120 observeTest('putIfAbsent does not change if already there', () {
117 map.putIfAbsent('b', () => 1234); 121 map.putIfAbsent('b', () => 1234);
118 expect(map, {'a': 1, 'b': 2, 'c': 3}); 122 expect(map, {'a': 1, 'b': 2, 'c': 3});
119 performMicrotaskCheckpoint(); 123 performMicrotaskCheckpoint();
120 expectChanges(changes, null); 124 expectChanges(changes, null);
121 }); 125 });
122 126
123 observeTest('change a different item', () { 127 observeTest('change a different item', () {
124 map['c'] = 9000; 128 map['c'] = 9000;
125 expect(map, {'a': 1, 'b': 2, 'c': 9000}); 129 expect(map, {'a': 1, 'b': 2, 'c': 9000});
126 performMicrotaskCheckpoint(); 130 performMicrotaskCheckpoint();
127 expectChanges(changes, []); 131 expectChanges(changes, []);
128 }); 132 });
129 133
130 observeTest('change the item', () { 134 observeTest('change the item', () {
131 map['b'] = 9001; 135 map['b'] = 9001;
132 map['b'] = 42; 136 map['b'] = 42;
133 expect(map, {'a': 1, 'b': 42, 'c': 3}); 137 expect(map, {'a': 1, 'b': 42, 'c': 3});
134 performMicrotaskCheckpoint(); 138 performMicrotaskCheckpoint();
135 expectChanges(changes, [_change('b'), _change('b')]); 139 expectChanges(changes, [
140 _changeKey('b', 2, 9001),
141 _changeKey('b', 9001, 42)
142 ]);
136 }); 143 });
137 144
138 observeTest('remove other items', () { 145 observeTest('remove other items', () {
139 map.remove('a'); 146 map.remove('a');
140 expect(map, {'b': 2, 'c': 3}); 147 expect(map, {'b': 2, 'c': 3});
141 performMicrotaskCheckpoint(); 148 performMicrotaskCheckpoint();
142 expectChanges(changes, []); 149 expectChanges(changes, []);
143 }); 150 });
144 151
145 observeTest('remove the item', () { 152 observeTest('remove the item', () {
146 map.remove('b'); 153 map.remove('b');
147 expect(map, {'a': 1, 'c': 3}); 154 expect(map, {'a': 1, 'c': 3});
148 performMicrotaskCheckpoint(); 155 performMicrotaskCheckpoint();
149 expectChanges(changes, [_change('b', isRemove: true)]); 156 expectChanges(changes, [_removeKey('b', 2)]);
150 }); 157 });
151 158
152 observeTest('remove and add back', () { 159 observeTest('remove and add back', () {
153 map.remove('b'); 160 map.remove('b');
154 map['b'] = 2; 161 map['b'] = 2;
155 expect(map, {'a': 1, 'b': 2, 'c': 3}); 162 expect(map, {'a': 1, 'b': 2, 'c': 3});
156 performMicrotaskCheckpoint(); 163 performMicrotaskCheckpoint();
157 expectChanges(changes, 164 expectChanges(changes,
158 [_change('b', isRemove: true), _change('b', isInsert: true)]); 165 [_removeKey('b', 2), _insertKey('b', 2)]);
159 }); 166 });
160 }); 167 });
161 168
162 observeTest('toString', () { 169 observeTest('toString', () {
163 var map = toObservable({'a': 1, 'b': 2}); 170 var map = toObservable({'a': 1, 'b': 2});
164 expect(map.toString(), '{a: 1, b: 2}'); 171 expect(map.toString(), '{a: 1, b: 2}');
165 }); 172 });
166 173
167 group('change records', () { 174 group('change records', () {
168 List<ChangeRecord> records; 175 List<ChangeRecord> records;
(...skipping 30 matching lines...) Expand all
199 206
200 observeTest('putIfAbsent', () { 207 observeTest('putIfAbsent', () {
201 map.putIfAbsent('a', () => 42); 208 map.putIfAbsent('a', () => 42);
202 expect(map, {'a': 1, 'b': 2}); 209 expect(map, {'a': 1, 'b': 2});
203 210
204 map.putIfAbsent('c', () => 3); 211 map.putIfAbsent('c', () => 3);
205 expect(map, {'a': 1, 'b': 2, 'c': 3}); 212 expect(map, {'a': 1, 'b': 2, 'c': 3});
206 213
207 performMicrotaskCheckpoint(); 214 performMicrotaskCheckpoint();
208 expectChanges(records, [ 215 expectChanges(records, [
209 _lengthChange, 216 _lengthChange(map, 2, 3),
210 _change('c', isInsert: true), 217 _insertKey('c', 3),
211 ]); 218 ]);
212 }); 219 });
213 220
214 observeTest('[]=', () { 221 observeTest('[]=', () {
215 map['a'] = 42; 222 map['a'] = 42;
216 expect(map, {'a': 42, 'b': 2}); 223 expect(map, {'a': 42, 'b': 2});
217 224
218 map['c'] = 3; 225 map['c'] = 3;
219 expect(map, {'a': 42, 'b': 2, 'c': 3}); 226 expect(map, {'a': 42, 'b': 2, 'c': 3});
220 227
221 performMicrotaskCheckpoint(); 228 performMicrotaskCheckpoint();
222 expectChanges(records, [ 229 expectChanges(records, [
223 _change('a'), 230 _changeKey('a', 1, 42),
224 _lengthChange, 231 _lengthChange(map, 2, 3),
225 _change('c', isInsert: true) 232 _insertKey('c', 3)
226 ]); 233 ]);
227 }); 234 });
228 235
229 observeTest('remove', () { 236 observeTest('remove', () {
230 map.remove('b'); 237 map.remove('b');
231 expect(map, {'a': 1}); 238 expect(map, {'a': 1});
232 239
233 performMicrotaskCheckpoint(); 240 performMicrotaskCheckpoint();
234 expectChanges(records, [ 241 expectChanges(records, [
235 _change('b', isRemove: true), 242 _removeKey('b', 2),
236 _lengthChange, 243 _lengthChange(map, 2, 1),
237 ]); 244 ]);
238 }); 245 });
239 246
240 observeTest('clear', () { 247 observeTest('clear', () {
241 map.clear(); 248 map.clear();
242 expect(map, {}); 249 expect(map, {});
243 250
244 performMicrotaskCheckpoint(); 251 performMicrotaskCheckpoint();
245 expectChanges(records, [ 252 expectChanges(records, [
246 _change('a', isRemove: true), 253 _removeKey('a', 1),
247 _change('b', isRemove: true), 254 _removeKey('b', 2),
248 _lengthChange, 255 _lengthChange(map, 2, 0),
249 ]); 256 ]);
250 }); 257 });
251 }); 258 });
252 } 259 }
253 260
254 final _lengthChange = new PropertyChangeRecord(#length); 261 _lengthChange(map, int oldValue, int newValue) =>
262 new PropertyChangeRecord(map, #length, oldValue, newValue);
255 263
256 _change(key, {isInsert: false, isRemove: false}) => 264 _changeKey(key, old, newValue) => new MapChangeRecord(key, old, newValue);
257 new MapChangeRecord(key, isInsert: isInsert, isRemove: isRemove); 265
266 _insertKey(key, newValue) => new MapChangeRecord.insert(key, newValue);
267
268 _removeKey(key, oldValue) => new MapChangeRecord.remove(key, oldValue);
OLDNEW
« no previous file with comments | « pkg/observe/test/observable_list_test.dart ('k') | pkg/observe/test/observe_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698