in example/format.dart [58:143]
void runTest(String path, int line) {
var indentPattern = RegExp(r'^\(indent (\d+)\)\s*');
// Locate the "test" directory. Use mirrors so that this works with the test
// package, which loads this suite into an isolate.
var testDir = p.join(
p.dirname(currentMirrorSystem()
.findLibrary(#dart_style.example.format)
.uri
.path),
'../test');
var lines = File(p.join(testDir, path)).readAsLinesSync();
// The first line may have a "|" to indicate the page width.
var pageWidth = 80;
if (lines[0].endsWith('|')) {
pageWidth = lines[0].indexOf('|');
lines = lines.skip(1).toList();
}
var i = 0;
while (i < lines.length) {
var description = lines[i++].replaceAll('>>>', '').trim();
// Let the test specify a leading indentation. This is handy for
// regression tests which often come from a chunk of nested code.
var leadingIndent = 0;
var indentMatch = indentPattern.firstMatch(description);
if (indentMatch != null) {
leadingIndent = int.parse(indentMatch[1]!);
description = description.substring(indentMatch.end);
}
if (description == '') {
description = 'line ${i + 1}';
} else {
description = 'line ${i + 1}: $description';
}
var startLine = i + 1;
var input = '';
while (!lines[i].startsWith('<<<')) {
input += lines[i++] + '\n';
}
var expectedOutput = '';
while (++i < lines.length && !lines[i].startsWith('>>>')) {
expectedOutput += lines[i] + '\n';
}
if (line != startLine) continue;
var isCompilationUnit = p.extension(path) == '.unit';
var inputCode =
_extractSelection(input, isCompilationUnit: isCompilationUnit);
var expected =
_extractSelection(expectedOutput, isCompilationUnit: isCompilationUnit);
var formatter = DartFormatter(pageWidth: pageWidth, indent: leadingIndent);
var actual = formatter.formatSource(inputCode);
// The test files always put a newline at the end of the expectation.
// Statements from the formatter (correctly) don't have that, so add
// one to line up with the expected result.
var actualText = actual.text;
if (!isCompilationUnit) actualText += '\n';
print('$path $description');
drawRuler('before', pageWidth);
print(input);
if (actualText == expected.text) {
drawRuler('result', pageWidth);
print(actualText);
} else {
print('FAIL');
drawRuler('expected', pageWidth);
print(expected.text);
drawRuler('actual', pageWidth);
print(actualText);
}
}
}