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 | 4 |
5 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 | 6 |
7 // Tests that we can call functions through getters. | 7 // Tests that we can call functions through getters. |
8 | 8 |
9 const TOP_LEVEL_CONST = 1; | 9 const TOP_LEVEL_CONST = 1; |
10 const TOP_LEVEL_CONST_REF = TOP_LEVEL_CONST; | 10 const TOP_LEVEL_CONST_REF = TOP_LEVEL_CONST; |
(...skipping 11 matching lines...) Expand all Loading... |
22 } | 22 } |
23 | 23 |
24 static void testTopLevel() { | 24 static void testTopLevel() { |
25 topLevel = () { | 25 topLevel = () { |
26 return 2; | 26 return 2; |
27 }; | 27 }; |
28 Expect.equals(1, TOP_LEVEL_CONST); | 28 Expect.equals(1, TOP_LEVEL_CONST); |
29 Expect.equals(1, TOP_LEVEL_CONST_REF); | 29 Expect.equals(1, TOP_LEVEL_CONST_REF); |
30 Expect.equals(2, topLevel()); | 30 Expect.equals(2, topLevel()); |
31 | 31 |
32 expectThrowsNoSuchMethod(() { | 32 TOP_LEVEL_CONST(); //# 01: compile-time error |
33 TOP_LEVEL_CONST(); //# static type warning | 33 (TOP_LEVEL_CONST)(); //# 02: compile-time error |
34 }); | |
35 expectThrowsNoSuchMethod(() { | |
36 (TOP_LEVEL_CONST)(); // //# static type warning | |
37 }); | |
38 } | 34 } |
39 | 35 |
40 static void testField() { | 36 static void testField() { |
41 A a = new A(); | 37 A a = new A(); |
42 a.field = () => 42; | 38 a.field = () => 42; |
43 Expect.equals(42, a.field()); | 39 Expect.equals(42, a.field()); |
44 Expect.equals(42, (a.field)()); | 40 Expect.equals(42, (a.field)()); |
45 | 41 |
46 a.field = () => 87; | 42 a.field = () => 87; |
47 Expect.equals(87, a.field()); | 43 Expect.equals(87, a.field()); |
48 Expect.equals(87, (a.field)()); | 44 Expect.equals(87, (a.field)()); |
49 | 45 |
50 a.field = 99; | 46 a.field = 99; |
51 expectThrowsNoSuchMethod(() { | 47 Expect.throwsNoSuchMethodError(() { |
52 a.field(); | 48 a.field(); |
53 }); | 49 }); |
54 expectThrowsNoSuchMethod(() { | 50 Expect.throwsNoSuchMethodError(() { |
55 (a.field)(); | 51 (a.field)(); |
56 }); | 52 }); |
57 } | 53 } |
58 | 54 |
59 static void testGetter() { | 55 static void testGetter() { |
60 A a = new A(); | 56 A a = new A(); |
61 a.field = () => 42; | 57 a.field = () => 42; |
62 Expect.equals(42, a.getter()); | 58 Expect.equals(42, a.getter()); |
63 Expect.equals(42, (a.getter)()); | 59 Expect.equals(42, (a.getter)()); |
64 | 60 |
65 a.field = () => 87; | 61 a.field = () => 87; |
66 Expect.equals(87, a.getter()); | 62 Expect.equals(87, a.getter()); |
67 Expect.equals(87, (a.getter)()); | 63 Expect.equals(87, (a.getter)()); |
68 | 64 |
69 a.field = 99; | 65 a.field = 99; |
70 expectThrowsNoSuchMethod(() { | 66 Expect.throwsNoSuchMethodError(() { |
71 a.getter(); | 67 a.getter(); |
72 }); | 68 }); |
73 expectThrowsNoSuchMethod(() { | 69 Expect.throwsNoSuchMethodError(() { |
74 (a.getter)(); | 70 (a.getter)(); |
75 }); | 71 }); |
76 } | 72 } |
77 | 73 |
78 static void testMethod() { | 74 static void testMethod() { |
79 A a = new A(); | 75 A a = new A(); |
80 a.field = () => 42; | 76 a.field = () => 42; |
81 Expect.equals(true, a.method() is Function); | 77 Expect.equals(true, a.method() is Function); |
82 Expect.equals(42, a.method()()); | 78 Expect.equals(42, a.method()()); |
83 | 79 |
(...skipping 24 matching lines...) Expand all Loading... |
108 b = new B(); | 104 b = new B(); |
109 Expect.equals("xyzgf", b.g3(b.x, b.y, b.z)); | 105 Expect.equals("xyzgf", b.g3(b.x, b.y, b.z)); |
110 b = new B(); | 106 b = new B(); |
111 Expect.equals("gxyzf", (b.g3)(b.x, b.y, b.z)); | 107 Expect.equals("gxyzf", (b.g3)(b.x, b.y, b.z)); |
112 | 108 |
113 b = new B(); | 109 b = new B(); |
114 Expect.equals("yzxgf", b.g3(b.y, b.z, b.x)); | 110 Expect.equals("yzxgf", b.g3(b.y, b.z, b.x)); |
115 b = new B(); | 111 b = new B(); |
116 Expect.equals("gyzxf", (b.g3)(b.y, b.z, b.x)); | 112 Expect.equals("gyzxf", (b.g3)(b.y, b.z, b.x)); |
117 } | 113 } |
118 | |
119 static expectThrowsNoSuchMethod(fn) { | |
120 var exception = catchException(fn); | |
121 if (exception is! NoSuchMethodError) { | |
122 Expect.fail("Wrong exception. Expected: NoSuchMethodError" | |
123 " got: ${exception}"); | |
124 } | |
125 } | |
126 | |
127 static catchException(fn) { | |
128 bool caught = false; | |
129 var result = null; | |
130 try { | |
131 fn(); | |
132 Expect.equals(true, false); // Shouldn't reach this. | |
133 } catch (e) { | |
134 caught = true; | |
135 result = e; | |
136 } | |
137 Expect.equals(true, caught); | |
138 return result; | |
139 } | |
140 } | 114 } |
141 | 115 |
142 class A { | 116 class A { |
143 A() {} | 117 A() {} |
144 var field; | 118 var field; |
145 get getter { | 119 get getter { |
146 return field; | 120 return field; |
147 } | 121 } |
148 | 122 |
149 method() { | 123 method() { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 _order.write(m); | 175 _order.write(m); |
202 return _order.toString(); | 176 return _order.toString(); |
203 } | 177 } |
204 | 178 |
205 StringBuffer _order; | 179 StringBuffer _order; |
206 } | 180 } |
207 | 181 |
208 main() { | 182 main() { |
209 CallThroughGetterTest.testMain(); | 183 CallThroughGetterTest.testMain(); |
210 } | 184 } |
OLD | NEW |