OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, 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 // Dart test program testing that NoSuchMethod is properly called. |
| 5 |
| 6 import "package:expect/expect.dart"; |
| 7 |
| 8 class Cat { |
| 9 bool eatFood(String food) => true; |
| 10 int scratch(String furniture) => 100; |
| 11 } |
| 12 |
| 13 class MockCat implements Cat { |
| 14 dynamic noSuchMethod(Invocation invocation) { |
| 15 return (invocation.positionalArguments[0] as String).isNotEmpty; |
| 16 } |
| 17 } |
| 18 |
| 19 |
| 20 class MockCat2 extends MockCat { |
| 21 // this apparently works. |
| 22 noSuchMethod(_); |
| 23 } |
| 24 |
| 25 class MockCat3 extends MockCat2 implements Cat { |
| 26 bool eatFood(String food, {double amount}); |
| 27 int scratch(String furniture, [String furniture2]); |
| 28 |
| 29 dynamic noSuchMethod(Invocation invocation) { |
| 30 return (invocation.positionalArguments[0] as String).isNotEmpty && |
| 31 invocation.namedArguments[#amount] > 0.5; |
| 32 } |
| 33 } |
| 34 |
| 35 |
| 36 class MockWithGenerics { |
| 37 /*=T*/ doStuff/*<T>*/(/*=T*/ t); |
| 38 |
| 39 noSuchMethod(i) => i.positionalArguments[0] + 100; |
| 40 } |
| 41 |
| 42 |
| 43 void main() { |
| 44 MockCat mock = new MockCat(); |
| 45 Expect.isTrue((mock as dynamic).eatFood("cat food")); |
| 46 Expect.isFalse(mock.eatFood("")); |
| 47 |
| 48 // In strong mode this will be a runtime type error: |
| 49 // bool is not an int. VM will fail with noSuchMethod +. |
| 50 Expect.throws(() => mock.scratch("couch") + 0); |
| 51 |
| 52 var mock2 = new MockCat2(); |
| 53 Expect.isTrue(mock2.eatFood("cat food")); |
| 54 |
| 55 var mock3 = new MockCat3(); |
| 56 Expect.isTrue(mock3.eatFood("cat food", amount: 0.9)); |
| 57 Expect.isFalse(mock3.eatFood("cat food", amount: 0.3)); |
| 58 |
| 59 var g = new MockWithGenerics(); |
| 60 Expect.equals(g.doStuff(42), 142); |
| 61 Expect.throws(() => g.doStuff('hi')); |
| 62 } |
OLD | NEW |