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

Unified Diff: pkg/analyzer/test/generated/hint_code_test.dart

Issue 1863803002: Validation of `@required` params (#26182). (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 8 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
Index: pkg/analyzer/test/generated/hint_code_test.dart
diff --git a/pkg/analyzer/test/generated/hint_code_test.dart b/pkg/analyzer/test/generated/hint_code_test.dart
index 735fc78b33893a92dfcdb3c1d56dba06d64c04d3..fa3da216fadc4f776d7becfc8124e281c4ad2bae 100644
--- a/pkg/analyzer/test/generated/hint_code_test.dart
+++ b/pkg/analyzer/test/generated/hint_code_test.dart
@@ -107,7 +107,11 @@ const _Literal literal = const _Literal();
const _MustCallSuper mustCallSuper = const _MustCallSuper();
const _Override override = const _Override();
const _Protected protected = const _Protected();
-const _Required required = const _Required();
+const Required required = const Required();
+class Required {
+ final String reason;
+ const Required([this.reason]);
+}
class _Factory {
const _Factory();
@@ -1523,6 +1527,104 @@ class B extends A {
verify([source]);
}
+ void test_required_constructor_param() {
+ Source source = addSource(r'''
+import 'package:meta/meta.dart';
+
+class C {
+ C({@Required('must specify an `a`') int a}) {}
+}
+
+main() {
+ new C();
+}
+''');
+ computeLibrarySourceErrors(source);
+ assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM]);
+ verify([source]);
+ }
+
+ void test_required_constructor_param_no_reason() {
+ Source source = addSource(r'''
+import 'package:meta/meta.dart';
+
+class C {
+ C({@required int a}) {}
+}
+
+main() {
+ new C();
+}
+''');
+ computeLibrarySourceErrors(source);
+ assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM]);
+ verify([source]);
+ }
+
+ void test_required_constructor_param_null_reason() {
+ Source source = addSource(r'''
+import 'package:meta/meta.dart';
+
+class C {
+ C({@Required(null) int a}) {}
+}
+
+main() {
+ new C();
+}
+''');
+ computeLibrarySourceErrors(source);
+ assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM]);
+ verify([source]);
+ }
+
+ void test_required_constructor_param_OK() {
+ Source source = addSource(r'''
+import 'package:meta/meta.dart';
+
+class C {
+ C({@required int a}) {}
+}
+
+main() {
+ new C(a: 2);
+}
+''');
+ computeLibrarySourceErrors(source);
+ assertNoErrors(source);
+ verify([source]);
+ }
+
+ void test_required_function_param() {
+ Source source = addSource(r'''
+import 'package:meta/meta.dart';
+
+void f({@Required('must specify an `a`') int a}) {}
+
+main() {
+ f();
+}
+''');
+ computeLibrarySourceErrors(source);
+ assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM]);
+ verify([source]);
+ }
+
+ void test_required_method_param() {
+ Source source = addSource(r'''
+import 'package:meta/meta.dart';
+class A {
+ void m({@Required('must specify an `a`') int a}) {}
+}
+f() {
+ new A().m();
+}
+''');
+ computeLibrarySourceErrors(source);
+ assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM]);
+ verify([source]);
+ }
+
void test_typeCheck_type_is_Null() {
Source source = addSource(r'''
m(i) {

Powered by Google App Engine
This is Rietveld 408576698