| 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 // Tests that we can call functions through getters. | 5 // Tests that we can call functions through getters. |
| 6 | 6 |
| 7 const TOP_LEVEL_CONST = 1; | 7 const TOP_LEVEL_CONST = 1; |
| 8 const TOP_LEVEL_CONST_REF = TOP_LEVEL_CONST; | 8 const TOP_LEVEL_CONST_REF = TOP_LEVEL_CONST; |
| 9 const TOP_LEVEL_NULL = null; | 9 const TOP_LEVEL_NULL = null; |
| 10 | 10 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 } | 21 } |
| 22 | 22 |
| 23 static void testTopLevel() { | 23 static void testTopLevel() { |
| 24 topLevel = function() { | 24 topLevel = function() { |
| 25 return 2; | 25 return 2; |
| 26 }; | 26 }; |
| 27 Expect.equals(1, TOP_LEVEL_CONST); | 27 Expect.equals(1, TOP_LEVEL_CONST); |
| 28 Expect.equals(1, TOP_LEVEL_CONST_REF); | 28 Expect.equals(1, TOP_LEVEL_CONST_REF); |
| 29 Expect.equals(2, topLevel()); | 29 Expect.equals(2, topLevel()); |
| 30 | 30 |
| 31 expectThrowsNoSuchMethod(() { | 31 expectThrowsNotClosure(() { |
| 32 TOP_LEVEL_CONST(); /// static type warning | 32 TOP_LEVEL_CONST(); /// static type warning |
| 33 }); | 33 }); |
| 34 expectThrowsNoSuchMethod(() { | 34 expectThrowsNotClosure(() { |
| 35 (TOP_LEVEL_CONST)(); /// static type warning | 35 (TOP_LEVEL_CONST)(); /// static type warning |
| 36 }); | 36 }); |
| 37 } | 37 } |
| 38 | 38 |
| 39 static void testField() { | 39 static void testField() { |
| 40 A a = new A(); | 40 A a = new A(); |
| 41 a.field = () => 42; | 41 a.field = () => 42; |
| 42 Expect.equals(42, a.field()); | 42 Expect.equals(42, a.field()); |
| 43 Expect.equals(42, (a.field)()); | 43 Expect.equals(42, (a.field)()); |
| 44 | 44 |
| 45 a.field = () => 87; | 45 a.field = () => 87; |
| 46 Expect.equals(87, a.field()); | 46 Expect.equals(87, a.field()); |
| 47 Expect.equals(87, (a.field)()); | 47 Expect.equals(87, (a.field)()); |
| 48 | 48 |
| 49 a.field = 99; | 49 a.field = 99; |
| 50 expectThrowsNoSuchMethod(() { a.field(); }); | 50 expectThrowsNotClosure(() { a.field(); }); |
| 51 expectThrowsNoSuchMethod(() { (a.field)(); }); | 51 expectThrowsNotClosure(() { (a.field)(); }); |
| 52 } | 52 } |
| 53 | 53 |
| 54 static void testGetter() { | 54 static void testGetter() { |
| 55 A a = new A(); | 55 A a = new A(); |
| 56 a.field = () => 42; | 56 a.field = () => 42; |
| 57 Expect.equals(42, a.getter()); | 57 Expect.equals(42, a.getter()); |
| 58 Expect.equals(42, (a.getter)()); | 58 Expect.equals(42, (a.getter)()); |
| 59 | 59 |
| 60 a.field = () => 87; | 60 a.field = () => 87; |
| 61 Expect.equals(87, a.getter()); | 61 Expect.equals(87, a.getter()); |
| 62 Expect.equals(87, (a.getter)()); | 62 Expect.equals(87, (a.getter)()); |
| 63 | 63 |
| 64 a.field = 99; | 64 a.field = 99; |
| 65 expectThrowsNoSuchMethod(() { a.getter(); }); | 65 expectThrowsNotClosure(() { a.getter(); }); |
| 66 expectThrowsNoSuchMethod(() { (a.getter)(); }); | 66 expectThrowsNotClosure(() { (a.getter)(); }); |
| 67 } | 67 } |
| 68 | 68 |
| 69 static void testMethod() { | 69 static void testMethod() { |
| 70 A a = new A(); | 70 A a = new A(); |
| 71 a.field = () => 42; | 71 a.field = () => 42; |
| 72 Expect.equals(true, a.method() is Function); | 72 Expect.equals(true, a.method() is Function); |
| 73 Expect.equals(42, a.method()()); | 73 Expect.equals(42, a.method()()); |
| 74 | 74 |
| 75 a.field = () => 87; | 75 a.field = () => 87; |
| 76 Expect.equals(true, a.method() is Function); | 76 Expect.equals(true, a.method() is Function); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 100 Expect.equals("xyzgf", b.g3(b.x, b.y, b.z)); | 100 Expect.equals("xyzgf", b.g3(b.x, b.y, b.z)); |
| 101 b = new B(); | 101 b = new B(); |
| 102 Expect.equals("gxyzf", (b.g3)(b.x, b.y, b.z)); | 102 Expect.equals("gxyzf", (b.g3)(b.x, b.y, b.z)); |
| 103 | 103 |
| 104 b = new B(); | 104 b = new B(); |
| 105 Expect.equals("yzxgf", b.g3(b.y, b.z, b.x)); | 105 Expect.equals("yzxgf", b.g3(b.y, b.z, b.x)); |
| 106 b = new B(); | 106 b = new B(); |
| 107 Expect.equals("gyzxf", (b.g3)(b.y, b.z, b.x)); | 107 Expect.equals("gyzxf", (b.g3)(b.y, b.z, b.x)); |
| 108 } | 108 } |
| 109 | 109 |
| 110 static expectThrowsNoSuchMethod(fn) { | 110 static void expectThrowsNotClosure(fn) { |
| 111 var exception = catchException(fn); | 111 var exception = catchException(fn); |
| 112 if (exception is! NoSuchMethodError) { | 112 if (!(exception is ObjectNotClosureException)) { |
| 113 Expect.fail("Wrong exception. Expected: NoSuchMethodError" | 113 Expect.fail("Wrong exception. Expected: ObjectNotClosureException" |
| 114 " got: ${exception}"); | 114 " got: ${exception}"); |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 | 117 |
| 118 static catchException(fn) { | 118 static catchException(fn) { |
| 119 bool caught = false; | 119 bool caught = false; |
| 120 var result = null; | 120 var result = null; |
| 121 try { | 121 try { |
| 122 fn(); | 122 fn(); |
| 123 Expect.equals(true, false); // Shouldn't reach this. | 123 Expect.equals(true, false); // Shouldn't reach this. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 get z { _mark('z'); return 2; } | 156 get z { _mark('z'); return 2; } |
| 157 | 157 |
| 158 _mark(m) { _order.add(m); return _order.toString(); } | 158 _mark(m) { _order.add(m); return _order.toString(); } |
| 159 StringBuffer _order; | 159 StringBuffer _order; |
| 160 | 160 |
| 161 } | 161 } |
| 162 | 162 |
| 163 main() { | 163 main() { |
| 164 CallThroughGetterTest.testMain(); | 164 CallThroughGetterTest.testMain(); |
| 165 } | 165 } |
| OLD | NEW |