|
| 1 | +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | + |
| 7 | +import 'package:analyzer/dart/analysis/analysis_context.dart'; |
| 8 | +import 'package:analyzer/dart/analysis/analysis_context_collection.dart'; |
| 9 | +import 'package:analyzer/dart/analysis/results.dart'; |
| 10 | +import 'package:analyzer/error/error.dart'; |
| 11 | +import 'package:analyzer/file_system/file_system.dart'; |
| 12 | +import 'package:analyzer/file_system/overlay_file_system.dart'; |
| 13 | +import 'package:analyzer/file_system/physical_file_system.dart'; |
| 14 | +import 'package:analyzer/src/error/codes.dart'; |
| 15 | +import 'package:front_end/src/testing/package_root.dart' as package_root; |
| 16 | +import 'package:test/test.dart'; |
| 17 | + |
| 18 | +main() async { |
| 19 | + SnippetTester tester = SnippetTester(); |
| 20 | + await tester.verify(); |
| 21 | + if (tester.output.isNotEmpty) { |
| 22 | + fail(tester.output.toString()); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +class SnippetTester { |
| 27 | + OverlayResourceProvider provider; |
| 28 | + Folder docFolder; |
| 29 | + String snippetDirPath; |
| 30 | + String snippetPath; |
| 31 | + |
| 32 | + StringBuffer output = StringBuffer(); |
| 33 | + |
| 34 | + SnippetTester() { |
| 35 | + provider = OverlayResourceProvider(PhysicalResourceProvider.INSTANCE); |
| 36 | + String packageRoot = |
| 37 | + provider.pathContext.normalize(package_root.packageRoot); |
| 38 | + String analyzerPath = provider.pathContext.join(packageRoot, 'analyzer'); |
| 39 | + String docPath = provider.pathContext.join(analyzerPath, 'doc'); |
| 40 | + docFolder = provider.getFolder(docPath); |
| 41 | + snippetDirPath = |
| 42 | + provider.pathContext.join(analyzerPath, 'test', 'snippets'); |
| 43 | + snippetPath = provider.pathContext.join(snippetDirPath, 'snippet.dart'); |
| 44 | + } |
| 45 | + |
| 46 | + void verify() async { |
| 47 | + await verifyFolder(docFolder); |
| 48 | + } |
| 49 | + |
| 50 | + void verifyFile(File file) async { |
| 51 | + String content = file.readAsStringSync(); |
| 52 | + List<String> lines = const LineSplitter().convert(content); |
| 53 | + List<String> codeLines = []; |
| 54 | + bool inCode = false; |
| 55 | + for (int i = 0; i < lines.length; i++) { |
| 56 | + String line = lines[i]; |
| 57 | + if (line == '```dart') { |
| 58 | + if (inCode) { |
| 59 | + // TODO(brianwilkerson) Report this. |
| 60 | + } |
| 61 | + inCode = true; |
| 62 | + } else if (line == '```') { |
| 63 | + if (!inCode) { |
| 64 | + // TODO(brianwilkerson) Report this. |
| 65 | + } |
| 66 | + await verifySnippet(file, codeLines.join('\n')); |
| 67 | + codeLines.clear(); |
| 68 | + inCode = false; |
| 69 | + } else if (inCode) { |
| 70 | + codeLines.add(line); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + void verifyFolder(Folder folder) async { |
| 76 | + for (Resource child in folder.getChildren()) { |
| 77 | + if (child is File) { |
| 78 | + if (child.shortName.endsWith('.md')) { |
| 79 | + await verifyFile(child); |
| 80 | + } |
| 81 | + } else if (child is Folder) { |
| 82 | + await verifyFolder(child); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + void verifySnippet(File file, String snippet) async { |
| 88 | + // TODO(brianwilkerson) When the files outside of 'src' contain only public |
| 89 | + // API, write code to compute the list of imports so that new public API |
| 90 | + // will automatically be allowed. |
| 91 | + String imports = ''' |
| 92 | +import 'dart:math' as math; |
| 93 | +
|
| 94 | +import 'package:analyzer/dart/analysis/analysis_context.dart'; |
| 95 | +import 'package:analyzer/dart/analysis/analysis_context_collection.dart'; |
| 96 | +import 'package:analyzer/dart/analysis/results.dart'; |
| 97 | +import 'package:analyzer/dart/analysis/session.dart'; |
| 98 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 99 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 100 | +import 'package:analyzer/dart/element/element.dart'; |
| 101 | +import 'package:analyzer/dart/element/visitor.dart'; |
| 102 | +
|
| 103 | +'''; |
| 104 | + provider.setOverlay(snippetPath, |
| 105 | + content: ''' |
| 106 | +$imports |
| 107 | +$snippet |
| 108 | +''', |
| 109 | + modificationStamp: 1); |
| 110 | + try { |
| 111 | + AnalysisContextCollection collection = new AnalysisContextCollection( |
| 112 | + includedPaths: <String>[snippetDirPath], resourceProvider: provider); |
| 113 | + List<AnalysisContext> contexts = collection.contexts; |
| 114 | + if (contexts.length != 1) { |
| 115 | + fail('The snippets directory contains multiple analysis contexts.'); |
| 116 | + } |
| 117 | + ErrorsResult results = |
| 118 | + await contexts[0].currentSession.getErrors(snippetPath); |
| 119 | + Iterable<AnalysisError> errors = results.errors.where((error) { |
| 120 | + ErrorCode errorCode = error.errorCode; |
| 121 | + return errorCode != HintCode.UNUSED_IMPORT && |
| 122 | + errorCode != HintCode.UNUSED_LOCAL_VARIABLE; |
| 123 | + }); |
| 124 | + if (errors.isNotEmpty) { |
| 125 | + String filePath = |
| 126 | + provider.pathContext.relative(file.path, from: docFolder.path); |
| 127 | + if (output.isNotEmpty) { |
| 128 | + output.writeln(); |
| 129 | + } |
| 130 | + output.writeln('Errors in snippet in "$filePath":'); |
| 131 | + output.writeln(); |
| 132 | + output.writeln(snippet); |
| 133 | + output.writeln(); |
| 134 | + int importsLength = imports.length + 1; // account for the '\n'. |
| 135 | + for (var error in errors) { |
| 136 | + writeError(error, importsLength); |
| 137 | + } |
| 138 | + } |
| 139 | + } finally { |
| 140 | + provider.removeOverlay(snippetPath); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + void writeError(AnalysisError error, int prefixLength) { |
| 145 | + output.write(error.errorCode); |
| 146 | + output.write(' ('); |
| 147 | + output.write(error.offset - prefixLength); |
| 148 | + output.write(', '); |
| 149 | + output.write(error.length); |
| 150 | + output.write(') '); |
| 151 | + output.writeln(error.message); |
| 152 | + } |
| 153 | +} |
0 commit comments