| OLD | NEW |
| 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 30 matching lines...) Expand all Loading... |
| 41 } | 41 } |
| 42 } | 42 } |
| 43 '''); | 43 '''); |
| 44 } | 44 } |
| 45 | 45 |
| 46 void test_nullableTypes() { | 46 void test_nullableTypes() { |
| 47 // By default x can be set to null. | 47 // By default x can be set to null. |
| 48 checkFile('int x = null;'); | 48 checkFile('int x = null;'); |
| 49 } | 49 } |
| 50 | 50 |
| 51 void test_uninitialized_nonnullable() { | |
| 52 // If `int`s are non-nullable, then this code should throw an error. | |
| 53 addFile('int x;'); | |
| 54 check(nonnullableTypes: <String>['dart:core,int']); | |
| 55 } | |
| 56 | |
| 57 void test_initialize_nonnullable_with_null() { | 51 void test_initialize_nonnullable_with_null() { |
| 58 addFile('int x = /*error:INVALID_ASSIGNMENT*/null;'); | 52 addFile('int x = /*error:INVALID_ASSIGNMENT*/null;'); |
| 59 check(nonnullableTypes: <String>['dart:core,int']); | 53 check(nonnullableTypes: <String>['dart:core,int']); |
| 60 } | 54 } |
| 61 | 55 |
| 62 void test_initialize_nonnullable_with_valid_value() { | 56 void test_initialize_nonnullable_with_valid_value() { |
| 63 addFile('int x = 0;'); | 57 addFile('int x = 0;'); |
| 64 check(nonnullableTypes: <String>['dart:core,int']); | 58 check(nonnullableTypes: <String>['dart:core,int']); |
| 65 } | 59 } |
| 66 | 60 |
| 67 void test_assign_null_to_nonnullable() { | 61 void test_assign_null_to_nonnullable() { |
| 68 addFile(''' | 62 addFile(''' |
| 69 int x = 0; | 63 int x = 0; |
| 70 | 64 |
| 71 main() { | 65 main() { |
| 72 x = 1; | 66 x = 1; |
| 73 x = /*error:INVALID_ASSIGNMENT*/null; | 67 x = /*error:INVALID_ASSIGNMENT*/null; |
| 74 } | 68 } |
| 75 '''); | 69 '''); |
| 76 check(nonnullableTypes: <String>['dart:core,int']); | 70 check(nonnullableTypes: <String>['dart:core,int']); |
| 77 } | 71 } |
| 78 | 72 |
| 73 void test_uninitialized_nonnullable_local_variable() { |
| 74 // Ideally, we will do flow analysis and throw an error only if a variable |
| 75 // is used before it has been initialized. |
| 76 addFile('main() { int /*error:NON_NULLABLE_FIELD_NOT_INITIALIZED*/x; }'); |
| 77 check(nonnullableTypes: <String>['dart:core,int']); |
| 78 } |
| 79 |
| 80 void test_uninitialized_nonnullable_top_level_variable_declaration() { |
| 81 // If `int`s are non-nullable, then this code should throw an error. |
| 82 addFile('int /*error:NON_NULLABLE_FIELD_NOT_INITIALIZED*/x;'); |
| 83 check(nonnullableTypes: <String>['dart:core,int']); |
| 84 } |
| 85 |
| 86 void test_uninitialized_nonnullable_field_declaration() { |
| 87 addFile(''' |
| 88 void foo() {} |
| 89 |
| 90 class A { |
| 91 // Ideally, we should allow x to be init in the constructor, but that requires |
| 92 // too much complication in the checker, so for now we throw a static error at |
| 93 // the declaration site. |
| 94 int /*error:NON_NULLABLE_FIELD_NOT_INITIALIZED*/x; |
| 95 |
| 96 A(); |
| 97 } |
| 98 '''); |
| 99 check(nonnullableTypes: <String>['dart:core,int']); |
| 100 } |
| 101 |
| 102 void test_prefer_final_to_non_nullable_error() { |
| 103 addFile('main() { final int /*error:FINAL_NOT_INITIALIZED*/x; }'); |
| 104 addFile('final int /*error:FINAL_NOT_INITIALIZED*/x;'); |
| 105 addFile(''' |
| 106 void foo() {} |
| 107 |
| 108 class A { |
| 109 final int x; |
| 110 |
| 111 /*warning:FINAL_NOT_INITIALIZED_CONSTRUCTOR_1*/A(); |
| 112 } |
| 113 '''); |
| 114 check(nonnullableTypes: <String>['dart:core,int']); |
| 115 } |
| 116 |
| 79 // Default example from NNBD document. | 117 // Default example from NNBD document. |
| 80 final String defaultNnbdExample = ''' | 118 final String defaultNnbdExample = ''' |
| 81 class Point { | 119 class Point { |
| 82 final int x, y; | 120 final int x, y; |
| 83 Point(this.x, this.y); | 121 Point(this.x, this.y); |
| 84 Point operator +(Point other) => new Point(x + other.x, y + other.y); | 122 Point operator +(Point other) => new Point(x + other.x, y + other.y); |
| 85 String toString() => "x: \$x, y: \$y"; | 123 String toString() => "x: \$x, y: \$y"; |
| 86 } | 124 } |
| 87 | 125 |
| 88 void main() { | 126 void main() { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 // 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 |
| 141 // mode. | 179 // mode. |
| 142 addFile(_withError(defaultNnbdExampleMod2, "error:INVALID_ASSIGNMENT")); | 180 addFile(_withError(defaultNnbdExampleMod2, "error:INVALID_ASSIGNMENT")); |
| 143 check(nonnullableTypes: <String>['dart:core,int']); | 181 check(nonnullableTypes: <String>['dart:core,int']); |
| 144 } | 182 } |
| 145 } | 183 } |
| 146 | 184 |
| 147 String _withError(String file, String error) { | 185 String _withError(String file, String error) { |
| 148 return ("" + file).replaceFirst("boom", error); | 186 return ("" + file).replaceFirst("boom", error); |
| 149 } | 187 } |
| OLD | NEW |