OLD | NEW |
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/file_system/file_system.dart'; | 7 import 'package:analyzer/file_system/file_system.dart'; |
7 import 'package:analyzer/file_system/memory_file_system.dart'; | 8 import 'package:analyzer/file_system/memory_file_system.dart'; |
8 import 'package:analyzer/src/dart/sdk/patch.dart'; | 9 import 'package:analyzer/src/dart/sdk/patch.dart'; |
9 import 'package:analyzer/src/dart/sdk/sdk.dart'; | 10 import 'package:analyzer/src/dart/sdk/sdk.dart'; |
10 import 'package:analyzer/src/generated/engine.dart'; | 11 import 'package:analyzer/src/generated/engine.dart'; |
11 import 'package:analyzer/src/generated/sdk.dart'; | 12 import 'package:analyzer/src/generated/sdk.dart'; |
12 import 'package:analyzer/src/generated/source.dart'; | 13 import 'package:analyzer/src/generated/source.dart'; |
13 import 'package:analyzer/src/util/fast_uri.dart'; | 14 import 'package:analyzer/src/util/fast_uri.dart'; |
14 import 'package:test/test.dart'; | 15 import 'package:test/test.dart'; |
15 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 16 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 patches: {VM_PLATFORM: ['does_not_exists.dart']}), | 80 patches: {VM_PLATFORM: ['does_not_exists.dart']}), |
80 };'''); | 81 };'''); |
81 _createSdk(); | 82 _createSdk(); |
82 File file = provider.newFile(_p('/sdk/lib/test/test.dart'), ''); | 83 File file = provider.newFile(_p('/sdk/lib/test/test.dart'), ''); |
83 Source source = file.createSource(FastUri.parse('dart:test')); | 84 Source source = file.createSource(FastUri.parse('dart:test')); |
84 CompilationUnit unit = SdkPatcher.parse(source, true, listener); | 85 CompilationUnit unit = SdkPatcher.parse(source, true, listener); |
85 patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit); | 86 patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit); |
86 }, throwsArgumentError); | 87 }, throwsArgumentError); |
87 } | 88 } |
88 | 89 |
89 test_topLevel_append() { | |
90 CompilationUnit unit = _doTopLevelPatching( | |
91 r''' | |
92 int bar() => 2; | |
93 ''', | |
94 r''' | |
95 int _foo1() => 1; | |
96 int get _foo2 => 1; | |
97 void set _foo3(int val) {} | |
98 '''); | |
99 _assertUnitCode( | |
100 unit, | |
101 'int bar() => 2; int _foo1() => 1; ' | |
102 'int get _foo2 => 1; void set _foo3(int val) {}'); | |
103 } | |
104 | |
105 test_topLevel_fail_topLevelVariable() { | 90 test_topLevel_fail_topLevelVariable() { |
106 expect(() { | 91 expect(() { |
107 _doTopLevelPatching( | 92 _doTopLevelPatching( |
108 r''' | 93 r''' |
109 int foo() => 0; | 94 int foo() => 0; |
110 ''', | 95 ''', |
111 r''' | 96 r''' |
112 int _bar; | 97 int _bar; |
113 '''); | 98 '''); |
114 }, throwsArgumentError); | 99 }, throwsArgumentError); |
115 } | 100 } |
116 | 101 |
| 102 test_topLevel_function_append() { |
| 103 CompilationUnit unit = _doTopLevelPatching( |
| 104 r''' |
| 105 int foo() => 0; |
| 106 ''', |
| 107 r''' |
| 108 int _bar1() => 1; |
| 109 int _bar2() => 2; |
| 110 '''); |
| 111 _assertUnitCode( |
| 112 unit, 'int foo() => 0; int _bar1() => 1; int _bar2() => 2;'); |
| 113 |
| 114 FunctionDeclaration foo = unit.declarations[0]; |
| 115 FunctionDeclaration bar1 = unit.declarations[1]; |
| 116 FunctionDeclaration bar2 = unit.declarations[2]; |
| 117 |
| 118 _assertPrevNextToken(foo.endToken, bar1.beginToken); |
| 119 _assertPrevNextToken(bar1.endToken, bar2.beginToken); |
| 120 } |
| 121 |
117 test_topLevel_function_fail_noExternalKeyword() { | 122 test_topLevel_function_fail_noExternalKeyword() { |
118 expect(() { | 123 expect(() { |
119 _doTopLevelPatching( | 124 _doTopLevelPatching( |
120 r''' | 125 r''' |
121 int foo(); | 126 int foo(); |
122 ''', | 127 ''', |
123 r''' | 128 r''' |
124 @patch | 129 @patch |
125 int foo() => 1; | 130 int foo() => 1; |
126 '''); | 131 '''); |
(...skipping 11 matching lines...) Expand all Loading... |
138 '''); | 143 '''); |
139 }, throwsArgumentError); | 144 }, throwsArgumentError); |
140 } | 145 } |
141 | 146 |
142 test_topLevel_functionTypeAlias_append() { | 147 test_topLevel_functionTypeAlias_append() { |
143 CompilationUnit unit = _doTopLevelPatching( | 148 CompilationUnit unit = _doTopLevelPatching( |
144 r''' | 149 r''' |
145 int foo() => 0; | 150 int foo() => 0; |
146 ''', | 151 ''', |
147 r''' | 152 r''' |
148 typedef int _bar(); | 153 typedef int _bar1(); |
| 154 typedef int _bar2(); |
149 '''); | 155 '''); |
150 _assertUnitCode(unit, 'int foo() => 0; typedef int _bar();'); | 156 _assertUnitCode( |
| 157 unit, 'int foo() => 0; typedef int _bar1(); typedef int _bar2();'); |
| 158 |
| 159 FunctionDeclaration foo = unit.declarations[0]; |
| 160 FunctionTypeAlias bar1 = unit.declarations[1]; |
| 161 FunctionTypeAlias bar2 = unit.declarations[2]; |
| 162 |
| 163 _assertPrevNextToken(foo.endToken, bar1.beginToken); |
| 164 _assertPrevNextToken(bar1.endToken, bar2.beginToken); |
| 165 expect(unit.endToken.type, TokenType.EOF); |
| 166 expect(bar2.endToken.next, same(unit.endToken)); |
151 } | 167 } |
152 | 168 |
153 test_topLevel_functionTypeAlias_fail_hasAnnotation() { | 169 test_topLevel_functionTypeAlias_fail_hasAnnotation() { |
154 expect(() { | 170 expect(() { |
155 _doTopLevelPatching( | 171 _doTopLevelPatching( |
156 r''' | 172 r''' |
157 int foo() => 0; | 173 int foo() => 0; |
158 ''', | 174 ''', |
159 r''' | 175 r''' |
160 @patch | 176 @patch |
(...skipping 18 matching lines...) Expand all Loading... |
179 CompilationUnit unit = _doTopLevelPatching( | 195 CompilationUnit unit = _doTopLevelPatching( |
180 r''' | 196 r''' |
181 external int foo(); | 197 external int foo(); |
182 int bar() => 2; | 198 int bar() => 2; |
183 ''', | 199 ''', |
184 r''' | 200 r''' |
185 @patch | 201 @patch |
186 int foo() => 1; | 202 int foo() => 1; |
187 '''); | 203 '''); |
188 _assertUnitCode(unit, 'int foo() => 1; int bar() => 2;'); | 204 _assertUnitCode(unit, 'int foo() => 1; int bar() => 2;'); |
| 205 |
| 206 // Prepare functions. |
| 207 FunctionDeclaration foo = unit.declarations[0]; |
| 208 FunctionDeclaration bar = unit.declarations[1]; |
| 209 |
| 210 // The "external" token is removed from the stream. |
| 211 { |
| 212 expect(foo.externalKeyword, isNull); |
| 213 Token token = foo.beginToken; |
| 214 expect(token.lexeme, 'int'); |
| 215 expect(token.previous.type, TokenType.EOF); |
| 216 } |
| 217 |
| 218 // The body tokens are included into the patched token stream. |
| 219 { |
| 220 FunctionExpression fooExpr = foo.functionExpression; |
| 221 FunctionBody fooBody = fooExpr.body; |
| 222 expect(fooBody.beginToken.previous, same(fooExpr.parameters.endToken)); |
| 223 expect(fooBody.endToken.next, same(bar.beginToken)); |
| 224 } |
189 } | 225 } |
190 | 226 |
191 test_topLevel_patch_function_blockBody() { | 227 test_topLevel_patch_function_blockBody() { |
192 CompilationUnit unit = _doTopLevelPatching( | 228 CompilationUnit unit = _doTopLevelPatching( |
193 r''' | 229 r''' |
194 external int foo(); | 230 external int foo(); |
195 ''', | 231 ''', |
196 r''' | 232 r''' |
197 @patch | 233 @patch |
198 int foo() {int v = 1; return v + 2;} | 234 int foo() {int v = 1; return v + 2;} |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit); | 288 patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit); |
253 return unit; | 289 return unit; |
254 } | 290 } |
255 | 291 |
256 String _p(String path) => provider.convertPath(path); | 292 String _p(String path) => provider.convertPath(path); |
257 | 293 |
258 void _setSdkLibraries(String code) { | 294 void _setSdkLibraries(String code) { |
259 provider.newFile( | 295 provider.newFile( |
260 _p('/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart'), code); | 296 _p('/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart'), code); |
261 } | 297 } |
| 298 |
| 299 static void _assertPrevNextToken(Token prev, Token next) { |
| 300 expect(prev.next, same(next)); |
| 301 expect(next.previous, same(prev)); |
| 302 } |
262 } | 303 } |
OLD | NEW |