File size: 14,891 Bytes
83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 b7f6949 83fa459 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | // Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert';
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:path/path.dart' as path;
import 'package:platform/platform.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:snippets/snippets.dart';
import 'package:test/test.dart' hide TypeMatcher, isInstanceOf;
import '../bin/snippets.dart' as snippets_main;
import 'fake_process_manager.dart';
class FakeFlutterInformation extends FlutterInformation {
FakeFlutterInformation(this.flutterRoot);
final Directory flutterRoot;
@override
Directory getFlutterRoot() {
return flutterRoot;
}
@override
Map<String, dynamic> getFlutterInformation() {
return <String, dynamic>{
'flutterRoot': flutterRoot,
'frameworkVersion': Version(2, 10, 0),
'dartSdkVersion': Version(2, 12, 1),
};
}
}
void main() {
group('Generator', () {
late MemoryFileSystem memoryFileSystem = MemoryFileSystem();
late FlutterRepoSnippetConfiguration configuration;
late SnippetGenerator generator;
late Directory tmpDir;
void writeSkeleton(String type) {
switch (type) {
case 'dartpad':
configuration.getHtmlSkeletonFile('dartpad').writeAsStringSync('''
<div>HTML Bits (DartPad-style)</div>
<iframe class="snippet-dartpad" src="https://dartpad.dev/embed-flutter.html?split=60&run=true&sample_id={{id}}&sample_channel={{channel}}"></iframe>
<div>More HTML Bits</div>
''');
case 'sample':
case 'snippet':
configuration.getHtmlSkeletonFile(type).writeAsStringSync('''
<div>HTML Bits</div>
{{description}}
<pre>{{code}}</pre>
<pre>{{app}}</pre>
<div>More HTML Bits</div>
''');
}
}
setUp(() {
// Create a new filesystem.
memoryFileSystem = MemoryFileSystem();
tmpDir = memoryFileSystem.systemTempDirectory.createTempSync('flutter_snippets_test.');
configuration = FlutterRepoSnippetConfiguration(
flutterRoot: memoryFileSystem.directory(path.join(tmpDir.absolute.path, 'flutter')),
filesystem: memoryFileSystem,
);
configuration.skeletonsDirectory.createSync(recursive: true);
<String>['dartpad', 'sample', 'snippet'].forEach(writeSkeleton);
FlutterInformation.instance = FakeFlutterInformation(configuration.flutterRoot);
generator = SnippetGenerator(
configuration: configuration,
filesystem: memoryFileSystem,
flutterRoot: configuration.skeletonsDirectory.parent,
);
});
test('generates samples', () async {
final File inputFile =
memoryFileSystem.file(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
..createSync(recursive: true)
..writeAsStringSync(r'''
A description of the sample.
On several lines.
** See code in examples/api/widgets/foo/foo_example.0.dart **
''');
final String examplePath = path.join(
configuration.flutterRoot.path,
'examples/api/widgets/foo/foo_example.0.dart',
);
memoryFileSystem.file(examplePath)
..create(recursive: true)
..writeAsStringSync('''
// Copyright
// Flutter code sample for [MyElement].
void main() {
runApp(MaterialApp(title: 'foo'));
}\n''');
final File outputFile = memoryFileSystem.file(
path.join(tmpDir.absolute.path, 'snippet_out.txt'),
);
final SnippetDartdocParser sampleParser = SnippetDartdocParser(memoryFileSystem);
const String sourcePath = 'packages/flutter/lib/src/widgets/foo.dart';
const int sourceLine = 222;
final SourceElement element = sampleParser.parseFromDartdocToolFile(
inputFile,
element: 'MyElement',
startLine: sourceLine,
sourceFile: memoryFileSystem.file(sourcePath),
type: 'sample',
);
expect(element.samples, isNotEmpty);
element.samples.first.metadata.addAll(<String, Object?>{'channel': 'stable'});
final String code = generator.generateCode(element.samples.first, output: outputFile);
expect(code, contains("runApp(MaterialApp(title: 'foo'));"));
final String html = generator.generateHtml(element.samples.first);
expect(html, contains('<div>HTML Bits</div>'));
expect(html, contains('<div>More HTML Bits</div>'));
expect(html, contains(r'''runApp(MaterialApp(title: 'foo'));'''));
expect(html, isNot(contains('sample_channel=stable')));
expect(
html,
contains(
'A description of the sample.\n'
'\n'
'On several lines.{@inject-html}</div>',
),
);
expect(html, contains('void main() {'));
final String outputContents = outputFile.readAsStringSync();
expect(outputContents, contains('void main() {'));
});
test('generates snippets', () async {
final File inputFile =
memoryFileSystem.file(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
..createSync(recursive: true)
..writeAsStringSync(r'''
A description of the snippet.
On several lines.
```code
void main() {
print('The actual $name.');
}
```
''');
final SnippetDartdocParser sampleParser = SnippetDartdocParser(memoryFileSystem);
const String sourcePath = 'packages/flutter/lib/src/widgets/foo.dart';
const int sourceLine = 222;
final SourceElement element = sampleParser.parseFromDartdocToolFile(
inputFile,
element: 'MyElement',
startLine: sourceLine,
sourceFile: memoryFileSystem.file(sourcePath),
type: 'snippet',
);
expect(element.samples, isNotEmpty);
element.samples.first.metadata.addAll(<String, Object>{'channel': 'stable'});
final String code = generator.generateCode(element.samples.first);
expect(code, contains('// A description of the snippet.'));
final String html = generator.generateHtml(element.samples.first);
expect(html, contains('<div>HTML Bits</div>'));
expect(html, contains('<div>More HTML Bits</div>'));
expect(html, contains(r' print('The actual $name.');'));
expect(
html,
contains(
'<div class="snippet-description">{@end-inject-html}A description of the snippet.\n\n'
'On several lines.{@inject-html}</div>\n',
),
);
expect(html, contains('main() {'));
});
test('generates dartpad samples', () async {
final File inputFile =
memoryFileSystem.file(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
..createSync(recursive: true)
..writeAsStringSync(r'''
A description of the snippet.
On several lines.
** See code in examples/api/widgets/foo/foo_example.0.dart **
''');
final String examplePath = path.join(
configuration.flutterRoot.path,
'examples/api/widgets/foo/foo_example.0.dart',
);
memoryFileSystem.file(examplePath)
..create(recursive: true)
..writeAsStringSync('''
// Copyright
// Flutter code sample for [MyElement].
void main() {
runApp(MaterialApp(title: 'foo'));
}\n''');
final SnippetDartdocParser sampleParser = SnippetDartdocParser(memoryFileSystem);
const String sourcePath = 'packages/flutter/lib/src/widgets/foo.dart';
const int sourceLine = 222;
final SourceElement element = sampleParser.parseFromDartdocToolFile(
inputFile,
element: 'MyElement',
startLine: sourceLine,
sourceFile: memoryFileSystem.file(sourcePath),
type: 'dartpad',
);
expect(element.samples, isNotEmpty);
element.samples.first.metadata.addAll(<String, Object>{'channel': 'stable'});
final String code = generator.generateCode(element.samples.first);
expect(code, contains("runApp(MaterialApp(title: 'foo'));"));
final String html = generator.generateHtml(element.samples.first);
expect(html, contains('<div>HTML Bits (DartPad-style)</div>'));
expect(html, contains('<div>More HTML Bits</div>'));
expect(
html,
contains(
'<iframe class="snippet-dartpad" src="https://dartpad.dev/embed-flutter.html?split=60&run=true&sample_id=MyElement.0&sample_channel=stable"></iframe>\n',
),
);
});
test('generates sample metadata', () async {
final File inputFile =
memoryFileSystem.file(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
..createSync(recursive: true)
..writeAsStringSync(r'''
A description of the snippet.
On several lines.
```dart
void main() {
print('The actual $name.');
}
```
''');
final File outputFile = memoryFileSystem.file(
path.join(tmpDir.absolute.path, 'snippet_out.dart'),
);
final File expectedMetadataFile = memoryFileSystem.file(
path.join(tmpDir.absolute.path, 'snippet_out.json'),
);
final SnippetDartdocParser sampleParser = SnippetDartdocParser(memoryFileSystem);
const String sourcePath = 'packages/flutter/lib/src/widgets/foo.dart';
const int sourceLine = 222;
final SourceElement element = sampleParser.parseFromDartdocToolFile(
inputFile,
element: 'MyElement',
startLine: sourceLine,
sourceFile: memoryFileSystem.file(sourcePath),
type: 'sample',
);
expect(element.samples, isNotEmpty);
element.samples.first.metadata.addAll(<String, Object>{'channel': 'stable'});
generator.generateCode(element.samples.first, output: outputFile);
expect(expectedMetadataFile.existsSync(), isTrue);
final Map<String, dynamic> json =
jsonDecode(expectedMetadataFile.readAsStringSync()) as Map<String, dynamic>;
expect(json['id'], equals('MyElement.0'));
expect(json['channel'], equals('stable'));
expect(json['file'], equals('snippet_out.dart'));
expect(json['description'], equals('A description of the snippet.\n\nOn several lines.'));
expect(json['sourcePath'], equals('packages/flutter/lib/src/widgets/foo.dart'));
});
});
group('snippets command line argument test', () {
late MemoryFileSystem memoryFileSystem = MemoryFileSystem();
late Directory tmpDir;
late Directory flutterRoot;
late FakeProcessManager fakeProcessManager;
setUp(() {
fakeProcessManager = FakeProcessManager();
memoryFileSystem = MemoryFileSystem();
tmpDir = memoryFileSystem.systemTempDirectory.createTempSync('flutter_snippets_test.');
flutterRoot = memoryFileSystem.directory(path.join(tmpDir.absolute.path, 'flutter'))
..createSync(recursive: true);
});
test('command line arguments are parsed and passed to generator', () {
final FakePlatform platform = FakePlatform(
environment: <String, String>{
'PACKAGE_NAME': 'dart:ui',
'LIBRARY_NAME': 'library',
'ELEMENT_NAME': 'element',
'FLUTTER_ROOT': flutterRoot.absolute.path,
// The details here don't really matter other than the flutter root.
'FLUTTER_VERSION': '''
{
"frameworkVersion": "2.5.0-6.0.pre.55",
"channel": "use_snippets_pkg",
"repositoryUrl": "git@github.com:flutter/flutter.git",
"frameworkRevision": "fec4641e1c88923ecd6c969e2ff8a0dd12dc0875",
"frameworkCommitDate": "2021-08-11 15:19:48 -0700",
"engineRevision": "d8bbebed60a77b3d4fe9c840dc94dfbce159d951",
"dartSdkVersion": "2.14.0 (build 2.14.0-393.0.dev)",
"flutterRoot": "${flutterRoot.absolute.path}"
}''',
},
);
final FlutterInformation flutterInformation = FlutterInformation(
filesystem: memoryFileSystem,
processManager: fakeProcessManager,
platform: platform,
);
FlutterInformation.instance = flutterInformation;
MockSnippetGenerator mockSnippetGenerator = MockSnippetGenerator();
snippets_main.snippetGenerator = mockSnippetGenerator;
String errorMessage = '';
errorExit = (String message) {
errorMessage = message;
};
snippets_main.platform = platform;
snippets_main.filesystem = memoryFileSystem;
snippets_main.processManager = fakeProcessManager;
final File input = memoryFileSystem.file(tmpDir.childFile('input.snippet'))
..writeAsString('/// Test file');
snippets_main.main(<String>['--input=${input.absolute.path}']);
final Map<String, dynamic> metadata = mockSnippetGenerator.sample.metadata;
// Ignore the channel, because channel is really just the branch, and will be
// different on development workstations.
metadata.remove('channel');
expect(
metadata,
equals(<String, dynamic>{
'id': 'dart_ui.library.element',
'element': 'element',
'sourcePath': 'unknown.dart',
'sourceLine': 1,
'serial': '',
'package': 'dart:ui',
'library': 'library',
}),
);
snippets_main.main(<String>[]);
expect(
errorMessage,
equals(
'The --input option must be specified, either on the command line, or in the INPUT environment variable.',
),
);
errorMessage = '';
snippets_main.main(<String>['--input=${input.absolute.path}', '--type=snippet']);
expect(errorMessage, equals(''));
errorMessage = '';
mockSnippetGenerator = MockSnippetGenerator();
snippets_main.snippetGenerator = mockSnippetGenerator;
snippets_main.main(<String>[
'--input=${input.absolute.path}',
'--type=snippet',
'--no-format-output',
]);
expect(mockSnippetGenerator.formatOutput, equals(false));
errorMessage = '';
input.deleteSync();
snippets_main.main(<String>['--input=${input.absolute.path}']);
expect(errorMessage, equals('The input file ${input.absolute.path} does not exist.'));
errorMessage = '';
});
});
}
class MockSnippetGenerator extends SnippetGenerator {
late CodeSample sample;
File? output;
String? copyright;
String? description;
late bool formatOutput;
late bool addSectionMarkers;
late bool includeAssumptions;
@override
String generateCode(
CodeSample sample, {
File? output,
String? copyright,
String? description,
bool formatOutput = true,
bool addSectionMarkers = false,
bool includeAssumptions = false,
}) {
this.sample = sample;
this.output = output;
this.copyright = copyright;
this.description = description;
this.formatOutput = formatOutput;
this.addSectionMarkers = addSectionMarkers;
this.includeAssumptions = includeAssumptions;
return '';
}
@override
String generateHtml(CodeSample sample) {
return '';
}
}
|