trphoenix
2018-11-12 29fbfc5dd1d55d189f23eb6d32f000252f92985f
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
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
 
@TestOn('vm')
 
import 'dart:io';
 
import 'package:test/test.dart';
import 'package:path/path.dart' as p;
 
void main() {
  test('README example', () {
    final readmeContent = File('README.md').readAsStringSync();
 
    final exampleContent = _getExampleContent('example.dart');
    expect(readmeContent, contains(exampleContent));
 
    final exampleGeneratedContent = _getExampleContent('example.g.dart');
    expect(readmeContent, contains(exampleGeneratedContent));
  });
}
 
String _getExampleContent(String fileName) {
  final lines = File(p.join('example', fileName)).readAsLinesSync();
 
  var lastHadContent = false;
 
  // All lines with content, except those starting with `/`.
  // Also exclude blank lines that follow other blank lines
  final cleanedSource = lines.where((l) {
    if (l.startsWith(r'/')) {
      return false;
    }
 
    if (l.trim().isNotEmpty) {
      lastHadContent = true;
      return true;
    }
 
    if (lastHadContent) {
      lastHadContent = false;
      return true;
    }
 
    return false;
  }).join('\n');
 
  return '''
```dart
$cleanedSource
```''';
}