2015-06-05 20:33:51 -04:00
|
|
|
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';
|
2015-06-02 21:09:10 -04:00
|
|
|
import {reflector} from 'angular2/src/reflection/reflection';
|
|
|
|
import {ReflectionCapabilities} from 'angular2/src/reflection/reflection_capabilities';
|
2015-05-14 16:14:45 -04:00
|
|
|
|
|
|
|
var _parser = new Parser(new Lexer());
|
|
|
|
|
2015-06-05 20:33:51 -04:00
|
|
|
function _createBindingRecords(expression: string): List<BindingRecord> {
|
2015-06-02 21:09:10 -04:00
|
|
|
reflector.reflectionCapabilities = new ReflectionCapabilities();
|
2015-05-14 16:14:45 -04:00
|
|
|
var ast = _parser.parseBinding(expression, 'location');
|
2015-06-05 20:33:51 -04:00
|
|
|
return [BindingRecord.createForElement(ast, 0, PROP_NAME)];
|
|
|
|
}
|
2015-05-14 16:14:45 -04:00
|
|
|
|
2015-06-05 20:33:51 -04:00
|
|
|
function _convertLocalsToVariableBindings(locals: Locals): List<any> {
|
2015-05-14 16:14:45 -04:00
|
|
|
var variableBindings = [];
|
2015-06-05 20:33:51 -04:00
|
|
|
var loc = locals;
|
|
|
|
while (isPresent(loc) && isPresent(loc.current)) {
|
|
|
|
MapWrapper.forEach(loc.current, (v, k) => ListWrapper.push(variableBindings, k));
|
|
|
|
loc = loc.parent;
|
|
|
|
}
|
|
|
|
return variableBindings;
|
2015-05-14 16:14:45 -04:00
|
|
|
}
|
|
|
|
|
2015-06-05 17:19:49 -04:00
|
|
|
export var PROP_NAME = 'propName';
|
|
|
|
|
2015-05-14 16:14:45 -04:00
|
|
|
/**
|
|
|
|
* In this case, we expect `id` and `expression` to be the same string.
|
|
|
|
*/
|
2015-06-05 20:33:51 -04:00
|
|
|
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)) {
|
2015-05-14 16:14:45 -04:00
|
|
|
throw `No ChangeDetectorDefinition for ${id} available. Please modify this file if necessary.`;
|
|
|
|
}
|
2015-06-05 20:33:51 -04:00
|
|
|
|
|
|
|
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) {}
|
2015-05-14 16:14:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all available ChangeDetectorDefinition objects. Used to pre-generate Dart
|
|
|
|
* `ChangeDetector` classes.
|
|
|
|
*/
|
2015-06-05 20:33:51 -04:00
|
|
|
export function getAllDefinitions(): List<TestDefinition> {
|
2015-06-05 17:19:49 -04:00
|
|
|
return ListWrapper.map(_availableDefinitions, (id) => getDefinition(id));
|
2015-05-14 16:14:45 -04:00
|
|
|
}
|
|
|
|
|
2015-06-05 20:33:51 -04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-05-14 16:14:45 -04:00
|
|
|
var _availableDefinitions = [
|
|
|
|
'10',
|
|
|
|
'"str"',
|
|
|
|
'"a\n\nb"',
|
|
|
|
'10 + 2',
|
|
|
|
'10 - 2',
|
|
|
|
'10 * 2',
|
|
|
|
'10 / 2',
|
|
|
|
'11 % 2',
|
|
|
|
'1 == 1',
|
|
|
|
'1 != 1',
|
|
|
|
'1 == true',
|
|
|
|
'1 === 1',
|
|
|
|
'1 !== 1',
|
|
|
|
'1 === true',
|
|
|
|
'1 < 2',
|
|
|
|
'2 < 1',
|
|
|
|
'1 > 2',
|
|
|
|
'2 > 1',
|
|
|
|
'1 <= 2',
|
|
|
|
'2 <= 2',
|
|
|
|
'2 <= 1',
|
|
|
|
'2 >= 1',
|
|
|
|
'2 >= 2',
|
|
|
|
'1 >= 2',
|
|
|
|
'true && true',
|
|
|
|
'true && false',
|
|
|
|
'true || false',
|
|
|
|
'false || false',
|
|
|
|
'!true',
|
|
|
|
'!!true',
|
|
|
|
'1 < 2 ? 1 : 2',
|
|
|
|
'1 > 2 ? 1 : 2',
|
|
|
|
'["foo", "bar"][0]',
|
2015-06-02 21:09:10 -04:00
|
|
|
'{"foo": "bar"}["foo"]',
|
2015-06-08 18:23:27 -04:00
|
|
|
'name',
|
|
|
|
'[1, 2]',
|
|
|
|
'[1, a]',
|
|
|
|
'{z: 1}',
|
|
|
|
'{z: a}',
|
|
|
|
'name | pipe',
|
2015-06-02 21:09:10 -04:00
|
|
|
'value',
|
|
|
|
'a',
|
|
|
|
'address.city',
|
|
|
|
'address?.city',
|
|
|
|
'address?.toString()',
|
|
|
|
'sayHi("Jim")',
|
|
|
|
'a()(99)',
|
2015-06-05 20:33:51 -04:00
|
|
|
'a.sayHi("Jim")',
|
|
|
|
'valueFromLocals',
|
|
|
|
'functionFromLocals',
|
|
|
|
'nestedLocals',
|
|
|
|
'fallbackLocals',
|
|
|
|
'contextNestedPropertyWithLocals',
|
|
|
|
'localPropertyWithSimilarContext'
|
2015-05-14 16:14:45 -04:00
|
|
|
];
|
2015-06-05 20:33:51 -04:00
|
|
|
|
|
|
|
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']])))
|
|
|
|
]
|
|
|
|
]);
|