Skip to content

Commit de0848e

Browse files
committed
Analyzer: Move tests out of old test file into diagonstics.
* Add non_const_call_to_literal_constructor to test_all. * Clean up non_const_call_to_literal_constructor to avoid unused variables. * Update NON_CONST_MAP_AS_EXPRESSION_STATEMENT tests to be failing tests. Change-Id: I1ca9e18372c7b5acc7b8670f81ed89ed0aa18825 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/155927 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 49d7eba commit de0848e

File tree

6 files changed

+88
-133
lines changed

6 files changed

+88
-133
lines changed

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3433,6 +3433,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
34333433
return;
34343434
}
34353435

3436+
/// TODO(srawlins): Add any tests showing this is reported.
34363437
_errorReporter.reportErrorForNode(
34373438
CompileTimeErrorCode.NON_CONST_MAP_AS_EXPRESSION_STATEMENT, literal);
34383439
}

pkg/analyzer/test/generated/compile_time_error_code_test.dart

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/src/dart/error/syntactic_errors.dart';
6-
import 'package:analyzer/src/error/codes.dart';
75
import 'package:test_reflective_loader/test_reflective_loader.dart';
86

9-
import '../src/dart/resolution/driver_resolution.dart';
107
import 'compile_time_error_code.dart';
118

129
main() {
1310
defineReflectiveSuite(() {
1411
defineReflectiveTests(CompileTimeErrorCodeTest);
15-
defineReflectiveTests(ControlFlowCollectionsTest);
1612
});
1713
}
1814

@@ -24,105 +20,3 @@ class CompileTimeErrorCodeTest extends CompileTimeErrorCodeTestBase {
2420
return super.test_constEvalThrowsException();
2521
}
2622
}
27-
28-
@reflectiveTest
29-
class ControlFlowCollectionsTest extends DriverResolutionTest {
30-
test_listElementTypeNotAssignable_const() async {
31-
await assertErrorsInCode('''
32-
var v = const <String>[42];
33-
''', [
34-
error(CompileTimeErrorCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE, 23, 2),
35-
]);
36-
}
37-
38-
test_mapValueTypeNotAssignable_const() async {
39-
await assertErrorsInCode('''
40-
var v = const <String, String>{'a' : 2};
41-
''', [
42-
error(CompileTimeErrorCode.MAP_VALUE_TYPE_NOT_ASSIGNABLE, 37, 1),
43-
]);
44-
}
45-
46-
test_nonBoolCondition_for_declaration() async {
47-
// https://github.com/dart-lang/sdk/issues/24713
48-
await assertErrorsInCode(r'''
49-
f() {
50-
for (int i = 0; 3;) {}
51-
}
52-
''', [
53-
error(HintCode.UNUSED_LOCAL_VARIABLE, 17, 1),
54-
error(CompileTimeErrorCode.NON_BOOL_CONDITION, 24, 1),
55-
]);
56-
}
57-
58-
test_nonBoolCondition_for_expression() async {
59-
// https://github.com/dart-lang/sdk/issues/24713
60-
await assertErrorsInCode(r'''
61-
f() {
62-
int i;
63-
for (i = 0; 3;) {}
64-
}''', [
65-
error(HintCode.UNUSED_LOCAL_VARIABLE, 12, 1),
66-
error(CompileTimeErrorCode.NON_BOOL_CONDITION, 29, 1),
67-
]);
68-
}
69-
70-
test_nonConstMapAsExpressionStatement_begin() async {
71-
// TODO(danrubel) Fasta is not recovering well.
72-
// Ideally we would produce a single diagnostic:
73-
// CompileTimeErrorCode.NON_CONST_MAP_AS_EXPRESSION_STATEMENT
74-
await assertErrorsInCode(r'''
75-
f() {
76-
{'a' : 0, 'b' : 1}.length;
77-
}
78-
''', [
79-
error(ParserErrorCode.EXPECTED_TOKEN, 9, 3),
80-
error(ParserErrorCode.EXPECTED_TOKEN, 13, 1),
81-
error(ParserErrorCode.MISSING_IDENTIFIER, 13, 1),
82-
error(ParserErrorCode.UNEXPECTED_TOKEN, 13, 1),
83-
error(ParserErrorCode.EXPECTED_TOKEN, 15, 1),
84-
error(ParserErrorCode.UNEXPECTED_TOKEN, 16, 1),
85-
error(ParserErrorCode.MISSING_IDENTIFIER, 16, 1),
86-
error(ParserErrorCode.EXPECTED_TOKEN, 16, 1),
87-
error(ParserErrorCode.EXPECTED_TOKEN, 18, 3),
88-
error(ParserErrorCode.UNEXPECTED_TOKEN, 22, 1),
89-
error(ParserErrorCode.MISSING_IDENTIFIER, 22, 1),
90-
error(ParserErrorCode.EXPECTED_TOKEN, 22, 1),
91-
error(ParserErrorCode.EXPECTED_TOKEN, 24, 1),
92-
error(ParserErrorCode.MISSING_IDENTIFIER, 26, 1),
93-
]);
94-
}
95-
96-
test_nonConstMapAsExpressionStatement_only() async {
97-
// TODO(danrubel) Fasta is not recovering well.
98-
// Ideally we would produce a single diagnostic:
99-
// CompileTimeErrorCode.NON_CONST_MAP_AS_EXPRESSION_STATEMENT
100-
await assertErrorsInCode(r'''
101-
f() {
102-
{'a' : 0, 'b' : 1};
103-
}
104-
''', [
105-
error(ParserErrorCode.EXPECTED_TOKEN, 9, 3),
106-
error(ParserErrorCode.EXPECTED_TOKEN, 13, 1),
107-
error(ParserErrorCode.MISSING_IDENTIFIER, 13, 1),
108-
error(ParserErrorCode.UNEXPECTED_TOKEN, 13, 1),
109-
error(ParserErrorCode.EXPECTED_TOKEN, 15, 1),
110-
error(ParserErrorCode.UNEXPECTED_TOKEN, 16, 1),
111-
error(ParserErrorCode.MISSING_IDENTIFIER, 16, 1),
112-
error(ParserErrorCode.EXPECTED_TOKEN, 16, 1),
113-
error(ParserErrorCode.EXPECTED_TOKEN, 18, 3),
114-
error(ParserErrorCode.UNEXPECTED_TOKEN, 22, 1),
115-
error(ParserErrorCode.MISSING_IDENTIFIER, 22, 1),
116-
error(ParserErrorCode.EXPECTED_TOKEN, 22, 1),
117-
error(ParserErrorCode.EXPECTED_TOKEN, 24, 1),
118-
]);
119-
}
120-
121-
test_setElementTypeNotAssignable_const() async {
122-
await assertErrorsInCode('''
123-
var v = const <String>{42};
124-
''', [
125-
error(CompileTimeErrorCode.SET_ELEMENT_TYPE_NOT_ASSIGNABLE, 23, 2),
126-
]);
127-
}
128-
}

pkg/analyzer/test/src/diagnostics/non_bool_condition_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,30 @@ f() {
4646
]);
4747
}
4848

49+
test_for_declaration() async {
50+
// https://github.com/dart-lang/sdk/issues/24713
51+
await assertErrorsInCode(r'''
52+
f() {
53+
for (int i = 0; 3;) {}
54+
}
55+
''', [
56+
error(HintCode.UNUSED_LOCAL_VARIABLE, 17, 1),
57+
error(CompileTimeErrorCode.NON_BOOL_CONDITION, 24, 1),
58+
]);
59+
}
60+
61+
test_for_expression() async {
62+
// https://github.com/dart-lang/sdk/issues/24713
63+
await assertErrorsInCode(r'''
64+
f() {
65+
int i;
66+
for (i = 0; 3;) {}
67+
}''', [
68+
error(HintCode.UNUSED_LOCAL_VARIABLE, 12, 1),
69+
error(CompileTimeErrorCode.NON_BOOL_CONDITION, 29, 1),
70+
]);
71+
}
72+
4973
test_forElement() async {
5074
await assertErrorsInCode('''
5175
var v = [for (; 0;) 1];

pkg/analyzer/test/src/diagnostics/non_const_call_to_literal_constructor.dart renamed to pkg/analyzer/test/src/diagnostics/non_const_call_to_literal_constructor_test.dart

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ class A {
4040
@literal
4141
const A();
4242
}
43-
44-
void main() {
45-
const a = A();
46-
}
43+
const a = A();
4744
''');
4845
}
4946

@@ -54,10 +51,7 @@ class A {
5451
@literal
5552
const A();
5653
}
57-
58-
void main() {
59-
const a = const A();
60-
}
54+
const a = const A();
6155
''');
6256
}
6357

@@ -68,12 +62,9 @@ class A {
6862
@literal
6963
const A.named();
7064
}
71-
void main() {
72-
var a = A.named();
73-
}
65+
var a = A.named();
7466
''', [
75-
error(HintCode.UNUSED_LOCAL_VARIABLE, 95, 1),
76-
error(HintCode.NON_CONST_CALL_TO_LITERAL_CONSTRUCTOR, 99, 9),
67+
error(HintCode.NON_CONST_CALL_TO_LITERAL_CONSTRUCTOR, 83, 9),
7768
]);
7869
}
7970

@@ -84,12 +75,9 @@ class A {
8475
@literal
8576
const A();
8677
}
87-
void main() {
88-
var a = A();
89-
}
78+
var a = A();
9079
''', [
91-
error(HintCode.UNUSED_LOCAL_VARIABLE, 89, 1),
92-
error(HintCode.NON_CONST_CALL_TO_LITERAL_CONSTRUCTOR, 93, 3),
80+
error(HintCode.NON_CONST_CALL_TO_LITERAL_CONSTRUCTOR, 77, 3),
9381
]);
9482
}
9583

@@ -100,10 +88,7 @@ class A {
10088
@literal
10189
const A(List list);
10290
}
103-
104-
void main() {
105-
var a = A(new List());
106-
}
91+
var a = A(new List());
10792
''');
10893
}
10994

@@ -114,12 +99,9 @@ class A {
11499
@literal
115100
const A();
116101
}
117-
void main() {
118-
var a = new A();
119-
}
102+
var a = new A();
120103
''', [
121-
error(HintCode.UNUSED_LOCAL_VARIABLE, 89, 1),
122-
error(HintCode.NON_CONST_CALL_TO_LITERAL_CONSTRUCTOR_USING_NEW, 93, 7),
104+
error(HintCode.NON_CONST_CALL_TO_LITERAL_CONSTRUCTOR_USING_NEW, 77, 7),
123105
]);
124106
}
125107
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2020, 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+
import 'package:analyzer/src/error/codes.dart';
6+
import 'package:test/test.dart';
7+
import 'package:test_reflective_loader/test_reflective_loader.dart';
8+
9+
import '../dart/resolution/driver_resolution.dart';
10+
11+
main() {
12+
defineReflectiveSuite(() {
13+
defineReflectiveTests(NonConstMapAsExpressionStatementTest);
14+
});
15+
}
16+
17+
@reflectiveTest
18+
class NonConstMapAsExpressionStatementTest extends DriverResolutionTest {
19+
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/42850')
20+
test_beginningOfExpressiontatement() async {
21+
// TODO(srawlins) Fasta is not recovering well.
22+
// Ideally we would produce a single diagnostic:
23+
// CompileTimeErrorCode.NON_CONST_MAP_AS_EXPRESSION_STATEMENT
24+
addTestFile(r'''
25+
f() {
26+
{'a' : 0, 'b' : 1}.length;
27+
}
28+
''');
29+
await resolveTestFile();
30+
expect(result.errors[0].errorCode,
31+
CompileTimeErrorCode.NON_CONST_MAP_AS_EXPRESSION_STATEMENT);
32+
}
33+
34+
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/42850')
35+
test_expressionStatementOnly() async {
36+
// TODO(danrubel) Fasta is not recovering well.
37+
// Ideally we would produce a single diagnostic:
38+
// CompileTimeErrorCode.NON_CONST_MAP_AS_EXPRESSION_STATEMENT
39+
addTestFile(r'''
40+
f() {
41+
{'a' : 0, 'b' : 1};
42+
}
43+
''');
44+
await resolveTestFile();
45+
expect(result.errors[0].errorCode,
46+
CompileTimeErrorCode.NON_CONST_MAP_AS_EXPRESSION_STATEMENT);
47+
}
48+
}

pkg/analyzer/test/src/diagnostics/test_all.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ import 'non_bool_condition_test.dart' as non_bool_condition;
380380
import 'non_bool_expression_test.dart' as non_bool_expression;
381381
import 'non_bool_negation_expression_test.dart' as non_bool_negation_expression;
382382
import 'non_bool_operand_test.dart' as non_bool_operand;
383+
import 'non_const_call_to_literal_constructor_test.dart'
384+
as non_const_call_to_literal_constructor;
385+
import 'non_const_map_as_expression_statement_test.dart'
386+
as non_const_map_as_expression_statement;
383387
import 'non_constant_annotation_constructor_test.dart'
384388
as non_constant_annotation_constructor;
385389
import 'non_constant_case_expression_from_deferred_library_test.dart'
@@ -885,6 +889,8 @@ main() {
885889
non_void_return_for_setter.main();
886890
not_a_type.main();
887891
not_assigned_potentially_non_nullable_local_variable.main();
892+
non_const_call_to_literal_constructor.main();
893+
non_const_map_as_expression_statement.main();
888894
not_enough_positional_arguments.main();
889895
not_initialized_non_nullable_instance_field.main();
890896
not_initialized_non_nullable_variable.main();

0 commit comments

Comments
 (0)