OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'package:analyzer/dart/ast/ast.dart'; |
| 6 import 'package:analyzer/file_system/file_system.dart'; |
| 7 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/sdk.dart'; |
| 10 import 'package:analyzer/src/generated/engine.dart'; |
| 11 import 'package:analyzer/src/generated/sdk.dart'; |
| 12 import 'package:analyzer/src/generated/source.dart'; |
| 13 import 'package:analyzer/src/util/fast_uri.dart'; |
| 14 import 'package:test/test.dart'; |
| 15 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 16 |
| 17 main() { |
| 18 defineReflectiveSuite(() { |
| 19 defineReflectiveTests(SdkPatcherTest); |
| 20 }); |
| 21 } |
| 22 |
| 23 @reflectiveTest |
| 24 class SdkPatcherTest { |
| 25 MemoryResourceProvider provider = new MemoryResourceProvider(); |
| 26 Folder sdkFolder; |
| 27 FolderBasedDartSdk sdk; |
| 28 |
| 29 SdkPatcher patcher = new SdkPatcher(); |
| 30 RecordingErrorListener listener = new RecordingErrorListener(); |
| 31 |
| 32 void setUp() { |
| 33 sdkFolder = provider.getFolder(_p('/sdk')); |
| 34 } |
| 35 |
| 36 test_fail_noSuchLibrary() { |
| 37 expect(() { |
| 38 _setSdkLibraries('const LIBRARIES = const {};'); |
| 39 _createSdk(); |
| 40 File file = provider.newFile(_p('/sdk/lib/test/test.dart'), ''); |
| 41 Source source = file.createSource(FastUri.parse('dart:test')); |
| 42 CompilationUnit unit = SdkPatcher.parse(source, true, listener); |
| 43 patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit); |
| 44 }, throwsArgumentError); |
| 45 } |
| 46 |
| 47 test_fail_patchFileDoesNotExist() { |
| 48 expect(() { |
| 49 _setSdkLibraries(r''' |
| 50 final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> { |
| 51 'test' : const LibraryInfo( |
| 52 'test/test.dart', |
| 53 patches: {VM_PLATFORM: ['does_not_exists.dart']}), |
| 54 };'''); |
| 55 _createSdk(); |
| 56 File file = provider.newFile(_p('/sdk/lib/test/test.dart'), ''); |
| 57 Source source = file.createSource(FastUri.parse('dart:test')); |
| 58 CompilationUnit unit = SdkPatcher.parse(source, true, listener); |
| 59 patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit); |
| 60 }, throwsArgumentError); |
| 61 } |
| 62 |
| 63 test_topLevel_append() { |
| 64 CompilationUnit unit = _doTopLevelPatching( |
| 65 r''' |
| 66 int bar() => 2; |
| 67 ''', |
| 68 r''' |
| 69 int _foo1() => 1; |
| 70 int get _foo2 => 1; |
| 71 void set _foo3(int val) {} |
| 72 '''); |
| 73 _assertUnitCode( |
| 74 unit, |
| 75 'int bar() => 2; int _foo1() => 1; ' |
| 76 'int get _foo2 => 1; void set _foo3(int val) {}'); |
| 77 } |
| 78 |
| 79 test_topLevel_fail_append_notPrivate() { |
| 80 expect(() { |
| 81 _doTopLevelPatching( |
| 82 r''' |
| 83 int foo() => 1; |
| 84 ''', |
| 85 r''' |
| 86 int bar() => 2; |
| 87 '''); |
| 88 }, throwsArgumentError); |
| 89 } |
| 90 |
| 91 test_topLevel_fail_noExternalKeyword() { |
| 92 expect(() { |
| 93 _doTopLevelPatching( |
| 94 r''' |
| 95 int foo(); |
| 96 ''', |
| 97 r''' |
| 98 @patch |
| 99 int foo() => 1; |
| 100 '''); |
| 101 }, throwsArgumentError); |
| 102 } |
| 103 |
| 104 test_topLevel_patch_function() { |
| 105 CompilationUnit unit = _doTopLevelPatching( |
| 106 r''' |
| 107 external int foo(); |
| 108 int bar() => 2; |
| 109 ''', |
| 110 r''' |
| 111 @patch |
| 112 int foo() => 1; |
| 113 '''); |
| 114 _assertUnitCode(unit, 'int foo() => 1; int bar() => 2;'); |
| 115 } |
| 116 |
| 117 test_topLevel_patch_getter() { |
| 118 CompilationUnit unit = _doTopLevelPatching( |
| 119 r''' |
| 120 external int get foo; |
| 121 int bar() => 2; |
| 122 ''', |
| 123 r''' |
| 124 @patch |
| 125 int get foo => 1; |
| 126 '''); |
| 127 _assertUnitCode(unit, 'int get foo => 1; int bar() => 2;'); |
| 128 } |
| 129 |
| 130 test_topLevel_patch_setter() { |
| 131 CompilationUnit unit = _doTopLevelPatching( |
| 132 r''' |
| 133 external void set foo(int val); |
| 134 int bar() => 2; |
| 135 ''', |
| 136 r''' |
| 137 @patch |
| 138 void set foo(int val) {} |
| 139 '''); |
| 140 _assertUnitCode(unit, 'void set foo(int val) {} int bar() => 2;'); |
| 141 } |
| 142 |
| 143 void _assertUnitCode(CompilationUnit unit, String expectedCode) { |
| 144 expect(unit.toSource(), expectedCode); |
| 145 } |
| 146 |
| 147 void _createSdk() { |
| 148 sdk = new FolderBasedDartSdk(provider, sdkFolder); |
| 149 sdk.analysisOptions = new AnalysisOptionsImpl()..strongMode = true; |
| 150 } |
| 151 |
| 152 CompilationUnit _doTopLevelPatching(String baseCode, String patchCode) { |
| 153 _setSdkLibraries(r''' |
| 154 final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> { |
| 155 'test' : const LibraryInfo( |
| 156 'test/test.dart', |
| 157 patches: {VM_PLATFORM: ['test/test_patch.dart']}), |
| 158 };'''); |
| 159 File file = provider.newFile(_p('/sdk/lib/test/test.dart'), baseCode); |
| 160 provider.newFile(_p('/sdk/lib/test/test_patch.dart'), patchCode); |
| 161 |
| 162 _createSdk(); |
| 163 |
| 164 Source source = file.createSource(FastUri.parse('dart:test')); |
| 165 CompilationUnit unit = SdkPatcher.parse(source, true, listener); |
| 166 patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit); |
| 167 return unit; |
| 168 } |
| 169 |
| 170 String _p(String path) => provider.convertPath(path); |
| 171 |
| 172 void _setSdkLibraries(String code) { |
| 173 provider.newFile( |
| 174 _p('/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart'), code); |
| 175 } |
| 176 } |
OLD | NEW |