| OLD | NEW |
| 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 Loading... |
| 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 sum(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.when(callsTo('sum')).alwaysCall(real.sum); |
| 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 Loading... |
| 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.when(callsTo('foo', 1, 2)).thenReturn('A').thenReturn('B'); |
| 147 m.when(callsTo('foo', 1, 1)).thenReturn('C'); |
| 148 m.when(callsTo('foo', 9, anything)).thenReturn('D'); |
| 149 m.when(callsTo('bar', anything, anything)).thenReturn('E'); |
| 150 m.when(callsTo('foobar')).thenReturn('F'); |
| 151 |
| 152 var s = '${m.foo(1,2)}${m.foo(1,1)}${m.foo(9,10)}' |
| 153 '${m.bar(1,1)}${m.foo(1,2)}'; |
| 154 getLogs(m, callsTo('foo', anything, anything)).verify(calledExactly(4)); |
| 155 getLogs(m, callsTo('foo', 1, anything)).verify(calledExactly(3)); |
| 156 getLogs(m, callsTo('foo', 9, anything)).verify(calledOnce); |
| 157 getLogs(m, callsTo('foo', anything, 2)).verify(calledExactly(2)); |
| 158 getLogs(m, callsTo('foobar')).verify(neverCalled); |
| 159 getLogs(m, callsTo('foo', 10, anything)).verify(neverCalled); |
| 160 expect(s, 'ACDEB'); |
| 161 }); |
| 162 } else if (testName == 'mock test 2 (MockList)') { |
| 163 test(testName, () { |
| 164 var l = new MockList(); |
| 165 l.when(callsTo('length')).thenReturn(1); |
| 166 l.when(callsTo('add', anything)).alwaysReturn(0); |
| 167 l.add('foo'); |
| 168 expect(l.length(), 1); |
| 169 |
| 170 var m = new MockList(); |
| 171 m.when(callsTo('add', anything)).alwaysReturn(0); |
| 172 |
| 173 m.add('foo'); |
| 174 m.add('bar'); |
| 175 |
| 176 getLogs(m, callsTo('add')).verify(calledExactly(2)); |
| 177 getLogs(m, callsTo('add', 'foo')).verify(calledOnce); |
| 178 }); |
| 179 } else if (testName == 'mock test 3 (Spy)') { |
| 180 test(testName, () { |
| 181 var p = new FooSpy(); |
| 182 p.sum(1, 2, 3); |
| 183 getLogs(p, callsTo('sum')).verify(calledOnce); |
| 184 p.sum(2, 2, 2); |
| 185 getLogs(p, callsTo('sum')).verify(calledExactly(2)); |
| 186 getLogs(p, callsTo('sum')).verify(sometimeReturned(6)); |
| 187 getLogs(p, callsTo('sum')).verify(alwaysReturned(6)); |
| 188 getLogs(p, callsTo('sum')).verify(neverReturned(5)); |
| 189 p.sum(2, 2, 1); |
| 190 getLogs(p, callsTo('sum')).verify(sometimeReturned(5)); |
| 191 }); |
| 192 } else if (testName == 'mock test 4 (Excess calls)') { |
| 193 test(testName, () { |
| 194 var m = new Mock(); |
| 195 m.when(callsTo('foo')).alwaysReturn(null); |
| 196 m.foo(); |
| 197 m.foo(); |
| 198 getLogs(m, callsTo('foo')).verify(calledOnce); |
| 199 }); |
| 200 } else if (testName == 'mock test 5 (No behavior)') { |
| 201 test(testName, () { |
| 202 var m = new Mock(); |
| 203 m.when(callsTo('foo')).thenReturn(null); |
| 204 m.foo(); |
| 205 m.foo(); |
| 206 }); |
| 207 } else if (testName == 'mock test 6 (No matching return)') { |
| 208 test(testName, () { |
| 209 var p = new FooSpy(); |
| 210 p.sum(1, 2, 3); |
| 211 getLogs(p, callsTo('sum')).verify(sometimeReturned(0)); |
| 212 }); |
| 127 } | 213 } |
| 128 }); | 214 }); |
| 129 } | 215 } |
| 130 | 216 |
| 131 void nextTest(int testNum) { | 217 void nextTest(int testNum) { |
| 132 SendPort sport = spawnFunction(runTest); | 218 SendPort sport = spawnFunction(runTest); |
| 133 sport.call(tests[testNum]).then((msg) { | 219 sport.call(tests[testNum]).then((msg) { |
| 134 actual.add(msg); | 220 actual.add(msg); |
| 135 if (actual.length == expected.length) { | 221 if (actual.length == expected.length) { |
| 136 for (var i = 0; i < tests.length; i++) { | 222 for (var i = 0; i < tests.length; i++) { |
| 137 test(tests[i], () => expect(actual[i], equals(expected[i]))); | 223 test(tests[i], () => expect(actual[i], equals(expected[i]))); |
| 138 } | 224 } |
| 139 } else { | 225 } else { |
| 140 nextTest(testNum+1); | 226 nextTest(testNum+1); |
| 141 } | 227 } |
| 142 }); | 228 }); |
| 143 } | 229 } |
| 144 | 230 |
| 145 main() { | 231 main() { |
| 146 tests = [ | 232 tests = [ |
| 147 'single correct test', | 233 'single correct test', |
| 148 'single failing test', | 234 'single failing test', |
| 149 'exception test', | 235 'exception test', |
| 150 'group name test', | 236 'group name test', |
| 151 'setup test', | 237 'setup test', |
| 152 'teardown test', | 238 'teardown test', |
| 153 'setup and teardown test', | 239 'setup and teardown test', |
| 154 'correct callback test', | 240 'correct callback test', |
| 155 'excess callback test', | 241 'excess callback test', |
| 156 'completion test' | 242 'completion test', |
| 243 'mock test 1 (Mock)', |
| 244 'mock test 2 (MockList)', |
| 245 'mock test 3 (Spy)', |
| 246 'mock test 4 (Excess calls)', |
| 247 'mock test 5 (No behavior)', |
| 248 'mock test 6 (No matching return)' |
| 157 ]; | 249 ]; |
| 158 | 250 |
| 159 expected = [ | 251 expected = [ |
| 160 buildStatusString(1, 0, 0, tests[0]), | 252 buildStatusString(1, 0, 0, tests[0]), |
| 161 buildStatusString(0, 1, 0, tests[1], message: 'Expected: <5> but: was <4>'), | 253 buildStatusString(0, 1, 0, tests[1], message: 'Expected: <5> but: was <4>'), |
| 162 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: fail'), | 254 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: fail'), |
| 163 buildStatusString(2, 0, 0, 'a a::a b b'), | 255 buildStatusString(2, 0, 0, 'a a::a b b'), |
| 164 buildStatusString(1, 0, 0, 'a ${tests[4]}', 0, 'setup'), | 256 buildStatusString(1, 0, 0, 'a ${tests[4]}', 0, 'setup'), |
| 165 buildStatusString(1, 0, 0, 'a ${tests[5]}', 0, '', 'teardown'), | 257 buildStatusString(1, 0, 0, 'a ${tests[5]}', 0, '', 'teardown'), |
| 166 buildStatusString(1, 0, 0, 'a ${tests[6]}', 0, | 258 buildStatusString(1, 0, 0, 'a ${tests[6]}', 0, |
| 167 'setup', 'teardown'), | 259 'setup', 'teardown'), |
| 168 buildStatusString(1, 0, 0, tests[7], 1), | 260 buildStatusString(1, 0, 0, tests[7], 1), |
| 169 buildStatusString(0, 0, 1, tests[8], 1, | 261 buildStatusString(0, 0, 1, tests[8], 1, |
| 170 message: 'Callback called more times than expected (2 > 1)'), | 262 message: 'Callback called more times than expected (2 > 1)'), |
| 171 buildStatusString(1, 0, 0, tests[9], 10) | 263 buildStatusString(1, 0, 0, tests[9], 10), |
| 264 buildStatusString(1, 0, 0, tests[10]), |
| 265 buildStatusString(1, 0, 0, tests[11]), |
| 266 buildStatusString(1, 0, 0, tests[12]), |
| 267 buildStatusString(0, 1, 0, tests[13], |
| 268 message: 'Expected foo() to be called 1 times but:' |
| 269 ' was called 2 times'), |
| 270 buildStatusString(0, 1, 0, tests[14], |
| 271 message: 'Caught Exception: No behavior specified for method foo'), |
| 272 buildStatusString(0, 1, 0, tests[15], |
| 273 message: 'Expected sum() to sometimes return <0> but: never did') |
| 172 ]; | 274 ]; |
| 173 | 275 |
| 174 actual = []; | 276 actual = []; |
| 175 | 277 |
| 176 nextTest(0); | 278 nextTest(0); |
| 177 } | 279 } |
| 178 | 280 |
| OLD | NEW |