refactor(ChangeDetection): convert change detection tests to typescript
This commit is contained in:
parent
34cfc9f474
commit
9d90128463
|
@ -2,7 +2,7 @@ var _global: BrowserNodeGlobal = <any>(typeof window === 'undefined' ? global :
|
|||
export {_global as global};
|
||||
|
||||
export var Type = Function;
|
||||
export type Type = new (...args: any[]) => any;
|
||||
export type Type = new (... args: any[]) => any;
|
||||
|
||||
export class BaseException extends Error {
|
||||
message;
|
||||
|
@ -116,7 +116,7 @@ export class StringWrapper {
|
|||
}
|
||||
|
||||
static replaceAllMapped(s: string, from: RegExp, cb: Function): string {
|
||||
return s.replace(from, function(...matches) {
|
||||
return s.replace(from, function(... matches) {
|
||||
// Remove offset & string from the result array
|
||||
matches.splice(-2, 2);
|
||||
// The callback receives match, p1, ..., pn
|
||||
|
|
|
@ -78,8 +78,8 @@ export class TestBed {
|
|||
* @return {Promise<ViewProxy>}
|
||||
*/
|
||||
createView(component: Type,
|
||||
{context = null, html = null}: {context?: any,
|
||||
html?: string} = {}): Promise<ViewProxy> {
|
||||
{context = null,
|
||||
html = null}: {context?: any, html?: string} = {}): Promise<ViewProxy> {
|
||||
if (isBlank(component) && isBlank(context)) {
|
||||
throw new BaseException('You must specified at least a component or a context');
|
||||
}
|
||||
|
|
|
@ -72,25 +72,25 @@ class BeforeEachRunner {
|
|||
// Reset the test bindings before each test
|
||||
jsmBeforeEach(() => { testBindings = []; });
|
||||
|
||||
function _describe(jsmFn, ...args) {
|
||||
function _describe(jsmFn, ... args) {
|
||||
var parentRunner = runnerStack.length === 0 ? null : runnerStack[runnerStack.length - 1];
|
||||
var runner = new BeforeEachRunner(parentRunner);
|
||||
runnerStack.push(runner);
|
||||
var suite = jsmFn(...args);
|
||||
var suite = jsmFn(... args);
|
||||
runnerStack.pop();
|
||||
return suite;
|
||||
}
|
||||
|
||||
export function describe(...args) {
|
||||
return _describe(jsmDescribe, ...args);
|
||||
export function describe(... args) {
|
||||
return _describe(jsmDescribe, ... args);
|
||||
}
|
||||
|
||||
export function ddescribe(...args) {
|
||||
return _describe(jsmDDescribe, ...args);
|
||||
export function ddescribe(... args) {
|
||||
return _describe(jsmDDescribe, ... args);
|
||||
}
|
||||
|
||||
export function xdescribe(...args) {
|
||||
return _describe(jsmXDescribe, ...args);
|
||||
export function xdescribe(... args) {
|
||||
return _describe(jsmXDescribe, ... args);
|
||||
}
|
||||
|
||||
export function beforeEach(fn) {
|
||||
|
@ -123,7 +123,7 @@ export function beforeEachBindings(fn) {
|
|||
jsmBeforeEach(() => {
|
||||
var bindings = fn();
|
||||
if (!bindings) return;
|
||||
testBindings = [...testBindings, ...bindings];
|
||||
testBindings = [... testBindings, ... bindings];
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -142,7 +142,7 @@ function _it(jsmFn, name, fn) {
|
|||
return new AsyncTestCompleter(done);
|
||||
});
|
||||
|
||||
var injector = createTestInjector([...testBindings, completerBinding]);
|
||||
var injector = createTestInjector([... testBindings, completerBinding]);
|
||||
runner.run(injector);
|
||||
|
||||
if (!(fn instanceof FunctionWithParamTokens)) {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import {global} from 'angular2/src/facade/lang';
|
||||
|
||||
export function makeDecorator(annotationCls) {
|
||||
return function(...args) {
|
||||
return function(... args) {
|
||||
var Reflect = global.Reflect;
|
||||
if (!(Reflect && Reflect.getMetadata)) {
|
||||
throw 'reflect-metadata shim is required when using class decorators';
|
||||
|
@ -20,7 +20,7 @@ export function makeDecorator(annotationCls) {
|
|||
}
|
||||
|
||||
export function makeParamDecorator(annotationCls): any {
|
||||
return function(...args) {
|
||||
return function(... args) {
|
||||
var Reflect = global.Reflect;
|
||||
if (!(Reflect && Reflect.getMetadata)) {
|
||||
throw 'reflect-metadata shim is required when using parameter decorators';
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach} from 'angular2/test_lib';
|
||||
|
||||
import {PreGeneratedChangeDetection, ChangeDetectorDefinition, ProtoChangeDetector, DynamicProtoChangeDetector} from 'angular2/change_detection';
|
||||
import {
|
||||
PreGeneratedChangeDetection,
|
||||
ChangeDetectorDefinition,
|
||||
ProtoChangeDetector,
|
||||
DynamicProtoChangeDetector
|
||||
} from 'angular2/change_detection';
|
||||
|
||||
class DummyChangeDetector extends ProtoChangeDetector {}
|
||||
|
||||
|
@ -15,7 +20,7 @@ export function main() {
|
|||
});
|
||||
|
||||
it("should return a proto change detector when one is available", () => {
|
||||
var map = {'id' : (registry) => proto};
|
||||
var map = {'id': (registry) => proto};
|
||||
var cd = new PreGeneratedChangeDetection(null, map);
|
||||
|
||||
expect(cd.createProtoChangeDetector(def)).toBe(proto)
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -1,82 +0,0 @@
|
|||
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach} from 'angular2/test_lib';
|
||||
|
||||
import {coalesce} from 'angular2/src/change_detection/coalesce';
|
||||
import {RECORD_TYPE_SELF, ProtoRecord} from 'angular2/src/change_detection/proto_record';
|
||||
|
||||
export function main() {
|
||||
function r(funcOrValue, args, contextIndex, selfIndex, lastInBinding = false) {
|
||||
return new ProtoRecord(99, "name", funcOrValue, args, null, contextIndex, null, selfIndex,
|
||||
null, null, lastInBinding, false);
|
||||
}
|
||||
|
||||
describe("change detection - coalesce", () => {
|
||||
it("should work with an empty list", () => {
|
||||
expect(coalesce([])).toEqual([]);
|
||||
});
|
||||
|
||||
it("should remove non-terminal duplicate records" +
|
||||
" and update the context indices referencing them", () => {
|
||||
var rs = coalesce([
|
||||
r("user", [], 0, 1),
|
||||
r("first", [], 1, 2),
|
||||
r("user", [], 0, 3),
|
||||
r("last", [], 3, 4)
|
||||
]);
|
||||
|
||||
expect(rs).toEqual([
|
||||
r("user", [], 0, 1),
|
||||
r("first", [], 1, 2),
|
||||
r("last", [], 1, 3)
|
||||
]);
|
||||
});
|
||||
|
||||
it("should update indices of other records", () => {
|
||||
var rs = coalesce([
|
||||
r("dup", [], 0, 1),
|
||||
r("dup", [], 0, 2),
|
||||
r("user", [], 0, 3),
|
||||
r("first", [3], 3, 4)
|
||||
]);
|
||||
|
||||
expect(rs).toEqual([
|
||||
r("dup", [], 0, 1),
|
||||
r("user", [], 0, 2),
|
||||
r("first", [2], 2, 3)
|
||||
]);
|
||||
});
|
||||
|
||||
it("should remove non-terminal duplicate records" +
|
||||
" and update the args indices referencing them", () => {
|
||||
var rs = coalesce([
|
||||
r("user1", [], 0, 1),
|
||||
r("user2", [], 0, 2),
|
||||
r("hi", [1], 0, 3),
|
||||
r("hi", [1], 0, 4),
|
||||
r("hi", [2], 0, 5)
|
||||
]);
|
||||
|
||||
expect(rs).toEqual([
|
||||
r("user1", [], 0, 1),
|
||||
r("user2", [], 0, 2),
|
||||
r("hi", [1], 0, 3),
|
||||
r("hi", [2], 0, 4)
|
||||
]);
|
||||
});
|
||||
|
||||
it("should replace duplicate terminal records with" +
|
||||
" self records", () => {
|
||||
|
||||
var rs = coalesce([
|
||||
r("user", [], 0, 1, true),
|
||||
r("user", [], 0, 2, true)
|
||||
]);
|
||||
|
||||
expect(rs[1]).toEqual(new ProtoRecord(
|
||||
RECORD_TYPE_SELF, "self", null,
|
||||
[], null, 1, null, 2,
|
||||
null, null,
|
||||
true, false)
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach} from 'angular2/test_lib';
|
||||
|
||||
import {coalesce} from 'angular2/src/change_detection/coalesce';
|
||||
import {RECORD_TYPE_SELF, ProtoRecord} from 'angular2/src/change_detection/proto_record';
|
||||
|
||||
export function main() {
|
||||
function r(funcOrValue, args, contextIndex, selfIndex, lastInBinding = false) {
|
||||
return new ProtoRecord(99, "name", funcOrValue, args, null, contextIndex, null, selfIndex, null,
|
||||
null, lastInBinding, false);
|
||||
}
|
||||
|
||||
describe("change detection - coalesce", () => {
|
||||
it("should work with an empty list", () => { expect(coalesce([])).toEqual([]); });
|
||||
|
||||
it("should remove non-terminal duplicate records" +
|
||||
" and update the context indices referencing them",
|
||||
() => {
|
||||
var rs = coalesce([
|
||||
r("user", [], 0, 1),
|
||||
r("first", [], 1, 2),
|
||||
r("user", [], 0, 3),
|
||||
r("last", [], 3, 4)
|
||||
]);
|
||||
|
||||
expect(rs).toEqual([r("user", [], 0, 1), r("first", [], 1, 2), r("last", [], 1, 3)]);
|
||||
});
|
||||
|
||||
it("should update indices of other records", () => {
|
||||
var rs = coalesce(
|
||||
[r("dup", [], 0, 1), r("dup", [], 0, 2), r("user", [], 0, 3), r("first", [3], 3, 4)]);
|
||||
|
||||
expect(rs).toEqual([r("dup", [], 0, 1), r("user", [], 0, 2), r("first", [2], 2, 3)]);
|
||||
});
|
||||
|
||||
it("should remove non-terminal duplicate records" +
|
||||
" and update the args indices referencing them",
|
||||
() => {
|
||||
var rs = coalesce([
|
||||
r("user1", [], 0, 1),
|
||||
r("user2", [], 0, 2),
|
||||
r("hi", [1], 0, 3),
|
||||
r("hi", [1], 0, 4),
|
||||
r("hi", [2], 0, 5)
|
||||
]);
|
||||
|
||||
expect(rs).toEqual(
|
||||
[r("user1", [], 0, 1), r("user2", [], 0, 2), r("hi", [1], 0, 3), r("hi", [2], 0, 4)]);
|
||||
});
|
||||
|
||||
it("should replace duplicate terminal records with" + " self records", () => {
|
||||
|
||||
var rs = coalesce([r("user", [], 0, 1, true), r("user", [], 0, 2, true)]);
|
||||
|
||||
expect(rs[1]).toEqual(new ProtoRecord(RECORD_TYPE_SELF, "self", null, [], null, 1, null, 2,
|
||||
null, null, true, false));
|
||||
});
|
||||
});
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
export class TestIterable {
|
||||
constructor() {
|
||||
this.list = [];
|
||||
}
|
||||
|
||||
[Symbol.iterator]() {
|
||||
return this.list[Symbol.iterator]();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
import {List} from 'angular2/src/facade/collection';
|
||||
|
||||
export class TestIterable {
|
||||
list: List<int>;
|
||||
constructor() { this.list = []; }
|
||||
|
||||
[Symbol.iterator]() { return this.list[Symbol.iterator](); }
|
||||
}
|
|
@ -3,9 +3,9 @@ import {ddescribe, describe, it, expect} from 'angular2/test_lib';
|
|||
import {Lexer, Token} from 'angular2/src/change_detection/parser/lexer';
|
||||
|
||||
import {List, ListWrapper} from "angular2/src/facade/collection";
|
||||
import {StringWrapper, int} from "angular2/src/facade/lang";
|
||||
import {StringWrapper} from "angular2/src/facade/lang";
|
||||
|
||||
function lex(text:string):List {
|
||||
function lex(text: string): List<any> {
|
||||
return new Lexer().tokenize(text);
|
||||
}
|
||||
|
||||
|
@ -53,52 +53,49 @@ export function main() {
|
|||
describe('lexer', function() {
|
||||
describe('token', function() {
|
||||
it('should tokenize a simple identifier', function() {
|
||||
var tokens:List<int> = lex("j");
|
||||
var tokens: List<int> = lex("j");
|
||||
expect(tokens.length).toEqual(1);
|
||||
expectIdentifierToken(tokens[0], 0, 'j');
|
||||
});
|
||||
|
||||
it('should tokenize a dotted identifier', function() {
|
||||
var tokens:List<int> = lex("j.k");
|
||||
var tokens: List<int> = lex("j.k");
|
||||
expect(tokens.length).toEqual(3);
|
||||
expectIdentifierToken(tokens[0], 0, 'j');
|
||||
expectCharacterToken (tokens[1], 1, '.');
|
||||
expectCharacterToken(tokens[1], 1, '.');
|
||||
expectIdentifierToken(tokens[2], 2, 'k');
|
||||
});
|
||||
|
||||
it('should tokenize an operator', function() {
|
||||
var tokens:List<int> = lex("j-k");
|
||||
var tokens: List<int> = lex("j-k");
|
||||
expect(tokens.length).toEqual(3);
|
||||
expectOperatorToken(tokens[1], 1, '-');
|
||||
});
|
||||
|
||||
it('should tokenize an indexed operator', function() {
|
||||
var tokens:List<int> = lex("j[k]");
|
||||
var tokens: List<int> = lex("j[k]");
|
||||
expect(tokens.length).toEqual(4);
|
||||
expectCharacterToken(tokens[1], 1, "[");
|
||||
expectCharacterToken(tokens[3], 3, "]");
|
||||
});
|
||||
|
||||
it('should tokenize numbers', function() {
|
||||
var tokens:List<int> = lex("88");
|
||||
var tokens: List<int> = lex("88");
|
||||
expect(tokens.length).toEqual(1);
|
||||
expectNumberToken(tokens[0], 0, 88);
|
||||
});
|
||||
|
||||
it('should tokenize numbers within index ops', function() {
|
||||
expectNumberToken(lex("a[22]")[2], 2, 22);
|
||||
});
|
||||
it('should tokenize numbers within index ops',
|
||||
function() { expectNumberToken(lex("a[22]")[2], 2, 22); });
|
||||
|
||||
it('should tokenize simple quoted strings', function() {
|
||||
expectStringToken(lex('"a"')[0], 0, "a");
|
||||
});
|
||||
it('should tokenize simple quoted strings',
|
||||
function() { expectStringToken(lex('"a"')[0], 0, "a"); });
|
||||
|
||||
it('should tokenize quoted strings with escaped quotes', function() {
|
||||
expectStringToken(lex('"a\\""')[0], 0, 'a"');
|
||||
});
|
||||
it('should tokenize quoted strings with escaped quotes',
|
||||
function() { expectStringToken(lex('"a\\""')[0], 0, 'a"'); });
|
||||
|
||||
it('should tokenize a string', function() {
|
||||
var tokens:List<Token> = lex("j-a.bc[22]+1.3|f:'a\\\'c':\"d\\\"e\"");
|
||||
var tokens: List<Token> = lex("j-a.bc[22]+1.3|f:'a\\\'c':\"d\\\"e\"");
|
||||
expectIdentifierToken(tokens[0], 0, 'j');
|
||||
expectOperatorToken(tokens[1], 1, '-');
|
||||
expectIdentifierToken(tokens[2], 2, 'a');
|
||||
|
@ -118,39 +115,39 @@ export function main() {
|
|||
});
|
||||
|
||||
it('should tokenize undefined', function() {
|
||||
var tokens:List<Token> = lex("undefined");
|
||||
var tokens: List<Token> = lex("undefined");
|
||||
expectKeywordToken(tokens[0], 0, "undefined");
|
||||
expect(tokens[0].isKeywordUndefined()).toBe(true);
|
||||
});
|
||||
|
||||
it('should ignore whitespace', function() {
|
||||
var tokens:List<Token> = lex("a \t \n \r b");
|
||||
var tokens: List<Token> = lex("a \t \n \r b");
|
||||
expectIdentifierToken(tokens[0], 0, 'a');
|
||||
expectIdentifierToken(tokens[1], 8, 'b');
|
||||
});
|
||||
|
||||
it('should tokenize quoted string', function() {
|
||||
var str = "['\\'', \"\\\"\"]";
|
||||
var tokens:List<Token> = lex(str);
|
||||
var tokens: List<Token> = lex(str);
|
||||
expectStringToken(tokens[1], 1, "'");
|
||||
expectStringToken(tokens[3], 7, '"');
|
||||
});
|
||||
|
||||
it('should tokenize escaped quoted string', function() {
|
||||
var str = '"\\"\\n\\f\\r\\t\\v\\u00A0"';
|
||||
var tokens:List<Token> = lex(str);
|
||||
var tokens: List<Token> = lex(str);
|
||||
expect(tokens.length).toEqual(1);
|
||||
expect(tokens[0].toString()).toEqual('"\n\f\r\t\v\u00A0');
|
||||
});
|
||||
|
||||
it('should tokenize unicode', function() {
|
||||
var tokens:List<Token> = lex('"\\u00A0"');
|
||||
var tokens: List<Token> = lex('"\\u00A0"');
|
||||
expect(tokens.length).toEqual(1);
|
||||
expect(tokens[0].toString()).toEqual('\u00a0');
|
||||
});
|
||||
|
||||
it('should tokenize relation', function() {
|
||||
var tokens:List<Token> = lex("! == != < > <= >= === !==");
|
||||
var tokens: List<Token> = lex("! == != < > <= >= === !==");
|
||||
expectOperatorToken(tokens[0], 0, '!');
|
||||
expectOperatorToken(tokens[1], 2, '==');
|
||||
expectOperatorToken(tokens[2], 5, '!=');
|
||||
|
@ -163,7 +160,7 @@ export function main() {
|
|||
});
|
||||
|
||||
it('should tokenize statements', function() {
|
||||
var tokens:List<Token> = lex("a;b;");
|
||||
var tokens: List<Token> = lex("a;b;");
|
||||
expectIdentifierToken(tokens[0], 0, 'a');
|
||||
expectCharacterToken(tokens[1], 1, ';');
|
||||
expectIdentifierToken(tokens[2], 2, 'b');
|
||||
|
@ -171,19 +168,19 @@ export function main() {
|
|||
});
|
||||
|
||||
it('should tokenize function invocation', function() {
|
||||
var tokens:List<Token> = lex("a()");
|
||||
var tokens: List<Token> = lex("a()");
|
||||
expectIdentifierToken(tokens[0], 0, 'a');
|
||||
expectCharacterToken(tokens[1], 1, '(');
|
||||
expectCharacterToken(tokens[2], 2, ')');
|
||||
});
|
||||
|
||||
it('should tokenize simple method invocations', function() {
|
||||
var tokens:List<Token> = lex("a.method()");
|
||||
var tokens: List<Token> = lex("a.method()");
|
||||
expectIdentifierToken(tokens[2], 2, 'method');
|
||||
});
|
||||
|
||||
it('should tokenize method invocation', function() {
|
||||
var tokens:List<Token> = lex("a.b.c (d) - e.f()");
|
||||
var tokens: List<Token> = lex("a.b.c (d) - e.f()");
|
||||
expectIdentifierToken(tokens[0], 0, 'a');
|
||||
expectCharacterToken(tokens[1], 1, '.');
|
||||
expectIdentifierToken(tokens[2], 2, 'b');
|
||||
|
@ -201,7 +198,7 @@ export function main() {
|
|||
});
|
||||
|
||||
it('should tokenize number', function() {
|
||||
var tokens:List<Token> = lex("0.5");
|
||||
var tokens: List<Token> = lex("0.5");
|
||||
expectNumberToken(tokens[0], 0, 0.5);
|
||||
});
|
||||
|
||||
|
@ -212,7 +209,7 @@ export function main() {
|
|||
// });
|
||||
|
||||
it('should tokenize number with exponent', function() {
|
||||
var tokens:List<Token> = lex("0.5E-10");
|
||||
var tokens: List<Token> = lex("0.5E-10");
|
||||
expect(tokens.length).toEqual(1);
|
||||
expectNumberToken(tokens[0], 0, 0.5E-10);
|
||||
tokens = lex("0.5E+10");
|
||||
|
@ -220,28 +217,26 @@ export function main() {
|
|||
});
|
||||
|
||||
it('should throws exception for invalid exponent', function() {
|
||||
expect(function() {
|
||||
lex("0.5E-");
|
||||
}).toThrowError('Lexer Error: Invalid exponent at column 4 in expression [0.5E-]');
|
||||
expect(function() { lex("0.5E-"); })
|
||||
.toThrowError('Lexer Error: Invalid exponent at column 4 in expression [0.5E-]');
|
||||
|
||||
expect(function() {
|
||||
lex("0.5E-A");
|
||||
}).toThrowError('Lexer Error: Invalid exponent at column 4 in expression [0.5E-A]');
|
||||
expect(function() { lex("0.5E-A"); })
|
||||
.toThrowError('Lexer Error: Invalid exponent at column 4 in expression [0.5E-A]');
|
||||
});
|
||||
|
||||
it('should tokenize number starting with a dot', function() {
|
||||
var tokens:List<Token> = lex(".5");
|
||||
var tokens: List<Token> = lex(".5");
|
||||
expectNumberToken(tokens[0], 0, 0.5);
|
||||
});
|
||||
|
||||
it('should throw error on invalid unicode', function() {
|
||||
expect(function() {
|
||||
lex("'\\u1''bla'");
|
||||
}).toThrowError("Lexer Error: Invalid unicode escape [\\u1''b] at column 2 in expression ['\\u1''bla']");
|
||||
expect(function() { lex("'\\u1''bla'"); })
|
||||
.toThrowError(
|
||||
"Lexer Error: Invalid unicode escape [\\u1''b] at column 2 in expression ['\\u1''bla']");
|
||||
});
|
||||
|
||||
it('should tokenize hash as operator', function() {
|
||||
var tokens:List<Token> = lex("#");
|
||||
var tokens: List<Token> = lex("#");
|
||||
expectOperatorToken(tokens[0], 0, '#');
|
||||
});
|
||||
|
|
@ -8,8 +8,7 @@ export function main() {
|
|||
describe('Locals', () => {
|
||||
var locals;
|
||||
beforeEach(() => {
|
||||
locals = new Locals(null,
|
||||
MapWrapper.createFromPairs([['key', 'value'], ['nullKey', null]]));
|
||||
locals = new Locals(null, MapWrapper.createFromPairs([['key', 'value'], ['nullKey', null]]));
|
||||
});
|
||||
|
||||
it('should support getting values', () => {
|
||||
|
@ -28,9 +27,8 @@ export function main() {
|
|||
expect(locals.get('key')).toBe('bar');
|
||||
});
|
||||
|
||||
it('should not support setting keys that are not present already', () => {
|
||||
expect(() => locals.set('notPresent', 'bar')).toThrowError();
|
||||
});
|
||||
it('should not support setting keys that are not present already',
|
||||
() => { expect(() => locals.set('notPresent', 'bar')).toThrowError(); });
|
||||
|
||||
it('should clearValues', () => {
|
||||
locals.clearValues();
|
|
@ -5,59 +5,42 @@ import {MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
|
|||
import {Parser} from 'angular2/src/change_detection/parser/parser';
|
||||
import {Lexer} from 'angular2/src/change_detection/parser/lexer';
|
||||
import {Locals} from 'angular2/src/change_detection/parser/locals';
|
||||
import {Pipe, LiteralPrimitive} from 'angular2/src/change_detection/parser/ast';
|
||||
import {Pipe, LiteralPrimitive, AccessMember} from 'angular2/src/change_detection/parser/ast';
|
||||
|
||||
class TestData {
|
||||
a;
|
||||
b;
|
||||
fnReturnValue;
|
||||
constructor(a, b, fnReturnValue) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.fnReturnValue = fnReturnValue;
|
||||
}
|
||||
constructor(public a?: any, public b?: any, public fnReturnValue?: any) {}
|
||||
|
||||
fn() {
|
||||
return this.fnReturnValue;
|
||||
}
|
||||
fn() { return this.fnReturnValue; }
|
||||
|
||||
add(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
add(a, b) { return a + b; }
|
||||
}
|
||||
|
||||
export function main() {
|
||||
function td(a = 0, b = 0, fnReturnValue = "constant") {
|
||||
function td(a: any = 0, b: any = 0, fnReturnValue: any = "constant") {
|
||||
return new TestData(a, b, fnReturnValue);
|
||||
}
|
||||
|
||||
function createParser() {
|
||||
return new Parser(new Lexer(), reflector);
|
||||
}
|
||||
function createParser() { return new Parser(new Lexer(), reflector); }
|
||||
|
||||
function parseAction(text, location = null) {
|
||||
function parseAction(text, location = null): any {
|
||||
return createParser().parseAction(text, location);
|
||||
}
|
||||
|
||||
function parseBinding(text, location = null) {
|
||||
function parseBinding(text, location = null): any {
|
||||
return createParser().parseBinding(text, location);
|
||||
}
|
||||
|
||||
function parseTemplateBindings(text, location = null) {
|
||||
function parseTemplateBindings(text, location = null): any {
|
||||
return createParser().parseTemplateBindings(text, location);
|
||||
}
|
||||
|
||||
function parseInterpolation(text, location = null) {
|
||||
function parseInterpolation(text, location = null): any {
|
||||
return createParser().parseInterpolation(text, location);
|
||||
}
|
||||
|
||||
function addPipes(ast, pipes) {
|
||||
return createParser().addPipes(ast, pipes);
|
||||
}
|
||||
function addPipes(ast, pipes): any { return createParser().addPipes(ast, pipes); }
|
||||
|
||||
function emptyLocals() {
|
||||
return new Locals(null, MapWrapper.create());
|
||||
}
|
||||
function emptyLocals() { return new Locals(null, MapWrapper.create()); }
|
||||
|
||||
function expectEval(text, passedInContext = null, passedInLocals = null) {
|
||||
var c = isBlank(passedInContext) ? td() : passedInContext;
|
||||
|
@ -74,7 +57,7 @@ export function main() {
|
|||
function evalAsts(asts, passedInContext = null) {
|
||||
var c = isBlank(passedInContext) ? td() : passedInContext;
|
||||
var res = [];
|
||||
for (var i=0; i<asts.length; i++) {
|
||||
for (var i = 0; i < asts.length; i++) {
|
||||
ListWrapper.push(res, asts[i].eval(c, emptyLocals()));
|
||||
}
|
||||
return res;
|
||||
|
@ -83,18 +66,14 @@ export function main() {
|
|||
describe("parser", () => {
|
||||
describe("parseAction", () => {
|
||||
describe("basic expressions", () => {
|
||||
it('should parse numerical expressions', () => {
|
||||
expectEval("1").toEqual(1);
|
||||
});
|
||||
it('should parse numerical expressions', () => { expectEval("1").toEqual(1); });
|
||||
|
||||
it('should parse strings', () => {
|
||||
expectEval("'1'").toEqual('1');
|
||||
expectEval('"1"').toEqual('1');
|
||||
});
|
||||
|
||||
it('should parse null', () => {
|
||||
expectEval("null").toBe(null);
|
||||
});
|
||||
it('should parse null', () => { expectEval("null").toBe(null); });
|
||||
|
||||
it('should parse unary - expressions', () => {
|
||||
expectEval("-1").toEqual(-1);
|
||||
|
@ -107,13 +86,10 @@ export function main() {
|
|||
expectEval("!!!true").toEqual(!!!true);
|
||||
});
|
||||
|
||||
it('should parse multiplicative expressions', () => {
|
||||
expectEval("3*4/2%5").toEqual(3 * 4 / 2 % 5);
|
||||
});
|
||||
it('should parse multiplicative expressions',
|
||||
() => { expectEval("3*4/2%5").toEqual(3 * 4 / 2 % 5); });
|
||||
|
||||
it('should parse additive expressions', () => {
|
||||
expectEval("3+6-2").toEqual(3 + 6 - 2);
|
||||
});
|
||||
it('should parse additive expressions', () => { expectEval("3+6-2").toEqual(3 + 6 - 2); });
|
||||
|
||||
it('should parse relational expressions', () => {
|
||||
expectEval("2<3").toEqual(2 < 3);
|
||||
|
@ -124,23 +100,23 @@ export function main() {
|
|||
|
||||
it('should parse equality expressions', () => {
|
||||
expectEval("2==3").toEqual(2 == 3);
|
||||
expectEval("2=='2'").toEqual(2 == '2');
|
||||
expectEval("2=='3'").toEqual(2 == '3');
|
||||
expectEval("2=='2'").toEqual(2 == <any>'2');
|
||||
expectEval("2=='3'").toEqual(2 == <any>'3');
|
||||
expectEval("2!=3").toEqual(2 != 3);
|
||||
expectEval("2!='3'").toEqual(2 != '3');
|
||||
expectEval("2!='2'").toEqual(2 != '2');
|
||||
expectEval("2!=!false").toEqual(2!=!false);
|
||||
expectEval("2!='3'").toEqual(2 != <any>'3');
|
||||
expectEval("2!='2'").toEqual(2 != <any>'2');
|
||||
expectEval("2!=!false").toEqual(2 != <any>!false);
|
||||
});
|
||||
|
||||
it('should parse strict equality expressions', () => {
|
||||
expectEval("2===3").toEqual(2 === 3);
|
||||
expectEval("2==='3'").toEqual(2 === '3');
|
||||
expectEval("2==='2'").toEqual(2 === '2');
|
||||
expectEval("2==='3'").toEqual(2 === <any>'3');
|
||||
expectEval("2==='2'").toEqual(2 === <any>'2');
|
||||
expectEval("2!==3").toEqual(2 !== 3);
|
||||
expectEval("2!=='3'").toEqual(2 !== '3');
|
||||
expectEval("2!=='2'").toEqual(2 !== '2');
|
||||
expectEval("false===!true").toEqual(false===!true);
|
||||
expectEval("false!==!!true").toEqual(false!==!!true);
|
||||
expectEval("2!=='3'").toEqual(2 !== <any>'3');
|
||||
expectEval("2!=='2'").toEqual(2 !== <any>'2');
|
||||
expectEval("false===!true").toEqual(false === !true);
|
||||
expectEval("false!==!!true").toEqual(false !== !!true);
|
||||
});
|
||||
|
||||
it('should parse logicalAND expressions', () => {
|
||||
|
@ -153,21 +129,16 @@ export function main() {
|
|||
expectEval("false||false").toEqual(false || false);
|
||||
});
|
||||
|
||||
it('should short-circuit AND operator', () => {
|
||||
expectEval('false && a()', td(() => {throw "BOOM"})).toBe(false);
|
||||
});
|
||||
it('should short-circuit AND operator',
|
||||
() => { expectEval('false && a()', td(() => {throw "BOOM"})).toBe(false); });
|
||||
|
||||
it('should short-circuit OR operator', () => {
|
||||
expectEval('true || a()', td(() => {throw "BOOM"})).toBe(true);
|
||||
});
|
||||
it('should short-circuit OR operator',
|
||||
() => { expectEval('true || a()', td(() => {throw "BOOM"})).toBe(true); });
|
||||
|
||||
it('should evaluate grouped expressions', () => {
|
||||
expectEval("(1+2)*3").toEqual((1+2)*3);
|
||||
});
|
||||
it('should evaluate grouped expressions',
|
||||
() => { expectEval("(1+2)*3").toEqual((1 + 2) * 3); });
|
||||
|
||||
it('should parse an empty string', () => {
|
||||
expectEval('').toBeNull();
|
||||
});
|
||||
it('should parse an empty string', () => { expectEval('').toBeNull(); });
|
||||
});
|
||||
|
||||
describe("literals", () => {
|
||||
|
@ -190,8 +161,10 @@ export function main() {
|
|||
});
|
||||
|
||||
it('should only allow identifier, string, or keyword as map key', () => {
|
||||
expectEvalError('{(:0}').toThrowError(new RegExp('expected identifier, keyword, or string'));
|
||||
expectEvalError('{1234:0}').toThrowError(new RegExp('expected identifier, keyword, or string'));
|
||||
expectEvalError('{(:0}')
|
||||
.toThrowError(new RegExp('expected identifier, keyword, or string'));
|
||||
expectEvalError('{1234:0}')
|
||||
.toThrowError(new RegExp('expected identifier, keyword, or string'));
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -201,9 +174,8 @@ export function main() {
|
|||
expectEval("a.a", td(td(999))).toEqual(999);
|
||||
});
|
||||
|
||||
it('should throw when accessing a field on null', () => {
|
||||
expectEvalError("a.a.a").toThrowError();
|
||||
});
|
||||
it('should throw when accessing a field on null',
|
||||
() => { expectEvalError("a.a.a").toThrowError(); });
|
||||
|
||||
it('should only allow identifier or keyword as member names', () => {
|
||||
expectEvalError('x.(').toThrowError(new RegExp('identifier or keyword'));
|
||||
|
@ -212,23 +184,22 @@ export function main() {
|
|||
});
|
||||
|
||||
it("should read a field from Locals", () => {
|
||||
var locals = new Locals(null,
|
||||
MapWrapper.createFromPairs([["key", "value"]]));
|
||||
var locals = new Locals(null, MapWrapper.createFromPairs([["key", "value"]]));
|
||||
expectEval("key", null, locals).toEqual("value");
|
||||
});
|
||||
|
||||
it("should handle nested Locals", () => {
|
||||
var nested = new Locals(null,
|
||||
MapWrapper.createFromPairs([["key", "value"]]));
|
||||
var nested = new Locals(null, MapWrapper.createFromPairs([["key", "value"]]));
|
||||
var locals = new Locals(nested, MapWrapper.create());
|
||||
expectEval("key", null, locals).toEqual("value");
|
||||
});
|
||||
|
||||
it("should fall back to a regular field read when Locals "+
|
||||
"does not have the requested field", () => {
|
||||
var locals = new Locals(null, MapWrapper.create());
|
||||
expectEval("a", td(999), locals).toEqual(999);
|
||||
});
|
||||
it("should fall back to a regular field read when Locals " +
|
||||
"does not have the requested field",
|
||||
() => {
|
||||
var locals = new Locals(null, MapWrapper.create());
|
||||
expectEval("a", td(999), locals).toEqual(999);
|
||||
});
|
||||
});
|
||||
|
||||
describe("method calls", () => {
|
||||
|
@ -243,37 +214,30 @@ export function main() {
|
|||
expectEvalError("fn(1,2,3,4,5,6,7,8,9,10,11)").toThrowError(new RegExp('more than'));
|
||||
});
|
||||
|
||||
it('should throw when no method', () => {
|
||||
expectEvalError("blah()").toThrowError();
|
||||
});
|
||||
it('should throw when no method', () => { expectEvalError("blah()").toThrowError(); });
|
||||
|
||||
it('should evaluate a method from Locals', () => {
|
||||
var locals = new Locals(
|
||||
null,
|
||||
MapWrapper.createFromPairs([['fn', () => 'child']])
|
||||
);
|
||||
var locals = new Locals(null, MapWrapper.createFromPairs([['fn', () => 'child']]));
|
||||
expectEval("fn()", td(0, 0, 'parent'), locals).toEqual('child');
|
||||
});
|
||||
|
||||
it('should fall back to the parent context when Locals does not ' +
|
||||
'have the requested method', () => {
|
||||
var locals = new Locals(null, MapWrapper.create());
|
||||
expectEval("fn()", td(0, 0, 'parent'), locals).toEqual('parent');
|
||||
});
|
||||
'have the requested method',
|
||||
() => {
|
||||
var locals = new Locals(null, MapWrapper.create());
|
||||
expectEval("fn()", td(0, 0, 'parent'), locals).toEqual('parent');
|
||||
});
|
||||
});
|
||||
|
||||
describe("functional calls", () => {
|
||||
it("should evaluate function calls", () => {
|
||||
expectEval("fn()(1,2)", td(0, 0, (a, b) => a + b)).toEqual(3);
|
||||
});
|
||||
it("should evaluate function calls",
|
||||
() => { expectEval("fn()(1,2)", td(0, 0, (a, b) => a + b)).toEqual(3); });
|
||||
|
||||
it('should throw on non-function function calls', () => {
|
||||
expectEvalError("4()").toThrowError(new RegExp('4 is not a function'));
|
||||
});
|
||||
it('should throw on non-function function calls',
|
||||
() => { expectEvalError("4()").toThrowError(new RegExp('4 is not a function')); });
|
||||
|
||||
it('should parse functions for object indices', () => {
|
||||
expectEval('a[b()]()', td([()=>6], () => 0)).toEqual(6);
|
||||
});
|
||||
it('should parse functions for object indices',
|
||||
() => { expectEval('a[b()]()', td([() => 6], () => 0)).toEqual(6); });
|
||||
});
|
||||
|
||||
describe("conditional", () => {
|
||||
|
@ -283,8 +247,8 @@ export function main() {
|
|||
});
|
||||
|
||||
it('should throw on incorrect ternary operator syntax', () => {
|
||||
expectEvalError("true?1").
|
||||
toThrowError(new RegExp('Parser Error: Conditional expression true\\?1 requires all 3 expressions'));
|
||||
expectEvalError("true?1").toThrowError(new RegExp(
|
||||
'Parser Error: Conditional expression true\\?1 requires all 3 expressions'));
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -315,13 +279,13 @@ export function main() {
|
|||
});
|
||||
|
||||
it("should support map updates", () => {
|
||||
var context = td({"key" : 100});
|
||||
var context = td({"key": 100});
|
||||
expectEval('a["key"] = 200', context).toEqual(200);
|
||||
expect(context.a["key"]).toEqual(200);
|
||||
});
|
||||
|
||||
it("should support array/map updates", () => {
|
||||
var context = td([{"key" : 100}]);
|
||||
var context = td([{"key": 100}]);
|
||||
expectEval('a[0]["key"] = 200', context).toEqual(200);
|
||||
expect(context.a[0]["key"]).toEqual(200);
|
||||
});
|
||||
|
@ -345,28 +309,29 @@ export function main() {
|
|||
|
||||
it('should throw when reassigning a variable binding', () => {
|
||||
var locals = new Locals(null, MapWrapper.createFromPairs([["key", "value"]]));
|
||||
expectEvalError('key = 200', null, locals).toThrowError(new RegExp("Cannot reassign a variable binding"));
|
||||
expectEvalError('key = 200', null, locals)
|
||||
.toThrowError(new RegExp("Cannot reassign a variable binding"));
|
||||
});
|
||||
});
|
||||
|
||||
describe("general error handling", () => {
|
||||
it("should throw on an unexpected token", () => {
|
||||
expectEvalError("[1,2] trac")
|
||||
.toThrowError(new RegExp('Unexpected token \'trac\''));
|
||||
expectEvalError("[1,2] trac").toThrowError(new RegExp('Unexpected token \'trac\''));
|
||||
});
|
||||
|
||||
it('should throw a reasonable error for unconsumed tokens', () => {
|
||||
expectEvalError(")").toThrowError(new RegExp("Unexpected token \\) at column 1 in \\[\\)\\]"));
|
||||
expectEvalError(")")
|
||||
.toThrowError(new RegExp("Unexpected token \\) at column 1 in \\[\\)\\]"));
|
||||
});
|
||||
|
||||
it('should throw on missing expected token', () => {
|
||||
expectEvalError("a(b").toThrowError(new RegExp("Missing expected \\) at the end of the expression \\[a\\(b\\]"));
|
||||
expectEvalError("a(b").toThrowError(
|
||||
new RegExp("Missing expected \\) at the end of the expression \\[a\\(b\\]"));
|
||||
});
|
||||
});
|
||||
|
||||
it("should error when using pipes", () => {
|
||||
expectEvalError('x|blah').toThrowError(new RegExp('Cannot have a pipe'));
|
||||
});
|
||||
it("should error when using pipes",
|
||||
() => { expectEvalError('x|blah').toThrowError(new RegExp('Cannot have a pipe')); });
|
||||
|
||||
it('should pass exceptions', () => {
|
||||
expect(() => {
|
||||
|
@ -381,13 +346,11 @@ export function main() {
|
|||
});
|
||||
});
|
||||
|
||||
it('should store the source in the result', () => {
|
||||
expect(parseAction('someExpr').source).toBe('someExpr');
|
||||
});
|
||||
it('should store the source in the result',
|
||||
() => { expect(parseAction('someExpr').source).toBe('someExpr'); });
|
||||
|
||||
it('should store the passed-in location', () => {
|
||||
expect(parseAction('someExpr', 'location').location).toBe('location');
|
||||
});
|
||||
it('should store the passed-in location',
|
||||
() => { expect(parseAction('someExpr', 'location').location).toBe('location'); });
|
||||
});
|
||||
|
||||
describe("parseBinding", () => {
|
||||
|
@ -425,19 +388,19 @@ export function main() {
|
|||
|
||||
it('should only allow identifier or keyword as formatter names', () => {
|
||||
expect(() => parseBinding('"Foo"|(')).toThrowError(new RegExp('identifier or keyword'));
|
||||
expect(() => parseBinding('"Foo"|1234')).toThrowError(new RegExp('identifier or keyword'));
|
||||
expect(() => parseBinding('"Foo"|"uppercase"')).toThrowError(new RegExp('identifier or keyword'));
|
||||
expect(() => parseBinding('"Foo"|1234'))
|
||||
.toThrowError(new RegExp('identifier or keyword'));
|
||||
expect(() => parseBinding('"Foo"|"uppercase"'))
|
||||
.toThrowError(new RegExp('identifier or keyword'));
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('should store the source in the result', () => {
|
||||
expect(parseBinding('someExpr').source).toBe('someExpr');
|
||||
});
|
||||
it('should store the source in the result',
|
||||
() => { expect(parseBinding('someExpr').source).toBe('someExpr'); });
|
||||
|
||||
it('should store the passed-in location', () => {
|
||||
expect(parseBinding('someExpr', 'location').location).toBe('location');
|
||||
});
|
||||
it('should store the passed-in location',
|
||||
() => { expect(parseBinding('someExpr', 'location').location).toBe('location'); });
|
||||
|
||||
it('should throw on chain expressions', () => {
|
||||
expect(() => parseBinding("1;2")).toThrowError(new RegExp("contain chained expression"));
|
||||
|
@ -451,7 +414,7 @@ export function main() {
|
|||
describe('parseTemplateBindings', () => {
|
||||
|
||||
function keys(templateBindings) {
|
||||
return ListWrapper.map(templateBindings, (binding) => binding.key );
|
||||
return ListWrapper.map(templateBindings, (binding) => binding.key);
|
||||
}
|
||||
|
||||
function keyValues(templateBindings) {
|
||||
|
@ -459,19 +422,21 @@ export function main() {
|
|||
if (binding.keyIsVar) {
|
||||
return '#' + binding.key + (isBlank(binding.name) ? '' : '=' + binding.name);
|
||||
} else {
|
||||
return binding.key + (isBlank(binding.expression) ? '' : `=${binding.expression}`)
|
||||
return binding.key + (isBlank(binding.expression) ? '' : `=${binding.expression}`)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function exprSources(templateBindings) {
|
||||
return ListWrapper.map(templateBindings,
|
||||
(binding) => isPresent(binding.expression) ? binding.expression.source : null );
|
||||
return ListWrapper.map(templateBindings, (binding) => isPresent(binding.expression) ?
|
||||
binding.expression.source :
|
||||
null);
|
||||
}
|
||||
|
||||
function exprAsts(templateBindings) {
|
||||
return ListWrapper.map(templateBindings,
|
||||
(binding) => isPresent(binding.expression) ? binding.expression : null );
|
||||
return ListWrapper.map(templateBindings, (binding) => isPresent(binding.expression) ?
|
||||
binding.expression :
|
||||
null);
|
||||
}
|
||||
|
||||
it('should parse an empty string', () => {
|
||||
|
@ -497,13 +462,11 @@ export function main() {
|
|||
bindings = parseTemplateBindings("a-b:'c'");
|
||||
expect(keys(bindings)).toEqual(['a-b']);
|
||||
|
||||
expect( () => {
|
||||
parseTemplateBindings('(:0');
|
||||
}).toThrowError(new RegExp('expected identifier, keyword, or string'));
|
||||
expect(() => { parseTemplateBindings('(:0'); })
|
||||
.toThrowError(new RegExp('expected identifier, keyword, or string'));
|
||||
|
||||
expect( () => {
|
||||
parseTemplateBindings('1234:0');
|
||||
}).toThrowError(new RegExp('expected identifier, keyword, or string'));
|
||||
expect(() => { parseTemplateBindings('1234:0'); })
|
||||
.toThrowError(new RegExp('expected identifier, keyword, or string'));
|
||||
});
|
||||
|
||||
it('should detect expressions as value', () => {
|
||||
|
@ -565,20 +528,20 @@ export function main() {
|
|||
expect(keyValues(bindings)).toEqual(['keyword', '#item=\$implicit', '#i=k']);
|
||||
|
||||
bindings = parseTemplateBindings("directive: var item in expr; var a = b", 'location');
|
||||
expect(keyValues(bindings)).toEqual(['directive', '#item=\$implicit', 'directive-in=expr in location', '#a=b']);
|
||||
expect(keyValues(bindings))
|
||||
.toEqual(['directive', '#item=\$implicit', 'directive-in=expr in location', '#a=b']);
|
||||
});
|
||||
|
||||
it('should parse pipes', () => {
|
||||
var bindings = parseTemplateBindings('key value|pipe');
|
||||
var ast = bindings[0].expression.ast
|
||||
var ast = bindings[0].expression.ast;
|
||||
expect(ast).toBeAnInstanceOf(Pipe);
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseInterpolation', () => {
|
||||
it('should return null if no interpolation', () => {
|
||||
expect(parseInterpolation('nothing')).toBe(null);
|
||||
});
|
||||
it('should return null if no interpolation',
|
||||
() => { expect(parseInterpolation('nothing')).toBe(null); });
|
||||
|
||||
it('should parse no prefix/suffix interpolation', () => {
|
||||
var ast = parseInterpolation('{{a}}').ast;
|
||||
|
@ -621,7 +584,8 @@ export function main() {
|
|||
|
||||
describe('wrapLiteralPrimitive', () => {
|
||||
it('should wrap a literal primitive', () => {
|
||||
expect(createParser().wrapLiteralPrimitive("foo", null).eval(null, emptyLocals())).toEqual("foo");
|
||||
expect(createParser().wrapLiteralPrimitive("foo", null).eval(null, emptyLocals()))
|
||||
.toEqual("foo");
|
||||
});
|
||||
});
|
||||
});
|
|
@ -1,299 +0,0 @@
|
|||
import {describe, it, iit, xit, expect, beforeEach, afterEach} from 'angular2/test_lib';
|
||||
import {IterableChanges} from 'angular2/src/change_detection/pipes/iterable_changes';
|
||||
|
||||
import {NumberWrapper} from 'angular2/src/facade/lang';
|
||||
import {ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
|
||||
|
||||
import {TestIterable} from '../iterable';
|
||||
import {iterableChangesAsString} from '../util';
|
||||
|
||||
// todo(vicb): UnmodifiableListView / frozen object when implemented
|
||||
export function main() {
|
||||
describe('collection_changes', function() {
|
||||
describe('CollectionChanges', function() {
|
||||
var changes;
|
||||
var l;
|
||||
|
||||
beforeEach(() => {
|
||||
changes = new IterableChanges();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
changes = null;
|
||||
});
|
||||
|
||||
it('should support list and iterables', () => {
|
||||
expect(IterableChanges.supportsObj([])).toBeTruthy();
|
||||
expect(IterableChanges.supportsObj(new TestIterable())).toBeTruthy();
|
||||
expect(IterableChanges.supportsObj(MapWrapper.create())).toBeFalsy();
|
||||
expect(IterableChanges.supportsObj(null)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should support iterables', () => {
|
||||
l = new TestIterable();
|
||||
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: []
|
||||
}));
|
||||
|
||||
l.list = [1];
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['1[null->0]'],
|
||||
additions: ['1[null->0]']
|
||||
}));
|
||||
|
||||
l.list = [2, 1];
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['2[null->0]', '1[0->1]'],
|
||||
previous: ['1[0->1]'],
|
||||
additions: ['2[null->0]'],
|
||||
moves: ['1[0->1]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should detect additions', () => {
|
||||
l = [];
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: []
|
||||
}));
|
||||
|
||||
ListWrapper.push(l, 'a');
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['a[null->0]'],
|
||||
additions: ['a[null->0]']
|
||||
}));
|
||||
|
||||
ListWrapper.push(l, 'b');
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['a', 'b[null->1]'],
|
||||
previous: ['a'],
|
||||
additions: ['b[null->1]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should support changing the reference', () => {
|
||||
l = [0];
|
||||
changes.check(l);
|
||||
|
||||
l = [1, 0];
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['1[null->0]', '0[0->1]'],
|
||||
previous: ['0[0->1]'],
|
||||
additions: ['1[null->0]'],
|
||||
moves: ['0[0->1]']
|
||||
}));
|
||||
|
||||
l = [2, 1, 0];
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['2[null->0]', '1[0->1]', '0[1->2]'],
|
||||
previous: ['1[0->1]', '0[1->2]'],
|
||||
additions: ['2[null->0]'],
|
||||
moves: ['1[0->1]', '0[1->2]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should handle swapping element', () => {
|
||||
l = [1, 2];
|
||||
changes.check(l);
|
||||
|
||||
ListWrapper.clear(l);
|
||||
ListWrapper.push(l, 2);
|
||||
ListWrapper.push(l, 1);
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['2[1->0]', '1[0->1]'],
|
||||
previous: ['1[0->1]', '2[1->0]'],
|
||||
moves: ['2[1->0]', '1[0->1]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should handle swapping element', () => {
|
||||
l = ['a', 'b', 'c'];
|
||||
changes.check(l);
|
||||
|
||||
ListWrapper.removeAt(l, 1);
|
||||
ListWrapper.insert(l, 0, 'b');
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['b[1->0]', 'a[0->1]', 'c'],
|
||||
previous: ['a[0->1]', 'b[1->0]', 'c'],
|
||||
moves: ['b[1->0]', 'a[0->1]']
|
||||
}));
|
||||
|
||||
ListWrapper.removeAt(l, 1);
|
||||
ListWrapper.push(l, 'a');
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['b', 'c[2->1]', 'a[1->2]'],
|
||||
previous: ['b', 'a[1->2]', 'c[2->1]'],
|
||||
moves: ['c[2->1]', 'a[1->2]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should detect changes in list', () => {
|
||||
l = [];
|
||||
changes.check(l);
|
||||
|
||||
ListWrapper.push(l, 'a');
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['a[null->0]'],
|
||||
additions: ['a[null->0]']
|
||||
}));
|
||||
|
||||
ListWrapper.push(l, 'b');
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['a', 'b[null->1]'],
|
||||
previous: ['a'],
|
||||
additions: ['b[null->1]']
|
||||
}));
|
||||
|
||||
ListWrapper.push(l, 'c');
|
||||
ListWrapper.push(l, 'd');
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['a', 'b', 'c[null->2]', 'd[null->3]'],
|
||||
previous: ['a', 'b'],
|
||||
additions: ['c[null->2]', 'd[null->3]']
|
||||
}));
|
||||
|
||||
ListWrapper.removeAt(l, 2);
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['a', 'b', 'd[3->2]'],
|
||||
previous: ['a', 'b', 'c[2->null]', 'd[3->2]'],
|
||||
moves: ['d[3->2]'],
|
||||
removals: ['c[2->null]']
|
||||
}));
|
||||
|
||||
ListWrapper.clear(l);
|
||||
ListWrapper.push(l, 'd');
|
||||
ListWrapper.push(l, 'c');
|
||||
ListWrapper.push(l, 'b');
|
||||
ListWrapper.push(l, 'a');
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['d[2->0]', 'c[null->1]', 'b[1->2]', 'a[0->3]'],
|
||||
previous: ['a[0->3]', 'b[1->2]', 'd[2->0]'],
|
||||
additions: ['c[null->1]'],
|
||||
moves: ['d[2->0]', 'b[1->2]', 'a[0->3]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should test string by value rather than by reference (Dart)', () => {
|
||||
l = ['a', 'boo'];
|
||||
changes.check(l);
|
||||
|
||||
var b = 'b';
|
||||
var oo = 'oo';
|
||||
ListWrapper.set(l, 1, b + oo);
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['a', 'boo'],
|
||||
previous: ['a', 'boo']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should ignore [NaN] != [NaN] (JS)', () => {
|
||||
l = [NumberWrapper.NaN];
|
||||
changes.check(l);
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: [NumberWrapper.NaN],
|
||||
previous: [NumberWrapper.NaN]
|
||||
}));
|
||||
});
|
||||
|
||||
it('should detect [NaN] moves', () => {
|
||||
l = [NumberWrapper.NaN, NumberWrapper.NaN];
|
||||
changes.check(l);
|
||||
|
||||
ListWrapper.insert(l, 0, 'foo');
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['foo[null->0]', 'NaN[0->1]', 'NaN[1->2]'],
|
||||
previous: ['NaN[0->1]', 'NaN[1->2]'],
|
||||
additions: ['foo[null->0]'],
|
||||
moves: ['NaN[0->1]', 'NaN[1->2]']}
|
||||
));
|
||||
});
|
||||
|
||||
it('should remove and add same item', () => {
|
||||
l = ['a', 'b', 'c'];
|
||||
changes.check(l);
|
||||
|
||||
ListWrapper.removeAt(l, 1);
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['a', 'c[2->1]'],
|
||||
previous: ['a', 'b[1->null]', 'c[2->1]'],
|
||||
moves: ['c[2->1]'],
|
||||
removals: ['b[1->null]']
|
||||
}));
|
||||
|
||||
ListWrapper.insert(l, 1, 'b');
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['a', 'b[null->1]', 'c[1->2]'],
|
||||
previous: ['a', 'c[1->2]'],
|
||||
additions: ['b[null->1]'],
|
||||
moves: ['c[1->2]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should support duplicates', () => {
|
||||
l = ['a', 'a', 'a', 'b', 'b'];
|
||||
changes.check(l);
|
||||
|
||||
ListWrapper.removeAt(l, 0);
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['a', 'a', 'b[3->2]', 'b[4->3]'],
|
||||
previous: ['a', 'a', 'a[2->null]', 'b[3->2]', 'b[4->3]'],
|
||||
moves: ['b[3->2]', 'b[4->3]'],
|
||||
removals: ['a[2->null]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should support insertions/moves', () => {
|
||||
l = ['a', 'a', 'b', 'b'];
|
||||
changes.check(l);
|
||||
|
||||
ListWrapper.insert(l, 0, 'b');
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['b[2->0]', 'a[0->1]', 'a[1->2]', 'b', 'b[null->4]'],
|
||||
previous: ['a[0->1]', 'a[1->2]', 'b[2->0]', 'b'],
|
||||
additions: ['b[null->4]'],
|
||||
moves: ['b[2->0]', 'a[0->1]', 'a[1->2]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should not report unnecessary moves', () => {
|
||||
l = ['a', 'b', 'c'];
|
||||
changes.check(l);
|
||||
|
||||
ListWrapper.clear(l);
|
||||
ListWrapper.push(l, 'b');
|
||||
ListWrapper.push(l, 'a');
|
||||
ListWrapper.push(l, 'c');
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({
|
||||
collection: ['b[1->0]', 'a[0->1]', 'c'],
|
||||
previous: ['a[0->1]', 'b[1->0]', 'c'],
|
||||
moves: ['b[1->0]', 'a[0->1]']
|
||||
}));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,295 @@
|
|||
import {describe, it, iit, xit, expect, beforeEach, afterEach} from 'angular2/test_lib';
|
||||
import {IterableChanges} from 'angular2/src/change_detection/pipes/iterable_changes';
|
||||
|
||||
import {NumberWrapper} from 'angular2/src/facade/lang';
|
||||
import {ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
|
||||
|
||||
import {TestIterable} from '../iterable';
|
||||
import {iterableChangesAsString} from '../util';
|
||||
|
||||
// todo(vicb): UnmodifiableListView / frozen object when implemented
|
||||
export function main() {
|
||||
describe('collection_changes', function() {
|
||||
describe('CollectionChanges', function() {
|
||||
var changes;
|
||||
var l;
|
||||
|
||||
beforeEach(() => { changes = new IterableChanges(); });
|
||||
|
||||
afterEach(() => { changes = null; });
|
||||
|
||||
it('should support list and iterables', () => {
|
||||
expect(IterableChanges.supportsObj([])).toBeTruthy();
|
||||
expect(IterableChanges.supportsObj(new TestIterable())).toBeTruthy();
|
||||
expect(IterableChanges.supportsObj(MapWrapper.create())).toBeFalsy();
|
||||
expect(IterableChanges.supportsObj(null)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should support iterables', () => {
|
||||
l = new TestIterable();
|
||||
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({collection: []}));
|
||||
|
||||
l.list = [1];
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(
|
||||
iterableChangesAsString({collection: ['1[null->0]'], additions: ['1[null->0]']}));
|
||||
|
||||
l.list = [2, 1];
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString({
|
||||
collection: ['2[null->0]', '1[0->1]'],
|
||||
previous: ['1[0->1]'],
|
||||
additions: ['2[null->0]'],
|
||||
moves: ['1[0->1]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should detect additions', () => {
|
||||
l = [];
|
||||
changes.check(l);
|
||||
expect(changes.toString()).toEqual(iterableChangesAsString({collection: []}));
|
||||
|
||||
ListWrapper.push(l, 'a');
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(
|
||||
iterableChangesAsString({collection: ['a[null->0]'], additions: ['a[null->0]']}));
|
||||
|
||||
ListWrapper.push(l, 'b');
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString(
|
||||
{collection: ['a', 'b[null->1]'], previous: ['a'], additions: ['b[null->1]']}));
|
||||
});
|
||||
|
||||
it('should support changing the reference', () => {
|
||||
l = [0];
|
||||
changes.check(l);
|
||||
|
||||
l = [1, 0];
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString({
|
||||
collection: ['1[null->0]', '0[0->1]'],
|
||||
previous: ['0[0->1]'],
|
||||
additions: ['1[null->0]'],
|
||||
moves: ['0[0->1]']
|
||||
}));
|
||||
|
||||
l = [2, 1, 0];
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString({
|
||||
collection: ['2[null->0]', '1[0->1]', '0[1->2]'],
|
||||
previous: ['1[0->1]', '0[1->2]'],
|
||||
additions: ['2[null->0]'],
|
||||
moves: ['1[0->1]', '0[1->2]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should handle swapping element', () => {
|
||||
l = [1, 2];
|
||||
changes.check(l);
|
||||
|
||||
ListWrapper.clear(l);
|
||||
ListWrapper.push(l, 2);
|
||||
ListWrapper.push(l, 1);
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString({
|
||||
collection: ['2[1->0]', '1[0->1]'],
|
||||
previous: ['1[0->1]', '2[1->0]'],
|
||||
moves: ['2[1->0]', '1[0->1]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should handle swapping element', () => {
|
||||
l = ['a', 'b', 'c'];
|
||||
changes.check(l);
|
||||
|
||||
ListWrapper.removeAt(l, 1);
|
||||
ListWrapper.insert(l, 0, 'b');
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString({
|
||||
collection: ['b[1->0]', 'a[0->1]', 'c'],
|
||||
previous: ['a[0->1]', 'b[1->0]', 'c'],
|
||||
moves: ['b[1->0]', 'a[0->1]']
|
||||
}));
|
||||
|
||||
ListWrapper.removeAt(l, 1);
|
||||
ListWrapper.push(l, 'a');
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString({
|
||||
collection: ['b', 'c[2->1]', 'a[1->2]'],
|
||||
previous: ['b', 'a[1->2]', 'c[2->1]'],
|
||||
moves: ['c[2->1]', 'a[1->2]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should detect changes in list', () => {
|
||||
l = [];
|
||||
changes.check(l);
|
||||
|
||||
ListWrapper.push(l, 'a');
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(
|
||||
iterableChangesAsString({collection: ['a[null->0]'], additions: ['a[null->0]']}));
|
||||
|
||||
ListWrapper.push(l, 'b');
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString(
|
||||
{collection: ['a', 'b[null->1]'], previous: ['a'], additions: ['b[null->1]']}));
|
||||
|
||||
ListWrapper.push(l, 'c');
|
||||
ListWrapper.push(l, 'd');
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString({
|
||||
collection: ['a', 'b', 'c[null->2]', 'd[null->3]'],
|
||||
previous: ['a', 'b'],
|
||||
additions: ['c[null->2]', 'd[null->3]']
|
||||
}));
|
||||
|
||||
ListWrapper.removeAt(l, 2);
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString({
|
||||
collection: ['a', 'b', 'd[3->2]'],
|
||||
previous: ['a', 'b', 'c[2->null]', 'd[3->2]'],
|
||||
moves: ['d[3->2]'],
|
||||
removals: ['c[2->null]']
|
||||
}));
|
||||
|
||||
ListWrapper.clear(l);
|
||||
ListWrapper.push(l, 'd');
|
||||
ListWrapper.push(l, 'c');
|
||||
ListWrapper.push(l, 'b');
|
||||
ListWrapper.push(l, 'a');
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString({
|
||||
collection: ['d[2->0]', 'c[null->1]', 'b[1->2]', 'a[0->3]'],
|
||||
previous: ['a[0->3]', 'b[1->2]', 'd[2->0]'],
|
||||
additions: ['c[null->1]'],
|
||||
moves: ['d[2->0]', 'b[1->2]', 'a[0->3]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should test string by value rather than by reference (Dart)', () => {
|
||||
l = ['a', 'boo'];
|
||||
changes.check(l);
|
||||
|
||||
var b = 'b';
|
||||
var oo = 'oo';
|
||||
ListWrapper.set(l, 1, b + oo);
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString({collection: ['a', 'boo'], previous: ['a', 'boo']}));
|
||||
});
|
||||
|
||||
it('should ignore [NaN] != [NaN] (JS)', () => {
|
||||
l = [NumberWrapper.NaN];
|
||||
changes.check(l);
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString(
|
||||
{collection: [NumberWrapper.NaN], previous: [NumberWrapper.NaN]}));
|
||||
});
|
||||
|
||||
it('should detect [NaN] moves', () => {
|
||||
l = [NumberWrapper.NaN, NumberWrapper.NaN];
|
||||
changes.check(l);
|
||||
|
||||
ListWrapper.insert(l, 0, 'foo');
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString({
|
||||
collection: ['foo[null->0]', 'NaN[0->1]', 'NaN[1->2]'],
|
||||
previous: ['NaN[0->1]', 'NaN[1->2]'],
|
||||
additions: ['foo[null->0]'],
|
||||
moves: ['NaN[0->1]', 'NaN[1->2]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should remove and add same item', () => {
|
||||
l = ['a', 'b', 'c'];
|
||||
changes.check(l);
|
||||
|
||||
ListWrapper.removeAt(l, 1);
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString({
|
||||
collection: ['a', 'c[2->1]'],
|
||||
previous: ['a', 'b[1->null]', 'c[2->1]'],
|
||||
moves: ['c[2->1]'],
|
||||
removals: ['b[1->null]']
|
||||
}));
|
||||
|
||||
ListWrapper.insert(l, 1, 'b');
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString({
|
||||
collection: ['a', 'b[null->1]', 'c[1->2]'],
|
||||
previous: ['a', 'c[1->2]'],
|
||||
additions: ['b[null->1]'],
|
||||
moves: ['c[1->2]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should support duplicates', () => {
|
||||
l = ['a', 'a', 'a', 'b', 'b'];
|
||||
changes.check(l);
|
||||
|
||||
ListWrapper.removeAt(l, 0);
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString({
|
||||
collection: ['a', 'a', 'b[3->2]', 'b[4->3]'],
|
||||
previous: ['a', 'a', 'a[2->null]', 'b[3->2]', 'b[4->3]'],
|
||||
moves: ['b[3->2]', 'b[4->3]'],
|
||||
removals: ['a[2->null]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should support insertions/moves', () => {
|
||||
l = ['a', 'a', 'b', 'b'];
|
||||
changes.check(l);
|
||||
|
||||
ListWrapper.insert(l, 0, 'b');
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString({
|
||||
collection: ['b[2->0]', 'a[0->1]', 'a[1->2]', 'b', 'b[null->4]'],
|
||||
previous: ['a[0->1]', 'a[1->2]', 'b[2->0]', 'b'],
|
||||
additions: ['b[null->4]'],
|
||||
moves: ['b[2->0]', 'a[0->1]', 'a[1->2]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should not report unnecessary moves', () => {
|
||||
l = ['a', 'b', 'c'];
|
||||
changes.check(l);
|
||||
|
||||
ListWrapper.clear(l);
|
||||
ListWrapper.push(l, 'b');
|
||||
ListWrapper.push(l, 'a');
|
||||
ListWrapper.push(l, 'c');
|
||||
changes.check(l);
|
||||
expect(changes.toString())
|
||||
.toEqual(iterableChangesAsString({
|
||||
collection: ['b[1->0]', 'a[0->1]', 'c'],
|
||||
previous: ['a[0->1]', 'b[1->0]', 'c'],
|
||||
moves: ['b[1->0]', 'a[0->1]']
|
||||
}));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
|
@ -1,97 +0,0 @@
|
|||
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach,
|
||||
AsyncTestCompleter, inject, proxy, SpyObject, IS_DARTIUM} from 'angular2/test_lib';
|
||||
import {Json, RegExp, NumberWrapper, StringWrapper} from 'angular2/src/facade/lang';
|
||||
|
||||
import {JsonPipe} from 'angular2/src/change_detection/pipes/json_pipe';
|
||||
|
||||
export function main() {
|
||||
describe("JsonPipe", () => {
|
||||
var regNewLine = new RegExp('\n');
|
||||
var canHasUndefined; // because Dart doesn't like undefined;
|
||||
var inceptionObj;
|
||||
var inceptionObjString;
|
||||
var catString;
|
||||
var pipe;
|
||||
|
||||
function normalize(obj: string): string {
|
||||
return StringWrapper.replace(obj, regNewLine, '');
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
inceptionObj = {
|
||||
dream: {
|
||||
dream: {
|
||||
dream: 'Limbo'
|
||||
}
|
||||
}
|
||||
};
|
||||
inceptionObjString = "{\n" +
|
||||
" \"dream\": {\n" +
|
||||
" \"dream\": {\n" +
|
||||
" \"dream\": \"Limbo\"\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
|
||||
|
||||
catString = 'Inception Cat';
|
||||
pipe = new JsonPipe();
|
||||
});
|
||||
|
||||
describe("supports", () => {
|
||||
it("should support objects", () => {
|
||||
expect(pipe.supports(inceptionObj)).toBe(true);
|
||||
});
|
||||
|
||||
it("should support strings", () => {
|
||||
expect(pipe.supports(catString)).toBe(true);
|
||||
});
|
||||
|
||||
it("should support null", () => {
|
||||
expect(pipe.supports(null)).toBe(true);
|
||||
});
|
||||
|
||||
it("should support NaN", () => {
|
||||
expect(pipe.supports(NumberWrapper.NaN)).toBe(true);
|
||||
});
|
||||
|
||||
if (!IS_DARTIUM) {
|
||||
it("should support undefined", () => {
|
||||
expect(pipe.supports(canHasUndefined)).toBe(true);
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
describe("transform", () => {
|
||||
it("should return JSON-formatted string", () => {
|
||||
expect(pipe.transform(inceptionObj)).toEqual(inceptionObjString);
|
||||
});
|
||||
|
||||
it("should return JSON-formatted string even when normalized", () => {
|
||||
var dream1 = normalize(pipe.transform(inceptionObj));
|
||||
var dream2 = normalize(inceptionObjString);
|
||||
expect(dream1).toEqual(dream2);
|
||||
});
|
||||
|
||||
it("should return JSON-formatted string similar to Json.stringify", () => {
|
||||
var dream1 = normalize(pipe.transform(inceptionObj));
|
||||
var dream2 = normalize(Json.stringify(inceptionObj));
|
||||
expect(dream1).toEqual(dream2);
|
||||
});
|
||||
|
||||
it("should return same value when nothing has changed since the last call", () => {
|
||||
expect(pipe.transform(inceptionObj)).toEqual(inceptionObjString);
|
||||
expect(pipe.transform(inceptionObj)).toEqual(inceptionObjString);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe("onDestroy", () => {
|
||||
it("should do nothing when no latest value", () => {
|
||||
expect(() => pipe.onDestroy()).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
import {
|
||||
ddescribe,
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
xit,
|
||||
expect,
|
||||
beforeEach,
|
||||
afterEach,
|
||||
AsyncTestCompleter,
|
||||
inject,
|
||||
proxy,
|
||||
SpyObject,
|
||||
IS_DARTIUM
|
||||
} from 'angular2/test_lib';
|
||||
import {Json, RegExp, NumberWrapper, StringWrapper} from 'angular2/src/facade/lang';
|
||||
|
||||
import {JsonPipe} from 'angular2/src/change_detection/pipes/json_pipe';
|
||||
|
||||
export function main() {
|
||||
describe("JsonPipe", () => {
|
||||
var regNewLine = '\n';
|
||||
var canHasUndefined; // because Dart doesn't like undefined;
|
||||
var inceptionObj;
|
||||
var inceptionObjString;
|
||||
var catString;
|
||||
var pipe;
|
||||
|
||||
function normalize(obj: string): string { return StringWrapper.replace(obj, regNewLine, ''); }
|
||||
|
||||
beforeEach(() => {
|
||||
inceptionObj = {
|
||||
dream: {dream: {dream: 'Limbo'}}
|
||||
};
|
||||
inceptionObjString = "{\n" + " \"dream\": {\n" + " \"dream\": {\n" +
|
||||
" \"dream\": \"Limbo\"\n" + " }\n" + " }\n" + "}";
|
||||
|
||||
|
||||
catString = 'Inception Cat';
|
||||
pipe = new JsonPipe();
|
||||
});
|
||||
|
||||
describe("supports", () => {
|
||||
it("should support objects", () => { expect(pipe.supports(inceptionObj)).toBe(true); });
|
||||
|
||||
it("should support strings", () => { expect(pipe.supports(catString)).toBe(true); });
|
||||
|
||||
it("should support null", () => { expect(pipe.supports(null)).toBe(true); });
|
||||
|
||||
it("should support NaN", () => { expect(pipe.supports(NumberWrapper.NaN)).toBe(true); });
|
||||
|
||||
if (!IS_DARTIUM) {
|
||||
it("should support undefined",
|
||||
() => { expect(pipe.supports(canHasUndefined)).toBe(true); });
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
describe("transform", () => {
|
||||
it("should return JSON-formatted string",
|
||||
() => { expect(pipe.transform(inceptionObj)).toEqual(inceptionObjString); });
|
||||
|
||||
it("should return JSON-formatted string even when normalized", () => {
|
||||
var dream1 = normalize(pipe.transform(inceptionObj));
|
||||
var dream2 = normalize(inceptionObjString);
|
||||
expect(dream1).toEqual(dream2);
|
||||
});
|
||||
|
||||
it("should return JSON-formatted string similar to Json.stringify", () => {
|
||||
var dream1 = normalize(pipe.transform(inceptionObj));
|
||||
var dream2 = normalize(Json.stringify(inceptionObj));
|
||||
expect(dream1).toEqual(dream2);
|
||||
});
|
||||
|
||||
it("should return same value when nothing has changed since the last call", () => {
|
||||
expect(pipe.transform(inceptionObj)).toEqual(inceptionObjString);
|
||||
expect(pipe.transform(inceptionObj)).toEqual(inceptionObjString);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe("onDestroy", () => {
|
||||
it("should do nothing when no latest value",
|
||||
() => { expect(() => pipe.onDestroy()).not.toThrow(); });
|
||||
});
|
||||
|
||||
});
|
||||
}
|
|
@ -16,27 +16,21 @@ export function main() {
|
|||
m = MapWrapper.create();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
changes = null;
|
||||
});
|
||||
afterEach(() => { changes = null; });
|
||||
|
||||
it('should detect additions', () => {
|
||||
changes.check(m);
|
||||
|
||||
MapWrapper.set(m, 'a', 1);
|
||||
changes.check(m);
|
||||
expect(changes.toString()).toEqual(kvChangesAsString({
|
||||
map: ['a[null->1]'],
|
||||
additions: ['a[null->1]']
|
||||
}));
|
||||
expect(changes.toString())
|
||||
.toEqual(kvChangesAsString({map: ['a[null->1]'], additions: ['a[null->1]']}));
|
||||
|
||||
MapWrapper.set(m, 'b', 2);
|
||||
changes.check(m);
|
||||
expect(changes.toString()).toEqual(kvChangesAsString({
|
||||
map: ['a', 'b[null->2]'],
|
||||
previous: ['a'],
|
||||
additions: ['b[null->2]']
|
||||
}));
|
||||
expect(changes.toString())
|
||||
.toEqual(kvChangesAsString(
|
||||
{map: ['a', 'b[null->2]'], previous: ['a'], additions: ['b[null->2]']}));
|
||||
});
|
||||
|
||||
it('should handle changing key/values correctly', () => {
|
||||
|
@ -47,11 +41,12 @@ export function main() {
|
|||
MapWrapper.set(m, 2, 10);
|
||||
MapWrapper.set(m, 1, 20);
|
||||
changes.check(m);
|
||||
expect(changes.toString()).toEqual(kvChangesAsString({
|
||||
map: ['1[10->20]', '2[20->10]'],
|
||||
previous: ['1[10->20]', '2[20->10]'],
|
||||
changes: ['1[10->20]', '2[20->10]']
|
||||
}));
|
||||
expect(changes.toString())
|
||||
.toEqual(kvChangesAsString({
|
||||
map: ['1[10->20]', '2[20->10]'],
|
||||
previous: ['1[10->20]', '2[20->10]'],
|
||||
changes: ['1[10->20]', '2[20->10]']
|
||||
}));
|
||||
});
|
||||
|
||||
it('should expose previous and current value', () => {
|
||||
|
@ -63,12 +58,14 @@ export function main() {
|
|||
MapWrapper.set(m, 1, 20);
|
||||
changes.check(m);
|
||||
|
||||
changes.forEachChangedItem((record) => {
|
||||
previous = record.previousValue;
|
||||
current = record.currentValue;
|
||||
})
|
||||
changes.forEachChangedItem((record) =>
|
||||
{
|
||||
previous = record.previousValue;
|
||||
current = record.currentValue;
|
||||
})
|
||||
|
||||
expect(previous).toEqual(10);
|
||||
expect(previous)
|
||||
.toEqual(10);
|
||||
expect(current).toEqual(20);
|
||||
});
|
||||
|
||||
|
@ -77,43 +74,40 @@ export function main() {
|
|||
|
||||
MapWrapper.set(m, 'a', 'A');
|
||||
changes.check(m);
|
||||
expect(changes.toString()).toEqual(kvChangesAsString({
|
||||
map: ['a[null->A]'],
|
||||
additions: ['a[null->A]']
|
||||
}));
|
||||
expect(changes.toString())
|
||||
.toEqual(kvChangesAsString({map: ['a[null->A]'], additions: ['a[null->A]']}));
|
||||
|
||||
MapWrapper.set(m, 'b', 'B');
|
||||
changes.check(m);
|
||||
expect(changes.toString()).toEqual(kvChangesAsString({
|
||||
map: ['a', 'b[null->B]'],
|
||||
previous: ['a'],
|
||||
additions: ['b[null->B]']
|
||||
}));
|
||||
expect(changes.toString())
|
||||
.toEqual(kvChangesAsString(
|
||||
{map: ['a', 'b[null->B]'], previous: ['a'], additions: ['b[null->B]']}));
|
||||
|
||||
MapWrapper.set(m, 'b', 'BB');
|
||||
MapWrapper.set(m, 'd', 'D');
|
||||
changes.check(m);
|
||||
expect(changes.toString()).toEqual(kvChangesAsString({
|
||||
map: ['a', 'b[B->BB]', 'd[null->D]'],
|
||||
previous: ['a', 'b[B->BB]'],
|
||||
additions: ['d[null->D]'],
|
||||
changes: ['b[B->BB]']
|
||||
}));
|
||||
expect(changes.toString())
|
||||
.toEqual(kvChangesAsString({
|
||||
map: ['a', 'b[B->BB]', 'd[null->D]'],
|
||||
previous: ['a', 'b[B->BB]'],
|
||||
additions: ['d[null->D]'],
|
||||
changes: ['b[B->BB]']
|
||||
}));
|
||||
|
||||
MapWrapper.delete(m, 'b');
|
||||
changes.check(m);
|
||||
expect(changes.toString()).toEqual(kvChangesAsString({
|
||||
map: ['a', 'd'],
|
||||
previous: ['a', 'b[BB->null]', 'd'],
|
||||
removals: ['b[BB->null]']
|
||||
}));
|
||||
expect(changes.toString())
|
||||
.toEqual(kvChangesAsString({
|
||||
map: ['a', 'd'],
|
||||
previous: ['a', 'b[BB->null]', 'd'],
|
||||
removals: ['b[BB->null]']
|
||||
}));
|
||||
|
||||
MapWrapper.clear(m);
|
||||
changes.check(m);
|
||||
expect(changes.toString()).toEqual(kvChangesAsString({
|
||||
previous: ['a[A->null]', 'd[D->null]'],
|
||||
removals: ['a[A->null]', 'd[D->null]']
|
||||
}));
|
||||
expect(changes.toString())
|
||||
.toEqual(kvChangesAsString(
|
||||
{previous: ['a[A->null]', 'd[D->null]'], removals: ['a[A->null]', 'd[D->null]']}));
|
||||
});
|
||||
|
||||
it('should test string by value rather than by reference (DART)', () => {
|
||||
|
@ -128,10 +122,7 @@ export function main() {
|
|||
MapWrapper.set(m, f + oo, b + ar);
|
||||
changes.check(m);
|
||||
|
||||
expect(changes.toString()).toEqual(kvChangesAsString({
|
||||
map: ['foo'],
|
||||
previous: ['foo']
|
||||
}));
|
||||
expect(changes.toString()).toEqual(kvChangesAsString({map: ['foo'], previous: ['foo']}));
|
||||
});
|
||||
|
||||
it('should not see a NaN value as a change (JS)', () => {
|
||||
|
@ -139,10 +130,7 @@ export function main() {
|
|||
changes.check(m);
|
||||
|
||||
changes.check(m);
|
||||
expect(changes.toString()).toEqual(kvChangesAsString({
|
||||
map: ['foo'],
|
||||
previous: ['foo']
|
||||
}));
|
||||
expect(changes.toString()).toEqual(kvChangesAsString({map: ['foo'], previous: ['foo']}));
|
||||
});
|
||||
|
||||
// JS specific tests (JS Objects)
|
||||
|
@ -161,45 +149,44 @@ export function main() {
|
|||
|
||||
m['a'] = 'A';
|
||||
changes.check(m);
|
||||
expect(changes.toString()).toEqual(kvChangesAsString({
|
||||
map: ['a[null->A]'],
|
||||
additions: ['a[null->A]']
|
||||
}));
|
||||
expect(changes.toString())
|
||||
.toEqual(kvChangesAsString({map: ['a[null->A]'], additions: ['a[null->A]']}));
|
||||
|
||||
m['b'] = 'B';
|
||||
changes.check(m);
|
||||
expect(changes.toString()).toEqual(kvChangesAsString({
|
||||
map: ['a', 'b[null->B]'],
|
||||
previous: ['a'],
|
||||
additions: ['b[null->B]']
|
||||
}));
|
||||
expect(changes.toString())
|
||||
.toEqual(kvChangesAsString(
|
||||
{map: ['a', 'b[null->B]'], previous: ['a'], additions: ['b[null->B]']}));
|
||||
|
||||
m['b'] = 'BB';
|
||||
m['d'] = 'D';
|
||||
changes.check(m);
|
||||
expect(changes.toString()).toEqual(kvChangesAsString({
|
||||
map: ['a', 'b[B->BB]', 'd[null->D]'],
|
||||
previous: ['a', 'b[B->BB]'],
|
||||
additions: ['d[null->D]'],
|
||||
changes: ['b[B->BB]']
|
||||
}));
|
||||
expect(changes.toString())
|
||||
.toEqual(kvChangesAsString({
|
||||
map: ['a', 'b[B->BB]', 'd[null->D]'],
|
||||
previous: ['a', 'b[B->BB]'],
|
||||
additions: ['d[null->D]'],
|
||||
changes: ['b[B->BB]']
|
||||
}));
|
||||
|
||||
m = {};
|
||||
m['a'] = 'A';
|
||||
m['d'] = 'D';
|
||||
changes.check(m);
|
||||
expect(changes.toString()).toEqual(kvChangesAsString({
|
||||
map: ['a', 'd'],
|
||||
previous: ['a', 'b[BB->null]', 'd'],
|
||||
removals: ['b[BB->null]']
|
||||
}));
|
||||
expect(changes.toString())
|
||||
.toEqual(kvChangesAsString({
|
||||
map: ['a', 'd'],
|
||||
previous: ['a', 'b[BB->null]', 'd'],
|
||||
removals: ['b[BB->null]']
|
||||
}));
|
||||
|
||||
m = {};
|
||||
changes.check(m);
|
||||
expect(changes.toString()).toEqual(kvChangesAsString({
|
||||
previous: ['a[A->null]', 'd[D->null]'],
|
||||
removals: ['a[A->null]', 'd[D->null]']
|
||||
}));
|
||||
expect(changes.toString())
|
||||
.toEqual(kvChangesAsString({
|
||||
previous: ['a[A->null]', 'd[D->null]'],
|
||||
removals: ['a[A->null]', 'd[D->null]']
|
||||
}));
|
||||
});
|
||||
});
|
||||
}
|
|
@ -17,9 +17,7 @@ export function main() {
|
|||
});
|
||||
|
||||
describe("supports", () => {
|
||||
it("should support strings", () => {
|
||||
expect(pipe.supports(str)).toBe(true);
|
||||
});
|
||||
it("should support strings", () => { expect(pipe.supports(str)).toBe(true); });
|
||||
|
||||
it("should not support other objects", () => {
|
||||
expect(pipe.supports(new Object())).toBe(false);
|
|
@ -1,115 +0,0 @@
|
|||
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach,
|
||||
AsyncTestCompleter, inject, proxy, SpyObject} from 'angular2/test_lib';
|
||||
import {IMPLEMENTS} from 'angular2/src/facade/lang';
|
||||
|
||||
import {WrappedValue} from 'angular2/src/change_detection/pipes/pipe';
|
||||
import {ObservablePipe} from 'angular2/src/change_detection/pipes/observable_pipe';
|
||||
import {ChangeDetectorRef} from 'angular2/src/change_detection/change_detector_ref';
|
||||
import {EventEmitter, Observable, ObservableWrapper, TimerWrapper} from 'angular2/src/facade/async';
|
||||
|
||||
export function main() {
|
||||
describe("ObservablePipe", () => {
|
||||
var emitter;
|
||||
var pipe;
|
||||
var ref;
|
||||
var message = new Object();
|
||||
|
||||
beforeEach(() => {
|
||||
emitter = new EventEmitter();
|
||||
ref = new SpyChangeDetectorRef();
|
||||
pipe = new ObservablePipe(ref);
|
||||
});
|
||||
|
||||
describe("supports", () => {
|
||||
it("should support observables", () => {
|
||||
expect(pipe.supports(emitter)).toBe(true);
|
||||
});
|
||||
|
||||
it("should not support other objects", () => {
|
||||
expect(pipe.supports("string")).toBe(false);
|
||||
expect(pipe.supports(null)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("transform", () => {
|
||||
it("should return null when subscribing to an observable", () => {
|
||||
expect(pipe.transform(emitter)).toBe(null);
|
||||
});
|
||||
|
||||
it("should return the latest available value wrapped", inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(emitter);
|
||||
|
||||
ObservableWrapper.callNext(emitter, message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(pipe.transform(emitter)).toEqual(new WrappedValue(message));
|
||||
async.done();
|
||||
}, 0)
|
||||
}));
|
||||
|
||||
it("should return same value when nothing has changed since the last call",
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(emitter);
|
||||
ObservableWrapper.callNext(emitter, message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
pipe.transform(emitter);
|
||||
expect(pipe.transform(emitter)).toBe(message);
|
||||
async.done();
|
||||
}, 0)
|
||||
}));
|
||||
|
||||
it("should dispose of the existing subscription when subscribing to a new observable",
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(emitter);
|
||||
|
||||
var newEmitter = new EventEmitter();
|
||||
expect(pipe.transform(newEmitter)).toBe(null);
|
||||
|
||||
// this should not affect the pipe
|
||||
ObservableWrapper.callNext(emitter, message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(pipe.transform(newEmitter)).toBe(null);
|
||||
async.done();
|
||||
}, 0)
|
||||
}));
|
||||
|
||||
it("should request a change detection check upon receiving a new value",
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(emitter);
|
||||
ObservableWrapper.callNext(emitter, message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(ref.spy('requestCheck')).toHaveBeenCalled();
|
||||
async.done();
|
||||
}, 0)
|
||||
}));
|
||||
});
|
||||
|
||||
describe("onDestroy", () => {
|
||||
it("should do nothing when no subscription", () => {
|
||||
expect(() => pipe.onDestroy()).not.toThrow();
|
||||
});
|
||||
|
||||
it("should dispose of the existing subscription", inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(emitter);
|
||||
pipe.onDestroy();
|
||||
|
||||
ObservableWrapper.callNext(emitter, message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(pipe.transform(emitter)).toBe(null);
|
||||
async.done();
|
||||
}, 0)
|
||||
}));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@proxy
|
||||
@IMPLEMENTS(ChangeDetectorRef)
|
||||
class SpyChangeDetectorRef extends SpyObject {
|
||||
constructor(){super(ChangeDetectorRef);}
|
||||
noSuchMethod(m){return super.noSuchMethod(m)}
|
||||
}
|
|
@ -0,0 +1,125 @@
|
|||
import {
|
||||
ddescribe,
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
xit,
|
||||
expect,
|
||||
beforeEach,
|
||||
afterEach,
|
||||
AsyncTestCompleter,
|
||||
inject,
|
||||
proxy,
|
||||
SpyObject
|
||||
} from 'angular2/test_lib';
|
||||
|
||||
import {IMPLEMENTS} from 'angular2/src/facade/lang';
|
||||
import {WrappedValue} from 'angular2/src/change_detection/pipes/pipe';
|
||||
import {ObservablePipe} from 'angular2/src/change_detection/pipes/observable_pipe';
|
||||
import {ChangeDetectorRef} from 'angular2/src/change_detection/change_detector_ref';
|
||||
import {EventEmitter, ObservableWrapper, TimerWrapper} from 'angular2/src/facade/async';
|
||||
|
||||
export function main() {
|
||||
describe("ObservablePipe", () => {
|
||||
var emitter;
|
||||
var pipe;
|
||||
var ref;
|
||||
var message = new Object();
|
||||
|
||||
beforeEach(() => {
|
||||
emitter = new EventEmitter();
|
||||
ref = new SpyChangeDetectorRef();
|
||||
pipe = new ObservablePipe(ref);
|
||||
});
|
||||
|
||||
describe("supports", () => {
|
||||
it("should support observables", () => { expect(pipe.supports(emitter)).toBe(true); });
|
||||
|
||||
it("should not support other objects", () => {
|
||||
expect(pipe.supports("string")).toBe(false);
|
||||
expect(pipe.supports(null)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("transform", () => {
|
||||
it("should return null when subscribing to an observable",
|
||||
() => { expect(pipe.transform(emitter)).toBe(null); });
|
||||
|
||||
it("should return the latest available value wrapped",
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(emitter);
|
||||
|
||||
ObservableWrapper.callNext(emitter, message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(pipe.transform(emitter)).toEqual(new WrappedValue(message));
|
||||
async.done();
|
||||
}, 0)
|
||||
}));
|
||||
|
||||
|
||||
it("should return same value when nothing has changed since the last call",
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(emitter);
|
||||
ObservableWrapper.callNext(emitter, message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
pipe.transform(emitter);
|
||||
expect(pipe.transform(emitter)).toBe(message);
|
||||
async.done();
|
||||
}, 0)
|
||||
}));
|
||||
|
||||
it("should dispose of the existing subscription when subscribing to a new observable",
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(emitter);
|
||||
|
||||
var newEmitter = new EventEmitter();
|
||||
expect(pipe.transform(newEmitter)).toBe(null);
|
||||
|
||||
// this should not affect the pipe
|
||||
ObservableWrapper.callNext(emitter, message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(pipe.transform(newEmitter)).toBe(null);
|
||||
async.done();
|
||||
}, 0)
|
||||
}));
|
||||
|
||||
it("should request a change detection check upon receiving a new value",
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(emitter);
|
||||
ObservableWrapper.callNext(emitter, message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(ref.spy('requestCheck')).toHaveBeenCalled();
|
||||
async.done();
|
||||
}, 0)
|
||||
}));
|
||||
});
|
||||
|
||||
describe("onDestroy", () => {
|
||||
it("should do nothing when no subscription",
|
||||
() => { expect(() => pipe.onDestroy()).not.toThrow(); });
|
||||
|
||||
it("should dispose of the existing subscription", inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(emitter);
|
||||
pipe.onDestroy();
|
||||
|
||||
ObservableWrapper.callNext(emitter, message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(pipe.transform(emitter)).toBe(null);
|
||||
async.done();
|
||||
}, 0)
|
||||
}));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@proxy
|
||||
@IMPLEMENTS(ChangeDetectorRef)
|
||||
class SpyChangeDetectorRef extends SpyObject {
|
||||
constructor() { super(ChangeDetectorRef); }
|
||||
noSuchMethod(m) { return super.noSuchMethod(m) }
|
||||
}
|
|
@ -9,49 +9,37 @@ export function main() {
|
|||
var secondPipe = new Pipe();
|
||||
|
||||
it("should return the first pipe supporting the data type", () => {
|
||||
var r = new PipeRegistry({
|
||||
"type": [
|
||||
new PipeFactory(false, firstPipe),
|
||||
new PipeFactory(true, secondPipe)
|
||||
]
|
||||
});
|
||||
var r = new PipeRegistry(
|
||||
{"type": [new PipeFactory(false, firstPipe), new PipeFactory(true, secondPipe)]});
|
||||
|
||||
expect(r.get("type", "some object", null)).toBe(secondPipe);
|
||||
});
|
||||
|
||||
it("should throw when no matching type", () => {
|
||||
var r = new PipeRegistry({});
|
||||
expect(() => r.get("unknown", "some object", null)).toThrowError(
|
||||
`Cannot find 'unknown' pipe supporting object 'some object'`
|
||||
);
|
||||
expect(() => r.get("unknown", "some object", null))
|
||||
.toThrowError(`Cannot find 'unknown' pipe supporting object 'some object'`);
|
||||
});
|
||||
|
||||
it("should throw when no matching pipe", () => {
|
||||
var r = new PipeRegistry({
|
||||
"type" : []
|
||||
});
|
||||
var r = new PipeRegistry({"type": []});
|
||||
|
||||
expect(() => r.get("type", "some object", null)).toThrowError(
|
||||
`Cannot find 'type' pipe supporting object 'some object'`
|
||||
);
|
||||
expect(() => r.get("type", "some object", null))
|
||||
.toThrowError(`Cannot find 'type' pipe supporting object 'some object'`);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class PipeFactory {
|
||||
shouldSupport:boolean;
|
||||
pipe:any;
|
||||
shouldSupport: boolean;
|
||||
pipe: any;
|
||||
|
||||
constructor(shouldSupport:boolean, pipe:any) {
|
||||
constructor(shouldSupport: boolean, pipe: any) {
|
||||
this.shouldSupport = shouldSupport;
|
||||
this.pipe = pipe;
|
||||
}
|
||||
|
||||
supports(obj):boolean {
|
||||
return this.shouldSupport;
|
||||
}
|
||||
supports(obj): boolean { return this.shouldSupport; }
|
||||
|
||||
create(cdRef):Pipe {
|
||||
return this.pipe;
|
||||
}
|
||||
create(cdRef): Pipe { return this.pipe; }
|
||||
}
|
|
@ -1,119 +0,0 @@
|
|||
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach,
|
||||
AsyncTestCompleter, inject, proxy, SpyObject} from 'angular2/test_lib';
|
||||
import {IMPLEMENTS} from 'angular2/src/facade/lang';
|
||||
import {PromisePipe} from 'angular2/src/change_detection/pipes/promise_pipe';
|
||||
import {WrappedValue} from 'angular2/src/change_detection/pipes/pipe';
|
||||
import {ChangeDetectorRef} from 'angular2/src/change_detection/change_detector_ref';
|
||||
import {PromiseWrapper, TimerWrapper} from 'angular2/src/facade/async';
|
||||
import {DOM} from 'angular2/src/dom/dom_adapter';
|
||||
|
||||
export function main() {
|
||||
describe("PromisePipe", () => {
|
||||
var message = new Object();
|
||||
var pipe;
|
||||
var completer;
|
||||
var ref;
|
||||
//adds longer timers for passing tests in IE
|
||||
var timer = (DOM.getUserAgent().indexOf("Trident") > -1) ? 50 : 0;
|
||||
|
||||
beforeEach(() => {
|
||||
completer = PromiseWrapper.completer();
|
||||
ref = new SpyChangeDetectorRef();
|
||||
pipe = new PromisePipe(ref);
|
||||
});
|
||||
|
||||
describe("supports", () => {
|
||||
it("should support promises", () => {
|
||||
expect(pipe.supports(completer.promise)).toBe(true);
|
||||
});
|
||||
|
||||
it("should not support other objects", () => {
|
||||
expect(pipe.supports("string")).toBe(false);
|
||||
expect(pipe.supports(null)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("transform", () => {
|
||||
it("should return null when subscribing to a promise", () => {
|
||||
expect(pipe.transform(completer.promise)).toBe(null);
|
||||
});
|
||||
|
||||
it("should return the latest available value", inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(completer.promise);
|
||||
|
||||
completer.resolve(message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(pipe.transform(completer.promise)).toEqual(new WrappedValue(message));
|
||||
async.done();
|
||||
}, timer)
|
||||
}));
|
||||
|
||||
it("should return unwrapped value when nothing has changed since the last call",
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(completer.promise);
|
||||
completer.resolve(message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
pipe.transform(completer.promise);
|
||||
expect(pipe.transform(completer.promise)).toBe(message);
|
||||
async.done();
|
||||
}, timer)
|
||||
}));
|
||||
|
||||
it("should dispose of the existing subscription when subscribing to a new promise",
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(completer.promise);
|
||||
|
||||
var newCompleter = PromiseWrapper.completer();
|
||||
expect(pipe.transform(newCompleter.promise)).toBe(null);
|
||||
|
||||
// this should not affect the pipe, so it should return WrappedValue
|
||||
completer.resolve(message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(pipe.transform(newCompleter.promise)).toBe(null);
|
||||
async.done();
|
||||
}, timer)
|
||||
}));
|
||||
|
||||
it("should request a change detection check upon receiving a new value",
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(completer.promise);
|
||||
completer.resolve(message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(ref.spy('requestCheck')).toHaveBeenCalled();
|
||||
async.done();
|
||||
}, timer)
|
||||
}));
|
||||
|
||||
describe("onDestroy", () => {
|
||||
it("should do nothing when no source", () => {
|
||||
expect(() => pipe.onDestroy()).not.toThrow();
|
||||
});
|
||||
|
||||
it("should dispose of the existing source", inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(completer.promise);
|
||||
expect(pipe.transform(completer.promise)).toBe(null);
|
||||
completer.resolve(message)
|
||||
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(pipe.transform(completer.promise)).toEqual(new WrappedValue(message));
|
||||
pipe.onDestroy();
|
||||
expect(pipe.transform(completer.promise)).toBe(null);
|
||||
async.done();
|
||||
}, timer);
|
||||
}));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@proxy
|
||||
@IMPLEMENTS(ChangeDetectorRef)
|
||||
class SpyChangeDetectorRef extends SpyObject {
|
||||
constructor(){super(ChangeDetectorRef);}
|
||||
noSuchMethod(m){return super.noSuchMethod(m)}
|
||||
}
|
|
@ -0,0 +1,127 @@
|
|||
import {
|
||||
ddescribe,
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
xit,
|
||||
expect,
|
||||
beforeEach,
|
||||
afterEach,
|
||||
AsyncTestCompleter,
|
||||
inject,
|
||||
proxy,
|
||||
SpyObject
|
||||
} from 'angular2/test_lib';
|
||||
import {IMPLEMENTS, isBlank} from 'angular2/src/facade/lang';
|
||||
import {PromisePipe} from 'angular2/src/change_detection/pipes/promise_pipe';
|
||||
import {WrappedValue} from 'angular2/src/change_detection/pipes/pipe';
|
||||
import {ChangeDetectorRef} from 'angular2/src/change_detection/change_detector_ref';
|
||||
import {PromiseWrapper, TimerWrapper} from 'angular2/src/facade/async';
|
||||
import {DOM} from 'angular2/src/dom/dom_adapter';
|
||||
|
||||
export function main() {
|
||||
describe("PromisePipe", () => {
|
||||
var message = new Object();
|
||||
var pipe;
|
||||
var completer;
|
||||
var ref;
|
||||
// adds longer timers for passing tests in IE
|
||||
var timer = (!isBlank(DOM) && DOM.getUserAgent().indexOf("Trident") > -1) ? 50 : 0;
|
||||
|
||||
beforeEach(() => {
|
||||
completer = PromiseWrapper.completer();
|
||||
ref = new SpyChangeDetectorRef();
|
||||
pipe = new PromisePipe(ref);
|
||||
});
|
||||
|
||||
describe("supports", () => {
|
||||
it("should support promises", () => { expect(pipe.supports(completer.promise)).toBe(true); });
|
||||
|
||||
it("should not support other objects", () => {
|
||||
expect(pipe.supports("string")).toBe(false);
|
||||
expect(pipe.supports(null)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("transform", () => {
|
||||
it("should return null when subscribing to a promise",
|
||||
() => { expect(pipe.transform(completer.promise)).toBe(null); });
|
||||
|
||||
it("should return the latest available value", inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(completer.promise);
|
||||
|
||||
completer.resolve(message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(pipe.transform(completer.promise)).toEqual(new WrappedValue(message));
|
||||
async.done();
|
||||
}, timer)
|
||||
}));
|
||||
|
||||
it("should return unwrapped value when nothing has changed since the last call",
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(completer.promise);
|
||||
completer.resolve(message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
pipe.transform(completer.promise);
|
||||
expect(pipe.transform(completer.promise)).toBe(message);
|
||||
async.done();
|
||||
}, timer)
|
||||
}));
|
||||
|
||||
it("should dispose of the existing subscription when subscribing to a new promise",
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(completer.promise);
|
||||
|
||||
var newCompleter = PromiseWrapper.completer();
|
||||
expect(pipe.transform(newCompleter.promise)).toBe(null);
|
||||
|
||||
// this should not affect the pipe, so it should return WrappedValue
|
||||
completer.resolve(message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(pipe.transform(newCompleter.promise)).toBe(null);
|
||||
async.done();
|
||||
}, timer)
|
||||
}));
|
||||
|
||||
it("should request a change detection check upon receiving a new value",
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(completer.promise);
|
||||
completer.resolve(message);
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(ref.spy('requestCheck')).toHaveBeenCalled();
|
||||
async.done();
|
||||
}, timer)
|
||||
}));
|
||||
|
||||
describe("onDestroy", () => {
|
||||
it("should do nothing when no source",
|
||||
() => { expect(() => pipe.onDestroy()).not.toThrow(); });
|
||||
|
||||
it("should dispose of the existing source", inject([AsyncTestCompleter], (async) => {
|
||||
pipe.transform(completer.promise);
|
||||
expect(pipe.transform(completer.promise)).toBe(null);
|
||||
completer.resolve(message)
|
||||
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
expect(pipe.transform(completer.promise)).toEqual(new WrappedValue(message));
|
||||
pipe.onDestroy();
|
||||
expect(pipe.transform(completer.promise)).toBe(null);
|
||||
async.done();
|
||||
}, timer);
|
||||
}));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@proxy
|
||||
@IMPLEMENTS(ChangeDetectorRef)
|
||||
class SpyChangeDetectorRef extends SpyObject {
|
||||
constructor() { super(ChangeDetectorRef); }
|
||||
noSuchMethod(m) { return super.noSuchMethod(m) }
|
||||
}
|
|
@ -17,9 +17,7 @@ export function main() {
|
|||
});
|
||||
|
||||
describe("supports", () => {
|
||||
it("should support strings", () => {
|
||||
expect(pipe.supports(str)).toBe(true);
|
||||
});
|
||||
it("should support strings", () => { expect(pipe.supports(str)).toBe(true); });
|
||||
|
||||
it("should not support other objects", () => {
|
||||
expect(pipe.supports(new Object())).toBe(false);
|
|
@ -1,29 +0,0 @@
|
|||
import {isBlank} from 'angular2/src/facade/lang';
|
||||
|
||||
export function iterableChangesAsString({collection, previous, additions, moves, removals}) {
|
||||
if (isBlank(collection)) collection = [];
|
||||
if (isBlank(previous)) previous = [];
|
||||
if (isBlank(additions)) additions = [];
|
||||
if (isBlank(moves)) moves = [];
|
||||
if (isBlank(removals)) removals = [];
|
||||
|
||||
return "collection: " + collection.join(', ') + "\n" +
|
||||
"previous: " + previous.join(', ') + "\n" +
|
||||
"additions: " + additions.join(', ') + "\n" +
|
||||
"moves: " + moves.join(', ') + "\n" +
|
||||
"removals: " + removals.join(', ') + "\n";
|
||||
}
|
||||
|
||||
export function kvChangesAsString({map, previous, additions, changes, removals}) {
|
||||
if (isBlank(map)) map = [];
|
||||
if (isBlank(previous)) previous = [];
|
||||
if (isBlank(additions)) additions = [];
|
||||
if (isBlank(changes)) changes = [];
|
||||
if (isBlank(removals)) removals = [];
|
||||
|
||||
return "map: " + map.join(', ') + "\n" +
|
||||
"previous: " + previous.join(', ') + "\n" +
|
||||
"additions: " + additions.join(', ') + "\n" +
|
||||
"changes: " + changes.join(', ') + "\n" +
|
||||
"removals: " + removals.join(', ') + "\n";
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
import {isBlank, CONST_EXPR} from 'angular2/src/facade/lang';
|
||||
|
||||
export function iterableChangesAsString({collection = CONST_EXPR([]), previous = CONST_EXPR([]),
|
||||
additions = CONST_EXPR([]), moves = CONST_EXPR([]),
|
||||
removals = CONST_EXPR([])}) {
|
||||
return "collection: " + collection.join(', ') + "\n" + "previous: " + previous.join(', ') + "\n" +
|
||||
"additions: " + additions.join(', ') + "\n" + "moves: " + moves.join(', ') + "\n" +
|
||||
"removals: " + removals.join(', ') + "\n";
|
||||
}
|
||||
|
||||
export function kvChangesAsString({map, previous, additions, changes, removals}:
|
||||
{
|
||||
map?:List<any>,
|
||||
previous?:List<any>,
|
||||
additions?: List<any>,
|
||||
changes?: List<any>,
|
||||
removals?: List<any>
|
||||
}):string {
|
||||
if (isBlank(map)) map = [];
|
||||
if (isBlank(previous)) previous = [];
|
||||
if (isBlank(additions)) additions = [];
|
||||
if (isBlank(changes)) changes = [];
|
||||
if (isBlank(removals)) removals = [];
|
||||
|
||||
return "map: " + map.join(', ') + "\n" + "previous: " + previous.join(', ') + "\n" +
|
||||
"additions: " + additions.join(', ') + "\n" + "changes: " + changes.join(', ') + "\n" +
|
||||
"removals: " + removals.join(', ') + "\n";
|
||||
}
|
|
@ -120,22 +120,19 @@ module.exports = function makeNodeTree(destinationPath) {
|
|||
// Transform all tests to make them runnable in node
|
||||
nodeTree = replace(nodeTree, {
|
||||
files: ['**/test/**/*_spec.js'],
|
||||
patterns: [
|
||||
{
|
||||
// Override the default DOM adapter with Parse5 for all tests
|
||||
match: /"use strict";/,
|
||||
replacement:
|
||||
"'use strict'; var parse5Adapter = require('angular2/src/dom/parse5_adapter'); " +
|
||||
"parse5Adapter.Parse5DomAdapter.makeCurrent();"
|
||||
},
|
||||
{
|
||||
// Append main() to all tests since all of our tests are wrapped in exported main fn
|
||||
match: /$/g,
|
||||
replacement: "\r\n main();"
|
||||
}
|
||||
]
|
||||
replaceWithPath: function(path, content) {
|
||||
return "var parse5Adapter = require('angular2/src/dom/parse5_adapter'); " +
|
||||
"parse5Adapter.Parse5DomAdapter.makeCurrent();" + content;
|
||||
},
|
||||
patterns: [{
|
||||
// Append main() to all tests since all of our tests are wrapped in exported main fn
|
||||
match: /$/g,
|
||||
replacement: "\r\n main();"
|
||||
}]
|
||||
});
|
||||
|
||||
|
||||
|
||||
// TODO(iminar): tree differ seems to have issues with trees created by mergeTrees, investigate!
|
||||
// ENOENT error is thrown while doing fs.readdirSync on inputRoot
|
||||
// in the meantime, we just do noop mv to create a new tree
|
||||
|
|
Loading…
Reference in New Issue