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_nonnullableTypes() { | 51 void test_uninitialized_nonnullable() { |
52 // If `int`s are non-nullable, then this code should throw an error. | 52 // If `int`s are non-nullable, then this code should throw an error. |
53 addFile('int x;'); | 53 addFile('int x;'); |
| 54 check(nonnullableTypes: <String>['dart:core,int']); |
| 55 } |
| 56 |
| 57 void test_initialize_nonnullable_with_null() { |
54 addFile('int x = /*error:INVALID_ASSIGNMENT*/null;'); | 58 addFile('int x = /*error:INVALID_ASSIGNMENT*/null;'); |
| 59 check(nonnullableTypes: <String>['dart:core,int']); |
| 60 } |
| 61 |
| 62 void test_initialize_nonnullable_with_valid_value() { |
55 addFile('int x = 0;'); | 63 addFile('int x = 0;'); |
| 64 check(nonnullableTypes: <String>['dart:core,int']); |
| 65 } |
| 66 |
| 67 void test_assign_null_to_nonnullable() { |
56 addFile(''' | 68 addFile(''' |
57 int x = 0; | 69 int x = 0; |
58 | 70 |
59 main() { | 71 main() { |
60 x = 1; | 72 x = 1; |
61 x = /*error:INVALID_ASSIGNMENT*/null; | 73 x = /*error:INVALID_ASSIGNMENT*/null; |
62 } | 74 } |
63 '''); | 75 '''); |
64 check(nonnullableTypes: <String>['dart:core,int']); | 76 check(nonnullableTypes: <String>['dart:core,int']); |
65 } | 77 } |
| 78 |
| 79 // Default example from NNBD document. |
| 80 final String defaultNnbdExample = ''' |
| 81 class Point { |
| 82 final int x, y; |
| 83 Point(this.x, this.y); |
| 84 Point operator +(Point other) => new Point(x + other.x, y + other.y); |
| 85 String toString() => "x: \$x, y: \$y"; |
66 } | 86 } |
| 87 |
| 88 void main() { |
| 89 Point p1 = new Point(0, 0); |
| 90 Point p2 = new Point(10, 10); |
| 91 print("p1 + p2 = \${p1 + p2}"); |
| 92 } |
| 93 '''; |
| 94 |
| 95 final String defaultNnbdExampleMod1 = ''' |
| 96 class Point { |
| 97 final int x, y; |
| 98 Point(this.x, this.y); |
| 99 Point operator +(Point other) => new Point(x + other.x, y + other.y); |
| 100 String toString() => "x: \$x, y: \$y"; |
| 101 } |
| 102 |
| 103 void main() { |
| 104 Point p1 = new Point(0, 0); |
| 105 Point p2 = new Point(10, /*boom*/null); // Change here. |
| 106 print("p1 + p2 = \${p1 + p2}"); |
| 107 } |
| 108 '''; |
| 109 |
| 110 final String defaultNnbdExampleMod2 = ''' |
| 111 class Point { |
| 112 final int x, y; |
| 113 Point(this.x, this.y); |
| 114 Point operator +(Point other) => new Point(x + other.x, y + other.y); |
| 115 String toString() => "x: \$x, y: \$y"; |
| 116 } |
| 117 |
| 118 void main() { |
| 119 bool f = false; // Necessary, because dead code is otherwise detected. |
| 120 Point p1 = new Point(0, 0); |
| 121 Point p2 = new Point(10, /*boom*/f ? 10 : null); // Change here. |
| 122 print("p1 + p2 = \${p1 + p2}"); |
| 123 } |
| 124 '''; |
| 125 |
| 126 void test_nullable_fields() { |
| 127 addFile(defaultNnbdExample); |
| 128 // `null` can be passed as an argument to `Point` in default mode. |
| 129 addFile(defaultNnbdExampleMod1); |
| 130 // A nullable expression can be passed as an argument to `Point` in default |
| 131 // mode. |
| 132 addFile(defaultNnbdExampleMod2); |
| 133 check(); |
| 134 } |
| 135 |
| 136 void test_nonnullable_fields() { |
| 137 addFile(defaultNnbdExample); |
| 138 // `null` can be passed as an argument to `Point` in default mode. |
| 139 addFile(_withError(defaultNnbdExampleMod1, "error:INVALID_ASSIGNMENT")); |
| 140 // A nullable expression can be passed as an argument to `Point` in default |
| 141 // mode. |
| 142 addFile(_withError(defaultNnbdExampleMod2, "error:INVALID_ASSIGNMENT")); |
| 143 check(nonnullableTypes: <String>['dart:core,int']); |
| 144 } |
| 145 } |
| 146 |
| 147 String _withError(String file, String error) { |
| 148 return ("" + file).replaceFirst("boom", error); |
| 149 } |
OLD | NEW |