| Index: tests/lib/unittest/unittest_test.dart
|
| ===================================================================
|
| --- tests/lib/unittest/unittest_test.dart (revision 9180)
|
| +++ tests/lib/unittest/unittest_test.dart (working copy)
|
| @@ -72,6 +72,21 @@
|
| }
|
| }
|
|
|
| +class MockList extends Mock implements List {
|
| +}
|
| +
|
| +class Foo {
|
| + bar(a, b, c) => a + b + c;
|
| +}
|
| +
|
| +class FooSpy extends Mock implements Foo {
|
| + FooSpy real;
|
| + FooSpy() {
|
| + real = new Foo();
|
| + this.whenThereAre(callsTo('bar')).alwaysCall(real.bar);
|
| + }
|
| +}
|
| +
|
| runTest() {
|
| port.receive((testName, sendport) {
|
| configure(_testconfig = new TestConfiguration(sendport));
|
| @@ -124,6 +139,77 @@
|
| () => (_testconfig.count == 10));
|
| _defer(_callback);
|
| });
|
| + } else if (testName == 'mock test 1 (Mock)') {
|
| + test(testName, () {
|
| + var m = new Mock();
|
| +
|
| + m.whenThereAre(callsTo('Sum', 1, 2)).thenReturn('A').thenReturn('B');
|
| + m.whenThereAre(callsTo('Sum', 1, 1)).thenReturn('C');
|
| + m.whenThereAre(callsTo('Sum', anything, anything)).thenReturn('D');
|
| + m.whenThereAre(callsTo('Sub', anything, anything)).thenReturn('E');
|
| + m.whenThereAre(callsTo('Mul')).thenReturn('F');
|
| +
|
| + var s = '${m.Sum(1,2)}${m.Sum(1,1)}${m.Sum(9,10)}'
|
| + '${m.Sub(1,1)}${m.Sum(1,2)}';
|
| + m.forThe(callsTo('Sum', anything, anything)).verify(calledExactly(4));
|
| + m.forThe(callsTo('Sum', 1, anything)).verify(calledExactly(3));
|
| + m.forThe(callsTo('Sum', 9, anything)).verify(calledOnce);
|
| + m.forThe(callsTo('Sum', anything, 2)).verify(calledExactly(2));
|
| + m.forThe(callsTo('Mul')).verify(neverCalled);
|
| + m.forThe(callsTo('Sum', 10, anything)).verify(neverCalled);
|
| + expect(s, 'ACDEB');
|
| + });
|
| + } else if (testName == 'mock test 2 (MockList)') {
|
| + test(testName, () {
|
| + MockList l = new MockList();
|
| + l.whenThereAre(callsTo('length')).thenReturn(1);
|
| + l.whenThereAre(callsTo('add', anything)).alwaysReturn(0);
|
| + l.add('foo');
|
| + expect(l.length(), 1);
|
| +
|
| + List m = new MockList();
|
| + m.whenThereAre(callsTo('add', anything)).alwaysReturn(0);
|
| +
|
| + m.add('foo');
|
| + m.add('bar');
|
| +
|
| + m.forThe(callsTo('add')).verify(calledExactly(2));
|
| + m.forThe(callsTo('add', 'foo')).verify(calledOnce);
|
| + });
|
| + } else if (testName == 'mock test 3 (Spy)') {
|
| + test(testName, () {
|
| + var p = new FooSpy();
|
| + p.bar(1, 2, 3);
|
| + p.forThe(callsTo('bar')).verify(calledOnce);
|
| + p.bar(2, 2, 2);
|
| + p.forThe(callsTo('bar')).verify(calledExactly(2));
|
| + p.forThe(callsTo('bar')).verify(sometimeReturned(6));
|
| + p.forThe(callsTo('bar')).verify(alwaysReturned(6));
|
| + p.forThe(callsTo('bar')).verify(neverReturned(5));
|
| + p.bar(2, 2, 1);
|
| + p.forThe(callsTo('bar')).verify(sometimeReturned(5));
|
| + });
|
| + } else if (testName == 'mock test 4 (Excess calls)') {
|
| + test(testName, () {
|
| + var m = new Mock();
|
| + m.whenThereAre(callsTo('Sum')).alwaysReturn(null);
|
| + m.Sum();
|
| + m.Sum();
|
| + m.forThe(callsTo('Sum')).verify(calledOnce);
|
| + });
|
| + } else if (testName == 'mock test 5 (No behavior)') {
|
| + test(testName, () {
|
| + var m = new Mock();
|
| + m.whenThereAre(callsTo('Sum')).thenReturn(null);
|
| + m.Sum();
|
| + m.Sum();
|
| + });
|
| + } else if (testName == 'mock test 6 (No matching return)') {
|
| + test(testName, () {
|
| + var p = new FooSpy();
|
| + p.bar(1, 2, 3);
|
| + p.forThe(callsTo('bar')).verify(sometimeReturned(0));
|
| + });
|
| }
|
| });
|
| }
|
| @@ -153,7 +239,13 @@
|
| 'setup and teardown test',
|
| 'correct callback test',
|
| 'excess callback test',
|
| - 'completion test'
|
| + 'completion test',
|
| + 'mock test 1 (Mock)',
|
| + 'mock test 2 (MockList)',
|
| + 'mock test 3 (Spy)',
|
| + 'mock test 4 (Excess calls)',
|
| + 'mock test 5 (No behavior)',
|
| + 'mock test 6 (No matching return)'
|
| ];
|
|
|
| expected = [
|
| @@ -168,7 +260,17 @@
|
| buildStatusString(1, 0, 0, tests[7], 1),
|
| buildStatusString(0, 0, 1, tests[8], 1,
|
| message: 'Callback called more times than expected (2 > 1)'),
|
| - buildStatusString(1, 0, 0, tests[9], 10)
|
| + buildStatusString(1, 0, 0, tests[9], 10),
|
| + buildStatusString(1, 0, 0, tests[10]),
|
| + buildStatusString(1, 0, 0, tests[11]),
|
| + buildStatusString(1, 0, 0, tests[12]),
|
| + buildStatusString(0, 1, 0, tests[13],
|
| + message: 'Expected Sum() to be called 1 times but:'
|
| + ' was called 2 times'),
|
| + buildStatusString(0, 1, 0, tests[14],
|
| + message: 'Caught Exception: No behavior specified for method Sum'),
|
| + buildStatusString(0, 1, 0, tests[15],
|
| + message: 'Expected bar() to sometimes return <0> but: never did')
|
| ];
|
|
|
| actual = [];
|
|
|