2016-01-06 17:13:44 -05:00
|
|
|
import {
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
it,
|
|
|
|
xit,
|
2016-04-28 20:50:03 -04:00
|
|
|
} from '@angular/core/testing/testing_internal';
|
2016-01-06 17:13:44 -05:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {IS_DART} from '../../src/facade/lang';
|
2016-01-06 17:13:44 -05:00
|
|
|
|
|
|
|
import * as typed from './output_emitter_codegen_typed';
|
|
|
|
import * as untyped from './output_emitter_codegen_untyped';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {jitStatements} from '@angular/compiler/src/output/output_jit';
|
|
|
|
import {interpretStatements} from '@angular/compiler/src/output/output_interpreter';
|
2016-01-06 17:13:44 -05:00
|
|
|
import {codegenStmts, ExternalClass, DynamicClassInstanceFactory} from './output_emitter_util';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {EventEmitter} from '@angular/core';
|
|
|
|
import {ViewType} from '@angular/core/src/linker/view_type';
|
|
|
|
import {BaseException} from '@angular/core';
|
|
|
|
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
|
2016-05-02 01:50:37 -04:00
|
|
|
import {
|
|
|
|
browserDetection
|
|
|
|
} from '@angular/platform-browser/testing'
|
2016-01-06 17:13:44 -05:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
export function
|
|
|
|
main() {
|
2016-01-06 17:13:44 -05:00
|
|
|
var outputDefs = [];
|
|
|
|
outputDefs.push({
|
|
|
|
'getExpressions': () => interpretStatements(codegenStmts, 'getExpressions',
|
|
|
|
new DynamicClassInstanceFactory()),
|
|
|
|
'name': 'interpreted'
|
|
|
|
});
|
2016-04-28 20:50:03 -04:00
|
|
|
if (IS_DART || !getDOM().supportsDOMEvents()) {
|
2016-01-06 17:13:44 -05:00
|
|
|
// 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...
|
2016-04-28 20:50:03 -04:00
|
|
|
if (!getDOM().supportsDOMEvents()) {
|
2016-01-06 17:13:44 -05:00
|
|
|
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'});
|
2016-04-22 18:33:32 -04:00
|
|
|
expect(expressions['invokeMethodExternalInstanceViaBind'])
|
|
|
|
.toEqual({'data': 'someValue', 'param': 'someParam'});
|
2016-01-06 17:13:44 -05:00
|
|
|
expect(expressions['invokeMethodDynamicInstance'])
|
|
|
|
.toEqual({'data': 'someValue', 'dynamicProp': 'dynamicValue', 'param': 'someParam'});
|
2016-04-22 18:33:32 -04:00
|
|
|
expect(expressions['invokeMethodDynamicInstanceViaBind'])
|
|
|
|
.toEqual({'data': 'someValue', 'dynamicProp': 'dynamicValue', 'param': 'someParam'});
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|