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