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

Side by Side Diff: pkg/analyzer/test/src/task/strong/non_null_checker_test.dart

Issue 2259333002: Add more non-nullability tests (Closed) Base URL: https://github.com/dart-lang/sdk@master
Patch Set: Created 4 years, 4 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 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 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 // These tests are for an experimental feature that treats Dart primitive types 5 // These tests are for an experimental feature that treats Dart primitive types
6 // (int, bool, double, etc.) as non-nullable. This file is not evidence for an 6 // (int, bool, double, etc.) as non-nullable. This file is not evidence for an
7 // intention to officially support non-nullable primitives in Dart (or general 7 // intention to officially support non-nullable primitives in Dart (or general
8 // NNBD, for that matter) so don't get too crazy about it. 8 // NNBD, for that matter) so don't get too crazy about it.
9 9
10 library analyzer.test.src.task.non_null_primitives.checker_test; 10 library analyzer.test.src.task.non_null_primitives.checker_test;
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 173
174 void test_nonnullable_fields() { 174 void test_nonnullable_fields() {
175 addFile(defaultNnbdExample); 175 addFile(defaultNnbdExample);
176 // `null` can be passed as an argument to `Point` in default mode. 176 // `null` can be passed as an argument to `Point` in default mode.
177 addFile(_withError(defaultNnbdExampleMod1, "error:INVALID_ASSIGNMENT")); 177 addFile(_withError(defaultNnbdExampleMod1, "error:INVALID_ASSIGNMENT"));
178 // A nullable expression can be passed as an argument to `Point` in default 178 // A nullable expression can be passed as an argument to `Point` in default
179 // mode. 179 // mode.
180 addFile(_withError(defaultNnbdExampleMod2, "error:INVALID_ASSIGNMENT")); 180 addFile(_withError(defaultNnbdExampleMod2, "error:INVALID_ASSIGNMENT"));
181 check(nonnullableTypes: <String>['dart:core,int']); 181 check(nonnullableTypes: <String>['dart:core,int']);
182 } 182 }
183
184 void test_method_call() {
185 addFile('''
186 int s(int x) {
187 return x + 1;
188 }
189
190 void main() {
191 s(10);
192 s(/*error:INVALID_ASSIGNMENT*/null);
193 }
194 ''');
195 check(nonnullableTypes: <String>['dart:core,int']);
196 }
197
198 void test_generics() {
199 addFile('''
200 class Foo<T> {
201 T x;
202
203 Foo(this.x);
204 }
205
206 void main() {
207 var f = new Foo<String>("hello");
208 var g = new Foo<int>(10);
209 var h = new Foo<String>(null);
210 var i = new Foo<int>(/*error:INVALID_ASSIGNMENT*/null);
211
212 print(f.x);
213 print(g.x);
214 print(h.x);
215 print(i.x);
216 }
217 ''');
218 addFile('''
219 class Foo<T> {
220 T x; // Should be annotated for a runtime check: x = (null as T)
221
222 Foo();
223 }
224
225 void main() {
226 var f = new Foo<String>();
227 var g = new Foo<int>(); // Should fail at runtime.
228 }
229 ''');
230 check(nonnullableTypes: <String>['dart:core,int']);
231 }
232
233 void test_map() {
234 addFile('''
235 class Pair<K, V> {
236 K first;
237 V second;
238
239 Pair(this.first, this.second);
240 }
241
242 class SlowMap<K, V> {
243 List<Pair<K, V>> array;
244 int arrayLength = 0;
245
246 SlowMap() : array = <Pair<K, V>>[];
247
248 void insert(K key, V value) {
249 array.add(new Pair<K, V>(key, value));
250 ++arrayLength;
251 }
252
253 bool has(K key) {
254 for (int i = 0; i < arrayLength; ++i) {
255 if (array[i].first == key) {
256 return true;
257 }
258 }
259 return false;
260 }
261
262 V get(K key) {
263 for (int i = 0; i < arrayLength; ++i) {
264 if (array[i].first == key) {
265 return array[i].second;
266 }
267 }
268 return null;
269 // TODO(stanm): generate explicit cast to V which will produce a runtime
270 // error if V is non-nullable. Optionally, generate a static warning too.
271 }
272 }
273
274 void main() {
275 var legs = new SlowMap<String, int>();
276 legs.insert("spider", 8);
277 legs.insert("goat", 4);
278 legs.insert("chicken", 2);
279
280 int x = legs.get("goat"); // This should not produce an error.
281 int y = legs.get("sheep"); // TODO(stanm): Runtime error here.
282 }
283 ''');
284 check(nonnullableTypes: <String>['dart:core,int']);
285 }
183 } 286 }
184 287
185 String _withError(String file, String error) { 288 String _withError(String file, String error) {
186 return ("" + file).replaceFirst("boom", error); 289 return ("" + file).replaceFirst("boom", error);
187 } 290 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698