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

Side by Side Diff: pkg/analyzer/test/src/dart/sdk/patch_test.dart

Issue 2412453007: Add support for patching class methods. (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 unified diff | Download patch
OLDNEW
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 import 'package:analyzer/dart/ast/ast.dart'; 5 import 'package:analyzer/dart/ast/ast.dart';
6 import 'package:analyzer/dart/ast/token.dart'; 6 import 'package:analyzer/dart/ast/token.dart';
7 import 'package:analyzer/file_system/file_system.dart'; 7 import 'package:analyzer/file_system/file_system.dart';
8 import 'package:analyzer/file_system/memory_file_system.dart'; 8 import 'package:analyzer/file_system/memory_file_system.dart';
9 import 'package:analyzer/src/dart/sdk/patch.dart'; 9 import 'package:analyzer/src/dart/sdk/patch.dart';
10 import 'package:analyzer/src/dart/sdk/sdk.dart'; 10 import 'package:analyzer/src/dart/sdk/sdk.dart';
(...skipping 16 matching lines...) Expand all
27 Folder sdkFolder; 27 Folder sdkFolder;
28 FolderBasedDartSdk sdk; 28 FolderBasedDartSdk sdk;
29 29
30 SdkPatcher patcher = new SdkPatcher(); 30 SdkPatcher patcher = new SdkPatcher();
31 RecordingErrorListener listener = new RecordingErrorListener(); 31 RecordingErrorListener listener = new RecordingErrorListener();
32 32
33 void setUp() { 33 void setUp() {
34 sdkFolder = provider.getFolder(_p('/sdk')); 34 sdkFolder = provider.getFolder(_p('/sdk'));
35 } 35 }
36 36
37 test_class_getter_append() {
38 CompilationUnit unit = _doTopLevelPatching(
39 r'''
40 class C {
41 void a() {}
42 }
43 ''',
44 r'''
45 @patch
46 class C {
47 int get _b => 2;
48 }
49 ''');
50 _assertUnitCode(unit, 'class C {void a() {} int get _b => 2;}');
51 }
52
53 test_class_method_append() {
54 CompilationUnit unit = _doTopLevelPatching(
55 r'''
56 class C {
57 void a() {}
58 }
59 ''',
60 r'''
61 @patch
62 class C {
63 void _b() {}
64 void _c() {}
65 }
66 ''');
67 _assertUnitCode(unit, 'class C {void a() {} void _b() {} void _c() {}}');
68 ClassDeclaration clazz = unit.declarations[0];
69 MethodDeclaration a = clazz.members[0];
70 MethodDeclaration b = clazz.members[1];
71 MethodDeclaration c = clazz.members[2];
72 _assertPrevNextToken(a.endToken, b.beginToken);
73 _assertPrevNextToken(b.endToken, c.beginToken);
74 _assertPrevNextToken(c.endToken, clazz.rightBracket);
75 }
76
77 test_class_method_fail_notPrivate() {
78 expect(() {
79 _doTopLevelPatching(
80 r'''
81 class A {}
82 ''',
83 r'''
84 @patch
85 class A {
86 void m() {}
87 }
88 ''');
89 }, throwsArgumentError);
90 }
91
92 test_class_method_patch() {
93 CompilationUnit unit = _doTopLevelPatching(
94 r'''
95 class C {
96 external int m();
97 }
98 ''',
99 r'''
100 @patch
101 class C {
102 @patch
103 int m() => 42;
104 }
105 ''');
106 _assertUnitCode(unit, 'class C {int m() => 42;}');
107 ClassDeclaration clazz = unit.declarations[0];
108 MethodDeclaration m = clazz.members[0];
109 expect(m.externalKeyword, isNull);
110 _assertPrevNextToken(m.parameters.rightParenthesis, m.body.beginToken);
111 _assertPrevNextToken(m.body.endToken, clazz.rightBracket);
112 }
113
114 test_class_method_patch_fail_noExternalKeyword() {
115 expect(() {
116 _doTopLevelPatching(
117 r'''
118 class C {
119 int m();
120 }
121 ''',
122 r'''
123 @patch
124 class C {
125 @patch
126 int m() => 42;
127 }
128 ''');
129 }, throwsArgumentError);
130 }
131
132 test_class_setter_append() {
133 CompilationUnit unit = _doTopLevelPatching(
134 r'''
135 class C {
136 void a() {}
137 }
138 ''',
139 r'''
140 @patch
141 class C {
142 void set _b(_) {}
143 }
144 ''');
145 _assertUnitCode(unit, 'class C {void a() {} void set _b(_) {}}');
146 }
147
37 test_directive_fail_export() { 148 test_directive_fail_export() {
38 expect(() { 149 expect(() {
39 _doTopLevelPatching( 150 _doTopLevelPatching(
40 r''' 151 r'''
41 import 'a.dart'; 152 import 'a.dart';
42 ''', 153 ''',
43 r''' 154 r'''
44 export 'c.dart'; 155 export 'c.dart';
45 '''); 156 ''');
46 }, throwsArgumentError); 157 }, throwsArgumentError);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 patches: {VM_PLATFORM: ['does_not_exists.dart']}), 191 patches: {VM_PLATFORM: ['does_not_exists.dart']}),
81 };'''); 192 };''');
82 _createSdk(); 193 _createSdk();
83 File file = provider.newFile(_p('/sdk/lib/test/test.dart'), ''); 194 File file = provider.newFile(_p('/sdk/lib/test/test.dart'), '');
84 Source source = file.createSource(FastUri.parse('dart:test')); 195 Source source = file.createSource(FastUri.parse('dart:test'));
85 CompilationUnit unit = SdkPatcher.parse(source, true, listener); 196 CompilationUnit unit = SdkPatcher.parse(source, true, listener);
86 patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit); 197 patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit);
87 }, throwsArgumentError); 198 }, throwsArgumentError);
88 } 199 }
89 200
201 test_topLevel_class_append() {
202 CompilationUnit unit = _doTopLevelPatching(
203 r'''
204 class A {}
205 ''',
206 r'''
207 class _B {
208 void mmm() {}
209 }
210 ''');
211 _assertUnitCode(unit, 'class A {} class _B {void mmm() {}}');
212 ClassDeclaration a = unit.declarations[0];
213 ClassDeclaration b = unit.declarations[1];
214 _assertPrevNextToken(a.endToken, b.beginToken);
215 }
216
217 test_topLevel_class_fail_mixinApplication() {
218 expect(() {
219 _doTopLevelPatching(
220 r'''
221 class A {}
222 ''',
223 r'''
224 class _B {}
225 class _C = Object with _B;
226 ''');
227 }, throwsArgumentError);
228 }
229
230 test_topLevel_class_fail_notPrivate() {
231 expect(() {
232 _doTopLevelPatching(
233 r'''
234 class A {}
235 ''',
236 r'''
237 class B {}
238 ''');
239 }, throwsArgumentError);
240 }
241
90 test_topLevel_fail_topLevelVariable() { 242 test_topLevel_fail_topLevelVariable() {
91 expect(() { 243 expect(() {
92 _doTopLevelPatching( 244 _doTopLevelPatching(
93 r''' 245 r'''
94 int foo() => 0; 246 int foo() => 0;
95 ''', 247 ''',
96 r''' 248 r'''
97 int _bar; 249 int _bar;
98 '''); 250 ''');
99 }, throwsArgumentError); 251 }, throwsArgumentError);
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 void _setSdkLibraries(String code) { 446 void _setSdkLibraries(String code) {
295 provider.newFile( 447 provider.newFile(
296 _p('/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart'), code); 448 _p('/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart'), code);
297 } 449 }
298 450
299 static void _assertPrevNextToken(Token prev, Token next) { 451 static void _assertPrevNextToken(Token prev, Token next) {
300 expect(prev.next, same(next)); 452 expect(prev.next, same(next));
301 expect(next.previous, same(prev)); 453 expect(next.previous, same(prev));
302 } 454 }
303 } 455 }
OLDNEW
« pkg/analyzer/lib/src/dart/sdk/patch.dart ('K') | « 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