
When converting all of the samples to use the snippet tool, I encountered some bugs/shortcomings: 1. The document production took 90 minutes, since the snippet tool was being invoked from the command line each time. I fixed this by snapshotting the executable before running, so it's down to 7 minutes. 2. The sample code was not being properly escaped by the snippet tool, so generics were causing issues in the HTML output. It is now quoted. 3. Code examples that used languages other than Dart were not supported. Anything that highlight.js was compiled for dartdoc with is now supported. 4. The comment color for highlight.js was light grey on white, which was pretty unreadable. It's now dark green and bold.
114 lines
3.3 KiB
Dart
114 lines
3.3 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: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('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', 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);
|
|
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\nOn several lines.\n'));
|
|
expect(html, contains('main() {'));
|
|
});
|
|
});
|
|
}
|