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

Side by Side Diff: tests/lib/observe/observable_map_test.dart

Issue 14732003: Implement Model-Driven-Views spec for Dart (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: small fix Created 7 years, 7 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
OLDNEW
(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 import '../../../pkg/unittest/lib/unittest.dart';
6 import '../../../sdk/lib/observe/observe.dart';
7
8 main() {
9 // TODO(jmesserly): need all standard Map API tests.
10
11 group('observe length', () {
12
13 ObservableMap map;
14 List<ChangeRecord> changes;
15
16 setUp(() {
17 map = toObservable({'a': 1, 'b': 2, 'c': 3});
18 changes = null;
19 map.changes.listen((records) {
20 changes = records.where((r) => r.key == 'length' &&
21 r.kind == ChangeRecord.FIELD).toList();
22 });
23 });
24
25 _change(x, y) => _record('length', x, y);
26
27 test('add item changes length', () {
28 map['d'] = 4;
29 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4});
30 deliverChangeRecords();
31 expect(changes, [_change(3, 4)]);
32 });
33
34 test('putIfAbsent changes length', () {
35 map.putIfAbsent('d', () => 4);
36 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4});
37 deliverChangeRecords();
38 expect(changes, [_change(3, 4)]);
39 });
40
41 test('remove changes length', () {
42 map.remove('c');
43 map.remove('a');
44 expect(map, {'b': 2});
45 deliverChangeRecords();
46 expect(changes, [_change(3, 2), _change(2, 1)]);
47 });
48
49 test('remove non-existent item does not change length', () {
50 map.remove('d');
51 expect(map, {'a': 1, 'b': 2, 'c': 3});
52 deliverChangeRecords();
53 expect(changes, null);
54 });
55
56 test('set existing item does not change length', () {
57 map['c'] = 9000;
58 expect(map, {'a': 1, 'b': 2, 'c': 9000});
59 deliverChangeRecords();
60 expect(changes, []);
61 });
62
63 test('clear changes length', () {
64 map.clear();
65 expect(map, {});
66 deliverChangeRecords();
67 expect(changes, [_change(3, 0)]);
68 });
69 });
70
71 group('observe item', () {
72
73 ObservableMap map;
74 List<ChangeRecord> changes;
75
76 setUp(() {
77 map = toObservable({'a': 1, 'b': 2, 'c': 3});
78 changes = null;
79 map.changes.listen((records) {
80 changes = records.where((r) => r.key == 'b' &&
81 (r.kind & ChangeRecord.INDEX) != 0).toList();
82 });
83 });
84
85 _change(x, y, {kind: ChangeRecord.INDEX}) => _record('b', x, y, kind);
86
87 test('putIfAbsent new item does not change existing item', () {
88 map.putIfAbsent('d', () => 4);
89 expect(map, {'a': 1, 'b': 2, 'c': 3, 'd': 4});
90 deliverChangeRecords();
91 expect(changes, []);
92 });
93
94 test('set item to null', () {
95 map['b'] = null;
96 expect(map, {'a': 1, 'b': null, 'c': 3});
97 deliverChangeRecords();
98 expect(changes, [_change(2, null)]);
99 });
100
101 test('set item to value', () {
102 map['b'] = 777;
103 expect(map, {'a': 1, 'b': 777, 'c': 3});
104 deliverChangeRecords();
105 expect(changes, [_change(2, 777)]);
106 });
107
108 test('putIfAbsent does not change if already there', () {
109 map.putIfAbsent('b', () => 1234);
110 expect(map, {'a': 1, 'b': 2, 'c': 3});
111 deliverChangeRecords();
112 expect(changes, null);
113 });
114
115 test('change a different item', () {
116 map['c'] = 9000;
117 expect(map, {'a': 1, 'b': 2, 'c': 9000});
118 deliverChangeRecords();
119 expect(changes, []);
120 });
121
122 test('change the item', () {
123 map['b'] = 9001;
124 map['b'] = 42;
125 expect(map, {'a': 1, 'b': 42, 'c': 3});
126 deliverChangeRecords();
127 expect(changes, [_change(2, 9001), _change(9001, 42)]);
128 });
129
130 test('remove other items', () {
131 map.remove('a');
132 expect(map, {'b': 2, 'c': 3});
133 deliverChangeRecords();
134 expect(changes, []);
135 });
136
137 test('remove the item', () {
138 map.remove('b');
139 expect(map, {'a': 1, 'c': 3});
140 deliverChangeRecords();
141 expect(changes, [_change(2, null, kind: ChangeRecord.REMOVE)]);
142 });
143
144 test('remove and add back', () {
145 map.remove('b');
146 map['b'] = 2;
147 expect(map, {'a': 1, 'b': 2, 'c': 3});
148 deliverChangeRecords();
149 expect(changes, [
150 _change(2, null, kind: ChangeRecord.REMOVE),
151 _change(null, 2, kind: ChangeRecord.INSERT)
152 ]);
153 });
154 });
155
156 test('toString', () {
157 var map = toObservable({'a': 1, 'b': 2});
158 expect(map.toString(), '{a: 1, b: 2}');
159 });
160
161 group('change records', () {
162 List<ChangeRecord> records;
163 ObservableMap map;
164
165 setUp(() {
166 map = toObservable({'a': 1, 'b': 2});
167 records = null;
168 map.changes.first.then((r) { records = r; });
169 });
170
171 test('read operations', () {
172 expect(map.length, 2);
173 expect(map.isEmpty, false);
174 expect(map['a'], 1);
175 expect(map.containsKey(2), false);
176 expect(map.containsValue(2), true);
177 expect(map.containsKey('b'), true);
178 expect(map.keys.toList(), ['a', 'b']);
179 expect(map.values.toList(), [1, 2]);
180 var copy = {};
181 map.forEach((k, v) { copy[k] = v; });
182 expect(copy, {'a': 1, 'b': 2});
183 deliverChangeRecords();
184
185 // no change from read-only operators
186 expect(records, null);
187 });
188
189 test('putIfAbsent', () {
190 map.putIfAbsent('a', () => 42);
191 expect(map, {'a': 1, 'b': 2});
192
193 map.putIfAbsent('c', () => 3);
194 expect(map, {'a': 1, 'b': 2, 'c': 3});
195
196 deliverChangeRecords();
197 expect(records, [
198 _record('length', 2, 3),
199 _record('c', null, 3, ChangeRecord.INSERT),
200 ]);
201 });
202
203 test('[]=', () {
204 map['a'] = 42;
205 expect(map, {'a': 42, 'b': 2});
206
207 map['c'] = 3;
208 expect(map, {'a': 42, 'b': 2, 'c': 3});
209
210 deliverChangeRecords();
211 expect(records, [
212 _record('a', 1, 42, ChangeRecord.INDEX),
213 _record('length', 2, 3),
214 _record('c', null, 3, ChangeRecord.INSERT)
215 ]);
216 });
217
218 test('remove', () {
219 map.remove('b');
220 expect(map, {'a': 1});
221
222 deliverChangeRecords();
223 expect(records, [
224 _record('b', 2, null, ChangeRecord.REMOVE),
225 _record('length', 2, 1),
226 ]);
227 });
228
229 test('clear', () {
230 map.clear();
231 expect(map, {});
232
233 deliverChangeRecords();
234 expect(records, [
235 _record('a', 1, null, ChangeRecord.REMOVE),
236 _record('b', 2, null, ChangeRecord.REMOVE),
237 _record('length', 2, 0),
238 ]);
239 });
240 });
241 }
242
243 _record(key, oldValue, newValue, [kind = ChangeRecord.FIELD]) =>
244 new ChangeRecord(key, oldValue, newValue, kind: kind);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698