forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tests): add helper to eval a module
Needed later for unit tests for code gen and runtime code in angular#3605
- Loading branch information
Showing
4 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters