Files
sdk/pkg/dev_compiler/test/codegen_test.dart
T

128 lines
4.5 KiB
Dart

/// Tests code generation.
/// Runs Dart Dev Compiler on all input in the `codegen` directory and checks
/// that the output is what we expected.
library ddc.test.codegen_test;
import 'dart:io';
import 'package:args/args.dart';
import 'package:cli_util/cli_util.dart' show getSdkDir;
import 'package:ddc/devc.dart';
import 'package:ddc/src/checker/resolver.dart' show TypeResolver;
import 'package:ddc/src/options.dart';
import 'package:logging/logging.dart' show Level;
import 'package:path/path.dart' as path;
import 'package:unittest/unittest.dart';
final ArgParser argParser = new ArgParser()
..addOption('dart-sdk', help: 'Dart SDK Path', defaultsTo: null)
..addFlag(
'dart-gen', abbr: 'd', help: 'Generate dart output', defaultsTo: false);
main(arguments) {
if (arguments == null) arguments = [];
ArgResults args = argParser.parse(arguments);
var script = Platform.script.path;
var dartGen = args['dart-gen'];
var filePattern = new RegExp(args.rest.length > 0 ? args.rest[0] : '.');
var compilerMessages = new StringBuffer();
var loggerSub;
setUp(() {
compilerMessages.clear();
loggerSub = setupLogger(Level.CONFIG, compilerMessages.writeln);
});
tearDown(() {
if (loggerSub != null) {
loggerSub.cancel();
loggerSub = null;
}
});
var testDir = path.absolute(path.dirname(script));
var inputDir = dartGen
? path.join(testDir, 'dart_codegen')
: path.join(testDir, 'codegen');
var actualDir = path.join(inputDir, 'actual');
var paths = new Directory(inputDir)
.listSync()
.where((f) => f is File)
.map((f) => f.path)
.where((p) => p.endsWith('.dart') && filePattern.hasMatch(p));
var options = new CompilerOptions(
outputDir: actualDir,
useColors: false,
outputDart: dartGen,
formatOutput: dartGen);
var realSdk = new TypeResolver.fromDir(getSdkDir(arguments).path, options);
// Validate that old output is gone before running.
// TODO(jmesserly): it'd be nice to do all cleanup here, including removing
// pub's 'packages' symlinks which mess up the diff. That way this test
// can be self contained instead of depending on a shell script.
if (new Directory(actualDir).existsSync()) {
throw 'Old compiler output should be cleaned up first. Use ./test/test.sh';
}
for (var filePath in paths) {
var filename = path.basenameWithoutExtension(filePath);
test('devc $filename.dart', () {
compilerMessages.writeln('// Messages from compiling $filename.dart');
var result = compile(filePath, realSdk, options);
var success = !result.failure;
// Write compiler messages to disk.
new File(path.join(actualDir, '$filename.txt'))
.writeAsStringSync(compilerMessages.toString());
var outputDir = new Directory(path.join(actualDir, filename));
expect(outputDir.existsSync(), success,
reason: '${outputDir.path} was created iff compilation succeeds');
// TODO(jmesserly): ideally we'd diff the output here. For now it
// happens in the containing shell script.
});
}
test('devc dart:core', () {
// Get the test SDK. We use a checked in copy so test expectations can be
// generated against a specific SDK version.
// TODO(jmesserly): eventually we should track compiler messages.
// For now we're just trying to get decent code generation.
var options = new CompilerOptions(
outputDir: actualDir,
checkSdk: true,
forceCompile: true,
outputDart: dartGen,
formatOutput: dartGen);
var testSdk = new TypeResolver.fromDir(path.join(testDir, 'sdk'), options);
var result = compile('dart:core', testSdk, options);
var outputDir = new Directory(path.join(actualDir, 'core'));
expect(outputDir.existsSync(), true,
reason: '${outputDir.path} was created for dart:core');
});
if (!dartGen) {
test('devc jscodegen html_input.html', () {
var filePath = path.join(inputDir, 'html_input.html');
compilerMessages.writeln('// Messages from compiling html_input.html');
var result = compile(filePath, realSdk, options);
var success = !result.failure;
// Write compiler messages to disk.
new File(path.join(actualDir, 'html_input.txt'))
.writeAsStringSync(compilerMessages.toString());
var outFile = new File(path.join(actualDir, 'html_input.html'));
expect(outFile.existsSync(), success,
reason: '${outFile.path} was created iff compilation succeeds');
});
}
}