Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:collection'; | 6 import 'dart:collection'; |
| 7 | 7 |
| 8 import 'package:async_helper/async_helper.dart'; | 8 import 'package:async_helper/async_helper.dart'; |
| 9 import 'package:expect/expect.dart'; | 9 import 'package:expect/expect.dart'; |
| 10 import 'package:compiler/src/constants/expressions.dart'; | 10 import 'package:compiler/src/constants/expressions.dart'; |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 classC.allSupertypes.toString()); | 125 classC.allSupertypes.toString()); |
| 126 }), | 126 }), |
| 127 MockCompiler.create((MockCompiler compiler) { | 127 MockCompiler.create((MockCompiler compiler) { |
| 128 compiler.parseScript(""" | 128 compiler.parseScript(""" |
| 129 class X<T> {} | 129 class X<T> {} |
| 130 class Foo extends X<Foo> {} | 130 class Foo extends X<Foo> {} |
| 131 class Bar extends Foo implements X<Bar> {} | 131 class Bar extends Foo implements X<Bar> {} |
| 132 """); | 132 """); |
| 133 compiler.resolveStatement("Bar bar;"); | 133 compiler.resolveStatement("Bar bar;"); |
| 134 ClassElement classBar = compiler.mainApp.find("Bar"); | 134 ClassElement classBar = compiler.mainApp.find("Bar"); |
| 135 Expect.equals(0, compiler.warnings.length); | 135 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 136 Expect.equals(1, compiler.errors.length); | 136 Expect.equals(0, collector.warnings.length); |
| 137 Expect.equals(1, collector.errors.length); | |
| 137 Expect.equals(MessageKind.MULTI_INHERITANCE, | 138 Expect.equals(MessageKind.MULTI_INHERITANCE, |
| 138 compiler.errors[0].message.kind); | 139 collector.errors.first.message.kind); |
| 139 Expect.equals(0, compiler.crashes.length); | 140 Expect.equals(0, collector.crashes.length); |
| 140 }), | 141 }), |
| 141 ]); | 142 ]); |
| 142 } | 143 } |
| 143 | 144 |
| 144 Future testTypeVariables() { | 145 Future testTypeVariables() { |
| 145 matchResolvedTypes(visitor, text, name, expectedElements) { | 146 matchResolvedTypes(visitor, text, name, expectedElements) { |
| 146 VariableDefinitions definition = parseStatement(text); | 147 VariableDefinitions definition = parseStatement(text); |
| 147 visitor.visit(definition.type); | 148 visitor.visit(definition.type); |
| 148 InterfaceType type = visitor.registry.mapping.getType(definition.type); | 149 InterfaceType type = visitor.registry.mapping.getType(definition.type); |
| 149 Expect.equals(definition.type.typeArguments.slowLength(), | 150 Expect.equals(definition.type.typeArguments.slowLength(), |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 165 matchResolvedTypes(visitor, 'Foo<int, String> x;', 'Foo', | 166 matchResolvedTypes(visitor, 'Foo<int, String> x;', 'Foo', |
| 166 [compiler.coreClasses.intClass, | 167 [compiler.coreClasses.intClass, |
| 167 compiler.coreClasses.stringClass]); | 168 compiler.coreClasses.stringClass]); |
| 168 matchResolvedTypes(visitor, 'Foo<Foo, Foo> x;', 'Foo', | 169 matchResolvedTypes(visitor, 'Foo<Foo, Foo> x;', 'Foo', |
| 169 [foo, foo]); | 170 [foo, foo]); |
| 170 }), | 171 }), |
| 171 | 172 |
| 172 MockCompiler.create((MockCompiler compiler) { | 173 MockCompiler.create((MockCompiler compiler) { |
| 173 compiler.parseScript('class Foo<T, U> {}'); | 174 compiler.parseScript('class Foo<T, U> {}'); |
| 174 compiler.resolveStatement('Foo<notype, int> x;'); | 175 compiler.resolveStatement('Foo<notype, int> x;'); |
| 175 Expect.equals(1, compiler.warnings.length); | 176 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 177 Expect.equals(1, collector.warnings.length); | |
| 176 Expect.equals(MessageKind.CANNOT_RESOLVE_TYPE, | 178 Expect.equals(MessageKind.CANNOT_RESOLVE_TYPE, |
| 177 compiler.warnings[0].message.kind); | 179 collector.warnings.first.message.kind); |
| 178 Expect.equals(0, compiler.errors.length); | 180 Expect.equals(0, collector.errors.length); |
| 179 }), | 181 }), |
| 180 | 182 |
| 181 MockCompiler.create((MockCompiler compiler) { | 183 MockCompiler.create((MockCompiler compiler) { |
| 182 compiler.parseScript('class Foo<T, U> {}'); | 184 compiler.parseScript('class Foo<T, U> {}'); |
| 183 compiler.resolveStatement('var x = new Foo<notype, int>();'); | 185 compiler.resolveStatement('var x = new Foo<notype, int>();'); |
| 184 Expect.equals(1, compiler.warnings.length); | 186 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 185 Expect.equals(0, compiler.errors.length); | 187 Expect.equals(1, collector.warnings.length); |
| 188 Expect.equals(0, collector.errors.length); | |
| 186 Expect.equals(MessageKind.CANNOT_RESOLVE_TYPE, | 189 Expect.equals(MessageKind.CANNOT_RESOLVE_TYPE, |
| 187 compiler.warnings[0].message.kind); | 190 collector.warnings.first.message.kind); |
| 188 }), | 191 }), |
| 189 | 192 |
| 190 MockCompiler.create((MockCompiler compiler) { | 193 MockCompiler.create((MockCompiler compiler) { |
| 191 compiler.parseScript('class Foo<T> {' | 194 compiler.parseScript('class Foo<T> {' |
| 192 ' Foo<T> t;' | 195 ' Foo<T> t;' |
| 193 ' foo(Foo<T> f) {}' | 196 ' foo(Foo<T> f) {}' |
| 194 ' bar() { g(Foo<T> f) {}; g(); }' | 197 ' bar() { g(Foo<T> f) {}; g(); }' |
| 195 '}'); | 198 '}'); |
| 196 ClassElement foo = compiler.mainApp.find('Foo'); | 199 ClassElement foo = compiler.mainApp.find('Foo'); |
| 197 foo.ensureResolved(compiler.resolution); | 200 foo.ensureResolved(compiler.resolution); |
| 198 foo.lookupLocalMember('t').computeType(compiler.resolution); | 201 foo.lookupLocalMember('t').computeType(compiler.resolution); |
| 199 foo.lookupLocalMember('foo').computeType(compiler.resolution); | 202 foo.lookupLocalMember('foo').computeType(compiler.resolution); |
| 200 compiler.resolver.resolve(foo.lookupLocalMember('bar')); | 203 compiler.resolver.resolve(foo.lookupLocalMember('bar')); |
| 201 Expect.equals(0, compiler.warnings.length); | 204 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 202 Expect.equals(0, compiler.errors.length); | 205 Expect.equals(0, collector.warnings.length); |
| 206 Expect.equals(0, collector.errors.length); | |
| 203 }), | 207 }), |
| 204 ]); | 208 ]); |
| 205 } | 209 } |
| 206 | 210 |
| 207 Future testSuperCalls() { | 211 Future testSuperCalls() { |
| 208 return MockCompiler.create((MockCompiler compiler) { | 212 return MockCompiler.create((MockCompiler compiler) { |
| 209 String script = """class A { foo() {} } | 213 String script = """class A { foo() {} } |
| 210 class B extends A { foo() => super.foo(); }"""; | 214 class B extends A { foo() => super.foo(); }"""; |
| 211 compiler.parseScript(script); | 215 compiler.parseScript(script); |
| 212 compiler.resolveStatement("B b;"); | 216 compiler.resolveStatement("B b;"); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 233 } | 237 } |
| 234 | 238 |
| 235 Future testSwitch() { | 239 Future testSwitch() { |
| 236 return MockCompiler.create((MockCompiler compiler) { | 240 return MockCompiler.create((MockCompiler compiler) { |
| 237 compiler.parseScript("class Foo { foo() {" | 241 compiler.parseScript("class Foo { foo() {" |
| 238 "switch (null) { case '': break; case 2: break; } } }"); | 242 "switch (null) { case '': break; case 2: break; } } }"); |
| 239 compiler.resolveStatement("Foo foo;"); | 243 compiler.resolveStatement("Foo foo;"); |
| 240 ClassElement fooElement = compiler.mainApp.find("Foo"); | 244 ClassElement fooElement = compiler.mainApp.find("Foo"); |
| 241 FunctionElement funElement = fooElement.lookupLocalMember("foo"); | 245 FunctionElement funElement = fooElement.lookupLocalMember("foo"); |
| 242 compiler.processQueue(compiler.enqueuer.resolution, funElement); | 246 compiler.processQueue(compiler.enqueuer.resolution, funElement); |
| 243 Expect.equals(0, compiler.warnings.length); | 247 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 244 Expect.equals(1, compiler.errors.length); | 248 Expect.equals(0, collector.warnings.length); |
| 249 Expect.equals(1, collector.errors.length); | |
| 245 Expect.equals(MessageKind.SWITCH_CASE_TYPES_NOT_EQUAL, | 250 Expect.equals(MessageKind.SWITCH_CASE_TYPES_NOT_EQUAL, |
| 246 compiler.errors[0].message.kind); | 251 collector.errors.first.message.kind); |
| 247 Expect.equals(2, compiler.infos.length); | 252 Expect.equals(2, collector.infos.length); |
| 248 Expect.equals(MessageKind.SWITCH_CASE_TYPES_NOT_EQUAL_CASE, | 253 Expect.equals(MessageKind.SWITCH_CASE_TYPES_NOT_EQUAL_CASE, |
| 249 compiler.infos[0].message.kind); | 254 collector.infos.first.message.kind); |
| 250 Expect.equals(MessageKind.SWITCH_CASE_TYPES_NOT_EQUAL_CASE, | 255 Expect.equals(MessageKind.SWITCH_CASE_TYPES_NOT_EQUAL_CASE, |
| 251 compiler.infos[1].message.kind); | 256 collector.infos.elementAt(1).message.kind); |
| 252 }); | 257 }); |
| 253 } | 258 } |
| 254 | 259 |
| 255 Future testThis() { | 260 Future testThis() { |
| 256 return Future.wait([ | 261 return Future.wait([ |
| 257 MockCompiler.create((MockCompiler compiler) { | 262 MockCompiler.create((MockCompiler compiler) { |
| 258 compiler.parseScript("class Foo { foo() { return this; } }"); | 263 compiler.parseScript("class Foo { foo() { return this; } }"); |
| 259 compiler.resolveStatement("Foo foo;"); | 264 compiler.resolveStatement("Foo foo;"); |
| 260 ClassElement fooElement = compiler.mainApp.find("Foo"); | 265 ClassElement fooElement = compiler.mainApp.find("Foo"); |
| 261 FunctionElement funElement = fooElement.lookupLocalMember("foo"); | 266 FunctionElement funElement = fooElement.lookupLocalMember("foo"); |
| 262 ResolverVisitor visitor = | 267 ResolverVisitor visitor = |
| 263 new ResolverVisitor(compiler, funElement, | 268 new ResolverVisitor(compiler, funElement, |
| 264 new ResolutionRegistry(compiler, | 269 new ResolutionRegistry(compiler, |
| 265 new CollectingTreeElements(funElement))); | 270 new CollectingTreeElements(funElement))); |
| 266 FunctionExpression function = | 271 FunctionExpression function = |
| 267 (funElement as FunctionElementX).parseNode(compiler.parsing); | 272 (funElement as FunctionElementX).parseNode(compiler.parsing); |
| 268 visitor.visit(function.body); | 273 visitor.visit(function.body); |
| 269 Map mapping = map(visitor); | 274 Map mapping = map(visitor); |
| 270 List<Element> values = mapping.values.toList(); | 275 List<Element> values = mapping.values.toList(); |
| 276 DiagnosticCollector collector = compiler.diagnosticCollector; | |
| 271 Expect.equals(0, mapping.length); | 277 Expect.equals(0, mapping.length); |
| 272 Expect.equals(0, compiler.warnings.length); | 278 Expect.equals(0, collector.warnings.length); |
| 273 }), | 279 }), |
| 274 MockCompiler.create((MockCompiler compiler) { | 280 MockCompiler.create((MockCompiler compiler) { |
| 275 compiler.resolveStatement("main() { return this; }"); | 281 compiler.resolveStatement("main() { return this; }"); |
| 276 Expect.equals(0, compiler.warnings.length); | 282 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 277 Expect.equals(1, compiler.errors.length); | 283 Expect.equals(0, collector.warnings.length); |
| 284 Expect.equals(1, collector.errors.length); | |
| 278 Expect.equals(MessageKind.NO_INSTANCE_AVAILABLE, | 285 Expect.equals(MessageKind.NO_INSTANCE_AVAILABLE, |
| 279 compiler.errors[0].message.kind); | 286 collector.errors.first.message.kind); |
| 280 }), | 287 }), |
| 281 MockCompiler.create((MockCompiler compiler) { | 288 MockCompiler.create((MockCompiler compiler) { |
| 282 compiler.parseScript("class Foo { static foo() { return this; } }"); | 289 compiler.parseScript("class Foo { static foo() { return this; } }"); |
| 283 compiler.resolveStatement("Foo foo;"); | 290 compiler.resolveStatement("Foo foo;"); |
| 284 ClassElement fooElement = compiler.mainApp.find("Foo"); | 291 ClassElement fooElement = compiler.mainApp.find("Foo"); |
| 285 FunctionElement funElement = fooElement.lookupLocalMember("foo"); | 292 FunctionElement funElement = fooElement.lookupLocalMember("foo"); |
| 286 ResolverVisitor visitor = new ResolverVisitor(compiler, funElement, | 293 ResolverVisitor visitor = new ResolverVisitor(compiler, funElement, |
| 287 new ResolutionRegistry(compiler, | 294 new ResolutionRegistry(compiler, |
| 288 new CollectingTreeElements(funElement))); | 295 new CollectingTreeElements(funElement))); |
| 289 FunctionExpression function = | 296 FunctionExpression function = |
| 290 (funElement as FunctionElementX).parseNode(compiler.parsing); | 297 (funElement as FunctionElementX).parseNode(compiler.parsing); |
| 291 visitor.visit(function.body); | 298 visitor.visit(function.body); |
| 292 Expect.equals(0, compiler.warnings.length); | 299 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 293 Expect.equals(1, compiler.errors.length); | 300 Expect.equals(0, collector.warnings.length); |
| 301 Expect.equals(1, collector.errors.length); | |
| 294 Expect.equals(MessageKind.NO_INSTANCE_AVAILABLE, | 302 Expect.equals(MessageKind.NO_INSTANCE_AVAILABLE, |
| 295 compiler.errors[0].message.kind); | 303 collector.errors.first.message.kind); |
| 296 }), | 304 }), |
| 297 ]); | 305 ]); |
| 298 } | 306 } |
| 299 | 307 |
| 300 Future testLocalsOne() { | 308 Future testLocalsOne() { |
| 301 return Future.forEach([ | 309 return Future.forEach([ |
| 302 () => testLocals([["foo", false]]), | 310 () => testLocals([["foo", false]]), |
| 303 () => testLocals([["foo", false], ["bar", false]]), | 311 () => testLocals([["foo", false], ["bar", false]]), |
| 304 () => testLocals([["foo", false], ["bar", false], ["foobar", false]]), | 312 () => testLocals([["foo", false], ["bar", false], ["foobar", false]]), |
| 305 | 313 |
| 306 () => testLocals([["foo", true]]), | 314 () => testLocals([["foo", true]]), |
| 307 () => testLocals([["foo", false], ["bar", true]]), | 315 () => testLocals([["foo", false], ["bar", true]]), |
| 308 () => testLocals([["foo", true], ["bar", true]]), | 316 () => testLocals([["foo", true], ["bar", true]]), |
| 309 | 317 |
| 310 () => testLocals([["foo", false], ["bar", false], ["foobar", true]]), | 318 () => testLocals([["foo", false], ["bar", false], ["foobar", true]]), |
| 311 () => testLocals([["foo", false], ["bar", true], ["foobar", true]]), | 319 () => testLocals([["foo", false], ["bar", true], ["foobar", true]]), |
| 312 () => testLocals([["foo", true], ["bar", true], ["foobar", true]]), | 320 () => testLocals([["foo", true], ["bar", true], ["foobar", true]]), |
| 313 | 321 |
| 314 () => testLocals([["foo", false], ["foo", false]]) | 322 () => testLocals([["foo", false], ["foo", false]]) |
| 315 .then((MockCompiler compiler) { | 323 .then((MockCompiler compiler) { |
| 316 Expect.equals(1, compiler.errors.length); | 324 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 325 Expect.equals(1, collector.errors.length); | |
| 317 Expect.equals( | 326 Expect.equals( |
| 318 new Message( | 327 new Message( |
| 319 MessageTemplate.TEMPLATES[MessageKind.DUPLICATE_DEFINITION], | 328 MessageTemplate.TEMPLATES[MessageKind.DUPLICATE_DEFINITION], |
| 320 {'name': 'foo'}, false), | 329 {'name': 'foo'}, false), |
| 321 compiler.errors[0].message); | 330 collector.errors.first.message); |
| 322 })], (f) => f()); | 331 })], (f) => f()); |
| 323 } | 332 } |
| 324 | 333 |
| 325 | 334 |
| 326 Future testLocalsTwo() { | 335 Future testLocalsTwo() { |
| 327 return MockCompiler.create((MockCompiler compiler) { | 336 return MockCompiler.create((MockCompiler compiler) { |
| 328 ResolverVisitor visitor = compiler.resolverVisitor(); | 337 ResolverVisitor visitor = compiler.resolverVisitor(); |
| 329 Node tree = parseStatement("if (true) { var a = 1; var b = 2; }"); | 338 Node tree = parseStatement("if (true) { var a = 1; var b = 2; }"); |
| 330 ResolutionResult result = visitor.visit(tree); | 339 ResolutionResult result = visitor.visit(tree); |
| 331 Expect.equals(const NoneResult(), result); | 340 Expect.equals(const NoneResult(), result); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 479 } | 488 } |
| 480 | 489 |
| 481 Future testTypeAnnotation() { | 490 Future testTypeAnnotation() { |
| 482 return MockCompiler.create((MockCompiler compiler) { | 491 return MockCompiler.create((MockCompiler compiler) { |
| 483 String statement = "Foo bar;"; | 492 String statement = "Foo bar;"; |
| 484 | 493 |
| 485 // Test that we get a warning when Foo is not defined. | 494 // Test that we get a warning when Foo is not defined. |
| 486 Map mapping = compiler.resolveStatement(statement).map; | 495 Map mapping = compiler.resolveStatement(statement).map; |
| 487 | 496 |
| 488 Expect.equals(1, mapping.length); // Only [bar] has an element. | 497 Expect.equals(1, mapping.length); // Only [bar] has an element. |
| 489 Expect.equals(1, compiler.warnings.length); | 498 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 490 | 499 Expect.equals(1, collector.warnings.length); |
| 491 Node warningNode = compiler.warnings[0].node; | |
| 492 | 500 |
| 493 Expect.equals( | 501 Expect.equals( |
| 494 new Message( | 502 new Message( |
| 495 MessageTemplate.TEMPLATES[MessageKind.CANNOT_RESOLVE_TYPE], | 503 MessageTemplate.TEMPLATES[MessageKind.CANNOT_RESOLVE_TYPE], |
| 496 {'typeName': 'Foo'}, false), | 504 {'typeName': 'Foo'}, false), |
| 497 compiler.warnings[0].message); | 505 collector.warnings.first.message); |
| 498 VariableDefinitions definition = compiler.parsedTree; | 506 collector.clear(); |
| 499 Expect.equals(warningNode, definition.type); | |
| 500 compiler.clearMessages(); | |
| 501 | 507 |
| 502 // Test that there is no warning after defining Foo. | 508 // Test that there is no warning after defining Foo. |
| 503 compiler.parseScript("class Foo {}"); | 509 compiler.parseScript("class Foo {}"); |
| 504 mapping = compiler.resolveStatement(statement).map; | 510 mapping = compiler.resolveStatement(statement).map; |
| 505 Expect.equals(1, mapping.length); | 511 Expect.equals(1, mapping.length); |
| 506 Expect.equals(0, compiler.warnings.length); | 512 Expect.equals(0, collector.warnings.length); |
| 507 | 513 |
| 508 // Test that 'var' does not create a warning. | 514 // Test that 'var' does not create a warning. |
| 509 mapping = compiler.resolveStatement("var foo;").map; | 515 mapping = compiler.resolveStatement("var foo;").map; |
| 510 Expect.equals(1, mapping.length); | 516 Expect.equals(1, mapping.length); |
| 511 Expect.equals(0, compiler.warnings.length); | 517 Expect.equals(0, collector.warnings.length); |
| 512 }); | 518 }); |
| 513 } | 519 } |
| 514 | 520 |
| 515 Future testSuperclass() { | 521 Future testSuperclass() { |
| 516 return Future.wait([ | 522 return Future.wait([ |
| 517 MockCompiler.create((MockCompiler compiler) { | 523 MockCompiler.create((MockCompiler compiler) { |
| 518 compiler.parseScript("class Foo extends Bar {}"); | 524 compiler.parseScript("class Foo extends Bar {}"); |
| 519 compiler.resolveStatement("Foo bar;"); | 525 compiler.resolveStatement("Foo bar;"); |
| 520 Expect.equals(1, compiler.errors.length); | 526 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 527 Expect.equals(1, collector.errors.length); | |
| 521 var cannotResolveBar = new Message( | 528 var cannotResolveBar = new Message( |
| 522 MessageTemplate.TEMPLATES[MessageKind.CANNOT_EXTEND_MALFORMED], | 529 MessageTemplate.TEMPLATES[MessageKind.CANNOT_EXTEND_MALFORMED], |
| 523 {'className': 'Foo', 'malformedType': 'Bar'}, false); | 530 {'className': 'Foo', 'malformedType': 'Bar'}, false); |
| 524 Expect.equals(cannotResolveBar, compiler.errors[0].message); | 531 Expect.equals(cannotResolveBar, collector.errors.first.message); |
| 525 compiler.clearMessages(); | 532 collector.clear(); |
| 526 }), | 533 }), |
| 527 MockCompiler.create((MockCompiler compiler) { | 534 MockCompiler.create((MockCompiler compiler) { |
| 528 compiler.parseScript("class Foo extends Bar {}"); | 535 compiler.parseScript("class Foo extends Bar {}"); |
| 529 compiler.parseScript("class Bar {}"); | 536 compiler.parseScript("class Bar {}"); |
| 530 Map mapping = compiler.resolveStatement("Foo bar;").map; | 537 Map mapping = compiler.resolveStatement("Foo bar;").map; |
| 531 Expect.equals(1, mapping.length); | 538 Expect.equals(1, mapping.length); |
| 532 | 539 |
| 533 ClassElement fooElement = compiler.mainApp.find('Foo'); | 540 ClassElement fooElement = compiler.mainApp.find('Foo'); |
| 534 ClassElement barElement = compiler.mainApp.find('Bar'); | 541 ClassElement barElement = compiler.mainApp.find('Bar'); |
| 535 Expect.equals(barElement.computeType(compiler.resolution), | 542 Expect.equals(barElement.computeType(compiler.resolution), |
| 536 fooElement.supertype); | 543 fooElement.supertype); |
| 537 Expect.isTrue(fooElement.interfaces.isEmpty); | 544 Expect.isTrue(fooElement.interfaces.isEmpty); |
| 538 Expect.isTrue(barElement.interfaces.isEmpty); | 545 Expect.isTrue(barElement.interfaces.isEmpty); |
| 539 }), | 546 }), |
| 540 ]); | 547 ]); |
| 541 } | 548 } |
| 542 | 549 |
| 543 Future testVarSuperclass() { | 550 Future testVarSuperclass() { |
| 544 return MockCompiler.create((MockCompiler compiler) { | 551 return MockCompiler.create((MockCompiler compiler) { |
| 545 compiler.parseScript("class Foo extends var {}"); | 552 compiler.parseScript("class Foo extends var {}"); |
| 546 compiler.resolveStatement("Foo bar;"); | 553 compiler.resolveStatement("Foo bar;"); |
| 547 Expect.equals(1, compiler.errors.length); | 554 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 555 Expect.equals(1, collector.errors.length); | |
| 548 Expect.equals( | 556 Expect.equals( |
| 549 new Message( | 557 new Message( |
| 550 MessageTemplate.TEMPLATES[MessageKind.CANNOT_RESOLVE_TYPE], | 558 MessageTemplate.TEMPLATES[MessageKind.CANNOT_RESOLVE_TYPE], |
| 551 {'typeName': 'var'}, false), | 559 {'typeName': 'var'}, false), |
| 552 compiler.errors[0].message); | 560 collector.errors.first.message); |
| 553 compiler.clearMessages(); | 561 collector.clear(); |
| 554 }); | 562 }); |
| 555 } | 563 } |
| 556 | 564 |
| 557 Future testOneInterface() { | 565 Future testOneInterface() { |
| 558 return MockCompiler.create((MockCompiler compiler) { | 566 return MockCompiler.create((MockCompiler compiler) { |
| 559 compiler.parseScript("class Foo implements Bar {}"); | 567 compiler.parseScript("class Foo implements Bar {}"); |
| 560 compiler.resolveStatement("Foo bar;"); | 568 compiler.resolveStatement("Foo bar;"); |
| 561 Expect.equals(1, compiler.errors.length); | 569 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 570 Expect.equals(1, collector.errors.length); | |
| 562 Expect.equals( | 571 Expect.equals( |
| 563 new Message( | 572 new Message( |
| 564 MessageTemplate.TEMPLATES[MessageKind.CANNOT_RESOLVE_TYPE], | 573 MessageTemplate.TEMPLATES[MessageKind.CANNOT_RESOLVE_TYPE], |
| 565 {'typeName': 'bar'}, false), | 574 {'typeName': 'bar'}, false), |
| 566 compiler.errors[0].message); | 575 collector.errors.first.message); |
| 567 compiler.clearMessages(); | 576 collector.clear(); |
| 568 | 577 |
| 569 // Add the abstract class to the world and make sure everything is setup | 578 // Add the abstract class to the world and make sure everything is setup |
| 570 // correctly. | 579 // correctly. |
| 571 compiler.parseScript("abstract class Bar {}"); | 580 compiler.parseScript("abstract class Bar {}"); |
| 572 | 581 |
| 573 ResolverVisitor visitor = | 582 ResolverVisitor visitor = |
| 574 new ResolverVisitor(compiler, null, | 583 new ResolverVisitor(compiler, null, |
| 575 new ResolutionRegistry(compiler, | 584 new ResolutionRegistry(compiler, |
| 576 new CollectingTreeElements(null))); | 585 new CollectingTreeElements(null))); |
| 577 compiler.resolveStatement("Foo bar;"); | 586 compiler.resolveStatement("Foo bar;"); |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 692 FunctionExpression tree = (element as FunctionElement).node; | 701 FunctionExpression tree = (element as FunctionElement).node; |
| 693 ResolverVisitor visitor = | 702 ResolverVisitor visitor = |
| 694 new ResolverVisitor(compiler, element, | 703 new ResolverVisitor(compiler, element, |
| 695 new ResolutionRegistry(compiler, | 704 new ResolutionRegistry(compiler, |
| 696 new CollectingTreeElements(element))); | 705 new CollectingTreeElements(element))); |
| 697 new InitializerResolver(visitor, element, tree).resolveInitializers(); | 706 new InitializerResolver(visitor, element, tree).resolveInitializers(); |
| 698 visitor.visit(tree.body); | 707 visitor.visit(tree.body); |
| 699 Expect.equals(expectedElementCount, map(visitor).length, | 708 Expect.equals(expectedElementCount, map(visitor).length, |
| 700 "${map(visitor).values} for '$statement' in context of `$script`"); | 709 "${map(visitor).values} for '$statement' in context of `$script`"); |
| 701 | 710 |
| 702 compareWarningKinds(script, expectedWarnings, compiler.warnings); | 711 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 703 compareWarningKinds(script, expectedErrors, compiler.errors); | 712 compareWarningKinds(script, expectedWarnings, collector.warnings); |
| 704 compareWarningKinds(script, expectedInfos, compiler.infos); | 713 compareWarningKinds(script, expectedErrors, collector.errors); |
| 714 compareWarningKinds(script, expectedInfos, collector.infos); | |
| 705 }); | 715 }); |
| 706 } | 716 } |
| 707 | 717 |
| 708 Future testClassHierarchy() { | 718 Future testClassHierarchy() { |
| 709 final MAIN = "main"; | 719 final MAIN = "main"; |
| 710 return Future.wait([ | 720 return Future.wait([ |
| 711 MockCompiler.create((MockCompiler compiler) { | 721 MockCompiler.create((MockCompiler compiler) { |
| 712 compiler.parseScript("""class A extends A {} | 722 compiler.parseScript("""class A extends A {} |
| 713 main() { return new A(); }"""); | 723 main() { return new A(); }"""); |
| 714 FunctionElement mainElement = compiler.mainApp.find(MAIN); | 724 FunctionElement mainElement = compiler.mainApp.find(MAIN); |
| 715 compiler.resolver.resolve(mainElement); | 725 compiler.resolver.resolve(mainElement); |
| 716 Expect.equals(0, compiler.warnings.length); | 726 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 717 Expect.equals(1, compiler.errors.length); | 727 Expect.equals(0, collector.warnings.length); |
| 728 Expect.equals(1, collector.errors.length); | |
| 718 Expect.equals(MessageKind.CYCLIC_CLASS_HIERARCHY, | 729 Expect.equals(MessageKind.CYCLIC_CLASS_HIERARCHY, |
| 719 compiler.errors[0].message.kind); | 730 collector.errors.first.message.kind); |
| 720 }), | 731 }), |
| 721 MockCompiler.create((MockCompiler compiler) { | 732 MockCompiler.create((MockCompiler compiler) { |
| 722 compiler.parseScript("""class A extends B {} | 733 compiler.parseScript("""class A extends B {} |
| 723 class B extends A {} | 734 class B extends A {} |
| 724 main() { return new A(); }"""); | 735 main() { return new A(); }"""); |
| 725 FunctionElement mainElement = compiler.mainApp.find(MAIN); | 736 FunctionElement mainElement = compiler.mainApp.find(MAIN); |
| 726 compiler.resolver.resolve(mainElement); | 737 compiler.resolver.resolve(mainElement); |
| 727 Expect.equals(0, compiler.warnings.length); | 738 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 728 Expect.equals(2, compiler.errors.length); | 739 Expect.equals(0, collector.warnings.length); |
| 740 Expect.equals(2, collector.errors.length); | |
| 729 Expect.equals(MessageKind.CYCLIC_CLASS_HIERARCHY, | 741 Expect.equals(MessageKind.CYCLIC_CLASS_HIERARCHY, |
| 730 compiler.errors[0].message.kind); | 742 collector.errors.first.message.kind); |
| 731 Expect.equals(MessageKind.CANNOT_FIND_CONSTRUCTOR, | 743 Expect.equals(MessageKind.CANNOT_FIND_CONSTRUCTOR, |
| 732 compiler.errors[1].message.kind); | 744 collector.errors.elementAt(1).message.kind); |
| 733 }), | 745 }), |
| 734 MockCompiler.create((MockCompiler compiler) { | 746 MockCompiler.create((MockCompiler compiler) { |
| 735 compiler.parseScript("""abstract class A extends B {} | 747 compiler.parseScript("""abstract class A extends B {} |
| 736 abstract class B extends A {} | 748 abstract class B extends A {} |
| 737 class C implements A {} | 749 class C implements A {} |
| 738 main() { return new C(); }"""); | 750 main() { return new C(); }"""); |
| 739 FunctionElement mainElement = compiler.mainApp.find(MAIN); | 751 FunctionElement mainElement = compiler.mainApp.find(MAIN); |
| 740 compiler.resolver.resolve(mainElement); | 752 compiler.resolver.resolve(mainElement); |
| 741 Expect.equals(0, compiler.warnings.length); | 753 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 742 Expect.equals(1, compiler.errors.length); | 754 Expect.equals(0, collector.warnings.length); |
| 755 Expect.equals(1, collector.errors.length); | |
| 743 Expect.equals(MessageKind.CYCLIC_CLASS_HIERARCHY, | 756 Expect.equals(MessageKind.CYCLIC_CLASS_HIERARCHY, |
| 744 compiler.errors[0].message.kind); | 757 collector.errors.first.message.kind); |
| 745 }), | 758 }), |
| 746 MockCompiler.create((MockCompiler compiler) { | 759 MockCompiler.create((MockCompiler compiler) { |
| 747 compiler.parseScript("""class A extends B {} | 760 compiler.parseScript("""class A extends B {} |
| 748 class B extends C {} | 761 class B extends C {} |
| 749 class C {} | 762 class C {} |
| 750 main() { return new A(); }"""); | 763 main() { return new A(); }"""); |
| 751 FunctionElement mainElement = compiler.mainApp.find(MAIN); | 764 FunctionElement mainElement = compiler.mainApp.find(MAIN); |
| 752 compiler.resolver.resolve(mainElement); | 765 compiler.resolver.resolve(mainElement); |
| 753 Expect.equals(0, compiler.warnings.length); | 766 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 754 Expect.equals(0, compiler.errors.length); | 767 Expect.equals(0, collector.warnings.length); |
| 768 Expect.equals(0, collector.errors.length); | |
| 755 ClassElement aElement = compiler.mainApp.find("A"); | 769 ClassElement aElement = compiler.mainApp.find("A"); |
| 756 Link<DartType> supertypes = aElement.allSupertypes; | 770 Link<DartType> supertypes = aElement.allSupertypes; |
| 757 Expect.equals(<String>['B', 'C', 'Object'].toString(), | 771 Expect.equals(<String>['B', 'C', 'Object'].toString(), |
| 758 asSortedStrings(supertypes).toString()); | 772 asSortedStrings(supertypes).toString()); |
| 759 }), | 773 }), |
| 760 MockCompiler.create((MockCompiler compiler) { | 774 MockCompiler.create((MockCompiler compiler) { |
| 761 compiler.parseScript("""class A<T> {} | 775 compiler.parseScript("""class A<T> {} |
| 762 class B<Z,W> extends A<int> | 776 class B<Z,W> extends A<int> |
| 763 implements I<Z,List<W>> {} | 777 implements I<Z,List<W>> {} |
| 764 class I<X,Y> {} | 778 class I<X,Y> {} |
| 765 class C extends B<bool,String> {} | 779 class C extends B<bool,String> {} |
| 766 main() { return new C(); }"""); | 780 main() { return new C(); }"""); |
| 767 FunctionElement mainElement = compiler.mainApp.find(MAIN); | 781 FunctionElement mainElement = compiler.mainApp.find(MAIN); |
| 768 compiler.resolver.resolve(mainElement); | 782 compiler.resolver.resolve(mainElement); |
| 769 Expect.equals(0, compiler.warnings.length); | 783 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 770 Expect.equals(0, compiler.errors.length); | 784 Expect.equals(0, collector.warnings.length); |
| 785 Expect.equals(0, collector.errors.length); | |
| 771 ClassElement aElement = compiler.mainApp.find("C"); | 786 ClassElement aElement = compiler.mainApp.find("C"); |
| 772 Link<DartType> supertypes = aElement.allSupertypes; | 787 Link<DartType> supertypes = aElement.allSupertypes; |
| 773 // Object is once per inheritance path, that is from both A and I. | 788 // Object is once per inheritance path, that is from both A and I. |
| 774 Expect.equals(<String>['A<int>', 'B<bool, String>', | 789 Expect.equals(<String>['A<int>', 'B<bool, String>', |
| 775 'I<bool, List<String>>', 'Object'].toString(), | 790 'I<bool, List<String>>', 'Object'].toString(), |
| 776 asSortedStrings(supertypes).toString()); | 791 asSortedStrings(supertypes).toString()); |
| 777 }), | 792 }), |
| 778 MockCompiler.create((MockCompiler compiler) { | 793 MockCompiler.create((MockCompiler compiler) { |
| 779 compiler.parseScript("""class A<T> {} | 794 compiler.parseScript("""class A<T> {} |
| 780 class D extends A<E> {} | 795 class D extends A<E> {} |
| 781 class E extends D {} | 796 class E extends D {} |
| 782 main() { return new E(); }"""); | 797 main() { return new E(); }"""); |
| 783 FunctionElement mainElement = compiler.mainApp.find(MAIN); | 798 FunctionElement mainElement = compiler.mainApp.find(MAIN); |
| 784 compiler.resolver.resolve(mainElement); | 799 compiler.resolver.resolve(mainElement); |
| 785 Expect.equals(0, compiler.warnings.length); | 800 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 786 Expect.equals(0, compiler.errors.length); | 801 Expect.equals(0, collector.warnings.length); |
| 802 Expect.equals(0, collector.errors.length); | |
| 787 ClassElement aElement = compiler.mainApp.find("E"); | 803 ClassElement aElement = compiler.mainApp.find("E"); |
| 788 Link<DartType> supertypes = aElement.allSupertypes; | 804 Link<DartType> supertypes = aElement.allSupertypes; |
| 789 Expect.equals(<String>['A<E>', 'D', 'Object'].toString(), | 805 Expect.equals(<String>['A<E>', 'D', 'Object'].toString(), |
| 790 asSortedStrings(supertypes).toString()); | 806 asSortedStrings(supertypes).toString()); |
| 791 }), | 807 }), |
| 792 MockCompiler.create((MockCompiler compiler) { | 808 MockCompiler.create((MockCompiler compiler) { |
| 793 compiler.parseScript("""class A<T> {} | 809 compiler.parseScript("""class A<T> {} |
| 794 class D extends A<int> implements A<double> {} | 810 class D extends A<int> implements A<double> {} |
| 795 main() { return new D(); }"""); | 811 main() { return new D(); }"""); |
| 796 FunctionElement mainElement = compiler.mainApp.find(MAIN); | 812 FunctionElement mainElement = compiler.mainApp.find(MAIN); |
| 797 compiler.resolver.resolve(mainElement); | 813 compiler.resolver.resolve(mainElement); |
| 798 Expect.equals(0, compiler.warnings.length); | 814 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 799 Expect.equals(1, compiler.errors.length); | 815 Expect.equals(0, collector.warnings.length); |
| 816 Expect.equals(1, collector.errors.length); | |
| 800 Expect.equals(MessageKind.MULTI_INHERITANCE, | 817 Expect.equals(MessageKind.MULTI_INHERITANCE, |
| 801 compiler.errors[0].message.kind); | 818 collector.errors.first.message.kind); |
| 802 Expect.equals(0, compiler.crashes.length); | 819 Expect.equals(0, collector.crashes.length); |
| 803 }), | 820 }), |
| 804 ]); | 821 ]); |
| 805 } | 822 } |
| 806 | 823 |
| 807 Future testEnumDeclaration() { | 824 Future testEnumDeclaration() { |
| 808 final MAIN = "main"; | 825 final MAIN = "main"; |
| 809 return Future.wait([ | 826 return Future.wait([ |
| 810 MockCompiler.create((MockCompiler compiler) { | 827 MockCompiler.create((MockCompiler compiler) { |
| 811 compiler.parseScript("""enum Enum {} | 828 compiler.parseScript("""enum Enum {} |
| 812 main() { Enum e; }"""); | 829 main() { Enum e; }"""); |
| 813 FunctionElement mainElement = compiler.mainApp.find(MAIN); | 830 FunctionElement mainElement = compiler.mainApp.find(MAIN); |
| 814 compiler.resolver.resolve(mainElement); | 831 compiler.resolver.resolve(mainElement); |
| 815 Expect.equals(0, compiler.warnings.length, | 832 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 816 'Unexpected warnings: ${compiler.warnings}'); | 833 Expect.equals(0, collector.warnings.length, |
| 817 Expect.equals(1, compiler.errors.length, | 834 'Unexpected warnings: ${collector.warnings}'); |
| 818 'Unexpected errors: ${compiler.errors}'); | 835 Expect.equals(1, collector.errors.length, |
| 836 'Unexpected errors: ${collector.errors}'); | |
| 819 }), | 837 }), |
| 820 | 838 |
| 821 MockCompiler.create((MockCompiler compiler) { | 839 MockCompiler.create((MockCompiler compiler) { |
| 822 compiler.parseScript("""enum Enum { A } | 840 compiler.parseScript("""enum Enum { A } |
| 823 main() { Enum e = Enum.A; }"""); | 841 main() { Enum e = Enum.A; }"""); |
| 824 FunctionElement mainElement = compiler.mainApp.find(MAIN); | 842 FunctionElement mainElement = compiler.mainApp.find(MAIN); |
| 825 compiler.resolver.resolve(mainElement); | 843 compiler.resolver.resolve(mainElement); |
| 826 Expect.equals(0, compiler.warnings.length, | 844 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 827 'Unexpected warnings: ${compiler.warnings}'); | 845 Expect.equals(0, collector.warnings.length, |
| 828 Expect.equals(0, compiler.errors.length, | 846 'Unexpected warnings: ${collector.warnings}'); |
| 829 'Unexpected errors: ${compiler.errors}'); | 847 Expect.equals(0, collector.errors.length, |
| 848 'Unexpected errors: ${collector.errors}'); | |
| 830 }), | 849 }), |
| 831 | 850 |
| 832 MockCompiler.create((MockCompiler compiler) { | 851 MockCompiler.create((MockCompiler compiler) { |
| 833 compiler.parseScript("""enum Enum { A } | 852 compiler.parseScript("""enum Enum { A } |
| 834 main() { Enum e = Enum.B; }"""); | 853 main() { Enum e = Enum.B; }"""); |
| 835 FunctionElement mainElement = compiler.mainApp.find(MAIN); | 854 FunctionElement mainElement = compiler.mainApp.find(MAIN); |
| 836 compiler.resolver.resolve(mainElement); | 855 compiler.resolver.resolve(mainElement); |
| 837 Expect.equals(1, compiler.warnings.length, | 856 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 838 'Unexpected warnings: ${compiler.warnings}'); | 857 Expect.equals(1, collector.warnings.length, |
| 858 'Unexpected warnings: ${collector.warnings}'); | |
| 839 Expect.equals(MessageKind.MEMBER_NOT_FOUND, | 859 Expect.equals(MessageKind.MEMBER_NOT_FOUND, |
| 840 compiler.warnings[0].message.kind); | 860 collector.warnings.first.message.kind); |
| 841 Expect.equals(0, compiler.errors.length, | 861 Expect.equals(0, collector.errors.length, |
| 842 'Unexpected errors: ${compiler.errors}'); | 862 'Unexpected errors: ${collector.errors}'); |
| 843 }), | 863 }), |
| 844 | 864 |
| 845 MockCompiler.create((MockCompiler compiler) { | 865 MockCompiler.create((MockCompiler compiler) { |
| 846 compiler.parseScript("""enum Enum { A } | 866 compiler.parseScript("""enum Enum { A } |
| 847 main() { List values = Enum.values; }"""); | 867 main() { List values = Enum.values; }"""); |
| 848 FunctionElement mainElement = compiler.mainApp.find(MAIN); | 868 FunctionElement mainElement = compiler.mainApp.find(MAIN); |
| 849 compiler.resolver.resolve(mainElement); | 869 compiler.resolver.resolve(mainElement); |
| 850 Expect.equals(0, compiler.warnings.length, | 870 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 851 'Unexpected warnings: ${compiler.warnings}'); | 871 Expect.equals(0, collector.warnings.length, |
| 852 Expect.equals(0, compiler.errors.length, | 872 'Unexpected warnings: ${collector.warnings}'); |
| 853 'Unexpected errors: ${compiler.errors}'); | 873 Expect.equals(0, collector.errors.length, |
| 874 'Unexpected errors: ${collector.errors}'); | |
| 854 }), | 875 }), |
| 855 | 876 |
| 856 MockCompiler.create((MockCompiler compiler) { | 877 MockCompiler.create((MockCompiler compiler) { |
| 857 compiler.parseScript("""enum Enum { A } | 878 compiler.parseScript("""enum Enum { A } |
| 858 main() { new Enum(0, ''); }"""); | 879 main() { new Enum(0, ''); }"""); |
| 859 FunctionElement mainElement = compiler.mainApp.find(MAIN); | 880 FunctionElement mainElement = compiler.mainApp.find(MAIN); |
| 860 compiler.resolver.resolve(mainElement); | 881 compiler.resolver.resolve(mainElement); |
| 861 Expect.equals(0, compiler.warnings.length, | 882 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 862 'Unexpected warnings: ${compiler.warnings}'); | 883 Expect.equals(0, collector.warnings.length, |
| 863 Expect.equals(1, compiler.errors.length, | 884 'Unexpected warnings: ${collector.warnings}'); |
| 864 'Unexpected errors: ${compiler.errors}'); | 885 Expect.equals(1, collector.errors.length, |
| 886 'Unexpected errors: ${collector.errors}'); | |
| 865 Expect.equals(MessageKind.CANNOT_INSTANTIATE_ENUM, | 887 Expect.equals(MessageKind.CANNOT_INSTANTIATE_ENUM, |
| 866 compiler.errors[0].message.kind); | 888 collector.errors.first.message.kind); |
| 867 }), | 889 }), |
| 868 | 890 |
| 869 MockCompiler.create((MockCompiler compiler) { | 891 MockCompiler.create((MockCompiler compiler) { |
|
sigurdm
2015/11/10 07:52:54
Not for this CL, but it seems these tests can be a
Johnni Winther
2015/11/10 09:38:52
Acknowledged.
| |
| 870 compiler.parseScript("""enum Enum { A } | 892 compiler.parseScript("""enum Enum { A } |
| 871 main() { const Enum(0, ''); }"""); | 893 main() { const Enum(0, ''); }"""); |
| 872 FunctionElement mainElement = compiler.mainApp.find(MAIN); | 894 FunctionElement mainElement = compiler.mainApp.find(MAIN); |
| 873 compiler.resolver.resolve(mainElement); | 895 compiler.resolver.resolve(mainElement); |
| 874 Expect.equals(0, compiler.warnings.length, | 896 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 875 'Unexpected warnings: ${compiler.warnings}'); | 897 Expect.equals(0, collector.warnings.length, |
| 876 Expect.equals(1, compiler.errors.length, | 898 'Unexpected warnings: ${collector.warnings}'); |
| 877 'Unexpected errors: ${compiler.errors}'); | 899 Expect.equals(1, collector.errors.length, |
| 900 'Unexpected errors: ${collector.errors}'); | |
| 878 Expect.equals(MessageKind.CANNOT_INSTANTIATE_ENUM, | 901 Expect.equals(MessageKind.CANNOT_INSTANTIATE_ENUM, |
| 879 compiler.errors[0].message.kind); | 902 collector.errors.first.message.kind); |
| 880 }), | 903 }), |
| 881 ]); | 904 ]); |
| 882 } | 905 } |
| 883 | 906 |
| 884 Future testInitializers() { | 907 Future testInitializers() { |
| 885 return Future.forEach([ | 908 return Future.forEach([ |
| 886 () { | 909 () { |
| 887 String script = | 910 String script = |
| 888 """class A { | 911 """class A { |
| 889 int foo; int bar; | 912 int foo; int bar; |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1032 'const {"a": 0, "b": 1, "c": 2}'], | 1055 'const {"a": 0, "b": 1, "c": 2}'], |
| 1033 'const <String, int>{"a": 0, "b": 1, "c": 2}': | 1056 'const <String, int>{"a": 0, "b": 1, "c": 2}': |
| 1034 const ['"a"', '0', '"b"', '1', '"c"', '2', | 1057 const ['"a"', '0', '"b"', '1', '"c"', '2', |
| 1035 'const <String, int>{"a": 0, "b": 1, "c": 2}'], | 1058 'const <String, int>{"a": 0, "b": 1, "c": 2}'], |
| 1036 }; | 1059 }; |
| 1037 return Future.forEach(testedConstants.keys, (String constant) { | 1060 return Future.forEach(testedConstants.keys, (String constant) { |
| 1038 return MockCompiler.create((MockCompiler compiler) { | 1061 return MockCompiler.create((MockCompiler compiler) { |
| 1039 CollectingTreeElements elements = | 1062 CollectingTreeElements elements = |
| 1040 compiler.resolveStatement("main() => $constant;"); | 1063 compiler.resolveStatement("main() => $constant;"); |
| 1041 List<String> expectedConstants = testedConstants[constant]; | 1064 List<String> expectedConstants = testedConstants[constant]; |
| 1042 Expect.equals(0, compiler.warnings.length); | 1065 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 1043 Expect.equals(0, compiler.errors.length); | 1066 Expect.equals(0, collector.warnings.length); |
| 1067 Expect.equals(0, collector.errors.length); | |
| 1044 List<ConstantExpression> constants = elements.constants; | 1068 List<ConstantExpression> constants = elements.constants; |
| 1045 String constantsText = | 1069 String constantsText = |
| 1046 '[${constants.map((c) => c.getText()).join(', ')}]'; | 1070 '[${constants.map((c) => c.getText()).join(', ')}]'; |
| 1047 Expect.equals(expectedConstants.length, constants.length, | 1071 Expect.equals(expectedConstants.length, constants.length, |
| 1048 "Expected ${expectedConstants.length} constants for `${constant}` " | 1072 "Expected ${expectedConstants.length} constants for `${constant}` " |
| 1049 "found $constantsText."); | 1073 "found $constantsText."); |
| 1050 for (int index = 0; index < expectedConstants.length; index++) { | 1074 for (int index = 0; index < expectedConstants.length; index++) { |
| 1051 Expect.equals(expectedConstants[index], constants[index].getText(), | 1075 Expect.equals(expectedConstants[index], constants[index].getText(), |
| 1052 "Expected ${expectedConstants} for `$constant`, " | 1076 "Expected ${expectedConstants} for `$constant`, " |
| 1053 "found $constantsText."); | 1077 "found $constantsText."); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1141 operator==(other) => true; | 1165 operator==(other) => true; |
| 1142 } | 1166 } |
| 1143 class B { | 1167 class B { |
| 1144 operator==(other) => true; | 1168 operator==(other) => true; |
| 1145 get hashCode => 0; | 1169 get hashCode => 0; |
| 1146 } | 1170 } |
| 1147 main() { | 1171 main() { |
| 1148 new A() == new B(); | 1172 new A() == new B(); |
| 1149 }"""; | 1173 }"""; |
| 1150 asyncTest(() => compileScript(script).then((compiler) { | 1174 asyncTest(() => compileScript(script).then((compiler) { |
| 1151 Expect.equals(0, compiler.warnings.length); | 1175 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 1152 Expect.equals(0, compiler.infos.length); | 1176 Expect.equals(0, collector.warnings.length); |
| 1153 Expect.equals(1, compiler.hints.length); | 1177 Expect.equals(0, collector.infos.length); |
| 1178 Expect.equals(1, collector.hints.length); | |
| 1154 Expect.equals(MessageKind.OVERRIDE_EQUALS_NOT_HASH_CODE, | 1179 Expect.equals(MessageKind.OVERRIDE_EQUALS_NOT_HASH_CODE, |
| 1155 compiler.hints[0].message.kind); | 1180 collector.hints.first.message.kind); |
| 1156 Expect.equals(0, compiler.errors.length); | 1181 Expect.equals(0, collector.errors.length); |
| 1157 })); | 1182 })); |
| 1158 } | 1183 } |
| 1159 | 1184 |
| 1160 testConstConstructorAndNonFinalFields() { | 1185 testConstConstructorAndNonFinalFields() { |
| 1161 void expect(compiler, List errors, List infos) { | 1186 void expect(compiler, List errors, List infos) { |
| 1162 Expect.equals(errors.length, compiler.errors.length); | 1187 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 1188 Expect.equals(errors.length, collector.errors.length); | |
| 1163 for (int i = 0 ; i < errors.length ; i++) { | 1189 for (int i = 0 ; i < errors.length ; i++) { |
| 1164 Expect.equals(errors[i], compiler.errors[i].message.kind); | 1190 Expect.equals(errors[i], collector.errors.elementAt(i).message.kind); |
| 1165 } | 1191 } |
| 1166 Expect.equals(0, compiler.warnings.length); | 1192 Expect.equals(0, collector.warnings.length); |
| 1167 Expect.equals(infos.length, compiler.infos.length); | 1193 Expect.equals(infos.length, collector.infos.length); |
| 1168 for (int i = 0 ; i < infos.length ; i++) { | 1194 for (int i = 0 ; i < infos.length ; i++) { |
| 1169 Expect.equals(infos[i], compiler.infos[i].message.kind); | 1195 Expect.equals(infos[i], collector.infos.elementAt(i).message.kind); |
| 1170 } | 1196 } |
| 1171 } | 1197 } |
| 1172 | 1198 |
| 1173 final script1 = r""" | 1199 final script1 = r""" |
| 1174 class A { | 1200 class A { |
| 1175 var a; | 1201 var a; |
| 1176 const A(this.a); | 1202 const A(this.a); |
| 1177 } | 1203 } |
| 1178 main() { | 1204 main() { |
| 1179 new A(0); | 1205 new A(0); |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1363 } | 1389 } |
| 1364 ''', [MessageKind.SETTER_NOT_FOUND_IN_SUPER, | 1390 ''', [MessageKind.SETTER_NOT_FOUND_IN_SUPER, |
| 1365 // TODO(johnniwinther): Avoid duplicate warnings. | 1391 // TODO(johnniwinther): Avoid duplicate warnings. |
| 1366 MessageKind.SETTER_NOT_FOUND]); | 1392 MessageKind.SETTER_NOT_FOUND]); |
| 1367 } | 1393 } |
| 1368 | 1394 |
| 1369 /// Helper to test that [script] produces all the given [warnings]. | 1395 /// Helper to test that [script] produces all the given [warnings]. |
| 1370 checkWarningOn(String script, List<MessageKind> warnings) { | 1396 checkWarningOn(String script, List<MessageKind> warnings) { |
| 1371 Expect.isTrue(warnings.length >= 0 && warnings.length <= 2); | 1397 Expect.isTrue(warnings.length >= 0 && warnings.length <= 2); |
| 1372 asyncTest(() => compileScript(script).then((compiler) { | 1398 asyncTest(() => compileScript(script).then((compiler) { |
| 1373 Expect.equals(0, compiler.errors.length, | 1399 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 1374 'Unexpected errors in\n$script\n${compiler.errors}'); | 1400 Expect.equals(0, collector.errors.length, |
| 1375 Expect.equals(warnings.length, compiler.warnings.length, | 1401 'Unexpected errors in\n$script\n${collector.errors}'); |
| 1402 Expect.equals(warnings.length, collector.warnings.length, | |
| 1376 'Unexpected warnings in\n$script\n' | 1403 'Unexpected warnings in\n$script\n' |
| 1377 'Expected:$warnings\nFound:${compiler.warnings}'); | 1404 'Expected:$warnings\nFound:${collector.warnings}'); |
| 1378 for (int i = 0; i < warnings.length; i++) { | 1405 for (int i = 0; i < warnings.length; i++) { |
| 1379 Expect.equals(warnings[i], compiler.warnings[i].message.kind); | 1406 Expect.equals(warnings[i], collector.warnings.elementAt(i).message.kind); |
| 1380 } | 1407 } |
| 1381 })); | 1408 })); |
| 1382 } | 1409 } |
| 1383 | 1410 |
| 1384 testAwaitHint() { | 1411 testAwaitHint() { |
| 1385 check(String script, {String className, String functionName}) { | 1412 check(String script, {String className, String functionName}) { |
| 1386 var prefix = className == null | 1413 var prefix = className == null |
| 1387 ? "Cannot resolve 'await'" | 1414 ? "Cannot resolve 'await'" |
| 1388 : "No member named 'await' in class '$className'"; | 1415 : "No member named 'await' in class '$className'"; |
| 1389 var where = functionName == null | 1416 var where = functionName == null |
| 1390 ? 'the enclosing function' : "'$functionName'"; | 1417 ? 'the enclosing function' : "'$functionName'"; |
| 1391 asyncTest(() => compileScript(script).then((compiler) { | 1418 asyncTest(() => compileScript(script).then((compiler) { |
| 1392 Expect.equals(0, compiler.errors.length); | 1419 DiagnosticCollector collector = compiler.diagnosticCollector; |
| 1393 Expect.equals(1, compiler.warnings.length); | 1420 Expect.equals(0, collector.errors.length); |
| 1421 Expect.equals(1, collector.warnings.length); | |
| 1394 Expect.equals("$prefix.\n" | 1422 Expect.equals("$prefix.\n" |
| 1395 "Did you mean to add the 'async' marker to $where?", | 1423 "Did you mean to add the 'async' marker to $where?", |
| 1396 '${compiler.warnings[0].message}'); | 1424 '${collector.warnings.first.message}'); |
| 1397 })); | 1425 })); |
| 1398 } | 1426 } |
| 1399 check('main() { await -3; }', functionName: 'main'); | 1427 check('main() { await -3; }', functionName: 'main'); |
| 1400 check('main() { () => await -3; }'); | 1428 check('main() { () => await -3; }'); |
| 1401 check('foo() => await -3; main() => foo();', functionName: 'foo'); | 1429 check('foo() => await -3; main() => foo();', functionName: 'foo'); |
| 1402 check(''' | 1430 check(''' |
| 1403 class A { | 1431 class A { |
| 1404 m() => await - 3; | 1432 m() => await - 3; |
| 1405 } | 1433 } |
| 1406 main() => new A().m(); | 1434 main() => new A().m(); |
| 1407 ''', className: 'A', functionName: 'm'); | 1435 ''', className: 'A', functionName: 'm'); |
| 1408 check(''' | 1436 check(''' |
| 1409 class A { | 1437 class A { |
| 1410 static m() => await - 3; | 1438 static m() => await - 3; |
| 1411 } | 1439 } |
| 1412 main() => A.m(); | 1440 main() => A.m(); |
| 1413 ''', functionName: 'm'); | 1441 ''', functionName: 'm'); |
| 1414 check(''' | 1442 check(''' |
| 1415 class A { | 1443 class A { |
| 1416 m() => () => await - 3; | 1444 m() => () => await - 3; |
| 1417 } | 1445 } |
| 1418 main() => new A().m(); | 1446 main() => new A().m(); |
| 1419 ''', className: 'A'); | 1447 ''', className: 'A'); |
| 1420 } | 1448 } |
| OLD | NEW |