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

Unified Diff: pkg/analyzer/test/src/dart/sdk/patch_test.dart

Issue 2417933002: Add support for patching constructors. (Closed)
Patch Set: Created 4 years, 2 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/dart/sdk/patch.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/dart/sdk/patch_test.dart
diff --git a/pkg/analyzer/test/src/dart/sdk/patch_test.dart b/pkg/analyzer/test/src/dart/sdk/patch_test.dart
index ad7127c76ebc6e63f538a13aee219fd6e602e62b..840d4c5280c7eac3f27d3c210147dc4c4043d597 100644
--- a/pkg/analyzer/test/src/dart/sdk/patch_test.dart
+++ b/pkg/analyzer/test/src/dart/sdk/patch_test.dart
@@ -34,6 +34,198 @@ class SdkPatcherTest {
sdkFolder = provider.getFolder(_p('/sdk'));
}
+ test_class_constructor_append_fail_notPrivate_named() {
+ expect(() {
+ _doTopLevelPatching(
+ r'''
+class C {}
+''',
+ r'''
+@patch
+class C {
+ C.named() {}
+}
+''');
+ }, throwsArgumentError);
+ }
+
+ test_class_constructor_append_fail_notPrivate_unnamed() {
+ expect(() {
+ _doTopLevelPatching(
+ r'''
+class C {}
+''',
+ r'''
+@patch
+class C {
+ C() {}
+}
+''');
+ }, throwsArgumentError);
+ }
+
+ test_class_constructor_append_named() {
+ CompilationUnit unit = _doTopLevelPatching(
+ r'''
+class C {
+}
+''',
+ r'''
+@patch
+class C {
+ C._named() {}
+}
+''');
+ _assertUnitCode(unit, 'class C {C._named() {}}');
+ ClassDeclaration clazz = unit.declarations[0];
+ ConstructorDeclaration constructor = clazz.members[0];
+ _assertPrevNextToken(clazz.leftBracket, constructor.beginToken);
+ _assertPrevNextToken(constructor.endToken, clazz.rightBracket);
+ }
+
+ test_class_constructor_append_unnamed() {
+ CompilationUnit unit = _doTopLevelPatching(
+ r'''
+class _C {
+}
+''',
+ r'''
+@patch
+class _C {
+ _C() {}
+}
+''');
+ _assertUnitCode(unit, 'class _C {_C() {}}');
+ ClassDeclaration clazz = unit.declarations[0];
+ ConstructorDeclaration constructor = clazz.members[0];
+ _assertPrevNextToken(clazz.leftBracket, constructor.beginToken);
+ _assertPrevNextToken(constructor.endToken, clazz.rightBracket);
+ }
+
+ test_class_constructor_patch() {
+ CompilationUnit unit = _doTopLevelPatching(
+ r'''
+class C {
+ external C.named();
+}
+''',
+ r'''
+@patch
+class C {
+ @patch
+ C.named() {
+ print(42);
+ }
+}
+''');
+ _assertUnitCode(unit, 'class C {C.named() {print(42);}}');
+ ClassDeclaration clazz = unit.declarations[0];
+ ConstructorDeclaration constructor = clazz.members[0];
+ expect(constructor.externalKeyword, isNull);
+ _assertPrevNextToken(
+ constructor.parameters.endToken, constructor.body.beginToken);
+ _assertPrevNextToken(constructor.endToken, clazz.rightBracket);
+ }
+
+ test_class_constructor_patch_fail_baseFactory_patchGenerative() {
+ expect(() {
+ _doTopLevelPatching(
+ r'''
+class C {
+ external factory C.named();
+}
+''',
+ r'''
+@patch
+class C {
+ @patch
+ C.named() {}
+}
+''');
+ }, throwsArgumentError);
+ }
+
+ test_class_constructor_patch_fail_baseGenerative_patchFactory() {
+ expect(() {
+ _doTopLevelPatching(
+ r'''
+class C {
+ external C.named();
+}
+''',
+ r'''
+@patch
+class C {
+ @patch
+ factory C.named() {}
+}
+''');
+ }, throwsArgumentError);
+ }
+
+ test_class_constructor_patch_fail_hasInitializers() {
+ expect(() {
+ _doTopLevelPatching(
+ r'''
+class C {
+ int f;
+ external C.named() : f = 1;
+}
+''',
+ r'''
+@patch
+class C {
+ @patch
+ C.named() : f = 2 {}
+}
+''');
+ }, throwsArgumentError);
+ }
+
+ test_class_constructor_patch_fail_noExternalKeyword() {
+ expect(() {
+ _doTopLevelPatching(
+ r'''
+class C {
+ C.named();
+}
+''',
+ r'''
+@patch
+class C {
+ @patch
+ C.named() {}
+}
+''');
+ }, throwsArgumentError);
+ }
+
+ test_class_constructor_patch_initializers() {
+ CompilationUnit unit = _doTopLevelPatching(
+ r'''
+class C {
+ int f;
+ external C.named();
+}
+''',
+ r'''
+@patch
+class C {
+ @patch
+ C.named() : f = 2 {
+ print(42);
+ }
+}
+''');
+ _assertUnitCode(unit, 'class C {int f; C.named() : f = 2 {print(42);}}');
+ ClassDeclaration clazz = unit.declarations[0];
+ ConstructorDeclaration constructor = clazz.members[1];
+ expect(constructor.externalKeyword, isNull);
+ _assertPrevNextToken(constructor.parameters.endToken,
+ constructor.initializers.beginToken.previous);
+ _assertPrevNextToken(constructor.endToken, clazz.rightBracket);
+ }
+
test_class_getter_append() {
CompilationUnit unit = _doTopLevelPatching(
r'''
« no previous file with comments | « pkg/analyzer/lib/src/dart/sdk/patch.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698