mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

This adds an "anchor button" to each of the samples so that the user can link to individual samples instead of having to link to just the page. Clicking on the anchor button jumps to the anchor, as well as copying the anchor URL to the clipboard. There is some oddness in the implementation: because dartdoc uses a <base> tag, the href for the link can't just be "#id", it has to calculate the URL from the current window href. I do that in the onmouseenter and onclick because onload doesn't get triggered for <a> tags (and onmouseenter doesn't get triggered for mobile platforms), but I still want the href to be updated before someone right-clicks it to copy the URL.
157 lines
4.7 KiB
Dart
157 lines
4.7 KiB
Dart
// Copyright 2018 The Chromium 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 'dart:io' hide Platform;
|
|
import 'package:path/path.dart' as path;
|
|
|
|
import 'package:test/test.dart' hide TypeMatcher, isInstanceOf;
|
|
|
|
import 'package:snippets/configuration.dart';
|
|
import 'package:snippets/snippets.dart';
|
|
|
|
void main() {
|
|
group('Generator', () {
|
|
Configuration configuration;
|
|
SnippetGenerator generator;
|
|
Directory tmpDir;
|
|
File template;
|
|
|
|
setUp(() {
|
|
tmpDir = Directory.systemTemp.createTempSync('flutter_snippets_test.');
|
|
configuration = Configuration(flutterRoot: Directory(path.join(
|
|
tmpDir.absolute.path, 'flutter')));
|
|
configuration.createOutputDirectory();
|
|
configuration.templatesDirectory.createSync(recursive: true);
|
|
configuration.skeletonsDirectory.createSync(recursive: true);
|
|
template = File(path.join(configuration.templatesDirectory.path, 'template.tmpl'));
|
|
template.writeAsStringSync('''
|
|
|
|
{{description}}
|
|
|
|
{{code-preamble}}
|
|
|
|
main() {
|
|
{{code}}
|
|
}
|
|
''');
|
|
configuration.getHtmlSkeletonFile(SnippetType.application).writeAsStringSync('''
|
|
<div>HTML Bits</div>
|
|
{{description}}
|
|
<pre>{{code}}</pre>
|
|
<pre>{{app}}</pre>
|
|
<div>More HTML Bits</div>
|
|
''');
|
|
configuration.getHtmlSkeletonFile(SnippetType.sample).writeAsStringSync('''
|
|
<div>HTML Bits</div>
|
|
{{description}}
|
|
<pre>{{code}}</pre>
|
|
<div>More HTML Bits</div>
|
|
''');
|
|
generator = SnippetGenerator(configuration: configuration);
|
|
});
|
|
tearDown(() {
|
|
tmpDir.deleteSync(recursive: true);
|
|
});
|
|
|
|
test('generates application snippets', () async {
|
|
final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
|
|
..createSync(recursive: true)
|
|
..writeAsStringSync('''
|
|
A description of the snippet.
|
|
|
|
On several lines.
|
|
|
|
```my-dart_language my-preamble
|
|
const String name = 'snippet';
|
|
```
|
|
|
|
```dart
|
|
void main() {
|
|
print('The actual \$name.');
|
|
}
|
|
```
|
|
''');
|
|
|
|
final String html = generator.generate(
|
|
inputFile,
|
|
SnippetType.application,
|
|
template: 'template',
|
|
metadata: <String, Object>{
|
|
'id': 'id',
|
|
},
|
|
);
|
|
expect(html, contains('<div>HTML Bits</div>'));
|
|
expect(html, contains('<div>More HTML Bits</div>'));
|
|
expect(html, contains('print('The actual \$name.');'));
|
|
expect(html, contains('A description of the snippet.\n'));
|
|
expect(
|
|
html,
|
|
contains('// A description of the snippet.\n'
|
|
'//\n'
|
|
'// On several lines.\n'));
|
|
expect(html, contains('void main() {'));
|
|
});
|
|
|
|
test('generates sample snippets', () async {
|
|
final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
|
|
..createSync(recursive: true)
|
|
..writeAsStringSync('''
|
|
A description of the snippet.
|
|
|
|
On several lines.
|
|
|
|
```code
|
|
void main() {
|
|
print('The actual \$name.');
|
|
}
|
|
```
|
|
''');
|
|
|
|
final String html = generator.generate(inputFile, SnippetType.sample, metadata: <String, Object>{'id': 'id'});
|
|
expect(html, contains('<div>HTML Bits</div>'));
|
|
expect(html, contains('<div>More HTML Bits</div>'));
|
|
expect(html, contains(' 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 snippet application metadata', () async {
|
|
final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
|
|
..createSync(recursive: true)
|
|
..writeAsStringSync('''
|
|
A description of the snippet.
|
|
|
|
On several lines.
|
|
|
|
```code
|
|
void main() {
|
|
print('The actual \$name.');
|
|
}
|
|
```
|
|
''');
|
|
|
|
final File outputFile = File(path.join(tmpDir.absolute.path, 'snippet_out.dart'));
|
|
final File expectedMetadataFile = File(path.join(tmpDir.absolute.path, 'snippet_out.json'));
|
|
|
|
generator.generate(
|
|
inputFile,
|
|
SnippetType.application,
|
|
template: 'template',
|
|
output: outputFile,
|
|
metadata: <String, Object>{'sourcePath': 'some/path.dart', 'id': 'id'},
|
|
);
|
|
expect(expectedMetadataFile.existsSync(), isTrue);
|
|
final Map<String, dynamic> json = jsonDecode(expectedMetadataFile.readAsStringSync());
|
|
expect(json['id'], equals('id'));
|
|
expect(json['file'], equals('snippet_out.dart'));
|
|
expect(json['description'], equals('A description of the snippet.\n\nOn several lines.'));
|
|
// Ensure any passed metadata is included in the output JSON too.
|
|
expect(json['sourcePath'], equals('some/path.dart'));
|
|
});
|
|
});
|
|
}
|