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

Side by Side Diff: tests/lib/unittest/unittest_test.dart

Issue 10718002: Changed the syntax some. All the handling of variable argument lists is (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 5 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
« lib/unittest/mock.dart ('K') | « lib/unittest/mock.dart ('k') | no next file » | 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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 // TODO(gram): 5 // TODO(gram):
6 // Unfortunately I can't seem to test anything that involves timeouts, e.g. 6 // Unfortunately I can't seem to test anything that involves timeouts, e.g.
7 // insufficient callbacks, because the timeout is controlled externally 7 // insufficient callbacks, because the timeout is controlled externally
8 // (test.dart?), and we would need to use a shorter timeout for the inner tests 8 // (test.dart?), and we would need to use a shorter timeout for the inner tests
9 // so the outer timeout doesn't fire. So I removed all such tests. 9 // so the outer timeout doesn't fire. So I removed all such tests.
10 // I'd like to revisit this at some point. 10 // I'd like to revisit this at some point.
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 TestConfiguration(this._port); 65 TestConfiguration(this._port);
66 66
67 void onDone(int passed, int failed, int errors, List<TestCase> results, 67 void onDone(int passed, int failed, int errors, List<TestCase> results,
68 String uncaughtError) { 68 String uncaughtError) {
69 var result = buildStatusString(passed, failed, errors, results, count, 69 var result = buildStatusString(passed, failed, errors, results, count,
70 setup, teardown, uncaughtError); 70 setup, teardown, uncaughtError);
71 _port.send(result); 71 _port.send(result);
72 } 72 }
73 } 73 }
74 74
75 class MockList extends Mock implements List {
76 }
77
78 class Foo {
79 bar(a, b, c) => a + b + c;
80 }
81
82 class FooSpy extends Mock implements Foo {
83 FooSpy real;
84 FooSpy() {
85 real = new Foo();
86 this.whenThereAre(callsTo('bar')).alwaysCall(real.bar);
87 }
88 }
89
75 runTest() { 90 runTest() {
76 port.receive((testName, sendport) { 91 port.receive((testName, sendport) {
77 configure(_testconfig = new TestConfiguration(sendport)); 92 configure(_testconfig = new TestConfiguration(sendport));
78 if (testName == 'single correct test') { 93 if (testName == 'single correct test') {
79 test(testName, () => expect(2 + 3, equals(5))); 94 test(testName, () => expect(2 + 3, equals(5)));
80 } else if (testName == 'single failing test') { 95 } else if (testName == 'single failing test') {
81 test(testName, () => expect(2 + 2, equals(5))); 96 test(testName, () => expect(2 + 2, equals(5)));
82 } else if (testName == 'exception test') { 97 } else if (testName == 'exception test') {
83 test(testName, () { throw new Exception('fail'); }); 98 test(testName, () { throw new Exception('fail'); });
84 } else if (testName == 'group name test') { 99 } else if (testName == 'group name test') {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 test(testName, () { 132 test(testName, () {
118 var _callback; 133 var _callback;
119 _callback = expectAsyncUntil0(() { 134 _callback = expectAsyncUntil0(() {
120 if (++_testconfig.count < 10) { 135 if (++_testconfig.count < 10) {
121 _defer(_callback); 136 _defer(_callback);
122 } 137 }
123 }, 138 },
124 () => (_testconfig.count == 10)); 139 () => (_testconfig.count == 10));
125 _defer(_callback); 140 _defer(_callback);
126 }); 141 });
142 } else if (testName == 'mock test 1 (Mock)') {
143 test(testName, () {
144 var m = new Mock();
145
146 m.whenThereAre(callsTo('Sum', 1, 2)).thenReturn('A').thenReturn('B');
Siggi Cherem (dart-lang) 2012/06/28 00:44:40 I'm playing around with other ideas to see if ther
147 m.whenThereAre(callsTo('Sum', 1, 1)).thenReturn('C');
148 m.whenThereAre(callsTo('Sum', anything, anything)).thenReturn('D');
149 m.whenThereAre(callsTo('Sub', anything, anything)).thenReturn('E');
Siggi Cherem (dart-lang) 2012/06/28 00:44:40 could we change the example to use lowercase metho
gram 2012/06/28 17:32:54 Done, and I changed the names to foo/bar which is
150
151 var s = '${m.Sum(1,2)}${m.Sum(1,1)}${m.Sum(9,10)}'
152 '${m.Sub(1,1)}${m.Sum(1,2)}';
153 m.forThe(callsTo('Sum', anything, anything)).verify(calledExactly(4));
Siggi Cherem (dart-lang) 2012/06/28 00:44:40 Ideas instead of 'm.forThe(_).verify(_)' - m.verif
154 m.forThe(callsTo('Sum', 1, anything)).verify(calledExactly(3));
155 m.forThe(callsTo('Sum', 9, anything)).verify(calledOnce);
156 m.forThe(callsTo('Sum', anything, 2)).verify(calledExactly(2));
157 expect(s, 'ACDEB');
158 });
159 } else if (testName == 'mock test 2 (MockList)') {
160 test(testName, () {
161 MockList l = new MockList();
162 l.whenThereAre(callsTo('length')).thenReturn(1);
163 l.whenThereAre(callsTo('add', anything)).alwaysReturn(0);
164 l.add('foo');
165 expect(l.length(), 1);
166
167 List m = new MockList();
168 m.whenThereAre(callsTo('add', anything)).alwaysReturn(0);
169
170 m.add('foo');
171 m.add('bar');
172
173 m.forThe(callsTo('add')).verify(calledExactly(2));
174 m.forThe(callsTo('add', 'foo')).verify(calledOnce);
175 });
176 } else if (testName == 'mock test 3 (Spy)') {
177 test(testName, () {
178 var p = new FooSpy();
179 p.bar(1, 2, 3);
180 p.forThe(callsTo('bar')).verify(calledOnce);
181 p.bar(2, 2, 2);
182 p.forThe(callsTo('bar')).verify(calledExactly(2));
183 p.forThe(callsTo('bar')).verify(sometimeReturned(6));
184 p.forThe(callsTo('bar')).verify(alwaysReturned(6));
185 p.forThe(callsTo('bar')).verify(neverReturned(5));
186 p.bar(2, 2, 1);
187 p.forThe(callsTo('bar')).verify(sometimeReturned(5));
188 });
127 } 189 }
128 }); 190 });
129 } 191 }
130 192
131 void nextTest(int testNum) { 193 void nextTest(int testNum) {
132 SendPort sport = spawnFunction(runTest); 194 SendPort sport = spawnFunction(runTest);
133 sport.call(tests[testNum]).then((msg) { 195 sport.call(tests[testNum]).then((msg) {
134 actual.add(msg); 196 actual.add(msg);
135 if (actual.length == expected.length) { 197 if (actual.length == expected.length) {
136 for (var i = 0; i < tests.length; i++) { 198 for (var i = 0; i < tests.length; i++) {
137 test(tests[i], () => expect(actual[i], equals(expected[i]))); 199 test(tests[i], () => expect(actual[i], equals(expected[i])));
138 } 200 }
139 } else { 201 } else {
140 nextTest(testNum+1); 202 nextTest(testNum+1);
141 } 203 }
142 }); 204 });
143 } 205 }
144 206
145 main() { 207 main() {
146 tests = [ 208 tests = [
147 'single correct test', 209 'single correct test',
148 'single failing test', 210 'single failing test',
149 'exception test', 211 'exception test',
150 'group name test', 212 'group name test',
151 'setup test', 213 'setup test',
152 'teardown test', 214 'teardown test',
153 'setup and teardown test', 215 'setup and teardown test',
154 'correct callback test', 216 'correct callback test',
155 'excess callback test', 217 'excess callback test',
156 'completion test' 218 'completion test',
219 'mock test 1 (Mock)',
220 'mock test 2 (MockList)',
221 'mock test 3 (Spy)'
157 ]; 222 ];
158 223
159 expected = [ 224 expected = [
160 buildStatusString(1, 0, 0, tests[0]), 225 buildStatusString(1, 0, 0, tests[0]),
161 buildStatusString(0, 1, 0, tests[1], message: 'Expected: <5> but: was <4>'), 226 buildStatusString(0, 1, 0, tests[1], message: 'Expected: <5> but: was <4>'),
162 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: fail'), 227 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: fail'),
163 buildStatusString(2, 0, 0, 'a a::a b b'), 228 buildStatusString(2, 0, 0, 'a a::a b b'),
164 buildStatusString(1, 0, 0, 'a ${tests[4]}', 0, 'setup'), 229 buildStatusString(1, 0, 0, 'a ${tests[4]}', 0, 'setup'),
165 buildStatusString(1, 0, 0, 'a ${tests[5]}', 0, '', 'teardown'), 230 buildStatusString(1, 0, 0, 'a ${tests[5]}', 0, '', 'teardown'),
166 buildStatusString(1, 0, 0, 'a ${tests[6]}', 0, 231 buildStatusString(1, 0, 0, 'a ${tests[6]}', 0,
167 'setup', 'teardown'), 232 'setup', 'teardown'),
168 buildStatusString(1, 0, 0, tests[7], 1), 233 buildStatusString(1, 0, 0, tests[7], 1),
169 buildStatusString(0, 0, 1, tests[8], 1, 234 buildStatusString(0, 0, 1, tests[8], 1,
170 message: 'Callback called more times than expected (2 > 1)'), 235 message: 'Callback called more times than expected (2 > 1)'),
171 buildStatusString(1, 0, 0, tests[9], 10) 236 buildStatusString(1, 0, 0, tests[9], 10),
237 buildStatusString(1, 0, 0, tests[10]),
238 buildStatusString(1, 0, 0, tests[11]),
239 buildStatusString(1, 0, 0, tests[12])
172 ]; 240 ];
173 241
174 actual = []; 242 actual = [];
175 243
176 nextTest(0); 244 nextTest(0);
177 } 245 }
178 246
OLDNEW
« lib/unittest/mock.dart ('K') | « lib/unittest/mock.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698