feat(tests): add helper to eval a module

Needed later for unit tests for code gen and runtime code
in #3605
This commit is contained in:
Tobias Bosch 2015-08-28 11:45:39 -07:00
parent 896add7d77
commit 2a126f72f3
4 changed files with 132 additions and 1 deletions

View File

@ -0,0 +1,48 @@
import "dart:isolate";
import "dart:async";
Uri toDartDataUri(String source) {
return Uri.parse("data:application/dart;charset=utf-8,"
"${Uri.encodeComponent(source)}");
}
createIsolateSource(String moduleSource, List<List<String>> moduleImports) {
var moduleSourceParts = [];
moduleImports.forEach((sourceImport) {
String modName = sourceImport[0];
String modAlias = sourceImport[1];
moduleSourceParts.add("import 'package:${modName}.dart' as ${modAlias};");
});
moduleSourceParts.add(moduleSource);
return """
import "dart:isolate";
import "${toDartDataUri(moduleSourceParts.join('\n'))}" as mut;
main(List args, SendPort replyPort) {
replyPort.send(mut.run(args));
}
""";
}
dynamic callModule(dynamic data) { return data.map( (a) => a+1); }
evalModule(String moduleSource, List<List<String>> moduleImports, List args) {
String source = createIsolateSource(moduleSource, moduleImports);
Completer completer = new Completer();
RawReceivePort receivePort;
receivePort = new RawReceivePort( (message) {
receivePort.close();
completer.complete(message);
});
var packageRoot = Uri.parse('/packages');
return Isolate.spawnUri(toDartDataUri(source), args, receivePort.sendPort, packageRoot: packageRoot).then( (isolate) {
RawReceivePort errorPort;
errorPort = new RawReceivePort( (message) {
completer.completeError(message);
});
isolate.addErrorListener(errorPort.sendPort);
return completer.future;
});
}

View File

@ -0,0 +1,33 @@
import {Promise, PromiseWrapper} from 'angular2/src/core/facade/async';
import {isPresent, global} from 'angular2/src/core/facade/lang';
var evalCounter = 0;
function nextModuleName() {
return `evalScript${evalCounter++}`;
}
export function evalModule(moduleSource: string, moduleImports: string[][], args: any[]):
Promise<any> {
var moduleName = nextModuleName();
var moduleSourceWithImports = [];
var importModuleNames = [];
moduleImports.forEach(sourceImport => {
var modName = sourceImport[0];
var modAlias = sourceImport[1];
importModuleNames.push(modName);
moduleSourceWithImports.push(`var ${modAlias} = require('${modName}');`);
});
moduleSourceWithImports.push(moduleSource);
var moduleBody = new Function('require', 'exports', 'module', moduleSourceWithImports.join('\n'));
var System = global['System'];
if (isPresent(System) && isPresent(System.registerDynamic)) {
System.registerDynamic(moduleName, importModuleNames, false, moduleBody);
return <Promise<any>>System.import(moduleName).then((module) => module.run(args));
} else {
var exports = {};
moduleBody(require, exports, {});
return PromiseWrapper.resolve(exports['run'](args));
}
}

View File

@ -0,0 +1,50 @@
import {
ddescribe,
describe,
xdescribe,
it,
iit,
xit,
expect,
beforeEach,
afterEach,
AsyncTestCompleter,
inject
} from 'angular2/test_lib';
import {PromiseWrapper} from 'angular2/src/core/facade/async';
import {IS_DART} from '../platform';
import {evalModule} from './eval_module';
import {SourceModule} from 'angular2/src/compiler/api';
// This export is used by this test code
// when evaling the test module!
export var TEST_VALUE = 23;
export function main() {
describe('evalModule', () => {
it('should call the "run" function and allow to use imports', inject([AsyncTestCompleter], (async) => {
var moduleSource = IS_DART ? testDartModule : testJsModule;
var imports = [['angular2/test/compiler/eval_module_spec', 'testMod']];
evalModule(moduleSource, imports, [1]).then( (value) => {
expect(value).toEqual([1, 23]);
async.done();
});
}));
});
}
var testDartModule = `
run(data) {
data.add(testMod.TEST_VALUE);
return data;
}
`;
var testJsModule = `
exports.run = function(data) {
data.push(testMod.TEST_VALUE);
return data;
}
`;

View File

@ -6,7 +6,7 @@ import 'package:angular2/src/test_lib/test_lib.dart' show testSetup;
main() {
unit.filterStacks = true;
unit.formatStacks = false;
unit.unittestConfiguration.timeout = new Duration(milliseconds: 100);
unit.unittestConfiguration.timeout = new Duration(milliseconds: 1000);
_printWarnings();