2016-10-13 19:34:37 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {CompileIdentifierMetadata} from '../../src/compile_metadata';
|
|
|
|
import * as o from '../../src/output/output_ast';
|
|
|
|
import {CompileView} from '../../src/view_compiler/compile_view';
|
|
|
|
import {getPropertyInView} from '../../src/view_compiler/util';
|
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('getPropertyInView', () => {
|
|
|
|
it('should return the expression if it is the same view', () => {
|
|
|
|
const expr = o.THIS_EXPR.prop('someProp');
|
|
|
|
const callingView = createCompileView({className: 'view'});
|
|
|
|
expect(getPropertyInView(expr, callingView, callingView)).toBe(expr);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should access an unknown property in a parent view', () => {
|
|
|
|
const expr = o.THIS_EXPR.prop('someProp');
|
|
|
|
const definedView = createCompileView({className: 'parentView'});
|
|
|
|
const callingView = createCompileView({className: 'childView', parent: definedView});
|
|
|
|
expect(getPropertyInView(expr, callingView, definedView))
|
2016-11-01 12:35:03 -04:00
|
|
|
.toEqual(o.THIS_EXPR.prop('parentView').prop('someProp'));
|
2016-10-13 19:34:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should access a known property in a parent view with cast', () => {
|
|
|
|
const expr = o.THIS_EXPR.prop('someProp');
|
|
|
|
const definedView = createCompileView({className: 'parentView', fields: ['someProp']});
|
|
|
|
const callingView = createCompileView({className: 'childView', parent: definedView});
|
|
|
|
expect(getPropertyInView(expr, callingView, definedView))
|
2016-11-01 12:35:03 -04:00
|
|
|
.toEqual(o.THIS_EXPR.prop('parentView').cast(definedView.classType).prop('someProp'));
|
2016-10-13 19:34:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should access a known property in a parent view with cast also for property chain expressions',
|
|
|
|
() => {
|
|
|
|
const expr = o.THIS_EXPR.prop('someProp').prop('context');
|
|
|
|
const definedView = createCompileView({className: 'parentView', fields: ['someProp']});
|
|
|
|
const callingView = createCompileView({className: 'childView', parent: definedView});
|
|
|
|
expect(getPropertyInView(expr, callingView, definedView))
|
2016-11-01 12:35:03 -04:00
|
|
|
.toEqual(o.THIS_EXPR.prop('parentView')
|
2016-10-13 19:34:37 -04:00
|
|
|
.cast(definedView.classType)
|
|
|
|
.prop('someProp')
|
|
|
|
.prop('context'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function createCompileView(config: {className: string, parent?: CompileView, fields?: string[]}):
|
|
|
|
CompileView {
|
|
|
|
const declarationElement: any = config.parent ? {view: config.parent} : {view: null};
|
|
|
|
const fields: o.ClassField[] = [];
|
|
|
|
if (config.fields) {
|
|
|
|
config.fields.forEach((fieldName) => { fields.push(new o.ClassField(fieldName)); });
|
|
|
|
}
|
|
|
|
return <any>{
|
2016-11-23 12:42:19 -05:00
|
|
|
classType: o.importType(new CompileIdentifierMetadata()),
|
2016-10-13 19:34:37 -04:00
|
|
|
fields: fields,
|
|
|
|
getters: [],
|
|
|
|
declarationElement: declarationElement
|
|
|
|
};
|
|
|
|
}
|