| 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 | |
| 5 // Test that type checks occur on native methods. | |
| 6 | |
| 7 import "package:expect/expect.dart"; | |
| 8 import 'native_metadata.dart'; | |
| 9 | |
| 10 @Native("*A") | |
| 11 class A { | |
| 12 @native int foo(int x); | |
| 13 @native int cmp(A other); | |
| 14 } | |
| 15 | |
| 16 @Native("*B") | |
| 17 class B { | |
| 18 @native String foo(String x); | |
| 19 @native int cmp(B other); | |
| 20 } | |
| 21 | |
| 22 @native A makeA() { return new A(); } | |
| 23 @native B makeB() { return new B(); } | |
| 24 | |
| 25 @Native(""" | |
| 26 function A() {} | |
| 27 A.prototype.foo = function (x) { return x + 1; }; | |
| 28 A.prototype.cmp = function (x) { return 0; }; | |
| 29 | |
| 30 function B() {} | |
| 31 B.prototype.foo = function (x) { return x + 'ha!'; }; | |
| 32 B.prototype.cmp = function (x) { return 1; }; | |
| 33 | |
| 34 makeA = function(){return new A;}; | |
| 35 makeB = function(){return new B;}; | |
| 36 """) | |
| 37 void setup(); | |
| 38 | |
| 39 expectThrows(action()) { | |
| 40 bool threw = false; | |
| 41 try { | |
| 42 action(); | |
| 43 } catch (e) { | |
| 44 threw = true; | |
| 45 } | |
| 46 Expect.isTrue(threw); | |
| 47 } | |
| 48 | |
| 49 checkedModeTest() { | |
| 50 var things = [makeA(), makeB()]; | |
| 51 var a = things[0]; | |
| 52 var b = things[1]; | |
| 53 | |
| 54 Expect.equals(124, a.foo(123)); | |
| 55 expectThrows(() => a.foo('xxx')); | |
| 56 | |
| 57 Expect.equals('helloha!', b.foo('hello')); | |
| 58 expectThrows(() => b.foo(123)); | |
| 59 | |
| 60 Expect.equals(0, a.cmp(a)); | |
| 61 expectThrows(() => a.cmp(b)); | |
| 62 expectThrows(() => a.cmp(5)); | |
| 63 | |
| 64 Expect.equals(1, b.cmp(b)); | |
| 65 expectThrows(() => b.cmp(a)); | |
| 66 expectThrows(() => b.cmp(5)); | |
| 67 | |
| 68 // Check that we throw the same errors when the locals are typed. | |
| 69 A aa = things[0]; | |
| 70 B bb = things[1]; | |
| 71 | |
| 72 Expect.equals(124, aa.foo(123)); | |
| 73 expectThrows(() => aa.foo('xxx')); | |
| 74 | |
| 75 Expect.equals('helloha!', bb.foo('hello')); | |
| 76 expectThrows(() => bb.foo(123)); | |
| 77 | |
| 78 Expect.equals(0, aa.cmp(aa)); | |
| 79 expectThrows(() => aa.cmp(bb)); | |
| 80 expectThrows(() => aa.cmp(5)); | |
| 81 | |
| 82 Expect.equals(1, bb.cmp(bb)); | |
| 83 expectThrows(() => bb.cmp(aa)); | |
| 84 expectThrows(() => bb.cmp(5)); | |
| 85 } | |
| 86 | |
| 87 uncheckedModeTest() { | |
| 88 var things = [makeA(), makeB()]; | |
| 89 var a = things[0]; | |
| 90 var b = things[1]; | |
| 91 | |
| 92 Expect.equals(124, a.foo(123)); | |
| 93 Expect.equals('xxx1', a.foo('xxx')); | |
| 94 | |
| 95 Expect.equals('helloha!', b.foo('hello')); | |
| 96 Expect.equals('123ha!', b.foo(123)); | |
| 97 | |
| 98 Expect.equals(0, a.cmp(a)); | |
| 99 Expect.equals(0, a.cmp(b)); | |
| 100 Expect.equals(0, a.cmp(5)); | |
| 101 | |
| 102 Expect.equals(1, b.cmp(b)); | |
| 103 Expect.equals(1, b.cmp(a)); | |
| 104 Expect.equals(1, b.cmp(5)); | |
| 105 | |
| 106 // Check that we do not throw errors when the locals are typed. | |
| 107 A aa = things[0]; | |
| 108 B bb = things[1]; | |
| 109 | |
| 110 Expect.equals(124, aa.foo(123)); | |
| 111 Expect.equals('xxx1', aa.foo('xxx')); | |
| 112 | |
| 113 Expect.equals('helloha!', bb.foo('hello')); | |
| 114 Expect.equals('123ha!', bb.foo(123)); | |
| 115 | |
| 116 Expect.equals(0, aa.cmp(aa)); | |
| 117 Expect.equals(0, aa.cmp(bb)); | |
| 118 Expect.equals(0, aa.cmp(5)); | |
| 119 | |
| 120 Expect.equals(1, bb.cmp(bb)); | |
| 121 Expect.equals(1, bb.cmp(aa)); | |
| 122 Expect.equals(1, bb.cmp(5)); | |
| 123 } | |
| 124 | |
| 125 bool isCheckedMode() { | |
| 126 var stuff = [1, 'string']; | |
| 127 var a = stuff[0]; | |
| 128 // Checked-mode detection. | |
| 129 try { | |
| 130 String s = a; | |
| 131 return false; | |
| 132 } catch (e) { | |
| 133 // Ignore. | |
| 134 } | |
| 135 return true; | |
| 136 } | |
| 137 | |
| 138 main() { | |
| 139 setup(); | |
| 140 | |
| 141 if (isCheckedMode()) { | |
| 142 checkedModeTest(); | |
| 143 } else { | |
| 144 uncheckedModeTest(); | |
| 145 } | |
| 146 } | |
| OLD | NEW |