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