Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: pkg/analyzer/test/generated/checked_mode_compile_time_error_code_test.dart

Issue 1782463002: Split resolver_test.dart into smaller files. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016, 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 analyzer.test.generated.checked_mode_compile_time_error_code_test;
6
7 import 'package:analyzer/src/generated/error.dart';
8 import 'package:analyzer/src/generated/source_io.dart';
9
10 import '../reflective_tests.dart';
11 import '../utils.dart';
12 import 'resolver_test_case.dart';
13
14 main() {
15 initializeTestEnvironment();
16 runReflectiveTests(CheckedModeCompileTimeErrorCodeTest);
17 }
18
19 @reflectiveTest
20 class CheckedModeCompileTimeErrorCodeTest extends ResolverTestCase {
21 void test_fieldFormalParameterAssignableToField_extends() {
22 // According to checked-mode type checking rules, a value of type B is
23 // assignable to a field of type A, because B extends A (and hence is a
24 // subtype of A).
25 Source source = addSource(r'''
26 class A {
27 const A();
28 }
29 class B extends A {
30 const B();
31 }
32 class C {
33 final A a;
34 const C(this.a);
35 }
36 var v = const C(const B());''');
37 computeLibrarySourceErrors(source);
38 assertNoErrors(source);
39 verify([source]);
40 }
41
42 void test_fieldFormalParameterAssignableToField_fieldType_unresolved_null() {
43 // Null always passes runtime type checks, even when the type is
44 // unresolved.
45 Source source = addSource(r'''
46 class A {
47 final Unresolved x;
48 const A(String this.x);
49 }
50 var v = const A(null);''');
51 computeLibrarySourceErrors(source);
52 assertErrors(source, [StaticWarningCode.UNDEFINED_CLASS]);
53 verify([source]);
54 }
55
56 void test_fieldFormalParameterAssignableToField_implements() {
57 // According to checked-mode type checking rules, a value of type B is
58 // assignable to a field of type A, because B implements A (and hence is a
59 // subtype of A).
60 Source source = addSource(r'''
61 class A {}
62 class B implements A {
63 const B();
64 }
65 class C {
66 final A a;
67 const C(this.a);
68 }
69 var v = const C(const B());''');
70 computeLibrarySourceErrors(source);
71 assertNoErrors(source);
72 verify([source]);
73 }
74
75 void test_fieldFormalParameterAssignableToField_list_dynamic() {
76 // [1, 2, 3] has type List<dynamic>, which is a subtype of List<int>.
77 Source source = addSource(r'''
78 class A {
79 const A(List<int> x);
80 }
81 var x = const A(const [1, 2, 3]);''');
82 computeLibrarySourceErrors(source);
83 assertNoErrors(source);
84 verify([source]);
85 }
86
87 void test_fieldFormalParameterAssignableToField_list_nonDynamic() {
88 // <int>[1, 2, 3] has type List<int>, which is a subtype of List<num>.
89 Source source = addSource(r'''
90 class A {
91 const A(List<num> x);
92 }
93 var x = const A(const <int>[1, 2, 3]);''');
94 computeLibrarySourceErrors(source);
95 assertNoErrors(source);
96 verify([source]);
97 }
98
99 void test_fieldFormalParameterAssignableToField_map_dynamic() {
100 // {1: 2} has type Map<dynamic, dynamic>, which is a subtype of
101 // Map<int, int>.
102 Source source = addSource(r'''
103 class A {
104 const A(Map<int, int> x);
105 }
106 var x = const A(const {1: 2});''');
107 computeLibrarySourceErrors(source);
108 assertNoErrors(source);
109 verify([source]);
110 }
111
112 void test_fieldFormalParameterAssignableToField_map_keyDifferent() {
113 // <int, int>{1: 2} has type Map<int, int>, which is a subtype of
114 // Map<num, int>.
115 Source source = addSource(r'''
116 class A {
117 const A(Map<num, int> x);
118 }
119 var x = const A(const <int, int>{1: 2});''');
120 computeLibrarySourceErrors(source);
121 assertNoErrors(source);
122 verify([source]);
123 }
124
125 void test_fieldFormalParameterAssignableToField_map_valueDifferent() {
126 // <int, int>{1: 2} has type Map<int, int>, which is a subtype of
127 // Map<int, num>.
128 Source source = addSource(r'''
129 class A {
130 const A(Map<int, num> x);
131 }
132 var x = const A(const <int, int>{1: 2});''');
133 computeLibrarySourceErrors(source);
134 assertNoErrors(source);
135 verify([source]);
136 }
137
138 void test_fieldFormalParameterAssignableToField_notype() {
139 // If a field is declared without a type, then any value may be assigned to
140 // it.
141 Source source = addSource(r'''
142 class A {
143 final x;
144 const A(this.x);
145 }
146 var v = const A(5);''');
147 computeLibrarySourceErrors(source);
148 assertNoErrors(source);
149 verify([source]);
150 }
151
152 void test_fieldFormalParameterAssignableToField_null() {
153 // Null is assignable to anything.
154 Source source = addSource(r'''
155 class A {
156 final int x;
157 const A(this.x);
158 }
159 var v = const A(null);''');
160 computeLibrarySourceErrors(source);
161 assertNoErrors(source);
162 verify([source]);
163 }
164
165 void test_fieldFormalParameterAssignableToField_typedef() {
166 // foo has the runtime type dynamic -> dynamic, so it should be assignable
167 // to A.f.
168 Source source = addSource(r'''
169 typedef String Int2String(int x);
170 class A {
171 final Int2String f;
172 const A(this.f);
173 }
174 foo(x) => 1;
175 var v = const A(foo);''');
176 computeLibrarySourceErrors(source);
177 assertNoErrors(source);
178 verify([source]);
179 }
180
181 void test_fieldFormalParameterAssignableToField_typeSubstitution() {
182 // foo has the runtime type dynamic -> dynamic, so it should be assignable
183 // to A.f.
184 Source source = addSource(r'''
185 class A<T> {
186 final T x;
187 const A(this.x);
188 }
189 var v = const A<int>(3);''');
190 computeLibrarySourceErrors(source);
191 assertNoErrors(source);
192 verify([source]);
193 }
194
195 void test_fieldFormalParameterNotAssignableToField() {
196 Source source = addSource(r'''
197 class A {
198 final int x;
199 const A(this.x);
200 }
201 var v = const A('foo');''');
202 computeLibrarySourceErrors(source);
203 assertErrors(source, [
204 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH,
205 StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE
206 ]);
207 verify([source]);
208 }
209
210 void test_fieldFormalParameterNotAssignableToField_extends() {
211 // According to checked-mode type checking rules, a value of type A is not
212 // assignable to a field of type B, because B extends A (the subtyping
213 // relationship is in the wrong direction).
214 Source source = addSource(r'''
215 class A {
216 const A();
217 }
218 class B extends A {
219 const B();
220 }
221 class C {
222 final B b;
223 const C(this.b);
224 }
225 var v = const C(const A());''');
226 computeLibrarySourceErrors(source);
227 assertErrors(source, [
228 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH
229 ]);
230 verify([source]);
231 }
232
233 void test_fieldFormalParameterNotAssignableToField_fieldType() {
234 Source source = addSource(r'''
235 class A {
236 final int x;
237 const A(String this.x);
238 }
239 var v = const A('foo');''');
240 computeLibrarySourceErrors(source);
241 assertErrors(source, [
242 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH,
243 StaticWarningCode.FIELD_INITIALIZING_FORMAL_NOT_ASSIGNABLE
244 ]);
245 verify([source]);
246 }
247
248 void test_fieldFormalParameterNotAssignableToField_fieldType_unresolved() {
249 Source source = addSource(r'''
250 class A {
251 final Unresolved x;
252 const A(String this.x);
253 }
254 var v = const A('foo');''');
255 computeLibrarySourceErrors(source);
256 assertErrors(source, [
257 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH,
258 StaticWarningCode.UNDEFINED_CLASS
259 ]);
260 verify([source]);
261 }
262
263 void test_fieldFormalParameterNotAssignableToField_implements() {
264 // According to checked-mode type checking rules, a value of type A is not
265 // assignable to a field of type B, because B implements A (the subtyping
266 // relationship is in the wrong direction).
267 Source source = addSource(r'''
268 class A {
269 const A();
270 }
271 class B implements A {}
272 class C {
273 final B b;
274 const C(this.b);
275 }
276 var v = const C(const A());''');
277 computeLibrarySourceErrors(source);
278 assertErrors(source, [
279 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH
280 ]);
281 verify([source]);
282 }
283
284 void test_fieldFormalParameterNotAssignableToField_list() {
285 // <num>[1, 2, 3] has type List<num>, which is not a subtype of List<int>.
286 Source source = addSource(r'''
287 class A {
288 const A(List<int> x);
289 }
290 var x = const A(const <num>[1, 2, 3]);''');
291 computeLibrarySourceErrors(source);
292 assertErrors(source, [
293 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH
294 ]);
295 verify([source]);
296 }
297
298 void test_fieldFormalParameterNotAssignableToField_map_keyMismatch() {
299 // <num, int>{1: 2} has type Map<num, int>, which is not a subtype of
300 // Map<int, int>.
301 Source source = addSource(r'''
302 class A {
303 const A(Map<int, int> x);
304 }
305 var x = const A(const <num, int>{1: 2});''');
306 computeLibrarySourceErrors(source);
307 assertErrors(source, [
308 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH
309 ]);
310 verify([source]);
311 }
312
313 void test_fieldFormalParameterNotAssignableToField_map_valueMismatch() {
314 // <int, num>{1: 2} has type Map<int, num>, which is not a subtype of
315 // Map<int, int>.
316 Source source = addSource(r'''
317 class A {
318 const A(Map<int, int> x);
319 }
320 var x = const A(const <int, num>{1: 2});''');
321 computeLibrarySourceErrors(source);
322 assertErrors(source, [
323 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH
324 ]);
325 verify([source]);
326 }
327
328 void test_fieldFormalParameterNotAssignableToField_optional() {
329 Source source = addSource(r'''
330 class A {
331 final int x;
332 const A([this.x = 'foo']);
333 }
334 var v = const A();''');
335 computeLibrarySourceErrors(source);
336 assertErrors(source, [
337 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH,
338 StaticTypeWarningCode.INVALID_ASSIGNMENT
339 ]);
340 verify([source]);
341 }
342
343 void test_fieldFormalParameterNotAssignableToField_typedef() {
344 // foo has the runtime type String -> int, so it should not be assignable
345 // to A.f (A.f requires it to be int -> String).
346 Source source = addSource(r'''
347 typedef String Int2String(int x);
348 class A {
349 final Int2String f;
350 const A(this.f);
351 }
352 int foo(String x) => 1;
353 var v = const A(foo);''');
354 computeLibrarySourceErrors(source);
355 assertErrors(source, [
356 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH,
357 StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE
358 ]);
359 verify([source]);
360 }
361
362 void test_fieldInitializerNotAssignable() {
363 Source source = addSource(r'''
364 class A {
365 final int x;
366 const A() : x = '';
367 }''');
368 computeLibrarySourceErrors(source);
369 assertErrors(source, [
370 CheckedModeCompileTimeErrorCode.CONST_FIELD_INITIALIZER_NOT_ASSIGNABLE,
371 StaticWarningCode.FIELD_INITIALIZER_NOT_ASSIGNABLE
372 ]);
373 verify([source]);
374 }
375
376 void test_fieldTypeMismatch() {
377 Source source = addSource(r'''
378 class A {
379 const A(x) : y = x;
380 final int y;
381 }
382 var v = const A('foo');''');
383 computeLibrarySourceErrors(source);
384 assertErrors(source, [
385 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_FIELD_TYPE_MISMATCH
386 ]);
387 verify([source]);
388 }
389
390 void test_fieldTypeMismatch_generic() {
391 Source source = addSource(r'''
392 class C<T> {
393 final T x = y;
394 const C();
395 }
396 const int y = 1;
397 var v = const C<String>();
398 ''');
399 computeLibrarySourceErrors(source);
400 assertErrors(source, [
401 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_FIELD_TYPE_MISMATCH,
402 StaticTypeWarningCode.INVALID_ASSIGNMENT
403 ]);
404 verify([source]);
405 }
406
407 void test_fieldTypeMismatch_unresolved() {
408 Source source = addSource(r'''
409 class A {
410 const A(x) : y = x;
411 final Unresolved y;
412 }
413 var v = const A('foo');''');
414 computeLibrarySourceErrors(source);
415 assertErrors(source, [
416 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_FIELD_TYPE_MISMATCH,
417 StaticWarningCode.UNDEFINED_CLASS
418 ]);
419 verify([source]);
420 }
421
422 void test_fieldTypeOk_generic() {
423 Source source = addSource(r'''
424 class C<T> {
425 final T x = y;
426 const C();
427 }
428 const int y = 1;
429 var v = const C<int>();
430 ''');
431 computeLibrarySourceErrors(source);
432 assertErrors(source, [StaticTypeWarningCode.INVALID_ASSIGNMENT]);
433 verify([source]);
434 }
435
436 void test_fieldTypeOk_null() {
437 Source source = addSource(r'''
438 class A {
439 const A(x) : y = x;
440 final int y;
441 }
442 var v = const A(null);''');
443 computeLibrarySourceErrors(source);
444 assertNoErrors(source);
445 verify([source]);
446 }
447
448 void test_fieldTypeOk_unresolved_null() {
449 // Null always passes runtime type checks, even when the type is
450 // unresolved.
451 Source source = addSource(r'''
452 class A {
453 const A(x) : y = x;
454 final Unresolved y;
455 }
456 var v = const A(null);''');
457 computeLibrarySourceErrors(source);
458 assertErrors(source, [StaticWarningCode.UNDEFINED_CLASS]);
459 verify([source]);
460 }
461
462 void test_listElementTypeNotAssignable() {
463 Source source = addSource("var v = const <String> [42];");
464 computeLibrarySourceErrors(source);
465 assertErrors(source, [
466 CheckedModeCompileTimeErrorCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE,
467 StaticWarningCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE
468 ]);
469 verify([source]);
470 }
471
472 void test_mapKeyTypeNotAssignable() {
473 Source source = addSource("var v = const <String, int > {1 : 2};");
474 computeLibrarySourceErrors(source);
475 assertErrors(source, [
476 CheckedModeCompileTimeErrorCode.MAP_KEY_TYPE_NOT_ASSIGNABLE,
477 StaticWarningCode.MAP_KEY_TYPE_NOT_ASSIGNABLE
478 ]);
479 verify([source]);
480 }
481
482 void test_mapValueTypeNotAssignable() {
483 Source source = addSource("var v = const <String, String> {'a' : 2};");
484 computeLibrarySourceErrors(source);
485 assertErrors(source, [
486 CheckedModeCompileTimeErrorCode.MAP_VALUE_TYPE_NOT_ASSIGNABLE,
487 StaticWarningCode.MAP_VALUE_TYPE_NOT_ASSIGNABLE
488 ]);
489 verify([source]);
490 }
491
492 void test_parameterAssignable_null() {
493 // Null is assignable to anything.
494 Source source = addSource(r'''
495 class A {
496 const A(int x);
497 }
498 var v = const A(null);''');
499 computeLibrarySourceErrors(source);
500 assertNoErrors(source);
501 verify([source]);
502 }
503
504 void test_parameterAssignable_typeSubstitution() {
505 Source source = addSource(r'''
506 class A<T> {
507 const A(T x);
508 }
509 var v = const A<int>(3);''');
510 computeLibrarySourceErrors(source);
511 assertNoErrors(source);
512 verify([source]);
513 }
514
515 void test_parameterAssignable_undefined_null() {
516 // Null always passes runtime type checks, even when the type is
517 // unresolved.
518 Source source = addSource(r'''
519 class A {
520 const A(Unresolved x);
521 }
522 var v = const A(null);''');
523 computeLibrarySourceErrors(source);
524 assertErrors(source, [StaticWarningCode.UNDEFINED_CLASS]);
525 verify([source]);
526 }
527
528 void test_parameterNotAssignable() {
529 Source source = addSource(r'''
530 class A {
531 const A(int x);
532 }
533 var v = const A('foo');''');
534 computeLibrarySourceErrors(source);
535 assertErrors(source, [
536 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH,
537 StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE
538 ]);
539 verify([source]);
540 }
541
542 void test_parameterNotAssignable_typeSubstitution() {
543 Source source = addSource(r'''
544 class A<T> {
545 const A(T x);
546 }
547 var v = const A<int>('foo');''');
548 computeLibrarySourceErrors(source);
549 assertErrors(source, [
550 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH,
551 StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE
552 ]);
553 verify([source]);
554 }
555
556 void test_parameterNotAssignable_undefined() {
557 Source source = addSource(r'''
558 class A {
559 const A(Unresolved x);
560 }
561 var v = const A('foo');''');
562 computeLibrarySourceErrors(source);
563 assertErrors(source, [
564 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH,
565 StaticWarningCode.UNDEFINED_CLASS
566 ]);
567 verify([source]);
568 }
569
570 void test_redirectingConstructor_paramTypeMismatch() {
571 Source source = addSource(r'''
572 class A {
573 const A.a1(x) : this.a2(x);
574 const A.a2(String x);
575 }
576 var v = const A.a1(0);''');
577 computeLibrarySourceErrors(source);
578 assertErrors(source, [
579 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH
580 ]);
581 verify([source]);
582 }
583
584 void test_topLevelVarAssignable_null() {
585 Source source = addSource("const int x = null;");
586 computeLibrarySourceErrors(source);
587 assertNoErrors(source);
588 verify([source]);
589 }
590
591 void test_topLevelVarAssignable_undefined_null() {
592 // Null always passes runtime type checks, even when the type is
593 // unresolved.
594 Source source = addSource("const Unresolved x = null;");
595 computeLibrarySourceErrors(source);
596 assertErrors(source, [StaticWarningCode.UNDEFINED_CLASS]);
597 verify([source]);
598 }
599
600 void test_topLevelVarNotAssignable() {
601 Source source = addSource("const int x = 'foo';");
602 computeLibrarySourceErrors(source);
603 assertErrors(source, [
604 CheckedModeCompileTimeErrorCode.VARIABLE_TYPE_MISMATCH,
605 StaticTypeWarningCode.INVALID_ASSIGNMENT
606 ]);
607 verify([source]);
608 }
609
610 void test_topLevelVarNotAssignable_undefined() {
611 Source source = addSource("const Unresolved x = 'foo';");
612 computeLibrarySourceErrors(source);
613 assertErrors(source, [
614 CheckedModeCompileTimeErrorCode.VARIABLE_TYPE_MISMATCH,
615 StaticWarningCode.UNDEFINED_CLASS
616 ]);
617 verify([source]);
618 }
619 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698