| 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 "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 import 'dart:async'; | 6 import 'dart:async'; |
| 7 import "package:async_helper/async_helper.dart"; | 7 import "package:async_helper/async_helper.dart"; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 | 9 |
| 10 import "package:compiler/src/resolution/resolution.dart"; | 10 import "package:compiler/src/resolution/resolution.dart"; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 testSuperCalls, | 82 testSuperCalls, |
| 83 testSwitch, | 83 testSwitch, |
| 84 testTypeVariables, | 84 testTypeVariables, |
| 85 testToString, | 85 testToString, |
| 86 testIndexedOperator, | 86 testIndexedOperator, |
| 87 testIncrementsAndDecrements, | 87 testIncrementsAndDecrements, |
| 88 testOverrideHashCodeCheck, | 88 testOverrideHashCodeCheck, |
| 89 testSupertypeOrder, | 89 testSupertypeOrder, |
| 90 testConstConstructorAndNonFinalFields, | 90 testConstConstructorAndNonFinalFields, |
| 91 testCantAssignMethods, | 91 testCantAssignMethods, |
| 92 testCantAssignFinalAndConsts, |
| 92 ], (f) => f())); | 93 ], (f) => f())); |
| 93 } | 94 } |
| 94 | 95 |
| 95 Future testSupertypeOrder() { | 96 Future testSupertypeOrder() { |
| 96 return Future.wait([ | 97 return Future.wait([ |
| 97 MockCompiler.create((MockCompiler compiler) { | 98 MockCompiler.create((MockCompiler compiler) { |
| 98 compiler.parseScript(""" | 99 compiler.parseScript(""" |
| 99 class I1 {} | 100 class I1 {} |
| 100 class I2 {} | 101 class I2 {} |
| 101 class J1 extends K1 {} | 102 class J1 extends K1 {} |
| (...skipping 1036 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1138 expect(compiler, | 1139 expect(compiler, |
| 1139 [MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS], | 1140 [MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS], |
| 1140 [MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_CONSTRUCTOR, | 1141 [MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_CONSTRUCTOR, |
| 1141 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_CONSTRUCTOR, | 1142 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_CONSTRUCTOR, |
| 1142 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_FIELD, | 1143 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_FIELD, |
| 1143 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_FIELD]); | 1144 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_FIELD]); |
| 1144 })); | 1145 })); |
| 1145 } | 1146 } |
| 1146 | 1147 |
| 1147 testCantAssignMethods() { | 1148 testCantAssignMethods() { |
| 1148 checkWarningOn(String script, List<String> errorLocations) { | |
| 1149 asyncTest(() => compileScript(script).then((compiler) { | |
| 1150 Expect.equals(0, compiler.errors.length); | |
| 1151 Expect.equals(errorLocations.length, compiler.warnings.length); | |
| 1152 for (var i = 0; i < errorLocations.length; i++) { | |
| 1153 Expect.equals(MessageKind.ASSIGNING_METHOD, | |
| 1154 compiler.warnings[i].message.kind); | |
| 1155 Expect.equals(script.indexOf(errorLocations[i]), | |
| 1156 compiler.warnings[i].node.getBeginToken().charOffset); | |
| 1157 } | |
| 1158 })); | |
| 1159 } | |
| 1160 | |
| 1161 // Can't override local functions | 1149 // Can't override local functions |
| 1162 checkWarningOn(''' | 1150 checkWarningOn(''' |
| 1163 main() { | 1151 main() { |
| 1164 mname() { mname = 2; }; | 1152 mname() { mname = 2; }; |
| 1165 mname(); | 1153 mname(); |
| 1166 } | 1154 } |
| 1167 ''', ['mname = 2']); | 1155 ''', [MessageKind.ASSIGNING_METHOD]); |
| 1168 | 1156 |
| 1169 checkWarningOn(''' | 1157 checkWarningOn(''' |
| 1170 main() { | 1158 main() { |
| 1171 mname() { }; | 1159 mname() { }; |
| 1172 mname = 3; | 1160 mname = 3; |
| 1173 } | 1161 } |
| 1174 ''', ['mname = 3']); | 1162 ''', [MessageKind.ASSIGNING_METHOD]); |
| 1175 | 1163 |
| 1176 // Can't override top-level functions | 1164 // Can't override top-level functions |
| 1177 checkWarningOn(''' | 1165 checkWarningOn(''' |
| 1178 m() {} | 1166 m() {} |
| 1179 main() { m = 4; } | 1167 main() { m = 4; } |
| 1180 ''', ['m = 4']); | 1168 ''', [MessageKind.ASSIGNING_METHOD]); |
| 1181 | 1169 |
| 1182 // Can't override instance methods | 1170 // Can't override instance methods |
| 1183 checkWarningOn(''' | 1171 checkWarningOn(''' |
| 1184 main() { new B().bar(); } | 1172 main() { new B().bar(); } |
| 1185 class B { | 1173 class B { |
| 1186 mname() {} | 1174 mname() {} |
| 1187 bar() { | 1175 bar() { |
| 1188 mname = () => null; | 1176 mname = () => null; |
| 1189 } | 1177 } |
| 1190 } | 1178 } |
| 1191 ''', ['mname = () => null']); | 1179 ''', [MessageKind.SETTER_NOT_FOUND]); |
| 1192 | 1180 |
| 1193 // Can't override super methods | 1181 // Can't override super methods |
| 1194 checkWarningOn(''' | 1182 checkWarningOn(''' |
| 1195 main() { new B().bar(); } | 1183 main() { new B().bar(); } |
| 1196 class A { | 1184 class A { |
| 1197 mname() {} | 1185 mname() {} |
| 1198 } | 1186 } |
| 1199 class B extends A { | 1187 class B extends A { |
| 1200 bar() { | 1188 bar() { |
| 1201 super.mname = () => 6; | 1189 super.mname = () => 6; |
| 1202 } | 1190 } |
| 1203 } | 1191 } |
| 1204 ''', ['mname = () => 6']); | 1192 ''', [MessageKind.SETTER_NOT_FOUND]); |
| 1205 | 1193 |
| 1206 // But fields are OK: | 1194 // But index operators should be OK |
| 1207 checkWarningOn(''' | |
| 1208 main() { new B().bar(); } | |
| 1209 class A { | |
| 1210 int fname; | |
| 1211 } | |
| 1212 class B extends A { | |
| 1213 bar() { | |
| 1214 super.fname = 3; | |
| 1215 } | |
| 1216 } | |
| 1217 ''', []); | |
| 1218 | |
| 1219 // And we shouldn't confuse index operators either: | |
| 1220 checkWarningOn(''' | 1195 checkWarningOn(''' |
| 1221 main() { new B().bar(); } | 1196 main() { new B().bar(); } |
| 1222 class B { | 1197 class B { |
| 1223 operator[]=(x, y) {} | 1198 operator[]=(x, y) {} |
| 1224 bar() { | 1199 bar() { |
| 1225 this[1] = 3; // This is OK | 1200 this[1] = 3; // This is OK |
| 1226 } | 1201 } |
| 1227 } | 1202 } |
| 1228 ''', []); | 1203 ''', []); |
| 1229 checkWarningOn(''' | 1204 checkWarningOn(''' |
| 1230 main() { new B().bar(); } | 1205 main() { new B().bar(); } |
| 1231 class A { | 1206 class A { |
| 1232 operator[]=(x, y) {} | 1207 operator[]=(x, y) {} |
| 1233 } | 1208 } |
| 1234 class B extends A { | 1209 class B extends A { |
| 1235 bar() { | 1210 bar() { |
| 1236 super[1] = 3; // This is OK | 1211 super[1] = 3; // This is OK |
| 1237 } | 1212 } |
| 1238 } | 1213 } |
| 1239 ''', []); | 1214 ''', []); |
| 1240 } | 1215 } |
| 1216 |
| 1217 testCantAssignFinalAndConsts() { |
| 1218 // Can't write final or const locals. |
| 1219 checkWarningOn(''' |
| 1220 main() { |
| 1221 final x = 1; |
| 1222 x = 2; |
| 1223 } |
| 1224 ''', [MessageKind.CANNOT_RESOLVE_SETTER]); |
| 1225 checkWarningOn(''' |
| 1226 main() { |
| 1227 const x = 1; |
| 1228 x = 2; |
| 1229 } |
| 1230 ''', [MessageKind.CANNOT_RESOLVE_SETTER]); |
| 1231 checkWarningOn(''' |
| 1232 final x = 1; |
| 1233 main() { x = 3; } |
| 1234 ''', [MessageKind.CANNOT_RESOLVE_SETTER]); |
| 1235 |
| 1236 checkWarningOn(''' |
| 1237 const x = 1; |
| 1238 main() { x = 3; } |
| 1239 ''', [MessageKind.CANNOT_RESOLVE_SETTER]); |
| 1240 |
| 1241 // Detect assignments to final fields: |
| 1242 checkWarningOn(''' |
| 1243 main() => new B().m(); |
| 1244 class B { |
| 1245 final x = 1; |
| 1246 m() { x = 2; } |
| 1247 } |
| 1248 ''', [MessageKind.SETTER_NOT_FOUND]); |
| 1249 |
| 1250 // ... and in super class: |
| 1251 checkWarningOn(''' |
| 1252 main() => new B().m(); |
| 1253 class A { |
| 1254 final x = 1; |
| 1255 } |
| 1256 class B extends A { |
| 1257 m() { super.x = 2; } |
| 1258 } |
| 1259 ''', [MessageKind.SETTER_NOT_FOUND]); |
| 1260 |
| 1261 // But non-final fields are OK: |
| 1262 checkWarningOn(''' |
| 1263 main() => new B().m(); |
| 1264 class A { |
| 1265 int x = 1; |
| 1266 } |
| 1267 class B extends A { |
| 1268 m() { super.x = 2; } |
| 1269 } |
| 1270 ''', []); |
| 1271 } |
| 1272 |
| 1273 |
| 1274 /// Helper to test that [script] produces all the given [warnings]. |
| 1275 checkWarningOn(String script, List<MessageKind> warnings) { |
| 1276 Expect.isTrue(warnings.length >= 0 && warnings.length <= 2); |
| 1277 asyncTest(() => compileScript(script).then((compiler) { |
| 1278 Expect.equals(0, compiler.errors.length); |
| 1279 Expect.equals(warnings.length, compiler.warnings.length); |
| 1280 for (int i = 0; i < warnings.length; i++) { |
| 1281 Expect.equals(warnings[i], compiler.warnings[i].message.kind); |
| 1282 } |
| 1283 })); |
| 1284 } |
| OLD | NEW |