OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 // Dart test program testing that NoSuchMethod is properly called. | 4 // Dart test program testing that NoSuchMethod is properly called. |
5 | 5 |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 | 7 |
8 class Cat { | 8 class Cat { |
9 bool eatFood(String food) => true; | 9 bool eatFood(String food) => true; |
10 String scratch(String furniture) => 'purr'; | 10 String scratch(String furniture) => 'purr'; |
(...skipping 18 matching lines...) Expand all Loading... |
29 if (invocation.memberName == #scratch) { | 29 if (invocation.memberName == #scratch) { |
30 return invocation.positionalArguments.join(','); | 30 return invocation.positionalArguments.join(','); |
31 } | 31 } |
32 | 32 |
33 return (invocation.positionalArguments[0] as String).isNotEmpty && | 33 return (invocation.positionalArguments[0] as String).isNotEmpty && |
34 invocation.namedArguments[#amount] > 0.5; | 34 invocation.namedArguments[#amount] > 0.5; |
35 } | 35 } |
36 } | 36 } |
37 | 37 |
38 class MockWithGenerics { | 38 class MockWithGenerics { |
39 /*=T*/ doStuff/*<T>*/(/*=T*/ t); | 39 List<Type> doStuff<T>(T t); |
40 | 40 |
41 noSuchMethod(i) => i.positionalArguments[0] + 100; | 41 noSuchMethod(i) => (i as dynamic).typeArguments; |
42 } | 42 } |
43 | 43 |
44 class MockWithGetterSetter { | 44 class MockWithGetterSetter { |
45 get getter; | 45 get getter; |
46 set setter(value); | 46 set setter(value); |
47 | 47 |
48 Invocation invocation; | 48 Invocation invocation; |
49 noSuchMethod(i) { | 49 noSuchMethod(i) { |
50 invocation = i; | 50 invocation = i; |
51 } | 51 } |
(...skipping 22 matching lines...) Expand all Loading... |
74 | 74 |
75 var mock3 = new MockCat3(); | 75 var mock3 = new MockCat3(); |
76 Expect.isTrue(mock3.eatFood("cat food", amount: 0.9)); | 76 Expect.isTrue(mock3.eatFood("cat food", amount: 0.9)); |
77 Expect.isFalse(mock3.eatFood("cat food", amount: 0.3)); | 77 Expect.isFalse(mock3.eatFood("cat food", amount: 0.3)); |
78 Expect.equals(mock3.scratch("chair"), "chair"); | 78 Expect.equals(mock3.scratch("chair"), "chair"); |
79 Expect.equals(mock3.scratch("chair", "couch"), "chair,couch"); | 79 Expect.equals(mock3.scratch("chair", "couch"), "chair,couch"); |
80 Expect.equals(mock3.scratch("chair", null), "chair,null"); | 80 Expect.equals(mock3.scratch("chair", null), "chair,null"); |
81 Expect.equals(mock3.scratch("chair", ""), "chair,"); | 81 Expect.equals(mock3.scratch("chair", ""), "chair,"); |
82 | 82 |
83 var g = new MockWithGenerics(); | 83 var g = new MockWithGenerics(); |
84 Expect.equals(g.doStuff(42), 142); | 84 Expect.listEquals(g.doStuff(42), [int]); |
85 Expect.throws(() { | 85 Expect.listEquals(g.doStuff<num>(42), [num]); |
86 g.doStuff('hi'); | 86 Expect.listEquals(g.doStuff('hi'), [String]); |
87 }); | |
88 | 87 |
89 var s = new MockWithGetterSetter(); | 88 var s = new MockWithGetterSetter(); |
90 s.getter; | 89 s.getter; |
91 Expect.equals(s.invocation.positionalArguments.length, 0); | 90 Expect.equals(s.invocation.positionalArguments.length, 0); |
92 Expect.equals(s.invocation.isGetter, true); | 91 Expect.equals(s.invocation.isGetter, true); |
93 Expect.equals(s.invocation.isSetter, false); | 92 Expect.equals(s.invocation.isSetter, false); |
94 Expect.equals(s.invocation.isMethod, false); | 93 Expect.equals(s.invocation.isMethod, false); |
95 s.setter = 42; | 94 s.setter = 42; |
96 Expect.equals(s.invocation.positionalArguments.single, 42); | 95 Expect.equals(s.invocation.positionalArguments.single, 42); |
97 Expect.equals(s.invocation.isGetter, false); | 96 Expect.equals(s.invocation.isGetter, false); |
98 Expect.equals(s.invocation.isSetter, true); | 97 Expect.equals(s.invocation.isSetter, true); |
99 Expect.equals(s.invocation.isMethod, false); | 98 Expect.equals(s.invocation.isMethod, false); |
100 | 99 |
101 Callable call = new MockCallable(); | 100 Callable call = new MockCallable(); |
102 Expect.equals(call(), 42); | 101 Expect.equals(call(), 42); |
103 Expect.equals((call as dynamic)(), 42); | 102 Expect.equals((call as dynamic)(), 42); |
104 Expect.equals(call.m(), 0); | 103 Expect.equals(call.m(), 0); |
105 Expect.equals((call as dynamic).m(), 0); | 104 Expect.equals((call as dynamic).m(), 0); |
106 } | 105 } |
OLD | NEW |