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

Side by Side Diff: tests/lib/observe/observable_list_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: trying 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 '../../../pkg/unittest/lib/unittest.dart';
6 import '../../../sdk/lib/observe/observe.dart';
7
8 main() {
9 // TODO(jmesserly): need all standard List API tests.
10
11 group('observe length', () {
12
13 ObservableList list;
14 List<ChangeRecord> changes;
15
16 setUp(() {
17 list = toObservable([1, 2, 3]);
18 changes = null;
19 list.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 changes length', () {
28 list.add(4);
29 expect(list, [1, 2, 3, 4]);
30 deliverChangeRecords();
31 expect(changes, [_change(3, 4)]);
32 });
33
34 test('removeRange changes length', () {
35 list.add(4);
36 list.removeRange(1, 3);
37 expect(list, [1, 4]);
38 deliverChangeRecords();
39 expect(changes, [_change(3, 4), _change(4, 2)]);
40 });
41
42 test('length= changes length', () {
43 list.length = 5;
44 expect(list, [1, 2, 3, null, null]);
45 deliverChangeRecords();
46 expect(changes, [_change(3, 5)]);
47 });
48
49 test('[]= does not change length', () {
50 list[2] = 9000;
51 expect(list, [1, 2, 9000]);
52 deliverChangeRecords();
53 expect(changes, []);
54 });
55
56 test('clear changes length', () {
57 list.clear();
58 expect(list, []);
59 deliverChangeRecords();
60 expect(changes, [_change(3, 0)]);
61 });
62 });
63
64 group('observe index', () {
65 ObservableList list;
66 List<ChangeRecord> changes;
67
68 setUp(() {
69 list = toObservable([1, 2, 3]);
70 changes = null;
71 list.changes.listen((records) {
72 changes = records.where((r) => r.key == 1 &&
73 (r.kind & ChangeRecord.INDEX) != 0).toList();
74 });
75 });
76
77 _change(x, y, [kind = ChangeRecord.INDEX]) => _record(1, x, y, kind);
78
79 test('add does not change existing items', () {
80 list.add(4);
81 expect(list, [1, 2, 3, 4]);
82 deliverChangeRecords();
83 expect(changes, []);
84 });
85
86 test('[]= changes item', () {
87 list[1] = 777;
88 expect(list, [1, 777, 3]);
89 deliverChangeRecords();
90 expect(changes, [_change(2, 777)]);
91 });
92
93 test('[]= on a different item does not fire change', () {
94 list[2] = 9000;
95 expect(list, [1, 2, 9000]);
96 deliverChangeRecords();
97 expect(changes, []);
98 });
99
100 test('set multiple times results in one change per set', () {
101 list[1] = 777;
102 list[1] = 42;
103 expect(list, [1, 42, 3]);
104 deliverChangeRecords();
105 expect(changes, [_change(2, 777), _change(777, 42)]);
106 });
107
108 test('set length without truncating item means no change', () {
109 list.length = 2;
110 expect(list, [1, 2]);
111 deliverChangeRecords();
112 expect(changes, []);
113 });
114
115 test('truncate removes item', () {
116 list.length = 1;
117 expect(list, [1]);
118 deliverChangeRecords();
119 expect(changes, [_change(2, null, ChangeRecord.REMOVE)]);
120 });
121
122 test('truncate item and add back', () {
123 list.length = 1;
124 list.add(42);
125 expect(list, [1, 42]);
126 deliverChangeRecords();
127 expect(changes, [
128 _change(2, null, ChangeRecord.REMOVE),
129 _change(null, 42, ChangeRecord.INSERT)
130 ]);
131 });
132
133 test('truncate and add same item back', () {
134 list.length = 1;
135 list.add(2);
136 expect(list, [1, 2]);
137 deliverChangeRecords();
138 expect(changes, [
139 _change(2, null, ChangeRecord.REMOVE),
140 _change(null, 2, ChangeRecord.INSERT)
141 ]);
142 });
143 });
144
145 test('toString', () {
146 var list = toObservable([1, 2, 3]);
147 expect(list.toString(), '[1, 2, 3]');
148 });
149
150 group('change records', () {
151
152 List<ChangeRecord> records;
153 ObservableList list;
154
155 setUp(() {
156 list = toObservable([1, 2, 3, 1, 3, 4]);
157 records = null;
158 list.changes.listen((r) { records = r; });
159 });
160
161 test('read operations', () {
162 expect(list.length, 6);
163 expect(list[0], 1);
164 expect(list.indexOf(4), 5);
165 expect(list.indexOf(1), 0);
166 expect(list.indexOf(1, 1), 3);
167 expect(list.lastIndexOf(1), 3);
168 expect(list.last, 4);
169 var copy = new List<int>();
170 list.forEach((i) { copy.add(i); });
171 expect(copy, orderedEquals([1, 2, 3, 1, 3, 4]));
172 deliverChangeRecords();
173
174 // no change from read-only operators
175 expect(records, null);
176 });
177
178 test('add', () {
179 list.add(5);
180 list.add(6);
181 expect(list, orderedEquals([1, 2, 3, 1, 3, 4, 5, 6]));
182
183 deliverChangeRecords();
184 expect(records, [
185 _record('length', 6, 7),
186 _record(6, null, 5, ChangeRecord.INSERT),
187 _record('length', 7, 8),
188 _record(7, null, 6, ChangeRecord.INSERT),
189 ]);
190 });
191
192 test('[]=', () {
193 list[1] = list.last;
194 expect(list, orderedEquals([1, 4, 3, 1, 3, 4]));
195
196 deliverChangeRecords();
197 expect(records, [ _record(1, 2, 4, ChangeRecord.INDEX) ]);
198 });
199
200 test('removeLast', () {
201 expect(list.removeLast(), 4);
202 expect(list, orderedEquals([1, 2, 3, 1, 3]));
203
204 deliverChangeRecords();
205 expect(records, [
206 _record(5, 4, null, ChangeRecord.REMOVE),
207 _record('length', 6, 5),
208 ]);
209 });
210
211 test('removeRange', () {
212 list.removeRange(1, 4);
213 expect(list, orderedEquals([1, 3, 4]));
214
215 deliverChangeRecords();
216 expect(records, [
217 _record(1, 2, null, ChangeRecord.REMOVE),
218 _record(2, 3, null, ChangeRecord.REMOVE),
219 _record(3, 1, null, ChangeRecord.REMOVE),
220 _record(1, 2, 3, ChangeRecord.INDEX),
221 _record(2, 3, 4, ChangeRecord.INDEX),
222 _record(5, 4, null, ChangeRecord.REMOVE),
223 _record(4, 3, null, ChangeRecord.REMOVE),
224 _record(3, 1, null, ChangeRecord.REMOVE),
225 _record('length', 6, 3)
226 ]);
227 });
228
229 test('sort', () {
230 list.sort((x, y) => x - y);
231 expect(list, orderedEquals([1, 1, 2, 3, 3, 4]));
232
233 deliverChangeRecords();
234 // TODO(jmesserly): this depends on implementation details of sort.
235 expect(records, [
236 _record(1, 2, 2, ChangeRecord.INDEX),
237 _record(2, 3, 3, ChangeRecord.INDEX),
238 _record(3, 1, 3, ChangeRecord.INDEX),
239 _record(2, 3, 2, ChangeRecord.INDEX),
240 _record(1, 2, 1, ChangeRecord.INDEX),
241 _record(4, 3, 3, ChangeRecord.INDEX),
242 _record(5, 4, 4, ChangeRecord.INDEX)
243 ]);
244 });
245
246 test('clear', () {
247 list.clear();
248 expect(list, []);
249
250 deliverChangeRecords();
251 expect(records, [
252 _record(5, 4, null, ChangeRecord.REMOVE),
253 _record(4, 3, null, ChangeRecord.REMOVE),
254 _record(3, 1, null, ChangeRecord.REMOVE),
255 _record(2, 3, null, ChangeRecord.REMOVE),
256 _record(1, 2, null, ChangeRecord.REMOVE),
257 _record(0, 1, null, ChangeRecord.REMOVE),
258 _record('length', 6, 0),
259 ]);
260 });
261 });
262 }
263
264 _record(key, oldValue, newValue, [kind = ChangeRecord.FIELD]) =>
265 new ChangeRecord(key, oldValue, newValue, kind: kind);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698