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

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

Issue 2411883003: Add SdkPatcher and implement top-level function / getter / setter patching. (Closed)
Patch Set: tweaks 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/sdk.dart ('k') | pkg/analyzer/test/src/dart/sdk/test_all.dart » ('j') | 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
new file mode 100644
index 0000000000000000000000000000000000000000..25fc34a5cd1fa24417467d90e9244c01b5e368a9
--- /dev/null
+++ b/pkg/analyzer/test/src/dart/sdk/patch_test.dart
@@ -0,0 +1,176 @@
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'package:analyzer/dart/ast/ast.dart';
+import 'package:analyzer/file_system/file_system.dart';
+import 'package:analyzer/file_system/memory_file_system.dart';
+import 'package:analyzer/src/dart/sdk/patch.dart';
+import 'package:analyzer/src/dart/sdk/sdk.dart';
+import 'package:analyzer/src/generated/engine.dart';
+import 'package:analyzer/src/generated/sdk.dart';
+import 'package:analyzer/src/generated/source.dart';
+import 'package:analyzer/src/util/fast_uri.dart';
+import 'package:test/test.dart';
+import 'package:test_reflective_loader/test_reflective_loader.dart';
+
+main() {
+ defineReflectiveSuite(() {
+ defineReflectiveTests(SdkPatcherTest);
+ });
+}
+
+@reflectiveTest
+class SdkPatcherTest {
+ MemoryResourceProvider provider = new MemoryResourceProvider();
+ Folder sdkFolder;
+ FolderBasedDartSdk sdk;
+
+ SdkPatcher patcher = new SdkPatcher();
+ RecordingErrorListener listener = new RecordingErrorListener();
+
+ void setUp() {
+ sdkFolder = provider.getFolder(_p('/sdk'));
+ }
+
+ test_fail_noSuchLibrary() {
+ expect(() {
+ _setSdkLibraries('const LIBRARIES = const {};');
+ _createSdk();
+ File file = provider.newFile(_p('/sdk/lib/test/test.dart'), '');
+ Source source = file.createSource(FastUri.parse('dart:test'));
+ CompilationUnit unit = SdkPatcher.parse(source, true, listener);
+ patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit);
+ }, throwsArgumentError);
+ }
+
+ test_fail_patchFileDoesNotExist() {
+ expect(() {
+ _setSdkLibraries(r'''
+final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> {
+ 'test' : const LibraryInfo(
+ 'test/test.dart',
+ patches: {VM_PLATFORM: ['does_not_exists.dart']}),
+};''');
+ _createSdk();
+ File file = provider.newFile(_p('/sdk/lib/test/test.dart'), '');
+ Source source = file.createSource(FastUri.parse('dart:test'));
+ CompilationUnit unit = SdkPatcher.parse(source, true, listener);
+ patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit);
+ }, throwsArgumentError);
+ }
+
+ test_topLevel_append() {
+ CompilationUnit unit = _doTopLevelPatching(
+ r'''
+int bar() => 2;
+''',
+ r'''
+int _foo1() => 1;
+int get _foo2 => 1;
+void set _foo3(int val) {}
+''');
+ _assertUnitCode(
+ unit,
+ 'int bar() => 2; int _foo1() => 1; '
+ 'int get _foo2 => 1; void set _foo3(int val) {}');
+ }
+
+ test_topLevel_fail_append_notPrivate() {
+ expect(() {
+ _doTopLevelPatching(
+ r'''
+int foo() => 1;
+''',
+ r'''
+int bar() => 2;
+''');
+ }, throwsArgumentError);
+ }
+
+ test_topLevel_fail_noExternalKeyword() {
+ expect(() {
+ _doTopLevelPatching(
+ r'''
+int foo();
+''',
+ r'''
+@patch
+int foo() => 1;
+''');
+ }, throwsArgumentError);
+ }
+
+ test_topLevel_patch_function() {
+ CompilationUnit unit = _doTopLevelPatching(
+ r'''
+external int foo();
+int bar() => 2;
+''',
+ r'''
+@patch
+int foo() => 1;
+''');
+ _assertUnitCode(unit, 'int foo() => 1; int bar() => 2;');
+ }
+
+ test_topLevel_patch_getter() {
+ CompilationUnit unit = _doTopLevelPatching(
+ r'''
+external int get foo;
+int bar() => 2;
+''',
+ r'''
+@patch
+int get foo => 1;
+''');
+ _assertUnitCode(unit, 'int get foo => 1; int bar() => 2;');
+ }
+
+ test_topLevel_patch_setter() {
+ CompilationUnit unit = _doTopLevelPatching(
+ r'''
+external void set foo(int val);
+int bar() => 2;
+''',
+ r'''
+@patch
+void set foo(int val) {}
+''');
+ _assertUnitCode(unit, 'void set foo(int val) {} int bar() => 2;');
+ }
+
+ void _assertUnitCode(CompilationUnit unit, String expectedCode) {
+ expect(unit.toSource(), expectedCode);
+ }
+
+ void _createSdk() {
+ sdk = new FolderBasedDartSdk(provider, sdkFolder);
+ sdk.analysisOptions = new AnalysisOptionsImpl()..strongMode = true;
+ }
+
+ CompilationUnit _doTopLevelPatching(String baseCode, String patchCode) {
+ _setSdkLibraries(r'''
+final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> {
+ 'test' : const LibraryInfo(
+ 'test/test.dart',
+ patches: {VM_PLATFORM: ['test/test_patch.dart']}),
+};''');
+ File file = provider.newFile(_p('/sdk/lib/test/test.dart'), baseCode);
+ provider.newFile(_p('/sdk/lib/test/test_patch.dart'), patchCode);
+
+ _createSdk();
+
+ Source source = file.createSource(FastUri.parse('dart:test'));
+ CompilationUnit unit = SdkPatcher.parse(source, true, listener);
+ patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit);
+ return unit;
+ }
+
+ String _p(String path) => provider.convertPath(path);
+
+ void _setSdkLibraries(String code) {
+ provider.newFile(
+ _p('/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart'), code);
+ }
+}
« no previous file with comments | « pkg/analyzer/lib/src/dart/sdk/sdk.dart ('k') | pkg/analyzer/test/src/dart/sdk/test_all.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698