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 import "package:expect/expect.dart"; |
| 6 |
| 7 // Bind a method to a variable that can be invoked as a function |
| 8 |
| 9 class A { |
| 10 int a; |
| 11 |
| 12 static var func; |
| 13 |
| 14 A(this.a) { } |
| 15 |
| 16 static foo() { return 4; } |
| 17 |
| 18 bar() { return a; } |
| 19 |
| 20 int baz() { return a; } |
| 21 |
| 22 getThis() { return this.bar; } |
| 23 |
| 24 getNoThis() { return bar; } |
| 25 |
| 26 methodArgs(arg) { return arg + a; } |
| 27 |
| 28 selfReference () { return selfReference; } |
| 29 |
| 30 invokeBaz() { return (baz)(); } |
| 31 |
| 32 invokeBar(var obj) { return (obj.bar)(); } |
| 33 |
| 34 invokeThisBar() { return (this.bar)(); } |
| 35 |
| 36 implicitStaticRef() { return foo; } |
| 37 } |
| 38 |
| 39 class B { |
| 40 static foo() { return -1; } |
| 41 } |
| 42 |
| 43 class C { |
| 44 C() { } |
| 45 var f; |
| 46 } |
| 47 |
| 48 topLevel99() { |
| 49 return 99; |
| 50 } |
| 51 |
| 52 var topFunc; |
| 53 |
| 54 class D extends A { |
| 55 D(a): super(a) { } |
| 56 getSuper() { return super.bar; } |
| 57 } |
| 58 |
| 59 class MethodBindingTest { |
| 60 static test() { |
| 61 |
| 62 // Create closure from global |
| 63 Expect.equals(99, topLevel99()); |
| 64 Function f99 = topLevel99; |
| 65 Expect.equals(99, f99()); |
| 66 |
| 67 // Invoke closure through a global |
| 68 topFunc = f99; |
| 69 Expect.equals(99, topFunc()); |
| 70 |
| 71 // Create closure from static method |
| 72 Function f4 = A.foo; |
| 73 Expect.equals(4, f4()); |
| 74 |
| 75 // Create closure from instance method |
| 76 var o5 = new A(5); |
| 77 Function f5 = o5.bar; |
| 78 Expect.equals(5, f5()); |
| 79 |
| 80 // Assign closure to field and invoke it |
| 81 var c = new C(); |
| 82 c.f = () => "success"; |
| 83 Expect.equals("success", c.f()); |
| 84 |
| 85 // referencing instance method with explicit 'this' qualiier |
| 86 var o6 = new A(6); |
| 87 var f6 = o6.getThis(); |
| 88 Expect.equals(6, f6()); |
| 89 |
| 90 // referencing an instance method with no qualifier |
| 91 var o7 = new A(7); |
| 92 var f7 = o7.getNoThis(); |
| 93 Expect.equals(7, f7()); |
| 94 |
| 95 // bind a method that takes arguments |
| 96 var o8 = new A(8); |
| 97 Function f8 = o8.methodArgs; |
| 98 Expect.equals(9, f8(1)); |
| 99 |
| 100 // Self referential method |
| 101 var o9 = new A(9); |
| 102 Function f9 = o9.selfReference; |
| 103 |
| 104 // invoking a known method as if it were a bound closure... |
| 105 var o10 = new A(10); |
| 106 Expect.equals(10, o10.invokeBaz()); |
| 107 |
| 108 // invoking a known method as if it were a bound closure... |
| 109 var o11 = new A(11); |
| 110 Expect.equals(10, o11.invokeBar(o10)); |
| 111 |
| 112 // invoking a known method as if it were a bound closure... |
| 113 var o12 = new A(12); |
| 114 Expect.equals(12, o12.invokeThisBar()); |
| 115 |
| 116 // bind to a static variable with no explicit class qualifier |
| 117 var o13 = new A(13); |
| 118 Function f13 = o13.implicitStaticRef(); |
| 119 Expect.equals(4, f13()); |
| 120 |
| 121 var o14 = new D(14); |
| 122 Function f14 = o14.getSuper(); |
| 123 Expect.equals(14, f14()); |
| 124 |
| 125 // Assign static field to a function and invoke it. |
| 126 A.func = A.foo; |
| 127 Expect.equals(4, A.func()); |
| 128 |
| 129 // bind a function that is possibly native in Javascript. |
| 130 String o15 = 'hithere'; |
| 131 var f15 = o15.substring; |
| 132 Expect.equals('i', f15(1, 2)); |
| 133 |
| 134 var o16 = 'hithere'; |
| 135 var f16 = o16.substring; |
| 136 Expect.equals('i', f16(1, 2)); |
| 137 |
| 138 var f17 = 'hithere'.substring; |
| 139 Expect.equals('i', f17(1, 2)); |
| 140 } |
| 141 |
| 142 static testMain() { |
| 143 test(); |
| 144 } |
| 145 } |
| 146 |
| 147 main() { |
| 148 MethodBindingTest.testMain(); |
| 149 } |
OLD | NEW |