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