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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | 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 e5fa7c662a856384501a5d0512af768d75f9e3b5..2be380ff76fc3038f302800af8f03c5b40c2cdbf 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
@@ -180,6 +180,109 @@ void main() {
addFile(_withError(defaultNnbdExampleMod2, "error:INVALID_ASSIGNMENT"));
check(nonnullableTypes: <String>['dart:core,int']);
}
+
+ void test_method_call() {
+ addFile('''
+int s(int x) {
+ return x + 1;
+}
+
+void main() {
+ s(10);
+ s(/*error:INVALID_ASSIGNMENT*/null);
+}
+''');
+ check(nonnullableTypes: <String>['dart:core,int']);
+ }
+
+ void test_generics() {
+ addFile('''
+class Foo<T> {
+ T x;
+
+ Foo(this.x);
+}
+
+void main() {
+ var f = new Foo<String>("hello");
+ var g = new Foo<int>(10);
+ var h = new Foo<String>(null);
+ var i = new Foo<int>(/*error:INVALID_ASSIGNMENT*/null);
+
+ print(f.x);
+ print(g.x);
+ print(h.x);
+ print(i.x);
+}
+''');
+ addFile('''
+class Foo<T> {
+ T x; // Should be annotated for a runtime check: x = (null as T)
+
+ Foo();
+}
+
+void main() {
+ var f = new Foo<String>();
+ var g = new Foo<int>(); // Should fail at runtime.
+}
+''');
+ check(nonnullableTypes: <String>['dart:core,int']);
+ }
+
+ void test_map() {
+ addFile('''
+class Pair<K, V> {
+ K first;
+ V second;
+
+ Pair(this.first, this.second);
+}
+
+class SlowMap<K, V> {
+ List<Pair<K, V>> array;
+ int arrayLength = 0;
+
+ SlowMap() : array = <Pair<K, V>>[];
+
+ void insert(K key, V value) {
+ array.add(new Pair<K, V>(key, value));
+ ++arrayLength;
+ }
+
+ bool has(K key) {
+ for (int i = 0; i < arrayLength; ++i) {
+ if (array[i].first == key) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ V get(K key) {
+ for (int i = 0; i < arrayLength; ++i) {
+ if (array[i].first == key) {
+ return array[i].second;
+ }
+ }
+ return null;
+ // TODO(stanm): generate explicit cast to V which will produce a runtime
+ // error if V is non-nullable. Optionally, generate a static warning too.
+ }
+}
+
+void main() {
+ var legs = new SlowMap<String, int>();
+ legs.insert("spider", 8);
+ legs.insert("goat", 4);
+ legs.insert("chicken", 2);
+
+ int x = legs.get("goat"); // This should not produce an error.
+ int y = legs.get("sheep"); // TODO(stanm): Runtime error here.
+}
+''');
+ check(nonnullableTypes: <String>['dart:core,int']);
+ }
}
String _withError(String file, String error) {
« 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