Skip to content

Commit 2a126f7

Browse files
committed
feat(tests): add helper to eval a module
Needed later for unit tests for code gen and runtime code in angular#3605
1 parent 896add7 commit 2a126f7

File tree

4 files changed

+132
-1
lines changed

4 files changed

+132
-1
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import "dart:isolate";
2+
import "dart:async";
3+
4+
Uri toDartDataUri(String source) {
5+
return Uri.parse("data:application/dart;charset=utf-8,"
6+
"${Uri.encodeComponent(source)}");
7+
}
8+
9+
createIsolateSource(String moduleSource, List<List<String>> moduleImports) {
10+
var moduleSourceParts = [];
11+
moduleImports.forEach((sourceImport) {
12+
String modName = sourceImport[0];
13+
String modAlias = sourceImport[1];
14+
moduleSourceParts.add("import 'package:${modName}.dart' as ${modAlias};");
15+
});
16+
moduleSourceParts.add(moduleSource);
17+
18+
return """
19+
import "dart:isolate";
20+
import "${toDartDataUri(moduleSourceParts.join('\n'))}" as mut;
21+
22+
main(List args, SendPort replyPort) {
23+
replyPort.send(mut.run(args));
24+
}
25+
""";
26+
27+
}
28+
29+
dynamic callModule(dynamic data) { return data.map( (a) => a+1); }
30+
31+
evalModule(String moduleSource, List<List<String>> moduleImports, List args) {
32+
String source = createIsolateSource(moduleSource, moduleImports);
33+
Completer completer = new Completer();
34+
RawReceivePort receivePort;
35+
receivePort = new RawReceivePort( (message) {
36+
receivePort.close();
37+
completer.complete(message);
38+
});
39+
var packageRoot = Uri.parse('/packages');
40+
return Isolate.spawnUri(toDartDataUri(source), args, receivePort.sendPort, packageRoot: packageRoot).then( (isolate) {
41+
RawReceivePort errorPort;
42+
errorPort = new RawReceivePort( (message) {
43+
completer.completeError(message);
44+
});
45+
isolate.addErrorListener(errorPort.sendPort);
46+
return completer.future;
47+
});
48+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {Promise, PromiseWrapper} from 'angular2/src/core/facade/async';
2+
import {isPresent, global} from 'angular2/src/core/facade/lang';
3+
4+
var evalCounter = 0;
5+
6+
function nextModuleName() {
7+
return `evalScript${evalCounter++}`;
8+
}
9+
10+
export function evalModule(moduleSource: string, moduleImports: string[][], args: any[]):
11+
Promise<any> {
12+
var moduleName = nextModuleName();
13+
var moduleSourceWithImports = [];
14+
var importModuleNames = [];
15+
moduleImports.forEach(sourceImport => {
16+
var modName = sourceImport[0];
17+
var modAlias = sourceImport[1];
18+
importModuleNames.push(modName);
19+
moduleSourceWithImports.push(`var ${modAlias} = require('${modName}');`);
20+
});
21+
moduleSourceWithImports.push(moduleSource);
22+
23+
var moduleBody = new Function('require', 'exports', 'module', moduleSourceWithImports.join('\n'));
24+
var System = global['System'];
25+
if (isPresent(System) && isPresent(System.registerDynamic)) {
26+
System.registerDynamic(moduleName, importModuleNames, false, moduleBody);
27+
return <Promise<any>>System.import(moduleName).then((module) => module.run(args));
28+
} else {
29+
var exports = {};
30+
moduleBody(require, exports, {});
31+
return PromiseWrapper.resolve(exports['run'](args));
32+
}
33+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {
2+
ddescribe,
3+
describe,
4+
xdescribe,
5+
it,
6+
iit,
7+
xit,
8+
expect,
9+
beforeEach,
10+
afterEach,
11+
AsyncTestCompleter,
12+
inject
13+
} from 'angular2/test_lib';
14+
import {PromiseWrapper} from 'angular2/src/core/facade/async';
15+
import {IS_DART} from '../platform';
16+
17+
import {evalModule} from './eval_module';
18+
import {SourceModule} from 'angular2/src/compiler/api';
19+
20+
// This export is used by this test code
21+
// when evaling the test module!
22+
export var TEST_VALUE = 23;
23+
24+
export function main() {
25+
describe('evalModule', () => {
26+
it('should call the "run" function and allow to use imports', inject([AsyncTestCompleter], (async) => {
27+
var moduleSource = IS_DART ? testDartModule : testJsModule;
28+
var imports = [['angular2/test/compiler/eval_module_spec', 'testMod']];
29+
30+
evalModule(moduleSource, imports, [1]).then( (value) => {
31+
expect(value).toEqual([1, 23]);
32+
async.done();
33+
});
34+
}));
35+
});
36+
}
37+
38+
var testDartModule = `
39+
run(data) {
40+
data.add(testMod.TEST_VALUE);
41+
return data;
42+
}
43+
`;
44+
45+
var testJsModule = `
46+
exports.run = function(data) {
47+
data.push(testMod.TEST_VALUE);
48+
return data;
49+
}
50+
`;

test-main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'package:angular2/src/test_lib/test_lib.dart' show testSetup;
66
main() {
77
unit.filterStacks = true;
88
unit.formatStacks = false;
9-
unit.unittestConfiguration.timeout = new Duration(milliseconds: 100);
9+
unit.unittestConfiguration.timeout = new Duration(milliseconds: 1000);
1010

1111
_printWarnings();
1212

0 commit comments

Comments
 (0)