| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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("../../../../lib/compiler/implementation/leg.dart"); | 5 #import("../../../../lib/compiler/implementation/leg.dart"); |
| 6 #import("../../../../lib/compiler/implementation/elements/elements.dart"); | 6 #import("../../../../lib/compiler/implementation/elements/elements.dart"); |
| 7 #import("../../../../lib/compiler/implementation/tree/tree.dart"); | 7 #import("../../../../lib/compiler/implementation/tree/tree.dart"); |
| 8 #import('../../../../lib/compiler/implementation/scanner/scannerlib.dart'); | 8 #import('../../../../lib/compiler/implementation/scanner/scannerlib.dart'); |
| 9 #import("../../../../lib/compiler/implementation/util/util.dart"); | 9 #import("../../../../lib/compiler/implementation/util/util.dart"); |
| 10 #import("mock_compiler.dart"); | 10 #import("mock_compiler.dart"); |
| 11 #import("parser_helper.dart"); | 11 #import("parser_helper.dart"); |
| 12 | 12 |
| 13 Type intType; | 13 Type intType; |
| 14 Type boolType; | 14 Type boolType; |
| 15 Type stringType; | 15 Type stringType; |
| 16 Type doubleType; | 16 Type doubleType; |
| 17 Type objectType; | 17 Type objectType; |
| 18 | 18 |
| 19 main() { | 19 main() { |
| 20 List tests = [testSimpleTypes, | 20 List tests = [testSimpleTypes, |
| 21 testReturn, | 21 testReturn, |
| 22 testFor, | 22 testFor, |
| 23 testWhile, | 23 testWhile, |
| 24 testOperators, | 24 testOperators, |
| 25 testConstructorInvocationArgumentCount, |
| 26 testConstructorInvocationArgumentTypes, |
| 25 testMethodInvocationArgumentCount, | 27 testMethodInvocationArgumentCount, |
| 26 testMethodInvocations, | 28 testMethodInvocations, |
| 27 testControlFlow, | 29 testControlFlow, |
| 28 // testNewExpression, | 30 // testNewExpression, |
| 29 testConditionalExpression, | 31 testConditionalExpression, |
| 30 testIfStatement, | 32 testIfStatement, |
| 31 testThis, | 33 testThis, |
| 32 testFunctionSubtyping]; | 34 testFunctionSubtyping]; |
| 33 for (Function test in tests) { | 35 for (Function test in tests) { |
| 34 setup(); | 36 setup(); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 for (final op in ['>', '<', '<=', '>=', '==', '!=', '===', '!==']) { | 127 for (final op in ['>', '<', '<=', '>=', '==', '!=', '===', '!==']) { |
| 126 analyze("{ bool b = 1 ${op} 2; }"); | 128 analyze("{ bool b = 1 ${op} 2; }"); |
| 127 analyze("{ int i = 1 ${op} 2; }", MessageKind.NOT_ASSIGNABLE); | 129 analyze("{ int i = 1 ${op} 2; }", MessageKind.NOT_ASSIGNABLE); |
| 128 analyze("{ int i; bool b = (i = true) ${op} 2; }", | 130 analyze("{ int i; bool b = (i = true) ${op} 2; }", |
| 129 MessageKind.NOT_ASSIGNABLE); | 131 MessageKind.NOT_ASSIGNABLE); |
| 130 analyze("{ int i; bool b = 1 ${op} (i = true); }", | 132 analyze("{ int i; bool b = 1 ${op} (i = true); }", |
| 131 MessageKind.NOT_ASSIGNABLE); | 133 MessageKind.NOT_ASSIGNABLE); |
| 132 } | 134 } |
| 133 } | 135 } |
| 134 | 136 |
| 137 void testConstructorInvocationArgumentCount() { |
| 138 compiler.parseScript(""" |
| 139 class C1 { C1(x, y); } |
| 140 class C2 { C2(int x, int y); } |
| 141 """); |
| 142 // calls to untyped constructor C1 |
| 143 analyze("new C1(1, 2);"); |
| 144 analyze("new C1();", MessageKind.MISSING_ARGUMENT); |
| 145 analyze("new C1(1);", MessageKind.MISSING_ARGUMENT); |
| 146 analyze("new C1(1, 2, 3);", MessageKind.ADDITIONAL_ARGUMENT); |
| 147 // calls to typed constructor C2 |
| 148 analyze("new C2(1, 2);"); |
| 149 analyze("new C2();", MessageKind.MISSING_ARGUMENT); |
| 150 analyze("new C2(1);", MessageKind.MISSING_ARGUMENT); |
| 151 analyze("new C2(1, 2, 3);", MessageKind.ADDITIONAL_ARGUMENT); |
| 152 } |
| 153 |
| 154 void testConstructorInvocationArgumentTypes() { |
| 155 compiler.parseScript(""" |
| 156 class C1 { C1(x); } |
| 157 class C2 { C2(int x); } |
| 158 """); |
| 159 analyze("new C1(42);"); |
| 160 analyze("new C1('string');"); |
| 161 analyze("new C2(42);"); |
| 162 analyze("new C2('string');", |
| 163 MessageKind.NOT_ASSIGNABLE); |
| 164 } |
| 165 |
| 135 void testMethodInvocationArgumentCount() { | 166 void testMethodInvocationArgumentCount() { |
| 136 compiler.parseScript(CLASS_WITH_METHODS); | 167 compiler.parseScript(CLASS_WITH_METHODS); |
| 137 final String header = "{ ClassWithMethods c; "; | 168 final String header = "{ ClassWithMethods c; "; |
| 138 analyze("${header}c.untypedNoArgumentMethod(1); }", | 169 analyze("${header}c.untypedNoArgumentMethod(1); }", |
| 139 MessageKind.ADDITIONAL_ARGUMENT); | 170 MessageKind.ADDITIONAL_ARGUMENT); |
| 140 analyze("${header}c.untypedOneArgumentMethod(); }", | 171 analyze("${header}c.untypedOneArgumentMethod(); }", |
| 141 MessageKind.MISSING_ARGUMENT); | 172 MessageKind.MISSING_ARGUMENT); |
| 142 analyze("${header}c.untypedOneArgumentMethod(1, 1); }", | 173 analyze("${header}c.untypedOneArgumentMethod(1, 1); }", |
| 143 MessageKind.ADDITIONAL_ARGUMENT); | 174 MessageKind.ADDITIONAL_ARGUMENT); |
| 144 analyze("${header}c.untypedTwoArgumentMethod(); }", | 175 analyze("${header}c.untypedTwoArgumentMethod(); }", |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 MessageKind.NOT_ASSIGNABLE); | 223 MessageKind.NOT_ASSIGNABLE); |
| 193 analyze("${header}int k = c.intOneArgumentMethod('string'); }", | 224 analyze("${header}int k = c.intOneArgumentMethod('string'); }", |
| 194 MessageKind.NOT_ASSIGNABLE); | 225 MessageKind.NOT_ASSIGNABLE); |
| 195 analyze("${header}int k = c.intOneArgumentMethod(i); }"); | 226 analyze("${header}int k = c.intOneArgumentMethod(i); }"); |
| 196 | 227 |
| 197 analyze("${header}int k = c.intTwoArgumentMethod(1, 'string'); }", | 228 analyze("${header}int k = c.intTwoArgumentMethod(1, 'string'); }", |
| 198 MessageKind.NOT_ASSIGNABLE); | 229 MessageKind.NOT_ASSIGNABLE); |
| 199 analyze("${header}int k = c.intTwoArgumentMethod(i, j); }"); | 230 analyze("${header}int k = c.intTwoArgumentMethod(i, j); }"); |
| 200 analyze("${header}ClassWithMethods x = c.intTwoArgumentMethod(i, j); }", | 231 analyze("${header}ClassWithMethods x = c.intTwoArgumentMethod(i, j); }", |
| 201 MessageKind.NOT_ASSIGNABLE); | 232 MessageKind.NOT_ASSIGNABLE); |
| 233 |
| 234 analyze("${header}c.intField(); }", MessageKind.METHOD_NOT_FOUND); |
| 235 analyze("${header}d.intField(); }", MessageKind.METHOD_NOT_FOUND); |
| 202 } | 236 } |
| 203 | 237 |
| 204 /** Tests analysis of returns (not required by the specification). */ | 238 /** Tests analysis of returns (not required by the specification). */ |
| 205 void testControlFlow() { | 239 void testControlFlow() { |
| 206 analyzeTopLevel("int foo() { if (true) { return 1; } }", | 240 analyzeTopLevel("int foo() { if (true) { return 1; } }", |
| 207 MessageKind.MAYBE_MISSING_RETURN); | 241 MessageKind.MAYBE_MISSING_RETURN); |
| 208 final bar = | 242 final bar = |
| 209 """void bar() { | 243 """void bar() { |
| 210 if (true) { | 244 if (true) { |
| 211 if (true) { return; } else { return; } | 245 if (true) { return; } else { return; } |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 parser.parseStatement(tokens); | 457 parser.parseStatement(tokens); |
| 424 Node node = listener.popNode(); | 458 Node node = listener.popNode(); |
| 425 TreeElements elements = compiler.resolveNodeStatement(node, classElement); | 459 TreeElements elements = compiler.resolveNodeStatement(node, classElement); |
| 426 TypeCheckerVisitor checker = new TypeCheckerVisitor(compiler, elements, | 460 TypeCheckerVisitor checker = new TypeCheckerVisitor(compiler, elements, |
| 427 types); | 461 types); |
| 428 compiler.clearWarnings(); | 462 compiler.clearWarnings(); |
| 429 checker.currentClass = classElement; | 463 checker.currentClass = classElement; |
| 430 checker.analyze(node); | 464 checker.analyze(node); |
| 431 compareWarningKinds(text, expectedWarnings, compiler.warnings); | 465 compareWarningKinds(text, expectedWarnings, compiler.warnings); |
| 432 } | 466 } |
| OLD | NEW |