refactor(view_compiler): codegen DI and Queries
BREAKING CHANGE: - Renderer: * renderComponent method is removed form `Renderer`, only present on `RootRenderer` * Renderer.setDebugInfo is removed. Renderer.createElement / createText / createTemplateAnchor now take the DebugInfo directly. - Query semantics: * Queries don't work with dynamically loaded components. * e.g. for router-outlet: loaded components can't be queries via @ViewQuery, but router-outlet emits an event `activate` now that emits the activated component - Exception classes and the context inside changed (renamed fields) - DebugElement.attributes is an Object and not a Map in JS any more - ChangeDetectorGenConfig was renamed into CompilerConfig - AppViewManager.createEmbeddedViewInContainer / AppViewManager.createHostViewInContainer are removed, use the methods in ViewContainerRef instead - Change detection order changed: * 1. dirty check component inputs * 2. dirty check content children * 3. update render nodes Closes #6301 Closes #6567
This commit is contained in:
@ -0,0 +1,38 @@
|
||||
import {
|
||||
AsyncTestCompleter,
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
describe,
|
||||
el,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
it,
|
||||
xit,
|
||||
TestComponentBuilder
|
||||
} from 'angular2/testing_internal';
|
||||
|
||||
import {escapeSingleQuoteString} from 'angular2/src/compiler/output/abstract_emitter';
|
||||
|
||||
export function main() {
|
||||
describe('AbstractEmitter', () => {
|
||||
describe('escapeSingleQuoteString', () => {
|
||||
it('should escape single quotes',
|
||||
() => { expect(escapeSingleQuoteString(`'`, false)).toEqual(`'\\''`); });
|
||||
|
||||
it('should escape backslash',
|
||||
() => { expect(escapeSingleQuoteString('\\', false)).toEqual(`'\\\\'`); });
|
||||
|
||||
it('should escape newlines',
|
||||
() => { expect(escapeSingleQuoteString('\n', false)).toEqual(`'\\n'`); });
|
||||
|
||||
it('should escape carriage returns',
|
||||
() => { expect(escapeSingleQuoteString('\r', false)).toEqual(`'\\r'`); });
|
||||
|
||||
it('should escape $', () => { expect(escapeSingleQuoteString('$', true)).toEqual(`'\\$'`); });
|
||||
it('should not escape $',
|
||||
() => { expect(escapeSingleQuoteString('$', false)).toEqual(`'$'`); });
|
||||
});
|
||||
|
||||
});
|
||||
}
|
310
modules/angular2/test/compiler/output/dart_emitter_spec.ts
Normal file
310
modules/angular2/test/compiler/output/dart_emitter_spec.ts
Normal file
@ -0,0 +1,310 @@
|
||||
import {
|
||||
AsyncTestCompleter,
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
describe,
|
||||
el,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
it,
|
||||
xit,
|
||||
TestComponentBuilder
|
||||
} from 'angular2/testing_internal';
|
||||
|
||||
import {isBlank} from 'angular2/src/facade/lang';
|
||||
import {DartEmitter} from 'angular2/src/compiler/output/dart_emitter';
|
||||
import {CompileIdentifierMetadata} from 'angular2/src/compiler/compile_metadata';
|
||||
import * as o from 'angular2/src/compiler/output/output_ast';
|
||||
|
||||
var someModuleUrl = 'asset:somePackage/lib/somePath';
|
||||
var anotherModuleUrl = 'asset:somePackage/lib/someOtherPath';
|
||||
|
||||
var sameModuleIdentifier =
|
||||
new CompileIdentifierMetadata({name: 'someLocalId', moduleUrl: someModuleUrl});
|
||||
|
||||
var externalModuleIdentifier =
|
||||
new CompileIdentifierMetadata({name: 'someExternalId', moduleUrl: anotherModuleUrl});
|
||||
|
||||
export function main() {
|
||||
// Not supported features of our OutputAst in Dart:
|
||||
// - declaring what should be exported via a special statement like `export`.
|
||||
// Dart exports everything that has no `_` in its name.
|
||||
// - declaring private fields via a statement like `private`.
|
||||
// Dart exports everything that has no `_` in its name.
|
||||
// - return types for function expressions
|
||||
|
||||
describe('DartEmitter', () => {
|
||||
var emitter: DartEmitter;
|
||||
var someVar: o.ReadVarExpr;
|
||||
|
||||
beforeEach(() => {
|
||||
emitter = new DartEmitter();
|
||||
someVar = o.variable('someVar');
|
||||
});
|
||||
|
||||
function emitStmt(stmt: o.Statement, exportedVars: string[] = null): string {
|
||||
if (isBlank(exportedVars)) {
|
||||
exportedVars = [];
|
||||
}
|
||||
return emitter.emitStatements(someModuleUrl, [stmt], exportedVars);
|
||||
}
|
||||
|
||||
it('should declare variables', () => {
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt())).toEqual(`var someVar = 1;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(null, [o.StmtModifier.Final])))
|
||||
.toEqual(`final someVar = 1;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1, new o.BuiltinType(o.BuiltinTypeName.Int,
|
||||
[o.TypeModifier.Const])))
|
||||
.toDeclStmt(null, [o.StmtModifier.Final])))
|
||||
.toEqual(`const int someVar = 1;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(), ['someVar']))
|
||||
.toEqual(`var someVar = 1;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(o.INT_TYPE)))
|
||||
.toEqual(`int someVar = 1;`);
|
||||
});
|
||||
|
||||
it('should read and write variables', () => {
|
||||
expect(emitStmt(someVar.toStmt())).toEqual(`someVar;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toStmt())).toEqual(`someVar = 1;`);
|
||||
expect(emitStmt(someVar.set(o.variable('someOtherVar').set(o.literal(1))).toStmt()))
|
||||
.toEqual(`someVar = (someOtherVar = 1);`);
|
||||
});
|
||||
|
||||
it('should read and write keys', () => {
|
||||
expect(emitStmt(o.variable('someMap').key(o.variable('someKey')).toStmt()))
|
||||
.toEqual(`someMap[someKey];`);
|
||||
expect(emitStmt(o.variable('someMap').key(o.variable('someKey')).set(o.literal(1)).toStmt()))
|
||||
.toEqual(`someMap[someKey] = 1;`);
|
||||
});
|
||||
|
||||
it('should read and write properties', () => {
|
||||
expect(emitStmt(o.variable('someObj').prop('someProp').toStmt()))
|
||||
.toEqual(`someObj.someProp;`);
|
||||
expect(emitStmt(o.variable('someObj').prop('someProp').set(o.literal(1)).toStmt()))
|
||||
.toEqual(`someObj.someProp = 1;`);
|
||||
});
|
||||
|
||||
it('should invoke functions and methods and constructors', () => {
|
||||
expect(emitStmt(o.variable('someFn').callFn([o.literal(1)]).toStmt())).toEqual('someFn(1);');
|
||||
expect(emitStmt(o.variable('someObj').callMethod('someMethod', [o.literal(1)]).toStmt()))
|
||||
.toEqual('someObj.someMethod(1);');
|
||||
expect(emitStmt(o.variable('SomeClass').instantiate([o.literal(1)]).toStmt()))
|
||||
.toEqual('new SomeClass(1);');
|
||||
});
|
||||
|
||||
it('should support builtin methods', () => {
|
||||
expect(emitStmt(o.variable('arr1')
|
||||
.callMethod(o.BuiltinMethod.ConcatArray, [o.variable('arr2')])
|
||||
.toStmt()))
|
||||
.toEqual('arr1..addAll(arr2);');
|
||||
|
||||
expect(emitStmt(o.variable('observable')
|
||||
.callMethod(o.BuiltinMethod.SubscribeObservable, [o.variable('listener')])
|
||||
.toStmt()))
|
||||
.toEqual('observable.listen(listener);');
|
||||
});
|
||||
|
||||
it('should support literals', () => {
|
||||
expect(emitStmt(o.literal(0).toStmt())).toEqual('0;');
|
||||
expect(emitStmt(o.literal(true).toStmt())).toEqual('true;');
|
||||
expect(emitStmt(o.literal('someStr').toStmt())).toEqual(`'someStr';`);
|
||||
expect(emitStmt(o.literal('$a').toStmt())).toEqual(`'\\$a';`);
|
||||
expect(emitStmt(o.literalArr([o.literal(1)]).toStmt())).toEqual(`[1];`);
|
||||
expect(emitStmt(o.literalMap([['someKey', o.literal(1)]]).toStmt()))
|
||||
.toEqual(`{'someKey': 1};`);
|
||||
expect(emitStmt(
|
||||
o.literalMap([['someKey', o.literal(1)]], new o.MapType(o.NUMBER_TYPE)).toStmt()))
|
||||
.toEqual(`<String, num>{'someKey': 1};`);
|
||||
});
|
||||
|
||||
it('should support external identifiers', () => {
|
||||
expect(emitStmt(o.importExpr(sameModuleIdentifier).toStmt())).toEqual('someLocalId;');
|
||||
expect(emitStmt(o.importExpr(externalModuleIdentifier).toStmt()))
|
||||
.toEqual([`import 'someOtherPath' as import0;`, `import0.someExternalId;`].join('\n'));
|
||||
});
|
||||
|
||||
it('should support operators', () => {
|
||||
var lhs = o.variable('lhs');
|
||||
var rhs = o.variable('rhs');
|
||||
expect(emitStmt(someVar.cast(o.INT_TYPE).toStmt())).toEqual('(someVar as int);');
|
||||
expect(emitStmt(o.not(someVar).toStmt())).toEqual('!someVar;');
|
||||
expect(
|
||||
emitStmt(someVar.conditional(o.variable('trueCase'), o.variable('falseCase')).toStmt()))
|
||||
.toEqual('someVar? trueCase: falseCase;');
|
||||
|
||||
expect(emitStmt(lhs.equals(rhs).toStmt())).toEqual('(lhs == rhs);');
|
||||
expect(emitStmt(lhs.notEquals(rhs).toStmt())).toEqual('(lhs != rhs);');
|
||||
expect(emitStmt(lhs.identical(rhs).toStmt())).toEqual('identical(lhs, rhs);');
|
||||
expect(emitStmt(lhs.notIdentical(rhs).toStmt())).toEqual('!identical(lhs, rhs);');
|
||||
expect(emitStmt(lhs.minus(rhs).toStmt())).toEqual('(lhs - rhs);');
|
||||
expect(emitStmt(lhs.plus(rhs).toStmt())).toEqual('(lhs + rhs);');
|
||||
expect(emitStmt(lhs.divide(rhs).toStmt())).toEqual('(lhs / rhs);');
|
||||
expect(emitStmt(lhs.multiply(rhs).toStmt())).toEqual('(lhs * rhs);');
|
||||
expect(emitStmt(lhs.modulo(rhs).toStmt())).toEqual('(lhs % rhs);');
|
||||
expect(emitStmt(lhs.and(rhs).toStmt())).toEqual('(lhs && rhs);');
|
||||
expect(emitStmt(lhs.or(rhs).toStmt())).toEqual('(lhs || rhs);');
|
||||
expect(emitStmt(lhs.lower(rhs).toStmt())).toEqual('(lhs < rhs);');
|
||||
expect(emitStmt(lhs.lowerEquals(rhs).toStmt())).toEqual('(lhs <= rhs);');
|
||||
expect(emitStmt(lhs.bigger(rhs).toStmt())).toEqual('(lhs > rhs);');
|
||||
expect(emitStmt(lhs.biggerEquals(rhs).toStmt())).toEqual('(lhs >= rhs);');
|
||||
});
|
||||
|
||||
it('should support function expressions', () => {
|
||||
expect(emitStmt(o.fn([], []).toStmt())).toEqual(['() {', '};'].join('\n'));
|
||||
expect(emitStmt(o.fn([new o.FnParam('param1', o.INT_TYPE)], []).toStmt()))
|
||||
.toEqual(['(int param1) {', '};'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support function statements', () => {
|
||||
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], [])))
|
||||
.toEqual(['void someFn() {', '}'].join('\n'));
|
||||
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], [new o.ReturnStatement(o.literal(1))],
|
||||
o.INT_TYPE)))
|
||||
.toEqual(['int someFn() {', ' return 1;', '}'].join('\n'));
|
||||
expect(
|
||||
emitStmt(new o.DeclareFunctionStmt('someFn', [new o.FnParam('param1', o.INT_TYPE)], [])))
|
||||
.toEqual(['void someFn(int param1) {', '}'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support comments',
|
||||
() => { expect(emitStmt(new o.CommentStmt('a\nb'))).toEqual(['// a', '// b'].join('\n')); });
|
||||
|
||||
it('should support if stmt', () => {
|
||||
var trueCase = o.variable('trueCase').callFn([]).toStmt();
|
||||
var falseCase = o.variable('falseCase').callFn([]).toStmt();
|
||||
expect(emitStmt(new o.IfStmt(o.variable('cond'), [trueCase])))
|
||||
.toEqual(['if (cond) { trueCase(); }'].join('\n'));
|
||||
expect(emitStmt(new o.IfStmt(o.variable('cond'), [trueCase], [falseCase])))
|
||||
.toEqual(['if (cond) {', ' trueCase();', '} else {', ' falseCase();', '}'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support try/catch', () => {
|
||||
var bodyStmt = o.variable('body').callFn([]).toStmt();
|
||||
var catchStmt = o.variable('catchFn').callFn([o.CATCH_ERROR_VAR, o.CATCH_STACK_VAR]).toStmt();
|
||||
expect(emitStmt(new o.TryCatchStmt([bodyStmt], [catchStmt])))
|
||||
.toEqual(
|
||||
['try {', ' body();', '} catch (error, stack) {', ' catchFn(error,stack);', '}']
|
||||
.join('\n'));
|
||||
});
|
||||
|
||||
it('should support support throwing',
|
||||
() => { expect(emitStmt(new o.ThrowStmt(someVar))).toEqual('throw someVar;'); });
|
||||
|
||||
describe('classes', () => {
|
||||
var callSomeMethod: o.Statement;
|
||||
|
||||
beforeEach(() => { callSomeMethod = o.THIS_EXPR.callMethod('someMethod', []).toStmt(); });
|
||||
|
||||
it('should support declaring classes', () => {
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null, [])))
|
||||
.toEqual(['class SomeClass {', '}'].join('\n'));
|
||||
expect(
|
||||
emitStmt(new o.ClassStmt('SomeClass', o.variable('SomeSuperClass'), [], [], null, [])))
|
||||
.toEqual(['class SomeClass extends SomeSuperClass {', '}'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support declaring constructors', () => {
|
||||
var superCall = o.SUPER_EXPR.callFn([o.variable('someParam')]).toStmt();
|
||||
expect(emitStmt(
|
||||
new o.ClassStmt('SomeClass', null, [], [], new o.ClassMethod(null, [], []), [])))
|
||||
.toEqual(['class SomeClass {', ' SomeClass() {', ' }', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [],
|
||||
new o.ClassMethod(null, [new o.FnParam('someParam', o.INT_TYPE)], []), [])))
|
||||
.toEqual(['class SomeClass {', ' SomeClass(int someParam) {', ' }', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [],
|
||||
new o.ClassMethod(null, [], [superCall]), [])))
|
||||
.toEqual(
|
||||
['class SomeClass {', ' SomeClass(): super(someParam) {', ' }', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [],
|
||||
new o.ClassMethod(null, [], [callSomeMethod]), [])))
|
||||
.toEqual(
|
||||
['class SomeClass {', ' SomeClass() {', ' this.someMethod();', ' }', '}'].join(
|
||||
'\n'));
|
||||
});
|
||||
|
||||
it('should support declaring fields', () => {
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [new o.ClassField('someField')], [],
|
||||
null, [])))
|
||||
.toEqual(['class SomeClass {', ' var someField;', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null,
|
||||
[new o.ClassField('someField', o.INT_TYPE)], [], null, [])))
|
||||
.toEqual(['class SomeClass {', ' int someField;', '}'].join('\n'));
|
||||
expect(
|
||||
emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null,
|
||||
[new o.ClassField('someField', o.INT_TYPE, [o.StmtModifier.Final])], [], null, [])))
|
||||
.toEqual(['class SomeClass {', ' final int someField;', '}'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support declaring getters', () => {
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [],
|
||||
[new o.ClassGetter('someGetter', [])], null, [])))
|
||||
.toEqual(['class SomeClass {', ' get someGetter {', ' }', '}'].join('\n'));
|
||||
expect(
|
||||
emitStmt(new o.ClassStmt('SomeClass', null, [],
|
||||
[new o.ClassGetter('someGetter', [], o.INT_TYPE)], null, [])))
|
||||
.toEqual(['class SomeClass {', ' int get someGetter {', ' }', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [],
|
||||
[new o.ClassGetter('someGetter', [callSomeMethod])], null,
|
||||
[])))
|
||||
.toEqual(
|
||||
['class SomeClass {', ' get someGetter {', ' this.someMethod();', ' }', '}']
|
||||
.join('\n'));
|
||||
});
|
||||
|
||||
it('should support methods', () => {
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null,
|
||||
[new o.ClassMethod('someMethod', [], [])])))
|
||||
.toEqual(['class SomeClass {', ' void someMethod() {', ' }', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null,
|
||||
[new o.ClassMethod('someMethod', [], [], o.INT_TYPE)])))
|
||||
.toEqual(['class SomeClass {', ' int someMethod() {', ' }', '}'].join('\n'));
|
||||
expect(
|
||||
emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [], null,
|
||||
[new o.ClassMethod('someMethod', [new o.FnParam('someParam', o.INT_TYPE)], [])])))
|
||||
.toEqual(
|
||||
['class SomeClass {', ' void someMethod(int someParam) {', ' }', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null,
|
||||
[new o.ClassMethod('someMethod', [], [callSomeMethod])])))
|
||||
.toEqual(
|
||||
['class SomeClass {', ' void someMethod() {', ' this.someMethod();', ' }', '}']
|
||||
.join('\n'));
|
||||
});
|
||||
});
|
||||
|
||||
it('should support builtin types', () => {
|
||||
var writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.DYNAMIC_TYPE))).toEqual('dynamic a = null;');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.BOOL_TYPE))).toEqual('bool a = null;');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.INT_TYPE))).toEqual('int a = null;');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.NUMBER_TYPE))).toEqual('num a = null;');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.STRING_TYPE))).toEqual('String a = null;');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.FUNCTION_TYPE))).toEqual('Function a = null;');
|
||||
});
|
||||
|
||||
it('should support external types', () => {
|
||||
var writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.importType(sameModuleIdentifier))))
|
||||
.toEqual('someLocalId a = null;');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.importType(externalModuleIdentifier))))
|
||||
.toEqual([`import 'someOtherPath' as import0;`, `import0.someExternalId a = null;`].join(
|
||||
'\n'));
|
||||
});
|
||||
|
||||
it('should support combined types', () => {
|
||||
var writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(new o.ArrayType(null))))
|
||||
.toEqual('List<dynamic> a = null;');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(new o.ArrayType(o.INT_TYPE))))
|
||||
.toEqual('List<int> a = null;');
|
||||
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(new o.MapType(null))))
|
||||
.toEqual('Map<String, dynamic> a = null;');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(new o.MapType(o.INT_TYPE))))
|
||||
.toEqual('Map<String, int> a = null;');
|
||||
});
|
||||
});
|
||||
}
|
296
modules/angular2/test/compiler/output/js_emitter_spec.ts
Normal file
296
modules/angular2/test/compiler/output/js_emitter_spec.ts
Normal file
@ -0,0 +1,296 @@
|
||||
import {
|
||||
AsyncTestCompleter,
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
describe,
|
||||
el,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
it,
|
||||
xit,
|
||||
TestComponentBuilder
|
||||
} from 'angular2/testing_internal';
|
||||
|
||||
import {isBlank} from 'angular2/src/facade/lang';
|
||||
import {JavaScriptEmitter} from 'angular2/src/compiler/output/js_emitter';
|
||||
import {CompileIdentifierMetadata} from 'angular2/src/compiler/compile_metadata';
|
||||
import * as o from 'angular2/src/compiler/output/output_ast';
|
||||
|
||||
var someModuleUrl = 'asset:somePackage/lib/somePath';
|
||||
var anotherModuleUrl = 'asset:somePackage/lib/someOtherPath';
|
||||
|
||||
var sameModuleIdentifier =
|
||||
new CompileIdentifierMetadata({name: 'someLocalId', moduleUrl: someModuleUrl});
|
||||
|
||||
var externalModuleIdentifier =
|
||||
new CompileIdentifierMetadata({name: 'someExternalId', moduleUrl: anotherModuleUrl});
|
||||
|
||||
export function main() {
|
||||
// Note supported features of our OutputAstin JavaScript / ES5:
|
||||
// - types
|
||||
// - declaring fields
|
||||
|
||||
describe('JavaScriptEmitter', () => {
|
||||
var emitter: JavaScriptEmitter;
|
||||
var someVar: o.ReadVarExpr;
|
||||
|
||||
beforeEach(() => {
|
||||
emitter = new JavaScriptEmitter();
|
||||
someVar = o.variable('someVar');
|
||||
});
|
||||
|
||||
function emitStmt(stmt: o.Statement, exportedVars: string[] = null): string {
|
||||
if (isBlank(exportedVars)) {
|
||||
exportedVars = [];
|
||||
}
|
||||
return emitter.emitStatements(someModuleUrl, [stmt], exportedVars);
|
||||
}
|
||||
|
||||
it('should declare variables', () => {
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt())).toEqual(`var someVar = 1;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(), ['someVar']))
|
||||
.toEqual([
|
||||
'var someVar = 1;',
|
||||
`Object.defineProperty(exports, 'someVar', { get: function() { return someVar; }});`
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should read and write variables', () => {
|
||||
expect(emitStmt(someVar.toStmt())).toEqual(`someVar;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toStmt())).toEqual(`someVar = 1;`);
|
||||
expect(emitStmt(someVar.set(o.variable('someOtherVar').set(o.literal(1))).toStmt()))
|
||||
.toEqual(`someVar = (someOtherVar = 1);`);
|
||||
});
|
||||
|
||||
it('should read and write keys', () => {
|
||||
expect(emitStmt(o.variable('someMap').key(o.variable('someKey')).toStmt()))
|
||||
.toEqual(`someMap[someKey];`);
|
||||
expect(emitStmt(o.variable('someMap').key(o.variable('someKey')).set(o.literal(1)).toStmt()))
|
||||
.toEqual(`someMap[someKey] = 1;`);
|
||||
});
|
||||
|
||||
it('should read and write properties', () => {
|
||||
expect(emitStmt(o.variable('someObj').prop('someProp').toStmt()))
|
||||
.toEqual(`someObj.someProp;`);
|
||||
expect(emitStmt(o.variable('someObj').prop('someProp').set(o.literal(1)).toStmt()))
|
||||
.toEqual(`someObj.someProp = 1;`);
|
||||
});
|
||||
|
||||
it('should invoke functions and methods and constructors', () => {
|
||||
expect(emitStmt(o.variable('someFn').callFn([o.literal(1)]).toStmt())).toEqual('someFn(1);');
|
||||
expect(emitStmt(o.variable('someObj').callMethod('someMethod', [o.literal(1)]).toStmt()))
|
||||
.toEqual('someObj.someMethod(1);');
|
||||
expect(emitStmt(o.variable('SomeClass').instantiate([o.literal(1)]).toStmt()))
|
||||
.toEqual('new SomeClass(1);');
|
||||
});
|
||||
|
||||
it('should support builtin methods', () => {
|
||||
expect(emitStmt(o.variable('arr1')
|
||||
.callMethod(o.BuiltinMethod.ConcatArray, [o.variable('arr2')])
|
||||
.toStmt()))
|
||||
.toEqual('arr1.concat(arr2);');
|
||||
|
||||
expect(emitStmt(o.variable('observable')
|
||||
.callMethod(o.BuiltinMethod.SubscribeObservable, [o.variable('listener')])
|
||||
.toStmt()))
|
||||
.toEqual('observable.subscribe(listener);');
|
||||
});
|
||||
|
||||
it('should support literals', () => {
|
||||
expect(emitStmt(o.literal(0).toStmt())).toEqual('0;');
|
||||
expect(emitStmt(o.literal(true).toStmt())).toEqual('true;');
|
||||
expect(emitStmt(o.literal('someStr').toStmt())).toEqual(`'someStr';`);
|
||||
expect(emitStmt(o.literalArr([o.literal(1)]).toStmt())).toEqual(`[1];`);
|
||||
expect(emitStmt(o.literalMap([['someKey', o.literal(1)]]).toStmt()))
|
||||
.toEqual(`{'someKey': 1};`);
|
||||
});
|
||||
|
||||
it('should support external identifiers', () => {
|
||||
expect(emitStmt(o.importExpr(sameModuleIdentifier).toStmt())).toEqual('someLocalId;');
|
||||
expect(emitStmt(o.importExpr(externalModuleIdentifier).toStmt()))
|
||||
.toEqual(
|
||||
[`var import0 = re` + `quire('./someOtherPath');`, `import0.someExternalId;`].join(
|
||||
'\n'));
|
||||
});
|
||||
|
||||
it('should support operators', () => {
|
||||
var lhs = o.variable('lhs');
|
||||
var rhs = o.variable('rhs');
|
||||
expect(emitStmt(o.not(someVar).toStmt())).toEqual('!someVar;');
|
||||
expect(
|
||||
emitStmt(someVar.conditional(o.variable('trueCase'), o.variable('falseCase')).toStmt()))
|
||||
.toEqual('someVar? trueCase: falseCase;');
|
||||
|
||||
expect(emitStmt(lhs.equals(rhs).toStmt())).toEqual('(lhs == rhs);');
|
||||
expect(emitStmt(lhs.notEquals(rhs).toStmt())).toEqual('(lhs != rhs);');
|
||||
expect(emitStmt(lhs.identical(rhs).toStmt())).toEqual('(lhs === rhs);');
|
||||
expect(emitStmt(lhs.notIdentical(rhs).toStmt())).toEqual('(lhs !== rhs);');
|
||||
expect(emitStmt(lhs.minus(rhs).toStmt())).toEqual('(lhs - rhs);');
|
||||
expect(emitStmt(lhs.plus(rhs).toStmt())).toEqual('(lhs + rhs);');
|
||||
expect(emitStmt(lhs.divide(rhs).toStmt())).toEqual('(lhs / rhs);');
|
||||
expect(emitStmt(lhs.multiply(rhs).toStmt())).toEqual('(lhs * rhs);');
|
||||
expect(emitStmt(lhs.modulo(rhs).toStmt())).toEqual('(lhs % rhs);');
|
||||
expect(emitStmt(lhs.and(rhs).toStmt())).toEqual('(lhs && rhs);');
|
||||
expect(emitStmt(lhs.or(rhs).toStmt())).toEqual('(lhs || rhs);');
|
||||
expect(emitStmt(lhs.lower(rhs).toStmt())).toEqual('(lhs < rhs);');
|
||||
expect(emitStmt(lhs.lowerEquals(rhs).toStmt())).toEqual('(lhs <= rhs);');
|
||||
expect(emitStmt(lhs.bigger(rhs).toStmt())).toEqual('(lhs > rhs);');
|
||||
expect(emitStmt(lhs.biggerEquals(rhs).toStmt())).toEqual('(lhs >= rhs);');
|
||||
});
|
||||
|
||||
it('should support function expressions', () => {
|
||||
expect(emitStmt(o.fn([], []).toStmt())).toEqual(['function() {', '};'].join('\n'));
|
||||
expect(emitStmt(o.fn([], [new o.ReturnStatement(o.literal(1))]).toStmt()))
|
||||
.toEqual(['function() {', ' return 1;\n};'].join('\n'));
|
||||
expect(emitStmt(o.fn([new o.FnParam('param1')], []).toStmt()))
|
||||
.toEqual(['function(param1) {', '};'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support function statements', () => {
|
||||
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], [])))
|
||||
.toEqual(['function someFn() {', '}'].join('\n'));
|
||||
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], []), ['someFn']))
|
||||
.toEqual([
|
||||
'function someFn() {',
|
||||
'}',
|
||||
`Object.defineProperty(exports, 'someFn', { get: function() { return someFn; }});`
|
||||
].join('\n'));
|
||||
expect(
|
||||
emitStmt(new o.DeclareFunctionStmt('someFn', [], [new o.ReturnStatement(o.literal(1))])))
|
||||
.toEqual(['function someFn() {', ' return 1;', '}'].join('\n'));
|
||||
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [new o.FnParam('param1')], [])))
|
||||
.toEqual(['function someFn(param1) {', '}'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support comments',
|
||||
() => { expect(emitStmt(new o.CommentStmt('a\nb'))).toEqual(['// a', '// b'].join('\n')); });
|
||||
|
||||
it('should support if stmt', () => {
|
||||
var trueCase = o.variable('trueCase').callFn([]).toStmt();
|
||||
var falseCase = o.variable('falseCase').callFn([]).toStmt();
|
||||
expect(emitStmt(new o.IfStmt(o.variable('cond'), [trueCase])))
|
||||
.toEqual(['if (cond) { trueCase(); }'].join('\n'));
|
||||
expect(emitStmt(new o.IfStmt(o.variable('cond'), [trueCase], [falseCase])))
|
||||
.toEqual(['if (cond) {', ' trueCase();', '} else {', ' falseCase();', '}'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support try/catch', () => {
|
||||
var bodyStmt = o.variable('body').callFn([]).toStmt();
|
||||
var catchStmt = o.variable('catchFn').callFn([o.CATCH_ERROR_VAR, o.CATCH_STACK_VAR]).toStmt();
|
||||
expect(emitStmt(new o.TryCatchStmt([bodyStmt], [catchStmt])))
|
||||
.toEqual([
|
||||
'try {',
|
||||
' body();',
|
||||
'} catch (error) {',
|
||||
' var stack = error.stack;',
|
||||
' catchFn(error,stack);',
|
||||
'}'
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support support throwing',
|
||||
() => { expect(emitStmt(new o.ThrowStmt(someVar))).toEqual('throw someVar;'); });
|
||||
|
||||
describe('classes', () => {
|
||||
var callSomeMethod: o.Statement;
|
||||
|
||||
beforeEach(() => { callSomeMethod = o.THIS_EXPR.callMethod('someMethod', []).toStmt(); });
|
||||
|
||||
it('should support declaring classes', () => {
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null, [])))
|
||||
.toEqual(['function SomeClass() {', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null, []), ['SomeClass']))
|
||||
.toEqual([
|
||||
'function SomeClass() {',
|
||||
'}',
|
||||
`Object.defineProperty(exports, 'SomeClass', { get: function() { return SomeClass; }});`
|
||||
].join('\n'));
|
||||
expect(
|
||||
emitStmt(new o.ClassStmt('SomeClass', o.variable('SomeSuperClass'), [], [], null, [])))
|
||||
.toEqual([
|
||||
'function SomeClass() {',
|
||||
'}',
|
||||
'SomeClass.prototype = Object.create(SomeSuperClass.prototype);'
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support declaring constructors', () => {
|
||||
var superCall = o.SUPER_EXPR.callFn([o.variable('someParam')]).toStmt();
|
||||
expect(emitStmt(
|
||||
new o.ClassStmt('SomeClass', null, [], [], new o.ClassMethod(null, [], []), [])))
|
||||
.toEqual(['function SomeClass() {', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [],
|
||||
new o.ClassMethod(null, [new o.FnParam('someParam')], []),
|
||||
[])))
|
||||
.toEqual(['function SomeClass(someParam) {', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', o.variable('SomeSuperClass'), [], [],
|
||||
new o.ClassMethod(null, [], [superCall]), [])))
|
||||
.toEqual([
|
||||
'function SomeClass() {',
|
||||
' var self = this;',
|
||||
' SomeSuperClass.call(this, someParam);',
|
||||
'}',
|
||||
'SomeClass.prototype = Object.create(SomeSuperClass.prototype);'
|
||||
].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [],
|
||||
new o.ClassMethod(null, [], [callSomeMethod]), [])))
|
||||
.toEqual(
|
||||
['function SomeClass() {', ' var self = this;', ' self.someMethod();', '}'].join(
|
||||
'\n'));
|
||||
});
|
||||
|
||||
it('should support declaring getters', () => {
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [],
|
||||
[new o.ClassGetter('someGetter', [])], null, [])))
|
||||
.toEqual([
|
||||
'function SomeClass() {',
|
||||
'}',
|
||||
`Object.defineProperty(SomeClass.prototype, 'someGetter', { get: function() {`,
|
||||
`}});`
|
||||
].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [],
|
||||
[new o.ClassGetter('someGetter', [callSomeMethod])], null,
|
||||
[])))
|
||||
.toEqual([
|
||||
'function SomeClass() {',
|
||||
'}',
|
||||
`Object.defineProperty(SomeClass.prototype, 'someGetter', { get: function() {`,
|
||||
` var self = this;`,
|
||||
` self.someMethod();`,
|
||||
`}});`
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support methods', () => {
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null,
|
||||
[new o.ClassMethod('someMethod', [], [])])))
|
||||
.toEqual([
|
||||
'function SomeClass() {',
|
||||
'}',
|
||||
'SomeClass.prototype.someMethod = function() {',
|
||||
'};'
|
||||
].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [], null,
|
||||
[new o.ClassMethod('someMethod', [new o.FnParam('someParam')], [])])))
|
||||
.toEqual([
|
||||
'function SomeClass() {',
|
||||
'}',
|
||||
'SomeClass.prototype.someMethod = function(someParam) {',
|
||||
'};'
|
||||
].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null,
|
||||
[new o.ClassMethod('someMethod', [], [callSomeMethod])])))
|
||||
.toEqual([
|
||||
'function SomeClass() {',
|
||||
'}',
|
||||
'SomeClass.prototype.someMethod = function() {',
|
||||
' var self = this;',
|
||||
' self.someMethod();',
|
||||
'};'
|
||||
].join('\n'));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
// ATTENTION: This file will be overwritten with generated code by main()
|
||||
import {print, IS_DART} from 'angular2/src/facade/lang';
|
||||
import {unimplemented} from 'angular2/src/facade/exceptions';
|
||||
import {codegenExportsVars, codegenStmts} from './output_emitter_util';
|
||||
import {TypeScriptEmitter} from 'angular2/src/compiler/output/ts_emitter';
|
||||
import {DartEmitter} from 'angular2/src/compiler/output/dart_emitter';
|
||||
import * as o from 'angular2/src/compiler/output/output_ast';
|
||||
|
||||
export function getExpressions(): any {
|
||||
return unimplemented();
|
||||
}
|
||||
|
||||
// Generator
|
||||
export function main(args: string[]) {
|
||||
var emitter = IS_DART ? new DartEmitter() : new TypeScriptEmitter();
|
||||
var emittedCode =
|
||||
emitter.emitStatements('asset:angular2/test/compiler/output/output_emitter_codegen_typed',
|
||||
codegenStmts, codegenExportsVars);
|
||||
// debug: console.error(emittedCode);
|
||||
print(emittedCode);
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
// ATTENTION: This file will be overwritten with generated code by main()
|
||||
import {print} from 'angular2/src/facade/lang';
|
||||
import {unimplemented} from 'angular2/src/facade/exceptions';
|
||||
import {codegenExportsVars, codegenStmts} from './output_emitter_util';
|
||||
import {JavaScriptEmitter} from 'angular2/src/compiler/output/js_emitter';
|
||||
|
||||
export function getExpressions(): any {
|
||||
return unimplemented();
|
||||
}
|
||||
|
||||
// Generator
|
||||
export function main(args: string[]) {
|
||||
var emitter = new JavaScriptEmitter();
|
||||
var emittedCode =
|
||||
emitter.emitStatements('asset:angular2/test/compiler/output/output_emitter_codegen_untyped',
|
||||
codegenStmts, codegenExportsVars);
|
||||
// debug: console.error(emittedCode);
|
||||
print(emittedCode);
|
||||
}
|
197
modules/angular2/test/compiler/output/output_emitter_spec.ts
Normal file
197
modules/angular2/test/compiler/output/output_emitter_spec.ts
Normal file
@ -0,0 +1,197 @@
|
||||
import {
|
||||
AsyncTestCompleter,
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
describe,
|
||||
el,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
it,
|
||||
xit,
|
||||
TestComponentBuilder,
|
||||
browserDetection
|
||||
} from 'angular2/testing_internal';
|
||||
|
||||
import {IS_DART} from 'angular2/src/facade/lang';
|
||||
|
||||
import * as typed from './output_emitter_codegen_typed';
|
||||
import * as untyped from './output_emitter_codegen_untyped';
|
||||
import {jitStatements} from 'angular2/src/compiler/output/output_jit';
|
||||
import {interpretStatements} from 'angular2/src/compiler/output/output_interpreter';
|
||||
import {codegenStmts, ExternalClass, DynamicClassInstanceFactory} from './output_emitter_util';
|
||||
import {EventEmitter} from 'angular2/src/facade/async';
|
||||
import {ViewType} from 'angular2/src/core/linker/view_type';
|
||||
import {BaseException} from 'angular2/src/facade/exceptions';
|
||||
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
|
||||
|
||||
export function main() {
|
||||
var outputDefs = [];
|
||||
outputDefs.push({
|
||||
'getExpressions': () => interpretStatements(codegenStmts, 'getExpressions',
|
||||
new DynamicClassInstanceFactory()),
|
||||
'name': 'interpreted'
|
||||
});
|
||||
if (IS_DART || !DOM.supportsDOMEvents()) {
|
||||
// Our generator only works on node.js and Dart...
|
||||
outputDefs.push({'getExpressions': () => typed.getExpressions, 'name': 'typed'});
|
||||
}
|
||||
if (!IS_DART) {
|
||||
// Our generator only works on node.js and Dart...
|
||||
if (!DOM.supportsDOMEvents()) {
|
||||
outputDefs.push({'getExpressions': () => untyped.getExpressions, 'name': 'untyped'});
|
||||
}
|
||||
outputDefs.push({
|
||||
'getExpressions': () => jitStatements('output_emitter_spec', codegenStmts, 'getExpressions'),
|
||||
'name': 'jit'
|
||||
});
|
||||
}
|
||||
|
||||
describe('output emitter', () => {
|
||||
outputDefs.forEach((outputDef) => {
|
||||
describe(`${outputDef['name']}`, () => {
|
||||
var expressions;
|
||||
beforeEach(() => { expressions = outputDef['getExpressions']()(); });
|
||||
|
||||
it('should support literals', () => {
|
||||
expect(expressions['stringLiteral']).toEqual('Hello World!');
|
||||
expect(expressions['intLiteral']).toEqual(42);
|
||||
expect(expressions['boolLiteral']).toEqual(true);
|
||||
expect(expressions['arrayLiteral']).toEqual([0]);
|
||||
expect(expressions['mapLiteral']).toEqual({'key0': 0});
|
||||
});
|
||||
|
||||
it('should support reading vars/keys/props', () => {
|
||||
expect(expressions['readVar']).toEqual('someValue');
|
||||
expect(expressions['readKey']).toEqual('someValue');
|
||||
expect(expressions['readPropExternalInstance']).toEqual('someValue');
|
||||
expect(expressions['readPropDynamicInstance']).toEqual('dynamicValue');
|
||||
expect(expressions['readGetterDynamicInstance'])
|
||||
.toEqual({'data': 'someValue', 'dynamicProp': 'dynamicValue'});
|
||||
});
|
||||
|
||||
it('should support writing to vars / keys / props', () => {
|
||||
expect(expressions['changedVar']).toEqual('changedValue');
|
||||
expect(expressions['changedKey']).toEqual('changedValue');
|
||||
expect(expressions['changedPropExternalInstance']).toEqual('changedValue');
|
||||
expect(expressions['changedPropDynamicInstance']).toEqual('changedValue');
|
||||
});
|
||||
|
||||
it('should support declaring functions with parameters and return', () => {
|
||||
expect(expressions['fn']('someParam')).toEqual({'param': 'someParam'});
|
||||
expect(expressions['closureInDynamicInstance']('someParam'))
|
||||
.toEqual({'param': 'someParam', 'data': 'someValue', 'dynamicProp': 'dynamicValue'});
|
||||
});
|
||||
|
||||
it('should support invoking functions and methods', () => {
|
||||
expect(expressions['invokeFn']).toEqual({'param': 'someParam'});
|
||||
expect(expressions['concatedArray']).toEqual([0, 1]);
|
||||
expect(expressions['invokeMethodExternalInstance'])
|
||||
.toEqual({'data': 'someValue', 'param': 'someParam'});
|
||||
expect(expressions['invokeMethodDynamicInstance'])
|
||||
.toEqual({'data': 'someValue', 'dynamicProp': 'dynamicValue', 'param': 'someParam'});
|
||||
});
|
||||
|
||||
it('should support conditionals', () => {
|
||||
expect(expressions['conditionalTrue']).toEqual('true');
|
||||
expect(expressions['conditionalFalse']).toEqual('false');
|
||||
});
|
||||
|
||||
it('should support not', () => { expect(expressions['not']).toEqual(true); });
|
||||
|
||||
it('should support reading external identifiers', () => {
|
||||
expect(expressions['externalTestIdentifier']).toBe(ExternalClass);
|
||||
expect(expressions['externalSrcIdentifier']).toBe(EventEmitter);
|
||||
expect(expressions['externalEnumIdentifier']).toBe(ViewType.HOST);
|
||||
});
|
||||
|
||||
it('should support instantiating classes', () => {
|
||||
expect(expressions['externalInstance']).toBeAnInstanceOf(ExternalClass);
|
||||
// Note: toBeAnInstanceOf does not check super classes in Dart...
|
||||
expect(expressions['dynamicInstance'] instanceof ExternalClass).toBe(true);
|
||||
});
|
||||
|
||||
describe('operators', () => {
|
||||
var ops;
|
||||
var aObj, bObj;
|
||||
beforeEach(() => {
|
||||
ops = expressions['operators'];
|
||||
aObj = new Object();
|
||||
bObj = new Object();
|
||||
});
|
||||
it('should support ==', () => {
|
||||
expect(ops['=='](aObj, aObj)).toBe(true);
|
||||
expect(ops['=='](aObj, bObj)).toBe(false);
|
||||
expect(ops['=='](1, 1)).toBe(true);
|
||||
expect(ops['=='](0, 1)).toBe(false);
|
||||
expect(ops['==']('a', 'a')).toBe(true);
|
||||
expect(ops['==']('a', 'b')).toBe(false);
|
||||
});
|
||||
it('should support !=', () => {
|
||||
expect(ops['!='](aObj, aObj)).toBe(false);
|
||||
expect(ops['!='](aObj, bObj)).toBe(true);
|
||||
expect(ops['!='](1, 1)).toBe(false);
|
||||
expect(ops['!='](0, 1)).toBe(true);
|
||||
expect(ops['!=']('a', 'a')).toBe(false);
|
||||
expect(ops['!=']('a', 'b')).toBe(true);
|
||||
});
|
||||
it('should support ===', () => {
|
||||
expect(ops['==='](aObj, aObj)).toBe(true);
|
||||
expect(ops['==='](aObj, bObj)).toBe(false);
|
||||
expect(ops['==='](1, 1)).toBe(true);
|
||||
expect(ops['==='](0, 1)).toBe(false);
|
||||
});
|
||||
it('should support !==', () => {
|
||||
expect(ops['!=='](aObj, aObj)).toBe(false);
|
||||
expect(ops['!=='](aObj, bObj)).toBe(true);
|
||||
expect(ops['!=='](1, 1)).toBe(false);
|
||||
expect(ops['!=='](0, 1)).toBe(true);
|
||||
});
|
||||
it('should support -', () => { expect(ops['-'](3, 2)).toEqual(1); });
|
||||
it('should support +', () => { expect(ops['+'](1, 2)).toEqual(3); });
|
||||
it('should support /', () => { expect(ops['/'](6, 2)).toEqual(3); });
|
||||
it('should support *', () => { expect(ops['*'](2, 3)).toEqual(6); });
|
||||
it('should support %', () => { expect(ops['%'](3, 2)).toEqual(1); });
|
||||
it('should support &&', () => {
|
||||
expect(ops['&&'](true, true)).toBe(true);
|
||||
expect(ops['&&'](true, false)).toBe(false);
|
||||
});
|
||||
it('should support ||', () => {
|
||||
expect(ops['||'](true, false)).toBe(true);
|
||||
expect(ops['||'](false, false)).toBe(false);
|
||||
});
|
||||
it('should support <', () => {
|
||||
expect(ops['<'](1, 2)).toBe(true);
|
||||
expect(ops['<'](1, 1)).toBe(false);
|
||||
});
|
||||
it('should support <=', () => {
|
||||
expect(ops['<='](1, 2)).toBe(true);
|
||||
expect(ops['<='](1, 1)).toBe(true);
|
||||
});
|
||||
it('should support >', () => {
|
||||
expect(ops['>'](2, 1)).toBe(true);
|
||||
expect(ops['>'](1, 1)).toBe(false);
|
||||
});
|
||||
it('should support >=', () => {
|
||||
expect(ops['>='](2, 1)).toBe(true);
|
||||
expect(ops['>='](1, 1)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('should support throwing errors',
|
||||
() => { expect(expressions['throwError']).toThrowError('someError'); });
|
||||
|
||||
it('should support catching errors', () => {
|
||||
function someOperation() { throw new BaseException('Boom!'); }
|
||||
|
||||
var errorAndStack = expressions['catchError'](someOperation);
|
||||
expect(errorAndStack[0].message).toEqual('Boom!');
|
||||
// Somehow we don't get stacktraces on ios7...
|
||||
if (!browserDetection.isIOS7 && !browserDetection.isIE) {
|
||||
expect(errorAndStack[1].toString()).toContain('someOperation');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
267
modules/angular2/test/compiler/output/output_emitter_util.ts
Normal file
267
modules/angular2/test/compiler/output/output_emitter_util.ts
Normal file
@ -0,0 +1,267 @@
|
||||
import {CompileIdentifierMetadata} from 'angular2/src/compiler/compile_metadata';
|
||||
import {EventEmitter} from 'angular2/src/facade/async';
|
||||
import {ViewType} from 'angular2/src/core/linker/view_type';
|
||||
import {BaseException} from 'angular2/src/facade/exceptions';
|
||||
import {InstanceFactory, DynamicInstance} from 'angular2/src/compiler/output/output_interpreter';
|
||||
import {MODULE_SUFFIX} from 'angular2/src/compiler/util';
|
||||
|
||||
import * as o from 'angular2/src/compiler/output/output_ast';
|
||||
|
||||
export class ExternalClass {
|
||||
changeable: any;
|
||||
constructor(public data: any) { this.changeable = data; }
|
||||
someMethod(a) { return {'param': a, 'data': this.data}; }
|
||||
}
|
||||
|
||||
var testDataIdentifier = new CompileIdentifierMetadata({
|
||||
name: 'ExternalClass',
|
||||
moduleUrl: `asset:angular2/test/compiler/output/output_emitter_util${MODULE_SUFFIX}`,
|
||||
runtime: ExternalClass
|
||||
});
|
||||
|
||||
var eventEmitterIdentifier = new CompileIdentifierMetadata({
|
||||
name: 'EventEmitter',
|
||||
moduleUrl: `asset:angular2/lib/src/facade/async${MODULE_SUFFIX}`,
|
||||
runtime: EventEmitter
|
||||
});
|
||||
|
||||
var enumIdentifier = new CompileIdentifierMetadata({
|
||||
name: 'ViewType.HOST',
|
||||
moduleUrl: `asset:angular2/lib/src/core/linker/view_type${MODULE_SUFFIX}`,
|
||||
runtime: ViewType.HOST
|
||||
});
|
||||
|
||||
var baseExceptionIdentifier = new CompileIdentifierMetadata({
|
||||
name: 'BaseException',
|
||||
moduleUrl: `asset:angular2/lib/src/facade/exceptions${MODULE_SUFFIX}`,
|
||||
runtime: BaseException
|
||||
});
|
||||
|
||||
export var codegenExportsVars = [
|
||||
'getExpressions',
|
||||
];
|
||||
|
||||
|
||||
var _getExpressionsStmts: o.Statement[] = [
|
||||
o.variable('readVar')
|
||||
.set(o.literal('someValue'))
|
||||
.toDeclStmt(),
|
||||
|
||||
o.variable('changedVar').set(o.literal('initialValue')).toDeclStmt(),
|
||||
o.variable('changedVar').set(o.literal('changedValue')).toStmt(),
|
||||
|
||||
o.variable('map')
|
||||
.set(o.literalMap([
|
||||
['someKey', o.literal('someValue')],
|
||||
['changeable', o.literal('initialValue')],
|
||||
]))
|
||||
.toDeclStmt(),
|
||||
o.variable('map').key(o.literal('changeable')).set(o.literal('changedValue')).toStmt(),
|
||||
|
||||
o.variable('externalInstance')
|
||||
.set(o.importExpr(testDataIdentifier).instantiate([o.literal('someValue')]))
|
||||
.toDeclStmt(),
|
||||
o.variable('externalInstance').prop('changeable').set(o.literal('changedValue')).toStmt(),
|
||||
|
||||
o.variable('fn')
|
||||
.set(o.fn([new o.FnParam('param')],
|
||||
[new o.ReturnStatement(o.literalMap([['param', o.variable('param')]]))],
|
||||
o.DYNAMIC_TYPE))
|
||||
.toDeclStmt(),
|
||||
|
||||
o.variable('throwError')
|
||||
.set(o.fn([],
|
||||
[
|
||||
new o.ThrowStmt(o.importExpr(baseExceptionIdentifier)
|
||||
.instantiate([o.literal('someError')]))
|
||||
]))
|
||||
.toDeclStmt(),
|
||||
|
||||
o.variable('catchError')
|
||||
.set(o.fn([new o.FnParam('runCb')],
|
||||
[
|
||||
new o.TryCatchStmt([o.variable('runCb').callFn([]).toStmt()],
|
||||
[
|
||||
new o.ReturnStatement(
|
||||
o.literalArr([o.CATCH_ERROR_VAR, o.CATCH_STACK_VAR]))
|
||||
])
|
||||
],
|
||||
o.DYNAMIC_TYPE))
|
||||
.toDeclStmt(),
|
||||
|
||||
o.variable('dynamicInstance')
|
||||
.set(o.variable('DynamicClass')
|
||||
.instantiate([o.literal('someValue'), o.literal('dynamicValue')]))
|
||||
.toDeclStmt(),
|
||||
o.variable('dynamicInstance').prop('dynamicChangeable').set(o.literal('changedValue')).toStmt(),
|
||||
|
||||
new o.ReturnStatement(o.literalMap([
|
||||
['stringLiteral', o.literal('Hello World!')],
|
||||
['intLiteral', o.literal(42)],
|
||||
['boolLiteral', o.literal(true)],
|
||||
['arrayLiteral', o.literalArr([o.literal(0)])],
|
||||
['mapLiteral', o.literalMap([['key0', o.literal(0)]])],
|
||||
|
||||
['readVar', o.variable('readVar')],
|
||||
['changedVar', o.variable('changedVar')],
|
||||
['readKey', o.variable('map').key(o.literal('someKey'))],
|
||||
['changedKey', o.variable('map').key(o.literal('changeable'))],
|
||||
['readPropExternalInstance', o.variable('externalInstance').prop('data')],
|
||||
['readPropDynamicInstance', o.variable('dynamicInstance').prop('dynamicProp')],
|
||||
['readGetterDynamicInstance', o.variable('dynamicInstance').prop('dynamicGetter')],
|
||||
['changedPropExternalInstance', o.variable('externalInstance').prop('changeable')],
|
||||
['changedPropDynamicInstance', o.variable('dynamicInstance').prop('dynamicChangeable')],
|
||||
|
||||
[
|
||||
'invokeMethodExternalInstance',
|
||||
o.variable('externalInstance').callMethod('someMethod', [o.literal('someParam')])
|
||||
],
|
||||
[
|
||||
'invokeMethodDynamicInstance',
|
||||
o.variable('dynamicInstance').callMethod('dynamicMethod', [o.literal('someParam')])
|
||||
],
|
||||
[
|
||||
'concatedArray',
|
||||
o.literalArr([o.literal(0)])
|
||||
.callMethod(o.BuiltinMethod.ConcatArray, [o.literalArr([o.literal(1)])])
|
||||
],
|
||||
|
||||
['fn', o.variable('fn')],
|
||||
['closureInDynamicInstance', o.variable('dynamicInstance').prop('closure')],
|
||||
['invokeFn', o.variable('fn').callFn([o.literal('someParam')])],
|
||||
|
||||
[
|
||||
'conditionalTrue',
|
||||
o.literal('')
|
||||
.prop('length')
|
||||
.equals(o.literal(0))
|
||||
.conditional(o.literal('true'), o.literal('false'))
|
||||
],
|
||||
[
|
||||
'conditionalFalse',
|
||||
o.literal('')
|
||||
.prop('length')
|
||||
.notEquals(o.literal(0))
|
||||
.conditional(o.literal('true'), o.literal('false'))
|
||||
],
|
||||
|
||||
['not', o.not(o.literal(false))],
|
||||
|
||||
['externalTestIdentifier', o.importExpr(testDataIdentifier)],
|
||||
['externalSrcIdentifier', o.importExpr(eventEmitterIdentifier)],
|
||||
['externalEnumIdentifier', o.importExpr(enumIdentifier)],
|
||||
|
||||
['externalInstance', o.variable('externalInstance')],
|
||||
['dynamicInstance', o.variable('dynamicInstance')],
|
||||
|
||||
['throwError', o.variable('throwError')],
|
||||
['catchError', o.variable('catchError')],
|
||||
|
||||
[
|
||||
'operators',
|
||||
o.literalMap([
|
||||
['==', createOperatorFn(o.BinaryOperator.Equals)],
|
||||
['!=', createOperatorFn(o.BinaryOperator.NotEquals)],
|
||||
['===', createOperatorFn(o.BinaryOperator.Identical)],
|
||||
['!==', createOperatorFn(o.BinaryOperator.NotIdentical)],
|
||||
['-', createOperatorFn(o.BinaryOperator.Minus)],
|
||||
['+', createOperatorFn(o.BinaryOperator.Plus)],
|
||||
['/', createOperatorFn(o.BinaryOperator.Divide)],
|
||||
['*', createOperatorFn(o.BinaryOperator.Multiply)],
|
||||
['%', createOperatorFn(o.BinaryOperator.Modulo)],
|
||||
['&&', createOperatorFn(o.BinaryOperator.And)],
|
||||
['||', createOperatorFn(o.BinaryOperator.Or)],
|
||||
['<', createOperatorFn(o.BinaryOperator.Lower)],
|
||||
['<=', createOperatorFn(o.BinaryOperator.LowerEquals)],
|
||||
['>', createOperatorFn(o.BinaryOperator.Bigger)],
|
||||
['>=', createOperatorFn(o.BinaryOperator.BiggerEquals)]
|
||||
])
|
||||
],
|
||||
]))
|
||||
];
|
||||
|
||||
export var codegenStmts: o.Statement[] = [
|
||||
new o.CommentStmt('This is a comment'),
|
||||
|
||||
new o.ClassStmt(
|
||||
'DynamicClass', o.importExpr(testDataIdentifier),
|
||||
[
|
||||
new o.ClassField('dynamicProp', o.DYNAMIC_TYPE),
|
||||
new o.ClassField('dynamicChangeable', o.DYNAMIC_TYPE),
|
||||
new o.ClassField('closure', o.FUNCTION_TYPE)
|
||||
],
|
||||
[
|
||||
new o.ClassGetter('dynamicGetter',
|
||||
[
|
||||
new o.ReturnStatement(o.literalMap([
|
||||
['data', o.THIS_EXPR.prop('data')],
|
||||
['dynamicProp', o.THIS_EXPR.prop('dynamicProp')]
|
||||
]))
|
||||
],
|
||||
new o.MapType(o.DYNAMIC_TYPE))
|
||||
],
|
||||
new o.ClassMethod(null,
|
||||
[
|
||||
new o.FnParam('dataParam', o.DYNAMIC_TYPE),
|
||||
new o.FnParam('dynamicPropParam', o.DYNAMIC_TYPE)
|
||||
],
|
||||
[
|
||||
o.SUPER_EXPR.callFn([o.variable('dataParam')])
|
||||
.toStmt(),
|
||||
o.THIS_EXPR.prop('dynamicProp')
|
||||
.set(o.variable('dynamicPropParam'))
|
||||
.toStmt(),
|
||||
o.THIS_EXPR.prop('dynamicChangeable')
|
||||
.set(o.variable('dynamicPropParam'))
|
||||
.toStmt(),
|
||||
o.THIS_EXPR.prop('closure')
|
||||
.set(o.fn([new o.FnParam('param', o.DYNAMIC_TYPE)],
|
||||
[
|
||||
new o.ReturnStatement(o.literalMap([
|
||||
['param', o.variable('param')],
|
||||
['data', o.THIS_EXPR.prop('data')],
|
||||
['dynamicProp', o.THIS_EXPR.prop('dynamicProp')]
|
||||
]))
|
||||
],
|
||||
o.DYNAMIC_TYPE))
|
||||
.toStmt(),
|
||||
]),
|
||||
[
|
||||
new o.ClassMethod('dynamicMethod', [new o.FnParam('param', o.DYNAMIC_TYPE)],
|
||||
[
|
||||
new o.ReturnStatement(o.literalMap([
|
||||
['param', o.variable('param')],
|
||||
['data', o.THIS_EXPR.prop('data')],
|
||||
['dynamicProp', o.THIS_EXPR.prop('dynamicProp')]
|
||||
]))
|
||||
],
|
||||
o.DYNAMIC_TYPE)
|
||||
]),
|
||||
|
||||
o.fn([], _getExpressionsStmts, o.DYNAMIC_TYPE).toDeclStmt('getExpressions')
|
||||
];
|
||||
|
||||
function createOperatorFn(op: o.BinaryOperator) {
|
||||
return o.fn(
|
||||
[new o.FnParam('a'), new o.FnParam('b')],
|
||||
[new o.ReturnStatement(new o.BinaryOperatorExpr(op, o.variable('a'), o.variable('b')))],
|
||||
o.DYNAMIC_TYPE);
|
||||
}
|
||||
|
||||
export class DynamicClassInstanceFactory implements InstanceFactory {
|
||||
createInstance(superClass: any, clazz: any, args: any[], props: Map<string, any>,
|
||||
getters: Map<string, Function>, methods: Map<string, Function>): any {
|
||||
if (superClass === ExternalClass) {
|
||||
return new _InterpretiveDynamicClass(args, clazz, props, getters, methods);
|
||||
}
|
||||
throw new BaseException(`Can't instantiate class ${superClass} in interpretative mode`);
|
||||
}
|
||||
}
|
||||
|
||||
class _InterpretiveDynamicClass extends ExternalClass implements DynamicInstance {
|
||||
constructor(args: any[], public clazz: any, public props: Map<string, any>,
|
||||
public getters: Map<string, Function>, public methods: Map<string, Function>) {
|
||||
super(args[0]);
|
||||
}
|
||||
childMethod(a) { return this.methods.get('childMethod')(a); }
|
||||
}
|
72
modules/angular2/test/compiler/output/path_util_spec.ts
Normal file
72
modules/angular2/test/compiler/output/path_util_spec.ts
Normal file
@ -0,0 +1,72 @@
|
||||
import {
|
||||
AsyncTestCompleter,
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
describe,
|
||||
el,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
it,
|
||||
xit,
|
||||
TestComponentBuilder
|
||||
} from 'angular2/testing_internal';
|
||||
|
||||
import {getImportModulePath, ImportEnv} from 'angular2/src/compiler/output/path_util';
|
||||
|
||||
export function main() {
|
||||
describe('PathUtils', () => {
|
||||
describe('getImportModulePath', () => {
|
||||
it('should calculate relative paths for JS and Dart', () => {
|
||||
expect(getImportModulePath('asset:somePkg/lib/modPath', 'asset:somePkg/lib/impPath',
|
||||
ImportEnv.JS))
|
||||
.toEqual('./impPath');
|
||||
expect(getImportModulePath('asset:somePkg/lib/modPath', 'asset:somePkg/lib/impPath',
|
||||
ImportEnv.Dart))
|
||||
.toEqual('impPath');
|
||||
});
|
||||
|
||||
it('should calculate relative paths for different constellations', () => {
|
||||
expect(getImportModulePath('asset:somePkg/test/modPath', 'asset:somePkg/test/impPath',
|
||||
ImportEnv.JS))
|
||||
.toEqual('./impPath');
|
||||
expect(getImportModulePath('asset:somePkg/lib/modPath', 'asset:somePkg/lib/dir2/impPath',
|
||||
ImportEnv.JS))
|
||||
.toEqual('./dir2/impPath');
|
||||
expect(getImportModulePath('asset:somePkg/lib/dir1/modPath', 'asset:somePkg/lib/impPath',
|
||||
ImportEnv.JS))
|
||||
.toEqual('../impPath');
|
||||
expect(getImportModulePath('asset:somePkg/lib/dir1/modPath',
|
||||
'asset:somePkg/lib/dir2/impPath', ImportEnv.JS))
|
||||
.toEqual('../dir2/impPath');
|
||||
});
|
||||
|
||||
it('should calculate absolute paths for JS and Dart', () => {
|
||||
expect(getImportModulePath('asset:somePkg/lib/modPath', 'asset:someOtherPkg/lib/impPath',
|
||||
ImportEnv.JS))
|
||||
.toEqual('someOtherPkg/impPath');
|
||||
expect(getImportModulePath('asset:somePkg/lib/modPath', 'asset:someOtherPkg/lib/impPath',
|
||||
ImportEnv.Dart))
|
||||
.toEqual('package:someOtherPkg/impPath');
|
||||
});
|
||||
|
||||
it('should not allow absolute imports of non lib modules', () => {
|
||||
expect(() => getImportModulePath('asset:somePkg/lib/modPath', 'asset:somePkg/test/impPath',
|
||||
ImportEnv.Dart))
|
||||
.toThrowError(
|
||||
`Can't import url asset:somePkg/test/impPath from asset:somePkg/lib/modPath`);
|
||||
});
|
||||
|
||||
it('should not allow non asset urls as base url', () => {
|
||||
expect(() => getImportModulePath('http:somePkg/lib/modPath', 'asset:somePkg/test/impPath',
|
||||
ImportEnv.Dart))
|
||||
.toThrowError(`Url http:somePkg/lib/modPath is not a valid asset: url`);
|
||||
});
|
||||
|
||||
it('should allow non asset urls as import urls and pass them through', () => {
|
||||
expect(getImportModulePath('asset:somePkg/lib/modPath', 'dart:html', ImportEnv.Dart))
|
||||
.toEqual('dart:html');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
321
modules/angular2/test/compiler/output/ts_emitter_spec.ts
Normal file
321
modules/angular2/test/compiler/output/ts_emitter_spec.ts
Normal file
@ -0,0 +1,321 @@
|
||||
import {
|
||||
AsyncTestCompleter,
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
describe,
|
||||
el,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
it,
|
||||
xit,
|
||||
TestComponentBuilder
|
||||
} from 'angular2/testing_internal';
|
||||
|
||||
import {isBlank} from 'angular2/src/facade/lang';
|
||||
import {TypeScriptEmitter} from 'angular2/src/compiler/output/ts_emitter';
|
||||
import {CompileIdentifierMetadata} from 'angular2/src/compiler/compile_metadata';
|
||||
import * as o from 'angular2/src/compiler/output/output_ast';
|
||||
|
||||
var someModuleUrl = 'asset:somePackage/lib/somePath';
|
||||
var anotherModuleUrl = 'asset:somePackage/lib/someOtherPath';
|
||||
|
||||
var sameModuleIdentifier =
|
||||
new CompileIdentifierMetadata({name: 'someLocalId', moduleUrl: someModuleUrl});
|
||||
|
||||
var externalModuleIdentifier =
|
||||
new CompileIdentifierMetadata({name: 'someExternalId', moduleUrl: anotherModuleUrl});
|
||||
|
||||
export function main() {
|
||||
// Note supported features of our OutputAstin TS:
|
||||
// - real `const` like in Dart
|
||||
// - final fields
|
||||
|
||||
describe('TypeScriptEmitter', () => {
|
||||
var emitter: TypeScriptEmitter;
|
||||
var someVar: o.ReadVarExpr;
|
||||
|
||||
beforeEach(() => {
|
||||
emitter = new TypeScriptEmitter();
|
||||
someVar = o.variable('someVar');
|
||||
});
|
||||
|
||||
function emitStmt(stmt: o.Statement, exportedVars: string[] = null): string {
|
||||
if (isBlank(exportedVars)) {
|
||||
exportedVars = [];
|
||||
}
|
||||
return emitter.emitStatements(someModuleUrl, [stmt], exportedVars);
|
||||
}
|
||||
|
||||
it('should declare variables', () => {
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt())).toEqual(`var someVar = 1;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(null, [o.StmtModifier.Final])))
|
||||
.toEqual(`const someVar = 1;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(), ['someVar']))
|
||||
.toEqual(`export var someVar = 1;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(o.INT_TYPE)))
|
||||
.toEqual(`var someVar:number = 1;`);
|
||||
});
|
||||
|
||||
it('should read and write variables', () => {
|
||||
expect(emitStmt(someVar.toStmt())).toEqual(`someVar;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toStmt())).toEqual(`someVar = 1;`);
|
||||
expect(emitStmt(someVar.set(o.variable('someOtherVar').set(o.literal(1))).toStmt()))
|
||||
.toEqual(`someVar = (someOtherVar = 1);`);
|
||||
});
|
||||
|
||||
it('should read and write keys', () => {
|
||||
expect(emitStmt(o.variable('someMap').key(o.variable('someKey')).toStmt()))
|
||||
.toEqual(`someMap[someKey];`);
|
||||
expect(emitStmt(o.variable('someMap').key(o.variable('someKey')).set(o.literal(1)).toStmt()))
|
||||
.toEqual(`someMap[someKey] = 1;`);
|
||||
});
|
||||
|
||||
it('should read and write properties', () => {
|
||||
expect(emitStmt(o.variable('someObj').prop('someProp').toStmt()))
|
||||
.toEqual(`someObj.someProp;`);
|
||||
expect(emitStmt(o.variable('someObj').prop('someProp').set(o.literal(1)).toStmt()))
|
||||
.toEqual(`someObj.someProp = 1;`);
|
||||
});
|
||||
|
||||
it('should invoke functions and methods and constructors', () => {
|
||||
expect(emitStmt(o.variable('someFn').callFn([o.literal(1)]).toStmt())).toEqual('someFn(1);');
|
||||
expect(emitStmt(o.variable('someObj').callMethod('someMethod', [o.literal(1)]).toStmt()))
|
||||
.toEqual('someObj.someMethod(1);');
|
||||
expect(emitStmt(o.variable('SomeClass').instantiate([o.literal(1)]).toStmt()))
|
||||
.toEqual('new SomeClass(1);');
|
||||
});
|
||||
|
||||
it('should support builtin methods', () => {
|
||||
expect(emitStmt(o.variable('arr1')
|
||||
.callMethod(o.BuiltinMethod.ConcatArray, [o.variable('arr2')])
|
||||
.toStmt()))
|
||||
.toEqual('arr1.concat(arr2);');
|
||||
|
||||
expect(emitStmt(o.variable('observable')
|
||||
.callMethod(o.BuiltinMethod.SubscribeObservable, [o.variable('listener')])
|
||||
.toStmt()))
|
||||
.toEqual('observable.subscribe(listener);');
|
||||
});
|
||||
|
||||
it('should support literals', () => {
|
||||
expect(emitStmt(o.literal(0).toStmt())).toEqual('0;');
|
||||
expect(emitStmt(o.literal(true).toStmt())).toEqual('true;');
|
||||
expect(emitStmt(o.literal('someStr').toStmt())).toEqual(`'someStr';`);
|
||||
expect(emitStmt(o.literalArr([o.literal(1)]).toStmt())).toEqual(`[1];`);
|
||||
expect(emitStmt(o.literalMap([['someKey', o.literal(1)]]).toStmt()))
|
||||
.toEqual(`{'someKey': 1};`);
|
||||
});
|
||||
|
||||
it('should support external identifiers', () => {
|
||||
expect(emitStmt(o.importExpr(sameModuleIdentifier).toStmt())).toEqual('someLocalId;');
|
||||
expect(emitStmt(o.importExpr(externalModuleIdentifier).toStmt()))
|
||||
.toEqual([`import * as import0 from './someOtherPath';`, `import0.someExternalId;`].join(
|
||||
'\n'));
|
||||
});
|
||||
|
||||
it('should support operators', () => {
|
||||
var lhs = o.variable('lhs');
|
||||
var rhs = o.variable('rhs');
|
||||
expect(emitStmt(someVar.cast(o.INT_TYPE).toStmt())).toEqual('(<number>someVar);');
|
||||
expect(emitStmt(o.not(someVar).toStmt())).toEqual('!someVar;');
|
||||
expect(
|
||||
emitStmt(someVar.conditional(o.variable('trueCase'), o.variable('falseCase')).toStmt()))
|
||||
.toEqual('someVar? trueCase: falseCase;');
|
||||
|
||||
expect(emitStmt(lhs.equals(rhs).toStmt())).toEqual('(lhs == rhs);');
|
||||
expect(emitStmt(lhs.notEquals(rhs).toStmt())).toEqual('(lhs != rhs);');
|
||||
expect(emitStmt(lhs.identical(rhs).toStmt())).toEqual('(lhs === rhs);');
|
||||
expect(emitStmt(lhs.notIdentical(rhs).toStmt())).toEqual('(lhs !== rhs);');
|
||||
expect(emitStmt(lhs.minus(rhs).toStmt())).toEqual('(lhs - rhs);');
|
||||
expect(emitStmt(lhs.plus(rhs).toStmt())).toEqual('(lhs + rhs);');
|
||||
expect(emitStmt(lhs.divide(rhs).toStmt())).toEqual('(lhs / rhs);');
|
||||
expect(emitStmt(lhs.multiply(rhs).toStmt())).toEqual('(lhs * rhs);');
|
||||
expect(emitStmt(lhs.modulo(rhs).toStmt())).toEqual('(lhs % rhs);');
|
||||
expect(emitStmt(lhs.and(rhs).toStmt())).toEqual('(lhs && rhs);');
|
||||
expect(emitStmt(lhs.or(rhs).toStmt())).toEqual('(lhs || rhs);');
|
||||
expect(emitStmt(lhs.lower(rhs).toStmt())).toEqual('(lhs < rhs);');
|
||||
expect(emitStmt(lhs.lowerEquals(rhs).toStmt())).toEqual('(lhs <= rhs);');
|
||||
expect(emitStmt(lhs.bigger(rhs).toStmt())).toEqual('(lhs > rhs);');
|
||||
expect(emitStmt(lhs.biggerEquals(rhs).toStmt())).toEqual('(lhs >= rhs);');
|
||||
});
|
||||
|
||||
it('should support function expressions', () => {
|
||||
expect(emitStmt(o.fn([], []).toStmt())).toEqual(['():void => {', '};'].join('\n'));
|
||||
expect(emitStmt(o.fn([], [new o.ReturnStatement(o.literal(1))], o.INT_TYPE).toStmt()))
|
||||
.toEqual(['():number => {', ' return 1;\n};'].join('\n'));
|
||||
expect(emitStmt(o.fn([new o.FnParam('param1', o.INT_TYPE)], []).toStmt()))
|
||||
.toEqual(['(param1:number):void => {', '};'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support function statements', () => {
|
||||
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], [])))
|
||||
.toEqual(['function someFn():void {', '}'].join('\n'));
|
||||
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], []), ['someFn']))
|
||||
.toEqual(['export function someFn():void {', '}'].join('\n'));
|
||||
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], [new o.ReturnStatement(o.literal(1))],
|
||||
o.INT_TYPE)))
|
||||
.toEqual(['function someFn():number {', ' return 1;', '}'].join('\n'));
|
||||
expect(
|
||||
emitStmt(new o.DeclareFunctionStmt('someFn', [new o.FnParam('param1', o.INT_TYPE)], [])))
|
||||
.toEqual(['function someFn(param1:number):void {', '}'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support comments',
|
||||
() => { expect(emitStmt(new o.CommentStmt('a\nb'))).toEqual(['// a', '// b'].join('\n')); });
|
||||
|
||||
it('should support if stmt', () => {
|
||||
var trueCase = o.variable('trueCase').callFn([]).toStmt();
|
||||
var falseCase = o.variable('falseCase').callFn([]).toStmt();
|
||||
expect(emitStmt(new o.IfStmt(o.variable('cond'), [trueCase])))
|
||||
.toEqual(['if (cond) { trueCase(); }'].join('\n'));
|
||||
expect(emitStmt(new o.IfStmt(o.variable('cond'), [trueCase], [falseCase])))
|
||||
.toEqual(['if (cond) {', ' trueCase();', '} else {', ' falseCase();', '}'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support try/catch', () => {
|
||||
var bodyStmt = o.variable('body').callFn([]).toStmt();
|
||||
var catchStmt = o.variable('catchFn').callFn([o.CATCH_ERROR_VAR, o.CATCH_STACK_VAR]).toStmt();
|
||||
expect(emitStmt(new o.TryCatchStmt([bodyStmt], [catchStmt])))
|
||||
.toEqual([
|
||||
'try {',
|
||||
' body();',
|
||||
'} catch (error) {',
|
||||
' const stack = error.stack;',
|
||||
' catchFn(error,stack);',
|
||||
'}'
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support support throwing',
|
||||
() => { expect(emitStmt(new o.ThrowStmt(someVar))).toEqual('throw someVar;'); });
|
||||
|
||||
describe('classes', () => {
|
||||
var callSomeMethod: o.Statement;
|
||||
|
||||
beforeEach(() => { callSomeMethod = o.THIS_EXPR.callMethod('someMethod', []).toStmt(); });
|
||||
|
||||
|
||||
it('should support declaring classes', () => {
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null, [])))
|
||||
.toEqual(['class SomeClass {', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null, []), ['SomeClass']))
|
||||
.toEqual(['export class SomeClass {', '}'].join('\n'));
|
||||
expect(
|
||||
emitStmt(new o.ClassStmt('SomeClass', o.variable('SomeSuperClass'), [], [], null, [])))
|
||||
.toEqual(['class SomeClass extends SomeSuperClass {', '}'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support declaring constructors', () => {
|
||||
var superCall = o.SUPER_EXPR.callFn([o.variable('someParam')]).toStmt();
|
||||
expect(emitStmt(
|
||||
new o.ClassStmt('SomeClass', null, [], [], new o.ClassMethod(null, [], []), [])))
|
||||
.toEqual(['class SomeClass {', ' constructor() {', ' }', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [],
|
||||
new o.ClassMethod(null, [new o.FnParam('someParam', o.INT_TYPE)], []), [])))
|
||||
.toEqual(
|
||||
['class SomeClass {', ' constructor(someParam:number) {', ' }', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [],
|
||||
new o.ClassMethod(null, [], [superCall]), [])))
|
||||
.toEqual(['class SomeClass {', ' constructor() {', ' super(someParam);', ' }', '}']
|
||||
.join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [],
|
||||
new o.ClassMethod(null, [], [callSomeMethod]), [])))
|
||||
.toEqual(
|
||||
['class SomeClass {', ' constructor() {', ' this.someMethod();', ' }', '}']
|
||||
.join('\n'));
|
||||
});
|
||||
|
||||
it('should support declaring fields', () => {
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [new o.ClassField('someField')], [],
|
||||
null, [])))
|
||||
.toEqual(['class SomeClass {', ' someField;', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null,
|
||||
[new o.ClassField('someField', o.INT_TYPE)], [], null, [])))
|
||||
.toEqual(['class SomeClass {', ' someField:number;', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null,
|
||||
[new o.ClassField('someField', o.INT_TYPE, [o.StmtModifier.Private])], [], null,
|
||||
[])))
|
||||
.toEqual(['class SomeClass {', ' private someField:number;', '}'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support declaring getters', () => {
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [],
|
||||
[new o.ClassGetter('someGetter', [])], null, [])))
|
||||
.toEqual(['class SomeClass {', ' get someGetter() {', ' }', '}'].join('\n'));
|
||||
expect(
|
||||
emitStmt(new o.ClassStmt('SomeClass', null, [],
|
||||
[new o.ClassGetter('someGetter', [], o.INT_TYPE)], null, [])))
|
||||
.toEqual(['class SomeClass {', ' get someGetter():number {', ' }', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [],
|
||||
[new o.ClassGetter('someGetter', [callSomeMethod])], null,
|
||||
[])))
|
||||
.toEqual(
|
||||
['class SomeClass {', ' get someGetter() {', ' this.someMethod();', ' }', '}']
|
||||
.join('\n'));
|
||||
expect(
|
||||
emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [],
|
||||
[new o.ClassGetter('someGetter', [], null, [o.StmtModifier.Private])], null, [])))
|
||||
.toEqual(['class SomeClass {', ' private get someGetter() {', ' }', '}'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support methods', () => {
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null,
|
||||
[new o.ClassMethod('someMethod', [], [])])))
|
||||
.toEqual(['class SomeClass {', ' someMethod():void {', ' }', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null,
|
||||
[new o.ClassMethod('someMethod', [], [], o.INT_TYPE)])))
|
||||
.toEqual(['class SomeClass {', ' someMethod():number {', ' }', '}'].join('\n'));
|
||||
expect(
|
||||
emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [], null,
|
||||
[new o.ClassMethod('someMethod', [new o.FnParam('someParam', o.INT_TYPE)], [])])))
|
||||
.toEqual(
|
||||
['class SomeClass {', ' someMethod(someParam:number):void {', ' }', '}'].join(
|
||||
'\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null,
|
||||
[new o.ClassMethod('someMethod', [], [callSomeMethod])])))
|
||||
.toEqual(
|
||||
['class SomeClass {', ' someMethod():void {', ' this.someMethod();', ' }', '}']
|
||||
.join('\n'));
|
||||
});
|
||||
});
|
||||
|
||||
it('should support builtin types', () => {
|
||||
var writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.DYNAMIC_TYPE))).toEqual('var a:any = null;');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.BOOL_TYPE))).toEqual('var a:boolean = null;');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.INT_TYPE))).toEqual('var a:number = null;');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.NUMBER_TYPE))).toEqual('var a:number = null;');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.STRING_TYPE))).toEqual('var a:string = null;');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.FUNCTION_TYPE))).toEqual('var a:Function = null;');
|
||||
});
|
||||
|
||||
it('should support external types', () => {
|
||||
var writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.importType(sameModuleIdentifier))))
|
||||
.toEqual('var a:someLocalId = null;');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.importType(externalModuleIdentifier))))
|
||||
.toEqual([
|
||||
`import * as import0 from './someOtherPath';`,
|
||||
`var a:import0.someExternalId = null;`
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support combined types', () => {
|
||||
var writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(new o.ArrayType(null))))
|
||||
.toEqual('var a:any[] = null;');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(new o.ArrayType(o.INT_TYPE))))
|
||||
.toEqual('var a:number[] = null;');
|
||||
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(new o.MapType(null))))
|
||||
.toEqual('var a:{[key: string]:any} = null;');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(new o.MapType(o.INT_TYPE))))
|
||||
.toEqual('var a:{[key: string]:number} = null;');
|
||||
});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user