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