• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'dart:convert';
6import 'dart:io' hide Platform;
7import 'package:path/path.dart' as path;
8
9import 'package:test/test.dart' hide TypeMatcher, isInstanceOf;
10
11import 'package:snippets/configuration.dart';
12import 'package:snippets/snippets.dart';
13
14void main() {
15  group('Generator', () {
16    Configuration configuration;
17    SnippetGenerator generator;
18    Directory tmpDir;
19    File template;
20
21    setUp(() {
22      tmpDir = Directory.systemTemp.createTempSync('flutter_snippets_test.');
23      configuration = Configuration(flutterRoot: Directory(path.join(
24          tmpDir.absolute.path, 'flutter')));
25      configuration.createOutputDirectory();
26      configuration.templatesDirectory.createSync(recursive: true);
27      configuration.skeletonsDirectory.createSync(recursive: true);
28      template = File(path.join(configuration.templatesDirectory.path, 'template.tmpl'));
29      template.writeAsStringSync('''
30
31{{description}}
32
33{{code-preamble}}
34
35main() {
36  {{code}}
37}
38''');
39      configuration.getHtmlSkeletonFile(SnippetType.application).writeAsStringSync('''
40<div>HTML Bits</div>
41{{description}}
42<pre>{{code}}</pre>
43<pre>{{app}}</pre>
44<div>More HTML Bits</div>
45''');
46      configuration.getHtmlSkeletonFile(SnippetType.sample).writeAsStringSync('''
47<div>HTML Bits</div>
48{{description}}
49<pre>{{code}}</pre>
50<div>More HTML Bits</div>
51''');
52      generator = SnippetGenerator(configuration: configuration);
53    });
54    tearDown(() {
55      tmpDir.deleteSync(recursive: true);
56    });
57
58    test('generates application snippets', () async {
59      final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
60        ..createSync(recursive: true)
61        ..writeAsStringSync('''
62A description of the snippet.
63
64On several lines.
65
66```my-dart_language my-preamble
67const String name = 'snippet';
68```
69
70```dart
71void main() {
72  print('The actual \$name.');
73}
74```
75''');
76
77      final String html = generator.generate(
78        inputFile,
79        SnippetType.application,
80        template: 'template',
81        metadata: <String, Object>{
82          'id': 'id',
83        },
84      );
85      expect(html, contains('<div>HTML Bits</div>'));
86      expect(html, contains('<div>More HTML Bits</div>'));
87      expect(html, contains('print(&#39;The actual \$name.&#39;);'));
88      expect(html, contains('A description of the snippet.\n'));
89      expect(
90          html,
91          contains('&#47;&#47; A description of the snippet.\n'
92              '&#47;&#47;\n'
93              '&#47;&#47; On several lines.\n'));
94      expect(html, contains('void main() {'));
95    });
96
97    test('generates sample snippets', () async {
98      final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
99        ..createSync(recursive: true)
100        ..writeAsStringSync('''
101A description of the snippet.
102
103On several lines.
104
105```code
106void main() {
107  print('The actual \$name.');
108}
109```
110''');
111
112      final String html = generator.generate(inputFile, SnippetType.sample, metadata: <String, Object>{'id': 'id'});
113      expect(html, contains('<div>HTML Bits</div>'));
114      expect(html, contains('<div>More HTML Bits</div>'));
115      expect(html, contains('  print(&#39;The actual \$name.&#39;);'));
116      expect(html, contains('<div class="snippet-description">'
117          '{@end-inject-html}A description of the snippet.\n\n'
118          'On several lines.{@inject-html}</div>\n'));
119      expect(html, contains('main() {'));
120    });
121
122    test('generates snippet application metadata', () async {
123      final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
124        ..createSync(recursive: true)
125        ..writeAsStringSync('''
126A description of the snippet.
127
128On several lines.
129
130```code
131void main() {
132  print('The actual \$name.');
133}
134```
135''');
136
137      final File outputFile = File(path.join(tmpDir.absolute.path, 'snippet_out.dart'));
138      final File expectedMetadataFile = File(path.join(tmpDir.absolute.path, 'snippet_out.json'));
139
140      generator.generate(
141        inputFile,
142        SnippetType.application,
143        template: 'template',
144        output: outputFile,
145        metadata: <String, Object>{'sourcePath': 'some/path.dart', 'id': 'id'},
146      );
147      expect(expectedMetadataFile.existsSync(), isTrue);
148      final Map<String, dynamic> json = jsonDecode(expectedMetadataFile.readAsStringSync());
149      expect(json['id'], equals('id'));
150      expect(json['file'], equals('snippet_out.dart'));
151      expect(json['description'], equals('A description of the snippet.\n\nOn several lines.'));
152      // Ensure any passed metadata is included in the output JSON too.
153      expect(json['sourcePath'], equals('some/path.dart'));
154    });
155  });
156}
157