| 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 assignment to fields of native methods. | |
| 6 | |
| 7 import "package:expect/expect.dart"; | |
| 8 import 'native_metadata.dart'; | |
| 9 | |
| 10 @Native("*A") | |
| 11 class A { | |
| 12 int foo; | |
| 13 } | |
| 14 | |
| 15 @Native("*B") | |
| 16 class B { | |
| 17 String foo; | |
| 18 } | |
| 19 | |
| 20 @native A makeA() { return new A(); } | |
| 21 @native B makeB() { return new B(); } | |
| 22 | |
| 23 @Native(""" | |
| 24 function A() {} | |
| 25 | |
| 26 function B() {} | |
| 27 | |
| 28 makeA = function(){return new A;}; | |
| 29 makeB = function(){return new B;}; | |
| 30 """) | |
| 31 void setup(); | |
| 32 | |
| 33 expectThrows(action()) { | |
| 34 bool threw = false; | |
| 35 try { | |
| 36 action(); | |
| 37 } catch (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 a.foo = 123; | |
| 49 expectThrows(() => a.foo = 'xxx'); | |
| 50 Expect.equals(123, a.foo); | |
| 51 | |
| 52 b.foo = 'hello'; | |
| 53 expectThrows(() => b.foo = 123); | |
| 54 Expect.equals('hello', b.foo); | |
| 55 | |
| 56 // Check that we throw the same errors when the locals are typed. | |
| 57 A aa = things[0]; | |
| 58 B bb = things[1]; | |
| 59 | |
| 60 aa.foo = 124; | |
| 61 expectThrows(() => aa.foo = 'xxx'); | |
| 62 Expect.equals(124, aa.foo); | |
| 63 | |
| 64 bb.foo = 'hello'; | |
| 65 expectThrows(() => bb.foo = 124); | |
| 66 Expect.equals('hello', bb.foo); | |
| 67 } | |
| 68 | |
| 69 uncheckedModeTest() { | |
| 70 var things = [makeA(), makeB()]; | |
| 71 var a = things[0]; | |
| 72 var b = things[1]; | |
| 73 | |
| 74 a.foo = 123; | |
| 75 Expect.equals(123, a.foo); | |
| 76 a.foo = 'xxx'; | |
| 77 Expect.equals('xxx', a.foo); | |
| 78 | |
| 79 b.foo = 'hello'; | |
| 80 Expect.equals('hello', b.foo); | |
| 81 b.foo = 123; | |
| 82 Expect.equals(b.foo, 123); | |
| 83 | |
| 84 // Check that we do not throw errors when the locals are typed. | |
| 85 A aa = things[0]; | |
| 86 B bb = things[1]; | |
| 87 | |
| 88 aa.foo = 124; | |
| 89 Expect.equals(124, aa.foo); | |
| 90 a.foo = 'yyy'; | |
| 91 Expect.equals('yyy', a.foo); | |
| 92 | |
| 93 b.foo = 'hello'; | |
| 94 Expect.equals('hello', b.foo); | |
| 95 b.foo = 124; | |
| 96 Expect.equals(b.foo, 124); | |
| 97 } | |
| 98 | |
| 99 bool isCheckedMode() { | |
| 100 var stuff = [1, 'string']; | |
| 101 var a = stuff[0]; | |
| 102 // Checked-mode detection. | |
| 103 try { | |
| 104 String s = a; | |
| 105 return false; | |
| 106 } catch (e) { | |
| 107 // Ignore. | |
| 108 } | |
| 109 return true; | |
| 110 } | |
| 111 | |
| 112 main() { | |
| 113 setup(); | |
| 114 | |
| 115 if (isCheckedMode()) { | |
| 116 checkedModeTest(); | |
| 117 } else { | |
| 118 uncheckedModeTest(); | |
| 119 } | |
| 120 } | |
| OLD | NEW |