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

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

Powered by Google App Engine
This is Rietveld 408576698