| 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/error/listener.dart'; | |
| 6 import 'package:analyzer/src/generated/parser.dart'; | |
| 7 import 'package:analyzer/src/summary/summarize_ast.dart'; | |
| 8 import 'package:front_end/src/base/library_info.dart'; | |
| 9 import 'package:front_end/src/libraries_reader.dart'; | |
| 10 import 'package:front_end/src/scanner/errors.dart'; | |
| 11 import 'package:front_end/src/scanner/reader.dart'; | |
| 12 import 'package:front_end/src/scanner/scanner.dart'; | |
| 13 import 'package:test/test.dart'; | |
| 14 import 'package:test_reflective_loader/test_reflective_loader.dart'; | |
| 15 | |
| 16 main() { | |
| 17 defineReflectiveSuite(() { | |
| 18 defineReflectiveTests(LibrariesReaderTest); | |
| 19 }); | |
| 20 } | |
| 21 | |
| 22 /// Generic URI resolver tests which do not depend on the particular path | |
| 23 /// context in use. | |
| 24 @reflectiveTest | |
| 25 class LibrariesReaderTest { | |
| 26 test_categoriesClient() { | |
| 27 var info = | |
| 28 _computeSingleInfo('const LibraryInfo("", categories: "Client")'); | |
| 29 expect(info.categories, [Category.client]); | |
| 30 expect(info.categoriesString, 'Client'); | |
| 31 } | |
| 32 | |
| 33 test_categoriesDefault() { | |
| 34 var info = _computeSingleInfo('const LibraryInfo("")'); | |
| 35 expect(info.categories, isEmpty); | |
| 36 expect(info.categoriesString, ''); | |
| 37 } | |
| 38 | |
| 39 test_categoriesMultiple() { | |
| 40 var info = _computeSingleInfo( | |
| 41 'const LibraryInfo("", categories: "Client,Server")'); | |
| 42 expect( | |
| 43 info.categories, unorderedEquals([Category.client, Category.server])); | |
| 44 expect(info.categoriesString, 'Client,Server'); | |
| 45 } | |
| 46 | |
| 47 test_categoriesNone() { | |
| 48 var info = _computeSingleInfo('const LibraryInfo("", categories: "")'); | |
| 49 expect(info.categories, isEmpty); | |
| 50 expect(info.categoriesString, ''); | |
| 51 } | |
| 52 | |
| 53 test_categoriesSingle() { | |
| 54 var info = | |
| 55 _computeSingleInfo('const LibraryInfo("", categories: "Client")'); | |
| 56 expect(info.categories, [Category.client]); | |
| 57 expect(info.categoriesString, 'Client'); | |
| 58 } | |
| 59 | |
| 60 test_complex() { | |
| 61 var info = _computeSingleInfo( | |
| 62 ''' | |
| 63 const LibraryInfo( | |
| 64 "async/async.dart", | |
| 65 categories: "Client,Server", | |
| 66 maturity: Maturity.STABLE, | |
| 67 dart2jsPatchPath: "_internal/js_runtime/lib/async_patch.dart")) | |
| 68 ''', | |
| 69 additionalDeclarations: ''' | |
| 70 class Maturity { | |
| 71 final int level; | |
| 72 final String name; | |
| 73 final String description; | |
| 74 | |
| 75 const Maturity(this.level, this.name, this.description); | |
| 76 | |
| 77 static const Maturity STABLE = const Maturity(4, "Stable", "Stable description
"); | |
| 78 } | |
| 79 '''); | |
| 80 expect(info.path, 'async/async.dart'); | |
| 81 expect( | |
| 82 info.categories, unorderedEquals([Category.client, Category.server])); | |
| 83 expect(info.maturity.name, 'Stable'); | |
| 84 expect(info.dart2jsPatchPath, '_internal/js_runtime/lib/async_patch.dart'); | |
| 85 } | |
| 86 | |
| 87 test_dart2jsPatchPathDefault() { | |
| 88 var info = _computeSingleInfo('const LibraryInfo("")'); | |
| 89 expect(info.dart2jsPatchPath, null); | |
| 90 } | |
| 91 | |
| 92 test_dart2jsPatchPathString() { | |
| 93 var info = _computeSingleInfo(''' | |
| 94 const LibraryInfo( | |
| 95 "", | |
| 96 dart2jsPatchPath: "_internal/js_runtime/lib/async_patch.dart") | |
| 97 '''); | |
| 98 expect(info.dart2jsPatchPath, '_internal/js_runtime/lib/async_patch.dart'); | |
| 99 } | |
| 100 | |
| 101 test_dart2jsPathDefault() { | |
| 102 var info = _computeSingleInfo('const LibraryInfo("")'); | |
| 103 expect(info.dart2jsPath, null); | |
| 104 } | |
| 105 | |
| 106 test_dart2jsPathString() { | |
| 107 var info = _computeSingleInfo( | |
| 108 'const LibraryInfo("", dart2jsPath: "html/dart2js/html_dart2js.dart"'); | |
| 109 expect(info.dart2jsPath, 'html/dart2js/html_dart2js.dart'); | |
| 110 } | |
| 111 | |
| 112 test_documentedDefault() { | |
| 113 var info = _computeSingleInfo('const LibraryInfo("")'); | |
| 114 expect(info.documented, true); | |
| 115 } | |
| 116 | |
| 117 test_documentedFalse() { | |
| 118 var info = _computeSingleInfo('const LibraryInfo("", documented: false)'); | |
| 119 expect(info.documented, false); | |
| 120 } | |
| 121 | |
| 122 test_documentedTrue() { | |
| 123 var info = _computeSingleInfo('const LibraryInfo("", documented: true)'); | |
| 124 expect(info.documented, true); | |
| 125 } | |
| 126 | |
| 127 test_implementationDefault() { | |
| 128 var info = _computeSingleInfo('const LibraryInfo("")'); | |
| 129 expect(info.implementation, false); | |
| 130 } | |
| 131 | |
| 132 test_implementationFalse() { | |
| 133 var info = | |
| 134 _computeSingleInfo('const LibraryInfo("", implementation: false)'); | |
| 135 expect(info.implementation, false); | |
| 136 } | |
| 137 | |
| 138 test_implementationTrue() { | |
| 139 var info = | |
| 140 _computeSingleInfo('const LibraryInfo("", implementation: true)'); | |
| 141 expect(info.implementation, true); | |
| 142 } | |
| 143 | |
| 144 test_maturityDefault() { | |
| 145 var info = _computeSingleInfo('const LibraryInfo("")'); | |
| 146 expect(info.maturity, Maturity.UNSPECIFIED); | |
| 147 } | |
| 148 | |
| 149 test_maturityStable() { | |
| 150 var info = | |
| 151 _computeSingleInfo('const LibraryInfo("", maturity: Maturity.FOO)', | |
| 152 additionalDeclarations: ''' | |
| 153 class Maturity { | |
| 154 final int level; | |
| 155 final String name; | |
| 156 final String description; | |
| 157 | |
| 158 const Maturity(this.level, this.name, this.description); | |
| 159 | |
| 160 static const Maturity FOO = const Maturity(10, "Foo", "Foo description"); | |
| 161 } | |
| 162 '''); | |
| 163 expect(info.maturity.level, 10); | |
| 164 expect(info.maturity.name, 'Foo'); | |
| 165 expect(info.maturity.description, 'Foo description'); | |
| 166 } | |
| 167 | |
| 168 test_multipleLibraries() { | |
| 169 var info = _computeLibraries(''' | |
| 170 const Map<String, LibraryInfo> libraries = const { | |
| 171 "async": const LibraryInfo("async/async.dart"), | |
| 172 "core": const LibraryInfo("core/core.dart") | |
| 173 } | |
| 174 '''); | |
| 175 expect(info.keys, unorderedEquals(['async', 'core'])); | |
| 176 expect(info['async'].path, 'async/async.dart'); | |
| 177 expect(info['core'].path, 'core/core.dart'); | |
| 178 } | |
| 179 | |
| 180 test_path() { | |
| 181 var info = _computeSingleInfo('const LibraryInfo("core/core.dart")'); | |
| 182 expect(info.path, 'core/core.dart'); | |
| 183 } | |
| 184 | |
| 185 test_platformsDefault() { | |
| 186 var info = _computeSingleInfo('const LibraryInfo("")'); | |
| 187 expect(info.platforms, DART2JS_PLATFORM | VM_PLATFORM); | |
| 188 } | |
| 189 | |
| 190 test_platformsMultiple() { | |
| 191 var info = _computeSingleInfo( | |
| 192 'const LibraryInfo("", platforms: VM_PLATFORM | DART2JS_PLATFORM)', | |
| 193 additionalDeclarations: ''' | |
| 194 const int DART2JS_PLATFORM = 1; | |
| 195 const int VM_PLATFORM = 2; | |
| 196 '''); | |
| 197 expect(info.platforms, 1 | 2); | |
| 198 } | |
| 199 | |
| 200 test_platformsSingle() { | |
| 201 var info = | |
| 202 _computeSingleInfo('const LibraryInfo("", platforms: VM_PLATFORM)', | |
| 203 additionalDeclarations: ''' | |
| 204 const int VM_PLATFORM = 2; | |
| 205 '''); | |
| 206 expect(info.platforms, 2); | |
| 207 } | |
| 208 | |
| 209 Map<String, LibraryInfo> _computeLibraries(String text, | |
| 210 {String additionalDeclarations: ''}) { | |
| 211 var fullText = '$text\n$additionalDeclarations'; | |
| 212 var scanner = new _Scanner(fullText); | |
| 213 var token = scanner.tokenize(); | |
| 214 var parser = new Parser(null, AnalysisErrorListener.NULL_LISTENER); | |
| 215 var compilationUnit = parser.parseCompilationUnit(token); | |
| 216 var unlinkedUnit = serializeAstUnlinked(compilationUnit); | |
| 217 return readLibraries(unlinkedUnit); | |
| 218 } | |
| 219 | |
| 220 LibraryInfo _computeSingleInfo(String text, | |
| 221 {String additionalDeclarations: ''}) { | |
| 222 var libraries = _computeLibraries( | |
| 223 'const Map<String, LibraryInfo> libraries = const { "x": $text };', | |
| 224 additionalDeclarations: additionalDeclarations); | |
| 225 return libraries['x']; | |
| 226 } | |
| 227 } | |
| 228 | |
| 229 class _Scanner extends Scanner { | |
| 230 _Scanner(String contents) : super.create(new CharSequenceReader(contents)) { | |
| 231 preserveComments = false; | |
| 232 } | |
| 233 | |
| 234 @override | |
| 235 void reportError( | |
| 236 ScannerErrorCode errorCode, int offset, List<Object> arguments) { | |
| 237 fail('Unexpected error($errorCode, $offset, $arguments)'); | |
| 238 } | |
| 239 } | |
| OLD | NEW |