From 2a126f72f31610a645a39ca40b6717179d5c97dc Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Fri, 28 Aug 2015 11:45:39 -0700 Subject: [PATCH] feat(tests): add helper to eval a module Needed later for unit tests for code gen and runtime code in #3605 --- .../angular2/test/compiler/eval_module.dart | 48 ++++++++++++++++++ modules/angular2/test/compiler/eval_module.ts | 33 ++++++++++++ .../test/compiler/eval_module_spec.ts | 50 +++++++++++++++++++ test-main.dart | 2 +- 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 modules/angular2/test/compiler/eval_module.dart create mode 100644 modules/angular2/test/compiler/eval_module.ts create mode 100644 modules/angular2/test/compiler/eval_module_spec.ts diff --git a/modules/angular2/test/compiler/eval_module.dart b/modules/angular2/test/compiler/eval_module.dart new file mode 100644 index 0000000000..e555b2b5ee --- /dev/null +++ b/modules/angular2/test/compiler/eval_module.dart @@ -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> 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> 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; + }); +} diff --git a/modules/angular2/test/compiler/eval_module.ts b/modules/angular2/test/compiler/eval_module.ts new file mode 100644 index 0000000000..cf5adac986 --- /dev/null +++ b/modules/angular2/test/compiler/eval_module.ts @@ -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 { + 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 >System.import(moduleName).then((module) => module.run(args)); + } else { + var exports = {}; + moduleBody(require, exports, {}); + return PromiseWrapper.resolve(exports['run'](args)); + } +} diff --git a/modules/angular2/test/compiler/eval_module_spec.ts b/modules/angular2/test/compiler/eval_module_spec.ts new file mode 100644 index 0000000000..040264d089 --- /dev/null +++ b/modules/angular2/test/compiler/eval_module_spec.ts @@ -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; + } +`; \ No newline at end of file diff --git a/test-main.dart b/test-main.dart index 0726d474bb..aca624b5c7 100644 --- a/test-main.dart +++ b/test-main.dart @@ -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();