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

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

Issue 2208233002: Make LUB algorithm aware of non-null types (Closed) Base URL: https://github.com/dart-lang/sdk@nnp
Patch Set: Fix logic bug in isNullableType 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/analyzer/lib/src/task/strong/checker.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/test/src/task/strong/non_null_checker_test.dart
diff --git a/pkg/analyzer/test/src/task/strong/non_null_checker_test.dart b/pkg/analyzer/test/src/task/strong/non_null_checker_test.dart
index ac056c2a5433a20a7011ff2bba3aca361027e3f7..9b616afb8c33d1cd7378ba41f453f2f63ae1cbc1 100644
--- a/pkg/analyzer/test/src/task/strong/non_null_checker_test.dart
+++ b/pkg/analyzer/test/src/task/strong/non_null_checker_test.dart
@@ -48,11 +48,23 @@ main() {
checkFile('int x = null;');
}
- void test_nonnullableTypes() {
+ void test_uninitialized_nonnullable() {
// If `int`s are non-nullable, then this code should throw an error.
addFile('int x;');
+ check(nonnullableTypes: <String>['dart:core,int']);
+ }
+
+ void test_initialize_nonnullable_with_null() {
addFile('int x = /*error:INVALID_ASSIGNMENT*/null;');
+ check(nonnullableTypes: <String>['dart:core,int']);
+ }
+
+ void test_initialize_nonnullable_with_valid_value() {
addFile('int x = 0;');
+ check(nonnullableTypes: <String>['dart:core,int']);
+ }
+
+ void test_assign_null_to_nonnullable() {
addFile('''
int x = 0;
@@ -63,4 +75,75 @@ main() {
''');
check(nonnullableTypes: <String>['dart:core,int']);
}
+
+ // Default example from NNBD document.
+ final String defaultNnbdExample = '''
+class Point {
+ final int x, y;
+ Point(this.x, this.y);
+ Point operator +(Point other) => new Point(x + other.x, y + other.y);
+ String toString() => "x: \$x, y: \$y";
+}
+
+void main() {
+ Point p1 = new Point(0, 0);
+ Point p2 = new Point(10, 10);
+ print("p1 + p2 = \${p1 + p2}");
+}
+''';
+
+ final String defaultNnbdExampleMod1 = '''
+class Point {
+ final int x, y;
+ Point(this.x, this.y);
+ Point operator +(Point other) => new Point(x + other.x, y + other.y);
+ String toString() => "x: \$x, y: \$y";
+}
+
+void main() {
+ Point p1 = new Point(0, 0);
+ Point p2 = new Point(10, /*boom*/null); // Change here.
+ print("p1 + p2 = \${p1 + p2}");
+}
+''';
+
+ final String defaultNnbdExampleMod2 = '''
+class Point {
+ final int x, y;
+ Point(this.x, this.y);
+ Point operator +(Point other) => new Point(x + other.x, y + other.y);
+ String toString() => "x: \$x, y: \$y";
+}
+
+void main() {
+ bool f = false; // Necessary, because dead code is otherwise detected.
+ Point p1 = new Point(0, 0);
+ Point p2 = new Point(10, /*boom*/f ? 10 : null); // Change here.
+ print("p1 + p2 = \${p1 + p2}");
+}
+''';
+
+ void test_nullable_fields() {
+ addFile(defaultNnbdExample);
+ // `null` can be passed as an argument to `Point` in default mode.
+ addFile(defaultNnbdExampleMod1);
+ // A nullable expression can be passed as an argument to `Point` in default
+ // mode.
+ addFile(defaultNnbdExampleMod2);
+ check();
+ }
+
+ void test_nonnullable_fields() {
+ addFile(defaultNnbdExample);
+ // `null` can be passed as an argument to `Point` in default mode.
+ addFile(_withError(defaultNnbdExampleMod1, "error:INVALID_ASSIGNMENT"));
+ // A nullable expression can be passed as an argument to `Point` in default
+ // mode.
+ addFile(_withError(defaultNnbdExampleMod2, "error:INVALID_ASSIGNMENT"));
+ check(nonnullableTypes: <String>['dart:core,int']);
+ }
+}
+
+String _withError(String file, String error) {
+ return ("" + file).replaceFirst("boom", error);
}
« no previous file with comments | « pkg/analyzer/lib/src/task/strong/checker.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698