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 // Test the feature where the native string declares the native method's name. | 5 // Test the feature where the native string declares the native method's name. |
6 | 6 |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 import 'dart:_js_helper' show Native, JSName; | 8 import 'dart:_js_helper' show Native, JSName; |
9 | 9 |
10 @Native("A") | 10 @Native("A") |
11 class A { | 11 class A { |
12 @JSName('fooA') | 12 @JSName('fooA') |
13 int foo() native; | 13 int foo() native ; |
14 } | 14 } |
15 | 15 |
16 @Native("B") | 16 @Native("B") |
17 class B extends A { | 17 class B extends A { |
18 @JSName('fooB') | 18 @JSName('fooB') |
19 int foo() native; | 19 int foo() native ; |
20 } | 20 } |
21 | 21 |
22 makeA() native; | 22 makeA() native ; |
23 makeB() native; | 23 makeB() native ; |
24 | 24 |
25 void setup() native """ | 25 void setup() native """ |
26 // This code is all inside 'setup' and so not accesible from the global scope. | 26 // This code is all inside 'setup' and so not accesible from the global scope. |
27 function inherits(child, parent) { | 27 function inherits(child, parent) { |
28 if (child.prototype.__proto__) { | 28 if (child.prototype.__proto__) { |
29 child.prototype.__proto__ = parent.prototype; | 29 child.prototype.__proto__ = parent.prototype; |
30 } else { | 30 } else { |
31 function tmp() {}; | 31 function tmp() {}; |
32 tmp.prototype = parent.prototype; | 32 tmp.prototype = parent.prototype; |
33 child.prototype = new tmp(); | 33 child.prototype = new tmp(); |
(...skipping 11 matching lines...) Expand all Loading... |
45 """; | 45 """; |
46 | 46 |
47 testDynamic() { | 47 testDynamic() { |
48 var things = [makeA(), makeB()]; | 48 var things = [makeA(), makeB()]; |
49 var a = things[0]; | 49 var a = things[0]; |
50 var b = things[1]; | 50 var b = things[1]; |
51 | 51 |
52 Expect.equals(100, a.foo()); | 52 Expect.equals(100, a.foo()); |
53 Expect.equals(200, b.foo()); | 53 Expect.equals(200, b.foo()); |
54 | 54 |
55 expectNoSuchMethod((){ a.fooA(); }, 'fooA should be invisible on A'); | 55 expectNoSuchMethod(() { |
56 expectNoSuchMethod((){ b.fooA(); }, 'fooA should be invisible on B'); | 56 a.fooA(); |
| 57 }, 'fooA should be invisible on A'); |
| 58 expectNoSuchMethod(() { |
| 59 b.fooA(); |
| 60 }, 'fooA should be invisible on B'); |
57 | 61 |
58 expectNoSuchMethod((){ a.fooB(); }, 'fooB should be absent on A'); | 62 expectNoSuchMethod(() { |
59 expectNoSuchMethod((){ b.fooB(); }, 'fooA should be invisible on B'); | 63 a.fooB(); |
| 64 }, 'fooB should be absent on A'); |
| 65 expectNoSuchMethod(() { |
| 66 b.fooB(); |
| 67 }, 'fooA should be invisible on B'); |
60 } | 68 } |
61 | 69 |
62 testTyped() { | 70 testTyped() { |
63 A a = makeA(); | 71 A a = makeA(); |
64 B b = makeB(); | 72 B b = makeB(); |
65 | 73 |
66 Expect.equals(100, a.foo()); | 74 Expect.equals(100, a.foo()); |
67 Expect.equals(200, b.foo()); | 75 Expect.equals(200, b.foo()); |
68 } | 76 } |
69 | 77 |
70 main() { | 78 main() { |
71 setup(); | 79 setup(); |
72 | 80 |
73 testDynamic(); | 81 testDynamic(); |
74 testTyped(); | 82 testTyped(); |
75 } | 83 } |
76 | 84 |
77 expectNoSuchMethod(action, note) { | 85 expectNoSuchMethod(action, note) { |
78 bool caught = false; | 86 bool caught = false; |
79 try { | 87 try { |
80 action(); | 88 action(); |
81 } catch (ex) { | 89 } catch (ex) { |
82 caught = true; | 90 caught = true; |
83 Expect.isTrue(ex is NoSuchMethodError, note); | 91 Expect.isTrue(ex is NoSuchMethodError, note); |
84 } | 92 } |
85 Expect.isTrue(caught, note); | 93 Expect.isTrue(caught, note); |
86 } | 94 } |
OLD | NEW |