feat(change_detection): add support for binary operations and literals

This commit is contained in:
vsavkin
2014-11-10 16:11:29 -08:00
parent b8e3617a1d
commit 79a9430f2c
6 changed files with 371 additions and 239 deletions

View File

@ -1,7 +1,8 @@
import {describe, it, xit, expect} from 'test_lib/test_lib';
import {ddescribe, describe, it, iit, xit, expect} from 'test_lib/test_lib';
import {List, ListWrapper} from 'facade/collection';
import {ImplicitReceiver, AccessMember} from 'change_detection/parser/ast';
import {Parser} from 'change_detection/parser/parser';
import {Lexer} from 'change_detection/parser/lexer';
import {ClosureMap} from 'change_detection/parser/closure_map';
import {
@ -16,77 +17,107 @@ import {Record} from 'change_detection/record';
export function main() {
function ast(exp:string) {
var parts = exp.split(".");
var cm = new ClosureMap();
return ListWrapper.reduce(parts, function (ast, fieldName) {
return new AccessMember(ast, fieldName, cm.getter(fieldName), cm.setter(fieldName));
}, new ImplicitReceiver());
var parser = new Parser(new Lexer(), new ClosureMap());
return parser.parseBinding(exp);
}
describe('change_detection', function() {
describe('ChangeDetection', function() {
it('should do simple watching', function() {
var person = new Person('misko', 38);
var pwg = new ProtoWatchGroup();
pwg.watch(ast('name'), 'name');
pwg.watch(ast('age'), 'age');
var dispatcher = new LoggingDispatcher();
var wg = pwg.instantiate(dispatcher);
wg.setContext(person);
function createChangeDetector(memo:string, exp:string, context = null) {
var pwg = new ProtoWatchGroup();
pwg.watch(ast(exp), memo, false);
var dispatcher = new LoggingDispatcher();
var wg = pwg.instantiate(dispatcher);
wg.setContext(context);
var cd = new ChangeDetector(wg);
return {"changeDetector" : cd, "dispatcher" : dispatcher};
}
function executeWatch(memo:string, exp:string, context = null) {
var res = createChangeDetector(memo, exp, context);
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"];
var cd = new ChangeDetector(wg);
cd.detectChanges();
expect(dispatcher.log).toEqual(['name=misko', 'age=38']);
expect(dispatcher.log).toEqual(['name=misko']);
dispatcher.clear();
cd.detectChanges();
expect(dispatcher.log).toEqual([]);
person.age = 1;
person.name = "Misko";
cd.detectChanges();
expect(dispatcher.log).toEqual(['name=Misko', 'age=1']);
expect(dispatcher.log).toEqual(['name=Misko']);
});
it('should watch chained properties', function() {
it('should watch chained properties', () => {
var address = new Address('Grenoble');
var person = new Person('Victor', 36, address);
var pwg = new ProtoWatchGroup();
pwg.watch(ast('address.city'), 'address.city', false);
var dispatcher = new LoggingDispatcher();
var wg = pwg.instantiate(dispatcher);
wg.setContext(person);
var person = new Person('Victor', address);
var cd = new ChangeDetector(wg);
cd.detectChanges();
expect(dispatcher.log).toEqual(['address.city=Grenoble']);
dispatcher.clear();
cd.detectChanges();
expect(dispatcher.log).toEqual([]);
address.city = 'Mountain View';
cd.detectChanges();
expect(dispatcher.log).toEqual(['address.city=Mountain View']);
expect(executeWatch('address.city', 'address.city', person))
.toEqual(['address.city=Grenoble']);
});
it("should watch literals", () => {
expect(executeWatch('const', '10')).toEqual(['const=10']);
});
it("should watch binary operations", () => {
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']);
});
});
});
}
class Person {
constructor(name:string, age:number, address:Address = null) {
constructor(name:string, address:Address = null) {
this.name = name;
this.age = age;
this.address = address;
}
toString():string {
var address = this.address == null ? '' : ' address=' + this.address.toString();
return 'name=' + this.name +
' age=' + this.age.toString() +
address;
return 'name=' + this.name + address;
}
}