OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 library source_map_name_test; |
| 6 |
| 7 import 'package:async_helper/async_helper.dart'; |
| 8 import 'package:expect/expect.dart'; |
| 9 import 'package:compiler/src/dart2jslib.dart'; |
| 10 import 'package:compiler/src/elements/elements.dart'; |
| 11 import 'package:compiler/src/io/source_information.dart'; |
| 12 import 'memory_compiler.dart'; |
| 13 |
| 14 const String SOURCE = ''' |
| 15 |
| 16 var toplevelField; |
| 17 void toplevelMethod() {} |
| 18 void toplevelAnonymous() { |
| 19 var foo = () {}; |
| 20 } |
| 21 void toplevelLocal() { |
| 22 void localMethod() {} |
| 23 } |
| 24 |
| 25 class Class { |
| 26 Class() { |
| 27 var foo = () {}; |
| 28 } |
| 29 Class.named() { |
| 30 void localMethod() {} |
| 31 } |
| 32 static var staticField; |
| 33 static staticMethod() {} |
| 34 static void staticAnonymous() { |
| 35 var foo = () {}; |
| 36 } |
| 37 static void staticLocal() { |
| 38 void localMethod() {} |
| 39 } |
| 40 var instanceField; |
| 41 instanceMethod() {} |
| 42 void instanceAnonymous() { |
| 43 var foo = () {}; |
| 44 } |
| 45 void instanceLocal() { |
| 46 void localMethod() {} |
| 47 } |
| 48 void instanceNestedLocal() { |
| 49 void localMethod() { |
| 50 var foo = () {}; |
| 51 void nestedLocalMethod() {} |
| 52 } |
| 53 } |
| 54 } |
| 55 |
| 56 main() { |
| 57 toplevelField = toplevelMethod(); |
| 58 toplevelAnonymous(); |
| 59 toplevelLocal(); |
| 60 |
| 61 Class.staticField = Class.staticMethod; |
| 62 Class.staticAnonymous(); |
| 63 Class.staticLocal(); |
| 64 |
| 65 var c = new Class(); |
| 66 c = new Class.named(); |
| 67 c.instanceField = c.instanceMethod(); |
| 68 c.instanceAnonymous(); |
| 69 c.instanceLocal(); |
| 70 c.instanceNestedLocal(); |
| 71 } |
| 72 '''; |
| 73 |
| 74 check(Element element, String expectedName) { |
| 75 String name = computeElementNameForSourceMaps(element); |
| 76 Expect.equals( |
| 77 expectedName, |
| 78 name, |
| 79 "Unexpected name '$name' for $element, expected '$expectedName'."); |
| 80 } |
| 81 |
| 82 main() { |
| 83 asyncTest(() async { |
| 84 Compiler compiler = compilerFor({'main.dart': SOURCE}); |
| 85 await compiler.run(Uri.parse('memory:main.dart')); |
| 86 |
| 87 Element lookup(String name) { |
| 88 Element element; |
| 89 int dotPosition = name.indexOf('.'); |
| 90 if (dotPosition != -1) { |
| 91 String clsName = name.substring(0, dotPosition); |
| 92 ClassElement cls = compiler.mainApp.find(clsName); |
| 93 Expect.isNotNull(cls, "Class '$clsName' not found."); |
| 94 element = cls.localLookup(name.substring(dotPosition + 1)); |
| 95 } else { |
| 96 element = compiler.mainApp.find(name); |
| 97 } |
| 98 Expect.isNotNull(element, "Element '$name' not found."); |
| 99 return element; |
| 100 } |
| 101 |
| 102 void checkName(String expectedName, |
| 103 [List<String> expectedClosureNames, |
| 104 String lookupName]) { |
| 105 if (lookupName == null) { |
| 106 lookupName = expectedName; |
| 107 } |
| 108 var element = lookup(lookupName); |
| 109 check(element, expectedName); |
| 110 if (element.isConstructor) { |
| 111 var constructorBody = |
| 112 element.enclosingClass.lookupBackendMember(element.name); |
| 113 Expect.isNotNull(element, |
| 114 "Constructor body '${element.name}' not found."); |
| 115 check(constructorBody, expectedName); |
| 116 } |
| 117 |
| 118 if (expectedClosureNames != null) { |
| 119 int index = 0; |
| 120 for (var closure in element.nestedClosures) { |
| 121 String expectedName = expectedClosureNames[index]; |
| 122 check(closure, expectedName); |
| 123 check(closure.expression, expectedName); |
| 124 check(closure.enclosingClass, expectedName); |
| 125 index++; |
| 126 } |
| 127 } |
| 128 } |
| 129 |
| 130 checkName('toplevelField'); |
| 131 checkName('toplevelMethod'); |
| 132 checkName('toplevelAnonymous', |
| 133 ['toplevelAnonymous.<anonymous function>']); |
| 134 checkName('toplevelLocal', |
| 135 ['toplevelLocal.localMethod']); |
| 136 checkName('Class'); |
| 137 checkName('main'); |
| 138 |
| 139 checkName('Class.staticField'); |
| 140 checkName('Class.staticMethod'); |
| 141 checkName('Class.staticAnonymous', |
| 142 ['Class.staticAnonymous.<anonymous function>']); |
| 143 checkName('Class.staticLocal', |
| 144 ['Class.staticLocal.localMethod']); |
| 145 |
| 146 checkName('Class', |
| 147 ['Class.<anonymous function>'], |
| 148 'Class.'); |
| 149 checkName('Class.named', |
| 150 ['Class.named.localMethod']); |
| 151 |
| 152 checkName('Class.instanceField'); |
| 153 checkName('Class.instanceMethod'); |
| 154 checkName('Class.instanceAnonymous', |
| 155 ['Class.instanceAnonymous.<anonymous function>']); |
| 156 checkName('Class.instanceLocal', |
| 157 ['Class.instanceLocal.localMethod']); |
| 158 checkName('Class.instanceNestedLocal', |
| 159 ['Class.instanceNestedLocal.localMethod', |
| 160 'Class.instanceNestedLocal.localMethod.<anonymous function>', |
| 161 'Class.instanceNestedLocal.localMethod.nestedLocalMethod']); |
| 162 |
| 163 |
| 164 }); |
| 165 } |
OLD | NEW |