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

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

Issue 1702733002: Support for line-level error suppression (#25685). (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 10 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 import 'package:analyzer/src/generated/engine.dart';
6 import 'package:analyzer/src/generated/error.dart';
7 import 'package:analyzer/src/generated/java_engine_io.dart';
8 import 'package:analyzer/src/generated/source.dart';
9 import 'package:analyzer/src/generated/source_io.dart';
10
11 import '../reflective_tests.dart';
12 import '../utils.dart';
13 import 'resolver_test.dart';
14 import 'test_support.dart';
15
16 main() {
17 initializeTestEnvironment();
18 runReflectiveTests(ErrorSuppressionTest);
19 }
20
21 @reflectiveTest
22 class ErrorSuppressionTest extends EngineTestCase {
Brian Wilkerson 2016/02/16 21:39:05 A lot of the support code in this class is already
pquitslund 2016/02/16 21:51:42 Funny. I started that way and then convinced myse
23 InternalAnalysisContext analysisContext;
24
25 Source addNamedSource(String filePath, String contents) {
26 Source source = cacheSource(filePath, contents);
27 ChangeSet changeSet = new ChangeSet();
28 changeSet.addedSource(source);
29 analysisContext.applyChanges(changeSet);
30 return source;
31 }
32
33 Source addSource(String contents) => addNamedSource("/test.dart", contents);
34
35 void assertErrors(Source source,
36 [List<ErrorCode> expectedErrorCodes = ErrorCode.EMPTY_LIST]) {
37 GatheringErrorListener errorListener = new GatheringErrorListener();
38 for (AnalysisError error in analysisContext.computeErrors(source)) {
39 errorListener.onError(error);
40 }
41 errorListener.assertErrorsWithCodes(expectedErrorCodes);
42 }
43
44 Source cacheSource(String filePath, String contents) {
45 Source source = new FileBasedSource(FileUtilities2.createFile(filePath));
46 analysisContext.setContents(source, contents);
47 return source;
48 }
49
50 void computeLibrarySourceErrors(Source librarySource) {
51 analysisContext.computeErrors(librarySource);
52 }
53
54 void reset() {
55 analysisContext = AnalysisContextFactory.contextWithCore();
56 }
57
58 @override
59 void setUp() {
60 super.setUp();
61 reset();
62 }
63
64 void test_error_code_mismatch() {
65 Source source = addSource('''
66 //# ignore: const_initialized_with_non_constant_value
67 int x = '';
68 const y = x; //CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
69 ''');
70 computeLibrarySourceErrors(source);
71 assertErrors(source, [
72 StaticTypeWarningCode.INVALID_ASSIGNMENT,
73 CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
74 ]);
75 }
76
77 void test_ignore_first() {
78 Source source = addSource('''
79 //# ignore: invalid_assignment
80 int x = '';
81 // ... but no ignore here ...
82 const y = x; //CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
83 ''');
84 computeLibrarySourceErrors(source);
85 assertErrors(source,
86 [CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE]);
87 }
88
89 void test_ignore_second() {
90 Source source = addSource('''
91 //INVALID_ASSIGNMENT
92 int x = '';
93 //# ignore: const_initialized_with_non_constant_value
94 const y = x; //CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
95 ''');
96 computeLibrarySourceErrors(source);
97 assertErrors(source, [StaticTypeWarningCode.INVALID_ASSIGNMENT]);
98 }
99
100 void test_invalid_error_code() {
101 Source source = addSource('''
102 //# ignore: right_format_wrong_code
103 int x = '';
104 const y = x; //CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
105 ''');
106 computeLibrarySourceErrors(source);
107 assertErrors(source, [
108 StaticTypeWarningCode.INVALID_ASSIGNMENT,
109 CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
110 ]);
111 }
112
113 void test_missing_error_codes() {
114 Source source = addSource('''
115 int x = 3;
116 //# ignore:
117 const String y = x; //INVALID_ASSIGNMENT, CONST_INITIALIZED_WITH_NON_CONSTANT_VA LUE
118 ''');
119 computeLibrarySourceErrors(source);
120 assertErrors(source, [
121 StaticTypeWarningCode.INVALID_ASSIGNMENT,
122 CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
123 ]);
124 }
125
126 void test_missing_metadata_prefix() {
127 Source source = addSource('''
128 // ignore: invalid_assignment
129 String y = 3; //INVALID_ASSIGNMENT
130 ''');
131 computeLibrarySourceErrors(source);
132 assertErrors(source, [StaticTypeWarningCode.INVALID_ASSIGNMENT]);
133 }
134
135 void test_multiple_comments() {
136 Source source = addSource('''
137 int x = ''; //This is the first comment...
138 //# ignore: const_initialized_with_non_constant_value
139 const y = x; //CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
140 ''');
141 computeLibrarySourceErrors(source);
142 assertErrors(source, [StaticTypeWarningCode.INVALID_ASSIGNMENT]);
143 }
144
145 void test_multiple_ignores() {
146 Source source = addSource('''
147 int x = 3;
148 //# ignore: invalid_assignment, const_initialized_with_non_constant_value
149 const String y = x; //INVALID_ASSIGNMENT, CONST_INITIALIZED_WITH_NON_CONSTANT_VA LUE
150 ''');
151 computeLibrarySourceErrors(source);
152 assertErrors(source, []);
153 }
154
155 void test_multiple_ignores_whitespace_variant_1() {
156 Source source = addSource('''
157 int x = 3;
158 //#ignore:invalid_assignment,const_initialized_with_non_constant_value
159 const String y = x; //INVALID_ASSIGNMENT, CONST_INITIALIZED_WITH_NON_CONSTANT_VA LUE
160 ''');
161 computeLibrarySourceErrors(source);
162 assertErrors(source, []);
163 }
164
165 void test_multiple_ignores_whitespace_variant_2() {
166 Source source = addSource('''
167 int x = 3;
168 //#ignore: invalid_assignment,const_initialized_with_non_constant_value
169 const String y = x; //INVALID_ASSIGNMENT, CONST_INITIALIZED_WITH_NON_CONSTANT_VA LUE
170 ''');
171 computeLibrarySourceErrors(source);
172 assertErrors(source, []);
173 }
174
175 void test_multiple_ignores_whitespace_variant_3() {
176 Source source = addSource('''
177 int x = 3;
178 //# ignore: invalid_assignment,const_initialized_with_non_constant_value
179 const String y = x; //INVALID_ASSIGNMENT, CONST_INITIALIZED_WITH_NON_CONSTANT_VA LUE
180 ''');
181 computeLibrarySourceErrors(source);
182 assertErrors(source, []);
183 }
184
185 void test_no_ignores() {
186 Source source = addSource('''
187 int x = ''; //INVALID_ASSIGNMENT
188 const y = x; //CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
189 ''');
190 computeLibrarySourceErrors(source);
191 assertErrors(source, [
192 StaticTypeWarningCode.INVALID_ASSIGNMENT,
193 CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
194 ]);
195 }
196 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698