2014-11-10 19:11:29 -05:00
|
|
|
import {ddescribe, describe, it, iit, xit, expect} from 'test_lib/test_lib';
|
2014-10-02 09:14:32 -04:00
|
|
|
|
2014-11-18 20:26:38 -05:00
|
|
|
import {isPresent} from 'facade/lang';
|
2014-11-11 20:47:45 -05:00
|
|
|
import {List, ListWrapper, MapWrapper} from 'facade/collection';
|
2014-11-26 12:44:31 -05:00
|
|
|
import {ContextWithVariableBindings} from 'change_detection/parser/context_with_variable_bindings';
|
2014-11-10 19:11:29 -05:00
|
|
|
import {Parser} from 'change_detection/parser/parser';
|
|
|
|
import {Lexer} from 'change_detection/parser/lexer';
|
2014-10-02 09:14:32 -04:00
|
|
|
|
|
|
|
import {
|
|
|
|
ChangeDetector,
|
2014-11-19 18:52:01 -05:00
|
|
|
ProtoRecordRange,
|
|
|
|
RecordRange,
|
2014-10-28 12:22:38 -04:00
|
|
|
WatchGroupDispatcher,
|
|
|
|
ProtoRecord
|
2014-10-02 09:14:32 -04:00
|
|
|
} from 'change_detection/change_detector';
|
|
|
|
|
|
|
|
import {Record} from 'change_detection/record';
|
|
|
|
|
|
|
|
export function main() {
|
2014-10-28 12:22:38 -04:00
|
|
|
function ast(exp:string) {
|
2014-11-20 15:07:48 -05:00
|
|
|
var parser = new Parser(new Lexer());
|
2014-11-18 19:38:36 -05:00
|
|
|
return parser.parseBinding(exp).ast;
|
2014-10-28 12:22:38 -04:00
|
|
|
}
|
|
|
|
|
2014-11-11 19:30:54 -05:00
|
|
|
function createChangeDetector(memo:string, exp:string, context = null, formatters = null) {
|
2014-11-19 18:52:01 -05:00
|
|
|
var prr = new ProtoRecordRange();
|
|
|
|
prr.addRecordsFromAST(ast(exp), memo, false);
|
2014-11-10 19:11:29 -05:00
|
|
|
|
|
|
|
var dispatcher = new LoggingDispatcher();
|
2014-11-19 18:52:01 -05:00
|
|
|
var rr = prr.instantiate(dispatcher, formatters);
|
|
|
|
rr.setContext(context);
|
2014-11-10 19:11:29 -05:00
|
|
|
|
2014-11-19 18:52:01 -05:00
|
|
|
var cd = new ChangeDetector(rr);
|
2014-11-10 19:11:29 -05:00
|
|
|
|
|
|
|
return {"changeDetector" : cd, "dispatcher" : dispatcher};
|
|
|
|
}
|
|
|
|
|
2014-11-11 19:30:54 -05:00
|
|
|
function executeWatch(memo:string, exp:string, context = null, formatters = null) {
|
|
|
|
var res = createChangeDetector(memo, exp, context, formatters);
|
2014-11-10 19:11:29 -05:00
|
|
|
res["changeDetector"].detectChanges();
|
|
|
|
return res["dispatcher"].log;
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('change_detection', () => {
|
|
|
|
describe('ChangeDetection', () => {
|
|
|
|
it('should do simple watching', () => {
|
|
|
|
var person = new Person("misko");
|
|
|
|
|
|
|
|
var c = createChangeDetector('name', 'name', person);
|
|
|
|
var cd = c["changeDetector"];
|
|
|
|
var dispatcher = c["dispatcher"];
|
|
|
|
|
2014-10-02 09:14:32 -04:00
|
|
|
cd.detectChanges();
|
2014-11-10 19:11:29 -05:00
|
|
|
expect(dispatcher.log).toEqual(['name=misko']);
|
2014-10-28 12:22:38 -04:00
|
|
|
|
2014-10-02 09:14:32 -04:00
|
|
|
dispatcher.clear();
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(dispatcher.log).toEqual([]);
|
2014-10-28 12:22:38 -04:00
|
|
|
|
2014-10-02 09:14:32 -04:00
|
|
|
person.name = "Misko";
|
|
|
|
cd.detectChanges();
|
2014-11-10 19:11:29 -05:00
|
|
|
expect(dispatcher.log).toEqual(['name=Misko']);
|
2014-10-02 09:14:32 -04:00
|
|
|
});
|
2014-10-27 12:57:36 -04:00
|
|
|
|
2014-11-11 19:30:54 -05:00
|
|
|
it('should support chained properties', () => {
|
2014-10-27 12:57:36 -04:00
|
|
|
var address = new Address('Grenoble');
|
2014-11-10 19:11:29 -05:00
|
|
|
var person = new Person('Victor', address);
|
2014-10-28 12:22:38 -04:00
|
|
|
|
2014-11-10 19:11:29 -05:00
|
|
|
expect(executeWatch('address.city', 'address.city', person))
|
|
|
|
.toEqual(['address.city=Grenoble']);
|
|
|
|
});
|
2014-10-28 12:22:38 -04:00
|
|
|
|
2014-11-11 19:54:15 -05:00
|
|
|
it("should support method calls", () => {
|
|
|
|
var person = new Person('Victor');
|
|
|
|
expect(executeWatch('m', 'sayHi("Jim")', person)).toEqual(['m=Hi, Jim']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should support function calls", () => {
|
|
|
|
var td = new TestData(() => (a) => a);
|
|
|
|
expect(executeWatch('value', 'a()(99)', td)).toEqual(['value=99']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should support chained method calls", () => {
|
|
|
|
var person = new Person('Victor');
|
|
|
|
var td = new TestData(person);
|
|
|
|
expect(executeWatch('m', 'a.sayHi("Jim")', td)).toEqual(['m=Hi, Jim']);
|
|
|
|
});
|
|
|
|
|
2014-11-11 19:30:54 -05:00
|
|
|
it("should support literals", () => {
|
2014-11-10 19:11:29 -05:00
|
|
|
expect(executeWatch('const', '10')).toEqual(['const=10']);
|
2014-11-11 20:39:40 -05:00
|
|
|
expect(executeWatch('const', '"str"')).toEqual(['const=str']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should support literal array", () => {
|
|
|
|
var c = createChangeDetector('array', '[1,2]');
|
|
|
|
c["changeDetector"].detectChanges();
|
|
|
|
expect(c["dispatcher"].loggedValues).toEqual([[1,2]]);
|
|
|
|
|
|
|
|
c = createChangeDetector('array', '[1,a]', new TestData(2));
|
|
|
|
c["changeDetector"].detectChanges();
|
|
|
|
expect(c["dispatcher"].loggedValues).toEqual([[1,2]]);
|
2014-10-27 12:57:36 -04:00
|
|
|
});
|
|
|
|
|
2014-11-11 20:47:45 -05:00
|
|
|
it("should support literal maps", () => {
|
|
|
|
var c = createChangeDetector('map', '{z:1}');
|
|
|
|
c["changeDetector"].detectChanges();
|
|
|
|
expect(MapWrapper.get(c["dispatcher"].loggedValues[0], 'z')).toEqual(1);
|
|
|
|
|
|
|
|
c = createChangeDetector('map', '{z:a}', new TestData(1));
|
|
|
|
c["changeDetector"].detectChanges();
|
|
|
|
expect(MapWrapper.get(c["dispatcher"].loggedValues[0], 'z')).toEqual(1);
|
|
|
|
});
|
|
|
|
|
2014-11-11 19:30:54 -05:00
|
|
|
it("should support binary operations", () => {
|
2014-11-10 19:11:29 -05:00
|
|
|
expect(executeWatch('exp', '10 + 2')).toEqual(['exp=12']);
|
|
|
|
expect(executeWatch('exp', '10 - 2')).toEqual(['exp=8']);
|
|
|
|
|
|
|
|
expect(executeWatch('exp', '10 * 2')).toEqual(['exp=20']);
|
|
|
|
expect(executeWatch('exp', '10 / 2')).toEqual([`exp=${5.0}`]); //dart exp=5.0, js exp=5
|
|
|
|
expect(executeWatch('exp', '11 % 2')).toEqual(['exp=1']);
|
|
|
|
|
|
|
|
expect(executeWatch('exp', '1 == 1')).toEqual(['exp=true']);
|
|
|
|
expect(executeWatch('exp', '1 != 1')).toEqual(['exp=false']);
|
|
|
|
|
|
|
|
expect(executeWatch('exp', '1 < 2')).toEqual(['exp=true']);
|
|
|
|
expect(executeWatch('exp', '2 < 1')).toEqual(['exp=false']);
|
|
|
|
|
|
|
|
expect(executeWatch('exp', '2 > 1')).toEqual(['exp=true']);
|
|
|
|
expect(executeWatch('exp', '2 < 1')).toEqual(['exp=false']);
|
|
|
|
|
|
|
|
expect(executeWatch('exp', '1 <= 2')).toEqual(['exp=true']);
|
|
|
|
expect(executeWatch('exp', '2 <= 2')).toEqual(['exp=true']);
|
|
|
|
expect(executeWatch('exp', '2 <= 1')).toEqual(['exp=false']);
|
|
|
|
|
|
|
|
expect(executeWatch('exp', '2 >= 1')).toEqual(['exp=true']);
|
|
|
|
expect(executeWatch('exp', '2 >= 2')).toEqual(['exp=true']);
|
|
|
|
expect(executeWatch('exp', '1 >= 2')).toEqual(['exp=false']);
|
|
|
|
|
|
|
|
expect(executeWatch('exp', 'true && true')).toEqual(['exp=true']);
|
|
|
|
expect(executeWatch('exp', 'true && false')).toEqual(['exp=false']);
|
|
|
|
|
|
|
|
expect(executeWatch('exp', 'true || false')).toEqual(['exp=true']);
|
|
|
|
expect(executeWatch('exp', 'false || false')).toEqual(['exp=false']);
|
|
|
|
});
|
2014-11-11 19:30:54 -05:00
|
|
|
|
2014-11-11 20:02:59 -05:00
|
|
|
it("should support negate", () => {
|
|
|
|
expect(executeWatch('exp', '!true')).toEqual(['exp=false']);
|
|
|
|
expect(executeWatch('exp', '!!true')).toEqual(['exp=true']);
|
|
|
|
});
|
|
|
|
|
2014-11-11 20:18:09 -05:00
|
|
|
it("should support conditionals", () => {
|
|
|
|
expect(executeWatch('m', '1 < 2 ? 1 : 2')).toEqual(['m=1']);
|
|
|
|
expect(executeWatch('m', '1 > 2 ? 1 : 2')).toEqual(['m=2']);
|
|
|
|
});
|
|
|
|
|
2014-11-14 18:35:41 -05:00
|
|
|
describe("formatters", () => {
|
|
|
|
it("should support formatters", () => {
|
|
|
|
var formatters = MapWrapper.createFromPairs([
|
|
|
|
['uppercase', (v) => v.toUpperCase()],
|
|
|
|
['wrap', (v, before, after) => `${before}${v}${after}`]]);
|
|
|
|
expect(executeWatch('str', '"aBc" | uppercase', null, formatters)).toEqual(['str=ABC']);
|
|
|
|
expect(executeWatch('str', '"b" | wrap:"a":"c"', null, formatters)).toEqual(['str=abc']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should rerun formatters only when arguments change", () => {
|
|
|
|
var counter = 0;
|
|
|
|
var formatters = MapWrapper.createFromPairs([
|
|
|
|
['formatter', (_) => {counter += 1; return 'value'}]
|
|
|
|
]);
|
|
|
|
|
|
|
|
var person = new Person('Jim');
|
|
|
|
|
|
|
|
var c = createChangeDetector('formatter', 'name | formatter', person, formatters);
|
|
|
|
var cd = c['changeDetector'];
|
|
|
|
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(counter).toEqual(1);
|
|
|
|
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(counter).toEqual(1);
|
|
|
|
|
|
|
|
person.name = 'bob';
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(counter).toEqual(2);
|
|
|
|
});
|
2014-11-11 19:30:54 -05:00
|
|
|
});
|
2014-11-26 12:44:31 -05:00
|
|
|
|
|
|
|
describe("ContextWithVariableBindings", () => {
|
|
|
|
it('should read a field from ContextWithVariableBindings', () => {
|
|
|
|
var locals = new ContextWithVariableBindings(null,
|
|
|
|
MapWrapper.createFromPairs([["key", "value"]]));
|
|
|
|
|
|
|
|
expect(executeWatch('key', 'key', locals))
|
|
|
|
.toEqual(['key=value']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle nested ContextWithVariableBindings', () => {
|
|
|
|
var nested = new ContextWithVariableBindings(null,
|
|
|
|
MapWrapper.createFromPairs([["key", "value"]]));
|
|
|
|
var locals = new ContextWithVariableBindings(nested, MapWrapper.create());
|
|
|
|
|
|
|
|
expect(executeWatch('key', 'key', locals))
|
|
|
|
.toEqual(['key=value']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fall back to a regular field read when ContextWithVariableBindings " +
|
|
|
|
"does not have the requested field", () => {
|
|
|
|
var locals = new ContextWithVariableBindings(new Person("Jim"),
|
|
|
|
MapWrapper.createFromPairs([["key", "value"]]));
|
|
|
|
|
|
|
|
expect(executeWatch('name', 'name', locals))
|
|
|
|
.toEqual(['name=Jim']);
|
|
|
|
});
|
|
|
|
});
|
2014-10-02 09:14:32 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class Person {
|
2014-11-22 00:19:23 -05:00
|
|
|
name:string;
|
|
|
|
address:Address;
|
2014-11-10 19:11:29 -05:00
|
|
|
constructor(name:string, address:Address = null) {
|
2014-10-02 09:14:32 -04:00
|
|
|
this.name = name;
|
2014-10-27 12:57:36 -04:00
|
|
|
this.address = address;
|
|
|
|
}
|
|
|
|
|
2014-11-11 19:54:15 -05:00
|
|
|
sayHi(m) {
|
|
|
|
return `Hi, ${m}`;
|
|
|
|
}
|
|
|
|
|
2014-10-27 12:57:36 -04:00
|
|
|
toString():string {
|
|
|
|
var address = this.address == null ? '' : ' address=' + this.address.toString();
|
|
|
|
|
2014-11-10 19:11:29 -05:00
|
|
|
return 'name=' + this.name + address;
|
2014-10-27 12:57:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Address {
|
2014-11-22 00:19:23 -05:00
|
|
|
city:string;
|
2014-10-27 12:57:36 -04:00
|
|
|
constructor(city:string) {
|
|
|
|
this.city = city;
|
2014-10-02 09:14:32 -04:00
|
|
|
}
|
|
|
|
|
2014-10-27 12:57:36 -04:00
|
|
|
toString():string {
|
|
|
|
return this.city;
|
2014-10-02 09:14:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 19:54:15 -05:00
|
|
|
class TestData {
|
2014-11-22 00:19:23 -05:00
|
|
|
a;
|
2014-11-11 19:54:15 -05:00
|
|
|
constructor(a) {
|
|
|
|
this.a = a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-02 09:14:32 -04:00
|
|
|
class LoggingDispatcher extends WatchGroupDispatcher {
|
2014-11-22 00:19:23 -05:00
|
|
|
log:List;
|
|
|
|
loggedValues:List;
|
2014-10-02 09:14:32 -04:00
|
|
|
constructor() {
|
|
|
|
this.log = null;
|
2014-11-11 20:39:40 -05:00
|
|
|
this.loggedValues = null;
|
2014-10-02 09:14:32 -04:00
|
|
|
this.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
this.log = ListWrapper.create();
|
2014-11-11 20:39:40 -05:00
|
|
|
this.loggedValues = ListWrapper.create();
|
2014-10-02 09:14:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
onRecordChange(record:Record, context) {
|
2014-11-11 20:39:40 -05:00
|
|
|
ListWrapper.push(this.loggedValues, record.currentValue);
|
2014-10-02 09:14:32 -04:00
|
|
|
ListWrapper.push(this.log, context + '=' + record.currentValue.toString());
|
|
|
|
}
|
|
|
|
}
|