OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 // Note: This test relies on LF line endings in the source file. |
| 6 |
| 7 import "dart:mirrors"; |
| 8 import "package:expect/expect.dart"; |
| 9 import "method_mirror_source_other.dart"; |
| 10 |
| 11 expectSource(Mirror mirror, String source) { |
| 12 MethodMirror methodMirror; |
| 13 if (mirror is ClosureMirror) { |
| 14 methodMirror = mirror.function; |
| 15 } else { |
| 16 methodMirror = mirror as MethodMirror; |
| 17 } |
| 18 Expect.isTrue(methodMirror is MethodMirror); |
| 19 Expect.equals(source, methodMirror.source); |
| 20 } |
| 21 |
| 22 foo1() {} |
| 23 doSomething(e) => e; |
| 24 |
| 25 int get x => 42; |
| 26 set x(value) { } |
| 27 |
| 28 class S {} |
| 29 |
| 30 class C extends S { |
| 31 |
| 32 var _x; |
| 33 var _y; |
| 34 |
| 35 C(this._x, y) |
| 36 : _y = y, |
| 37 super(); |
| 38 |
| 39 factory C.other(num z) {} |
| 40 factory C.other2() {} |
| 41 factory C.other3() = C.other2; |
| 42 |
| 43 static dynamic foo() { |
| 44 // Happy foo. |
| 45 } |
| 46 |
| 47 // Some comment. |
| 48 |
| 49 void bar() { /* Not so happy bar. */ } |
| 50 |
| 51 num get someX => |
| 52 181; |
| 53 |
| 54 set someX(v) { |
| 55 // Discard this one. |
| 56 } |
| 57 } |
| 58 |
| 59 |
| 60 main() { |
| 61 // Top-level members |
| 62 LibraryMirror lib = reflectClass(C).owner; |
| 63 expectSource(lib.declarations[#foo1], |
| 64 "foo1() {}"); |
| 65 expectSource(lib.declarations[#x], |
| 66 "int get x => 42;"); |
| 67 expectSource(lib.declarations[const Symbol("x=")], |
| 68 "set x(value) { }"); |
| 69 |
| 70 // Class members |
| 71 ClassMirror cm = reflectClass(C); |
| 72 expectSource(cm.declarations[#foo], |
| 73 "static dynamic foo() {\n" |
| 74 " // Happy foo.\n" |
| 75 " }"); |
| 76 expectSource(cm.declarations[#bar], |
| 77 "void bar() { /* Not so happy bar. */ }"); |
| 78 expectSource(cm.declarations[#someX], |
| 79 "num get someX =>\n" |
| 80 " 181;"); |
| 81 expectSource(cm.declarations[const Symbol("someX=")], |
| 82 "set someX(v) {\n" |
| 83 " // Discard this one.\n" |
| 84 " }"); |
| 85 expectSource(cm.declarations[#C], |
| 86 "C(this._x, y)\n" |
| 87 " : _y = y,\n" |
| 88 " super();"); |
| 89 expectSource(cm.declarations[#C.other], |
| 90 "factory C.other(num z) {}"); |
| 91 expectSource(cm.declarations[#C.other3], |
| 92 "factory C.other3() = C.other2;"); |
| 93 |
| 94 // Closures |
| 95 expectSource(reflect((){}), "(){}"); |
| 96 expectSource(reflect((x,y,z) { return x*y*z; }), "(x,y,z) { return x*y*z; }"); |
| 97 expectSource(reflect((e) => doSomething(e)), "(e) => doSomething(e)"); |
| 98 |
| 99 namedClosure(x,y,z) => 1; |
| 100 var a = () {}; |
| 101 expectSource(reflect(namedClosure), "namedClosure(x,y,z) => 1;"); |
| 102 expectSource(reflect(a), "() {}"); |
| 103 |
| 104 // Function at first line. |
| 105 LibraryMirror otherLib = reflectClass(SomethingInOther).owner; |
| 106 expectSource(otherLib.declarations[#main], |
| 107 """main() { |
| 108 print("Blah"); |
| 109 }"""); |
| 110 } |
OLD | NEW |