2015-06-08 21:07:06 -04:00
|
|
|
import {ListWrapper, MapWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
|
2015-06-05 20:33:51 -04:00
|
|
|
import {isBlank, isPresent} from 'angular2/src/facade/lang';
|
|
|
|
import {
|
2015-06-08 21:07:06 -04:00
|
|
|
DEFAULT,
|
|
|
|
ON_PUSH,
|
2015-06-05 20:33:51 -04:00
|
|
|
BindingRecord,
|
|
|
|
ChangeDetectorDefinition,
|
2015-06-08 21:07:06 -04:00
|
|
|
DirectiveIndex,
|
|
|
|
DirectiveRecord,
|
2015-06-05 20:33:51 -04:00
|
|
|
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
|
|
|
|
2015-06-10 17:56:08 -04:00
|
|
|
/*
|
|
|
|
* This file defines `ChangeDetectorDefinition` objects which are used in the tests defined in
|
|
|
|
* the change_detector_spec library. Please see that library for more information.
|
|
|
|
*/
|
|
|
|
|
2015-05-14 16:14:45 -04:00
|
|
|
var _parser = new Parser(new Lexer());
|
|
|
|
|
2015-06-09 21:00:24 -04:00
|
|
|
function _getParser() {
|
2015-06-02 21:09:10 -04:00
|
|
|
reflector.reflectionCapabilities = new ReflectionCapabilities();
|
2015-06-09 21:00:24 -04:00
|
|
|
return _parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _createBindingRecords(expression: string): List<BindingRecord> {
|
|
|
|
var ast = _getParser().parseBinding(expression, 'location');
|
2015-06-18 18:44:44 -04:00
|
|
|
return [BindingRecord.createForElementProperty(ast, 0, PROP_NAME)];
|
2015-06-05 20:33:51 -04:00
|
|
|
}
|
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)) {
|
2015-06-17 14:17:21 -04:00
|
|
|
MapWrapper.forEach(loc.current, (v, k) => variableBindings.push(k));
|
2015-06-05 20:33:51 -04:00
|
|
|
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 {
|
2015-06-08 21:07:06 -04:00
|
|
|
var testDef = null;
|
|
|
|
if (StringMapWrapper.contains(_ExpressionWithLocals.availableDefinitions, id)) {
|
|
|
|
let val = StringMapWrapper.get(_ExpressionWithLocals.availableDefinitions, id);
|
|
|
|
let cdDef = val.createChangeDetectorDefinition();
|
|
|
|
cdDef.id = id;
|
|
|
|
testDef = new TestDefinition(id, cdDef, val.locals);
|
|
|
|
} else if (StringMapWrapper.contains(_ExpressionWithMode.availableDefinitions, id)) {
|
|
|
|
let val = StringMapWrapper.get(_ExpressionWithMode.availableDefinitions, id);
|
|
|
|
let cdDef = val.createChangeDetectorDefinition();
|
|
|
|
cdDef.id = id;
|
|
|
|
testDef = new TestDefinition(id, cdDef, null);
|
2015-06-09 21:00:24 -04:00
|
|
|
} else if (StringMapWrapper.contains(_DirectiveUpdating.availableDefinitions, id)) {
|
|
|
|
let val = StringMapWrapper.get(_DirectiveUpdating.availableDefinitions, id);
|
|
|
|
let cdDef = val.createChangeDetectorDefinition();
|
|
|
|
cdDef.id = id;
|
|
|
|
testDef = new TestDefinition(id, cdDef, null);
|
2015-06-05 20:33:51 -04:00
|
|
|
} else if (ListWrapper.indexOf(_availableDefinitions, id) >= 0) {
|
2015-06-08 21:07:06 -04:00
|
|
|
var strategy = null;
|
|
|
|
var variableBindings = [];
|
|
|
|
var bindingRecords = _createBindingRecords(id);
|
|
|
|
var directiveRecords = [];
|
|
|
|
let cdDef = new ChangeDetectorDefinition(id, strategy, variableBindings, bindingRecords,
|
|
|
|
directiveRecords);
|
|
|
|
testDef = new TestDefinition(id, cdDef, null);
|
2015-06-05 20:33:51 -04:00
|
|
|
}
|
2015-06-08 21:07:06 -04:00
|
|
|
if (isBlank(testDef)) {
|
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
|
|
|
|
2015-06-08 21:07:06 -04:00
|
|
|
return testDef;
|
2015-06-05 20:33:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09 21:00:24 -04:00
|
|
|
var allDefs = _availableDefinitions;
|
2015-06-12 10:50:45 -04:00
|
|
|
allDefs = ListWrapper.concat(allDefs,
|
|
|
|
StringMapWrapper.keys(_ExpressionWithLocals.availableDefinitions));
|
|
|
|
allDefs =
|
|
|
|
ListWrapper.concat(allDefs, StringMapWrapper.keys(_ExpressionWithMode.availableDefinitions));
|
|
|
|
allDefs =
|
|
|
|
ListWrapper.concat(allDefs, StringMapWrapper.keys(_DirectiveUpdating.availableDefinitions));
|
2015-06-09 21:00:24 -04:00
|
|
|
return ListWrapper.map(allDefs, (id) => getDefinition(id));
|
2015-05-14 16:14:45 -04:00
|
|
|
}
|
|
|
|
|
2015-06-05 20:33:51 -04:00
|
|
|
class _ExpressionWithLocals {
|
2015-06-08 21:07:06 -04:00
|
|
|
constructor(private _expression: string, public locals: Locals) {}
|
|
|
|
|
|
|
|
createChangeDetectorDefinition(): ChangeDetectorDefinition {
|
|
|
|
var strategy = null;
|
|
|
|
var variableBindings = _convertLocalsToVariableBindings(this.locals);
|
|
|
|
var bindingRecords = _createBindingRecords(this._expression);
|
|
|
|
var directiveRecords = [];
|
|
|
|
return new ChangeDetectorDefinition('(empty id)', strategy, variableBindings, bindingRecords,
|
|
|
|
directiveRecords);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map from test id to _ExpressionWithLocals.
|
|
|
|
* Tests in this map define an expression and local values which those expressions refer to.
|
|
|
|
*/
|
|
|
|
static availableDefinitions: StringMap<string, _ExpressionWithLocals> = {
|
|
|
|
'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(
|
2015-06-17 19:21:40 -04:00
|
|
|
'key',
|
|
|
|
new Locals(new Locals(null, MapWrapper.createFromPairs([['key', 'value']])), new Map())),
|
2015-06-08 21:07:06 -04:00
|
|
|
'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']])))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ExpressionWithMode {
|
|
|
|
constructor(private _strategy: string, private _withRecords: boolean) {}
|
|
|
|
|
|
|
|
createChangeDetectorDefinition(): ChangeDetectorDefinition {
|
|
|
|
var variableBindings = [];
|
|
|
|
var bindingRecords = null;
|
|
|
|
var directiveRecords = null;
|
|
|
|
if (this._withRecords) {
|
|
|
|
var dirRecordWithOnPush =
|
|
|
|
new DirectiveRecord({directiveIndex: new DirectiveIndex(0, 0), changeDetection: ON_PUSH});
|
|
|
|
var updateDirWithOnPushRecord =
|
2015-06-09 21:00:24 -04:00
|
|
|
BindingRecord.createForDirective(_getParser().parseBinding('42', 'location'), 'a',
|
2015-06-08 21:07:06 -04:00
|
|
|
(o, v) => (<any>o).a = v, dirRecordWithOnPush);
|
|
|
|
bindingRecords = [updateDirWithOnPushRecord];
|
|
|
|
directiveRecords = [dirRecordWithOnPush];
|
|
|
|
} else {
|
|
|
|
bindingRecords = [];
|
|
|
|
directiveRecords = [];
|
|
|
|
}
|
|
|
|
return new ChangeDetectorDefinition('(empty id)', this._strategy, variableBindings,
|
|
|
|
bindingRecords, directiveRecords);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map from test id to _ExpressionWithMode.
|
|
|
|
* Definitions in this map define conditions which allow testing various change detector modes.
|
|
|
|
*/
|
|
|
|
static availableDefinitions: StringMap<string, _ExpressionWithMode> = {
|
|
|
|
'emptyUsingDefaultStrategy': new _ExpressionWithMode(DEFAULT, false),
|
|
|
|
'emptyUsingOnPushStrategy': new _ExpressionWithMode(ON_PUSH, false),
|
|
|
|
'onPushRecordsUsingDefaultStrategy': new _ExpressionWithMode(DEFAULT, true)
|
|
|
|
};
|
2015-06-05 20:33:51 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 21:00:24 -04:00
|
|
|
class _DirectiveUpdating {
|
|
|
|
constructor(private _bindingRecords: List<BindingRecord>,
|
|
|
|
private _directiveRecords: List<DirectiveRecord>) {}
|
|
|
|
|
|
|
|
createChangeDetectorDefinition(): ChangeDetectorDefinition {
|
|
|
|
var strategy = null;
|
|
|
|
var variableBindings = [];
|
|
|
|
|
|
|
|
return new ChangeDetectorDefinition('(empty id)', strategy, variableBindings,
|
|
|
|
this._bindingRecords, this._directiveRecords);
|
|
|
|
}
|
|
|
|
|
|
|
|
static updateA(expression: string, dirRecord): BindingRecord {
|
|
|
|
return BindingRecord.createForDirective(_getParser().parseBinding(expression, 'location'), 'a',
|
|
|
|
(o, v) => (<any>o).a = v, dirRecord);
|
|
|
|
}
|
|
|
|
|
|
|
|
static updateB(expression: string, dirRecord): BindingRecord {
|
|
|
|
return BindingRecord.createForDirective(_getParser().parseBinding(expression, 'location'), 'b',
|
|
|
|
(o, v) => (<any>o).b = v, dirRecord);
|
|
|
|
}
|
|
|
|
|
|
|
|
static basicRecords: List<DirectiveRecord> = [
|
|
|
|
new DirectiveRecord({
|
|
|
|
directiveIndex: new DirectiveIndex(0, 0),
|
|
|
|
callOnChange: true,
|
|
|
|
callOnCheck: true,
|
|
|
|
callOnAllChangesDone: true
|
|
|
|
}),
|
|
|
|
new DirectiveRecord({
|
|
|
|
directiveIndex: new DirectiveIndex(0, 1),
|
|
|
|
callOnChange: true,
|
|
|
|
callOnCheck: true,
|
|
|
|
callOnAllChangesDone: true
|
|
|
|
})
|
|
|
|
];
|
|
|
|
|
|
|
|
static recordNoCallbacks = new DirectiveRecord({
|
|
|
|
directiveIndex: new DirectiveIndex(0, 0),
|
|
|
|
callOnChange: false,
|
|
|
|
callOnCheck: false,
|
|
|
|
callOnAllChangesDone: false
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map from test id to _DirectiveUpdating.
|
|
|
|
* Definitions in this map define definitions which allow testing directive updating.
|
|
|
|
*/
|
|
|
|
static availableDefinitions: StringMap<string, _DirectiveUpdating> = {
|
|
|
|
'directNoDispatcher': new _DirectiveUpdating(
|
|
|
|
[_DirectiveUpdating.updateA('42', _DirectiveUpdating.basicRecords[0])],
|
|
|
|
[_DirectiveUpdating.basicRecords[0]]),
|
|
|
|
'groupChanges': new _DirectiveUpdating(
|
|
|
|
[
|
|
|
|
_DirectiveUpdating.updateA('1', _DirectiveUpdating.basicRecords[0]),
|
|
|
|
_DirectiveUpdating.updateB('2', _DirectiveUpdating.basicRecords[0]),
|
|
|
|
BindingRecord.createDirectiveOnChange(_DirectiveUpdating.basicRecords[0]),
|
|
|
|
_DirectiveUpdating.updateA('3', _DirectiveUpdating.basicRecords[1]),
|
|
|
|
BindingRecord.createDirectiveOnChange(_DirectiveUpdating.basicRecords[1])
|
|
|
|
],
|
|
|
|
[_DirectiveUpdating.basicRecords[0], _DirectiveUpdating.basicRecords[1]]),
|
|
|
|
'directiveOnCheck': new _DirectiveUpdating(
|
|
|
|
[BindingRecord.createDirectiveOnCheck(_DirectiveUpdating.basicRecords[0])],
|
|
|
|
[_DirectiveUpdating.basicRecords[0]]),
|
|
|
|
'directiveOnInit': new _DirectiveUpdating(
|
|
|
|
[BindingRecord.createDirectiveOnInit(_DirectiveUpdating.basicRecords[0])],
|
|
|
|
[_DirectiveUpdating.basicRecords[0]]),
|
|
|
|
'emptyWithDirectiveRecords': new _DirectiveUpdating(
|
|
|
|
[], [_DirectiveUpdating.basicRecords[0], _DirectiveUpdating.basicRecords[1]]),
|
|
|
|
'noCallbacks': new _DirectiveUpdating(
|
|
|
|
[_DirectiveUpdating.updateA('1', _DirectiveUpdating.recordNoCallbacks)],
|
|
|
|
[_DirectiveUpdating.recordNoCallbacks]),
|
2015-06-19 13:18:44 -04:00
|
|
|
'readingDirectives':
|
|
|
|
new _DirectiveUpdating(
|
|
|
|
[
|
|
|
|
BindingRecord.createForHostProperty(
|
|
|
|
new DirectiveIndex(0, 0), _getParser().parseBinding('a', 'location'), PROP_NAME)
|
|
|
|
],
|
|
|
|
[_DirectiveUpdating.basicRecords[0]]),
|
|
|
|
'interpolation':
|
|
|
|
new _DirectiveUpdating(
|
|
|
|
[
|
2015-06-18 18:44:44 -04:00
|
|
|
BindingRecord.createForElementProperty(
|
|
|
|
_getParser().parseInterpolation('B{{a}}A', 'location'), 0, PROP_NAME)
|
2015-06-19 13:18:44 -04:00
|
|
|
],
|
|
|
|
[])
|
2015-06-09 21:00:24 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-06-05 20:33:51 -04:00
|
|
|
/**
|
2015-06-08 21:07:06 -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-06-05 20:33:51 -04:00
|
|
|
*/
|
2015-06-09 21:00:24 -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]',
|
|
|
|
'{"foo": "bar"}["foo"]',
|
|
|
|
'name',
|
|
|
|
'[1, 2]',
|
|
|
|
'[1, a]',
|
|
|
|
'{z: 1}',
|
|
|
|
'{z: a}',
|
|
|
|
'name | pipe',
|
|
|
|
'value',
|
|
|
|
'a',
|
|
|
|
'address.city',
|
|
|
|
'address?.city',
|
|
|
|
'address?.toString()',
|
|
|
|
'sayHi("Jim")',
|
|
|
|
'a()(99)',
|
2015-06-17 15:56:11 -04:00
|
|
|
'a.sayHi("Jim")',
|
|
|
|
'passThrough([12])'
|
2015-06-09 21:00:24 -04:00
|
|
|
];
|