2015-02-10 01:39:29 -05:00
|
|
|
import {describe, it, iit, ddescribe, expect, tick, async, SpyObject, beforeEach, proxy} from 'angular2/test_lib';
|
2015-03-13 19:47:22 -04:00
|
|
|
import {MapWrapper} from 'angular2/src/facade/collection';
|
|
|
|
import {IMPLEMENTS} from 'angular2/src/facade/lang';
|
2014-10-29 14:26:52 -04:00
|
|
|
|
|
|
|
class TestObj {
|
2014-11-22 00:19:23 -05:00
|
|
|
prop;
|
2014-10-29 14:26:52 -04:00
|
|
|
constructor(prop) {
|
|
|
|
this.prop = prop;
|
|
|
|
}
|
2015-04-14 16:26:42 -04:00
|
|
|
someFunc():number {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-04-23 12:13:42 -04:00
|
|
|
someComplexFunc(a) {
|
|
|
|
return a;
|
|
|
|
}
|
2014-10-29 14:26:52 -04:00
|
|
|
}
|
|
|
|
|
2014-11-25 18:16:53 -05:00
|
|
|
@proxy
|
|
|
|
@IMPLEMENTS(TestObj)
|
2015-04-14 16:26:42 -04:00
|
|
|
class SpyTestObj extends SpyObject {
|
|
|
|
constructor(){super(TestObj);}
|
|
|
|
noSuchMethod(m){return super.noSuchMethod(m)}
|
|
|
|
}
|
2014-11-25 18:16:53 -05:00
|
|
|
|
2015-04-24 18:19:11 -04:00
|
|
|
|
2014-10-29 14:26:52 -04:00
|
|
|
export function main() {
|
2014-11-17 20:39:39 -05:00
|
|
|
describe('test_lib', () => {
|
|
|
|
describe('equality', () => {
|
|
|
|
it('should structurally compare objects', () => {
|
|
|
|
var expected = new TestObj(new TestObj({'one' : [1,2]}));
|
|
|
|
var actual = new TestObj(new TestObj({'one' : [1,2]}));
|
|
|
|
var falseActual = new TestObj(new TestObj({'one' : [1,3]}));
|
2014-10-29 14:26:52 -04:00
|
|
|
|
|
|
|
expect(actual).toEqual(expected);
|
|
|
|
expect(falseActual).not.toEqual(expected);
|
|
|
|
});
|
|
|
|
});
|
2014-11-17 20:39:39 -05:00
|
|
|
|
|
|
|
describe('toEqual for Maps', () => {
|
|
|
|
it('should detect equality for same reference', () => {
|
|
|
|
var m1 = MapWrapper.createFromStringMap({'a': 1});
|
|
|
|
expect(m1).toEqual(m1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect equality for same content', () => {
|
|
|
|
expect(MapWrapper.createFromStringMap({'a': 1})).toEqual(MapWrapper.createFromStringMap({'a': 1}));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect missing entries', () => {
|
|
|
|
expect(MapWrapper.createFromStringMap({'a': 1})).not.toEqual(MapWrapper.createFromStringMap({}));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect different values', () => {
|
|
|
|
expect(MapWrapper.createFromStringMap({'a': 1})).not.toEqual(MapWrapper.createFromStringMap({'a': 2}));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect additional entries', () => {
|
|
|
|
expect(MapWrapper.createFromStringMap({'a': 1})).not.toEqual(MapWrapper.createFromStringMap({'a': 1, 'b': 1}));
|
|
|
|
});
|
|
|
|
});
|
2014-11-25 18:16:53 -05:00
|
|
|
|
|
|
|
describe("spy objects", () => {
|
|
|
|
var spyObj;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
spyObj = new SpyTestObj();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should pass the runtime check", () => {
|
|
|
|
var t:TestObj = spyObj;
|
|
|
|
expect(t).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return a new spy func with no calls", () => {
|
|
|
|
expect(spyObj.spy("someFunc")).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should record function calls", () => {
|
2015-04-14 16:26:42 -04:00
|
|
|
spyObj.spy("someFunc").andCallFake((a,b) => {
|
|
|
|
return a + b
|
|
|
|
});
|
2014-11-25 18:16:53 -05:00
|
|
|
|
|
|
|
expect(spyObj.someFunc(1,2)).toEqual(3);
|
|
|
|
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(1,2);
|
|
|
|
});
|
2015-04-14 16:26:42 -04:00
|
|
|
|
2015-04-23 12:13:42 -04:00
|
|
|
it("should match multiple function calls", () => {
|
|
|
|
spyObj.someFunc(1,2);
|
|
|
|
spyObj.someFunc(3,4);
|
|
|
|
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(1,2);
|
|
|
|
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(3,4);
|
|
|
|
});
|
|
|
|
|
2015-05-07 20:18:42 -04:00
|
|
|
it("should match null arguments", () => {
|
|
|
|
spyObj.someFunc(null, "hello");
|
|
|
|
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(null,"hello");
|
|
|
|
});
|
|
|
|
|
2015-04-23 12:13:42 -04:00
|
|
|
it("should match using deep equality", () => {
|
|
|
|
spyObj.someComplexFunc([1]);
|
|
|
|
expect(spyObj.spy("someComplexFunc")).toHaveBeenCalledWith([1]);
|
|
|
|
});
|
|
|
|
|
2015-04-17 19:08:59 -04:00
|
|
|
it("should support stubs", () => {
|
|
|
|
var s = SpyObject.stub({"a":1}, {"b":2});
|
|
|
|
|
|
|
|
expect(s.a()).toEqual(1);
|
|
|
|
expect(s.b()).toEqual(2);
|
|
|
|
});
|
|
|
|
|
2015-04-14 16:26:42 -04:00
|
|
|
it('should create spys for all methods', () => {
|
2015-04-14 21:01:44 -04:00
|
|
|
expect(() => spyObj.someFunc()).not.toThrow();
|
2015-04-14 16:26:42 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should create a default spy that does not fail for numbers', () => {
|
|
|
|
// Need to return null instead of undefined so that rtts assert does
|
|
|
|
// not fail...
|
|
|
|
expect(spyObj.someFunc()).toBe(null);
|
|
|
|
});
|
2014-11-25 18:16:53 -05:00
|
|
|
});
|
2014-10-29 14:26:52 -04:00
|
|
|
});
|
2015-02-16 23:04:03 -05:00
|
|
|
}
|