Skip to content

Commit f06433f

Browse files
committed
feat(test_lib): implement SpyObject
1 parent 965f70b commit f06433f

File tree

7 files changed

+107
-22
lines changed

7 files changed

+107
-22
lines changed

modules/core/test/compiler/element_injector_spec.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {describe, ddescribe, it, iit, xit, xdescribe, expect, beforeEach, FakeObject} from 'test_lib/test_lib';
2-
import {isBlank, isPresent, FIELD, IMPLEMENTS} from 'facade/lang';
1+
import {describe, ddescribe, it, iit, xit, xdescribe, expect, beforeEach, SpyObject} from 'test_lib/test_lib';
2+
import {isBlank, isPresent, FIELD, IMPLEMENTS, proxy} from 'facade/lang';
33
import {ListWrapper, MapWrapper, List} from 'facade/collection';
44
import {ProtoElementInjector, PreBuiltObjects} from 'core/compiler/element_injector';
55
import {Parent, Ancestor} from 'core/annotations/visibility';
@@ -9,12 +9,10 @@ import {ProtoRecordRange} from 'change_detection/record_range';
99
import {ViewPort} from 'core/compiler/viewport';
1010
import {NgElement} from 'core/dom/element';
1111

12-
//TODO: vsavkin: use a spy object
13-
class DummyView extends View {
14-
constructor() {
15-
super(null, null, new ProtoRecordRange(), MapWrapper.create());
16-
}
17-
}
12+
@proxy
13+
@IMPLEMENTS(View)
14+
class DummyView extends SpyObject {noSuchMethod(m){super.noSuchMethod(m)}}
15+
1816

1917
class Directive {
2018
}

modules/facade/src/lang.es6

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {assert} from 'rtts_assert/rtts_assert';
2+
export {proxy} from 'rtts_assert/rtts_assert';
23

34
export var Type = Function;
45
export var Math = window.Math;

modules/rtts_assert/src/rtts_assert.es6

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ function argPositionName(i) {
1313

1414
var primitives = $traceurRuntime.type;
1515

16+
export function proxy(){
17+
}
18+
1619
function assertArgumentTypes(...params) {
1720
var actual, type;
1821
var currentArgErrors;
@@ -78,11 +81,14 @@ function prettyPrint(value) {
7881
}
7982

8083
function isType(value, T, errors) {
81-
8284
if (T === primitives.void) {
8385
return typeof value === 'undefined';
8486
}
8587

88+
if (_isProxy(value)) {
89+
return true;
90+
}
91+
8692
if (T === primitives.any || value === null) {
8793
return true;
8894
}
@@ -143,6 +149,11 @@ function isType(value, T, errors) {
143149
// return res;
144150
}
145151

152+
function _isProxy(obj) {
153+
if (!obj || !obj.constructor || !obj.constructor.annotations) return false;
154+
return obj.constructor.annotations.filter((a) => a instanceof proxy).length > 0;
155+
}
156+
146157
function formatErrors(errors, indent = ' ') {
147158
return errors.map((e) => {
148159
if (typeof e === 'string') return indent + '- ' + e;

modules/test_lib/src/test_lib.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Expect extends gns.Expect {
2424
void toEqual(expected) => toHaveSameProps(expected);
2525
void toThrowError([message=""]) => this.toThrowWith(message: message);
2626
void toBePromise() => _expect(actual is Future, equals(true));
27+
void toImplement(expected) => toBeA(expected);
2728
Function get _expect => gns.guinness.matchers.expect;
2829
}
2930

modules/test_lib/src/test_lib.es6

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,51 @@ window.beforeEach(function() {
7373
};
7474
}
7575
};
76+
},
77+
78+
toImplement: function() {
79+
return {
80+
compare: function(actualObject, expectedInterface) {
81+
var objProps = Object.keys(actualObject.constructor.prototype);
82+
var intProps = Object.keys(expectedInterface.prototype);
83+
84+
var missedMethods = [];
85+
intProps.forEach((k) => {
86+
if (!actualObject.constructor.prototype[k]) missedMethods.push(k);
87+
});
88+
89+
return {
90+
pass: missedMethods.length == 0,
91+
get message() {
92+
return 'Expected ' + actualObject + ' to have the following methods: ' + missedMethods.join(", ");
93+
}
94+
};
95+
}
96+
};
7697
}
7798
});
7899
});
79100

101+
export class SpyObject {
102+
spy(name){
103+
if (! this[name]) {
104+
this[name] = this._createGuinnessCompatibleSpy();
105+
}
106+
return this[name];
107+
}
108+
109+
rttsAssert(value) {
110+
return true;
111+
}
112+
113+
_createGuinnessCompatibleSpy(){
114+
var newSpy = jasmine.createSpy();
115+
newSpy.andCallFake = newSpy.and.callFake;
116+
return newSpy;
117+
}
118+
}
119+
120+
80121
function mapToString(m) {
81122
if (!m) {
82123
return ''+m;

modules/test_lib/test/test_lib_spec.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import {describe, it, iit, ddescribe, expect, tick, async} from 'test_lib/test_lib';
1+
import {describe, it, iit, ddescribe, expect, tick, async, SpyObject, beforeEach} from 'test_lib/test_lib';
22
import {MapWrapper, ListWrapper} from 'facade/collection';
33
import {PromiseWrapper} from 'facade/async';
4+
import {IMPLEMENTS, proxy} from 'facade/lang';
45

56
class TestObj {
67
prop;
@@ -9,6 +10,10 @@ class TestObj {
910
}
1011
}
1112

13+
@proxy
14+
@IMPLEMENTS(TestObj)
15+
class SpyTestObj extends SpyObject {noSuchMethod(m){return super.noSuchMethod(m)}}
16+
1217
export function main() {
1318
describe('test_lib', () => {
1419
describe('equality', () => {
@@ -48,5 +53,29 @@ export function main() {
4853
expect(MapWrapper.createFromStringMap({'a': 1})).not.toEqual(MapWrapper.createFromStringMap({'a': 1, 'b': 1}));
4954
});
5055
});
56+
57+
describe("spy objects", () => {
58+
var spyObj;
59+
60+
beforeEach(() => {
61+
spyObj = new SpyTestObj();
62+
});
63+
64+
it("should pass the runtime check", () => {
65+
var t:TestObj = spyObj;
66+
expect(t).toBeDefined();
67+
});
68+
69+
it("should return a new spy func with no calls", () => {
70+
expect(spyObj.spy("someFunc")).not.toHaveBeenCalled();
71+
});
72+
73+
it("should record function calls", () => {
74+
spyObj.spy("someFunc").andCallFake((a,b) => a + b);
75+
76+
expect(spyObj.someFunc(1,2)).toEqual(3);
77+
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(1,2);
78+
});
79+
});
5180
});
5281
}
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
import {ddescribe, describe, it, expect, IS_DARTIUM} from 'test_lib/test_lib';
1+
import {ddescribe, describe, it, iit, expect, IS_DARTIUM} from 'test_lib/test_lib';
22
import {IMPLEMENTS} from './fixtures/annotations';
33

4-
class Interface1 {}
5-
class Interface2 {}
4+
class Interface1 {
5+
one(){}
6+
}
7+
class Interface2 {
8+
two(){}
9+
}
610

711
@IMPLEMENTS(Interface1, Interface2)
8-
class SomeClass {}
12+
class SomeClass {
13+
one(){}
14+
two(){}
15+
}
916

1017
export function main() {
1118
describe('interfaces', function() {
12-
//TODO: remvoe when interfaces are supported in AtScript
13-
if (IS_DARTIUM) {
14-
it('should work', function () {
15-
var s = new SomeClass();
16-
expect(s instanceof Interface1).toBeTrue();
17-
expect(s instanceof Interface2).toBeTrue();
18-
});
19-
}
19+
it('should work', function () {
20+
var s = new SomeClass();
21+
expect(s).toImplement(Interface1);
22+
expect(s).toImplement(Interface2);
23+
});
2024
});
2125

2226
}

0 commit comments

Comments
 (0)