test(change detect): Port Locals tests to pregenerated cds
Move existing unit tests exercising Locals to also test Dart's pre-generated change detectors. See #502
This commit is contained in:
@ -1,20 +1,31 @@
|
||||
import {ListWrapper} from 'angular2/src/facade/collection';
|
||||
import {BindingRecord, ChangeDetectorDefinition, Lexer, Parser} from 'angular2/change_detection';
|
||||
import {ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
|
||||
import {isBlank, isPresent} from 'angular2/src/facade/lang';
|
||||
import {
|
||||
BindingRecord,
|
||||
ChangeDetectorDefinition,
|
||||
Lexer,
|
||||
Locals,
|
||||
Parser
|
||||
} from 'angular2/change_detection';
|
||||
import {reflector} from 'angular2/src/reflection/reflection';
|
||||
import {ReflectionCapabilities} from 'angular2/src/reflection/reflection_capabilities';
|
||||
|
||||
var _parser = new Parser(new Lexer());
|
||||
|
||||
function _createChangeDetectorDefinition(id: string, expression: string): ChangeDetectorDefinition {
|
||||
function _createBindingRecords(expression: string): List<BindingRecord> {
|
||||
reflector.reflectionCapabilities = new ReflectionCapabilities();
|
||||
var ast = _parser.parseBinding(expression, 'location');
|
||||
var bindingRecords = [BindingRecord.createForElement(ast, 0, PROP_NAME)];
|
||||
return [BindingRecord.createForElement(ast, 0, PROP_NAME)];
|
||||
}
|
||||
|
||||
var strategy = null;
|
||||
function _convertLocalsToVariableBindings(locals: Locals): List<any> {
|
||||
var variableBindings = [];
|
||||
var directiveRecords = [];
|
||||
return new ChangeDetectorDefinition(id, strategy, variableBindings, bindingRecords,
|
||||
directiveRecords);
|
||||
var loc = locals;
|
||||
while (isPresent(loc) && isPresent(loc.current)) {
|
||||
MapWrapper.forEach(loc.current, (v, k) => ListWrapper.push(variableBindings, k));
|
||||
loc = loc.parent;
|
||||
}
|
||||
return variableBindings;
|
||||
}
|
||||
|
||||
export var PROP_NAME = 'propName';
|
||||
@ -22,21 +33,46 @@ export var PROP_NAME = 'propName';
|
||||
/**
|
||||
* In this case, we expect `id` and `expression` to be the same string.
|
||||
*/
|
||||
export function getDefinition(id: string): ChangeDetectorDefinition {
|
||||
if (ListWrapper.indexOf(_availableDefinitions, id) < 0) {
|
||||
export function getDefinition(id: string): TestDefinition {
|
||||
var expression = null;
|
||||
var locals = null;
|
||||
if (MapWrapper.contains(_availableDefinitionsWithLocals, id)) {
|
||||
var val = MapWrapper.get(_availableDefinitionsWithLocals, id);
|
||||
expression = val.expression;
|
||||
locals = val.locals;
|
||||
} else if (ListWrapper.indexOf(_availableDefinitions, id) >= 0) {
|
||||
expression = id;
|
||||
}
|
||||
if (isBlank(expression)) {
|
||||
throw `No ChangeDetectorDefinition for ${id} available. Please modify this file if necessary.`;
|
||||
}
|
||||
return _createChangeDetectorDefinition(id, id);
|
||||
|
||||
var strategy = null;
|
||||
var variableBindings = isPresent(locals) ? _convertLocalsToVariableBindings(locals) : [];
|
||||
var bindingRecords = _createBindingRecords(expression);
|
||||
var directiveRecords = [];
|
||||
var cdDef = new ChangeDetectorDefinition(id, strategy, variableBindings, bindingRecords,
|
||||
directiveRecords);
|
||||
return new TestDefinition(id, cdDef, locals);
|
||||
}
|
||||
|
||||
export class TestDefinition {
|
||||
constructor(public id: string, public cdDef: ChangeDetectorDefinition, public locals: Locals) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all available ChangeDetectorDefinition objects. Used to pre-generate Dart
|
||||
* `ChangeDetector` classes.
|
||||
*/
|
||||
export function getAllDefinitions(): List<ChangeDetectorDefinition> {
|
||||
export function getAllDefinitions(): List<TestDefinition> {
|
||||
return ListWrapper.map(_availableDefinitions, (id) => getDefinition(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* The list of all test definitions this config supplies.
|
||||
* Items in this list that do not appear in other structures define tests with expressions
|
||||
* equivalent to their ids.
|
||||
*/
|
||||
var _availableDefinitions = [
|
||||
'10',
|
||||
'"str"',
|
||||
@ -79,5 +115,53 @@ var _availableDefinitions = [
|
||||
'address?.toString()',
|
||||
'sayHi("Jim")',
|
||||
'a()(99)',
|
||||
'a.sayHi("Jim")'
|
||||
'a.sayHi("Jim")',
|
||||
'valueFromLocals',
|
||||
'functionFromLocals',
|
||||
'nestedLocals',
|
||||
'fallbackLocals',
|
||||
'contextNestedPropertyWithLocals',
|
||||
'localPropertyWithSimilarContext'
|
||||
];
|
||||
|
||||
class _ExpressionWithLocals {
|
||||
constructor(public expression: string, public locals: Locals) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Map from test id to _ExpressionWithLocals.
|
||||
* Tests in this map define an expression and local values which those expressions refer to.
|
||||
*/
|
||||
var _availableDefinitionsWithLocals = MapWrapper.createFromPairs([
|
||||
[
|
||||
'valueFromLocals',
|
||||
new _ExpressionWithLocals('key',
|
||||
new Locals(null, MapWrapper.createFromPairs([['key', 'value']])))
|
||||
],
|
||||
[
|
||||
'functionFromLocals',
|
||||
new _ExpressionWithLocals(
|
||||
'key()', new Locals(null, MapWrapper.createFromPairs([['key', () => 'value']])))
|
||||
],
|
||||
[
|
||||
'nestedLocals',
|
||||
new _ExpressionWithLocals(
|
||||
'key', new Locals(new Locals(null, MapWrapper.createFromPairs([['key', 'value']])),
|
||||
MapWrapper.create()))
|
||||
],
|
||||
[
|
||||
'fallbackLocals',
|
||||
new _ExpressionWithLocals('name',
|
||||
new Locals(null, MapWrapper.createFromPairs([['key', 'value']])))
|
||||
],
|
||||
[
|
||||
'contextNestedPropertyWithLocals',
|
||||
new _ExpressionWithLocals('address.city',
|
||||
new Locals(null, MapWrapper.createFromPairs([['city', 'MTV']])))
|
||||
],
|
||||
[
|
||||
'localPropertyWithSimilarContext',
|
||||
new _ExpressionWithLocals('city',
|
||||
new Locals(null, MapWrapper.createFromPairs([['city', 'MTV']])))
|
||||
]
|
||||
]);
|
||||
|
Reference in New Issue
Block a user