Skip to content

Commit

Permalink
Code from prototype keys now is taken with checking if key is getter …
Browse files Browse the repository at this point in the history
…or setter
  • Loading branch information
NagRock committed Jan 25, 2017
1 parent 17c9557 commit fcde94a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import {ReturnValueMethodStub} from "./stub/ReturnValueMethodStub";
import {MethodStub} from "./stub/MethodStub";
import {RedundantMethodNameInCodeFinder} from "./utils/RedundantMethodNameInCodeFinder";
import {strictEqual} from "./ts-mockito";
import {PrototypeKeyCodeGetter} from "./utils/PrototypeKeyCodeGetter";

export class Mocker {
private methodStubCollections: any = {};
private methodActions: Array<MethodAction> = [];
private mock: any = {};
private instance: any = {};
private redundantMethodNameInCodeFinder = new RedundantMethodNameInCodeFinder();
private subKeysInCodeFinder = new PrototypeKeyCodeGetter();

constructor(private clazz: any) {
this.mock.__tsmockitoInstance = this.instance;
Expand Down Expand Up @@ -76,7 +78,7 @@ export class Mocker {

private createMethodStubsFromFunctionsCode(): void {
for (let key in this.clazz.prototype) {
const subKeys = this.redundantMethodNameInCodeFinder.find(this.clazz.prototype[key].toString());
const subKeys = this.redundantMethodNameInCodeFinder.find(this.subKeysInCodeFinder.get(this.clazz.prototype, key));
for (let subKey in subKeys) {
this.createMethodStub(subKey);
}
Expand Down Expand Up @@ -133,7 +135,7 @@ export class Mocker {

private createInstanceActionListenersFromFunctionsCode(): void {
for (let key in this.clazz.prototype) {
const subKeys = this.redundantMethodNameInCodeFinder.find(this.clazz.prototype[key].toString());
const subKeys = this.redundantMethodNameInCodeFinder.find(this.subKeysInCodeFinder.get(this.clazz.prototype, key));
for (let subKey in subKeys) {
this.createInstanceActionListener(subKey);
}
Expand Down
17 changes: 17 additions & 0 deletions src/utils/PrototypeKeyCodeGetter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export class PrototypeKeyCodeGetter {
get(proto: any, key: string): string {
const descriptor = Object.getOwnPropertyDescriptor(proto, key);
if (!descriptor.get && !descriptor.set) {
return proto[key].toString();
}
if (descriptor.get && descriptor.set) {
return descriptor.get.toString() + " " + descriptor.set.toString();
}
if (descriptor.get && !descriptor.set) {
return descriptor.get.toString();
}
if (!descriptor.get && descriptor.set) {
return descriptor.set.toString();
}
}
}
37 changes: 37 additions & 0 deletions test/mocking.getter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {mock, instance} from "../src/ts-mockito";
import {Bar} from "./utils/Bar";

describe('mocking', () => {
let mockedFoo: FooWithGetterAndSetter;
let foo: FooWithGetterAndSetter;

describe('mocking object with getters and setters', () => {
it('does not execute getter or setter code (not throwing null pointer exception)', () => {
// given

// when
mockedFoo = mock(FooWithGetterAndSetter);
foo = instance(mockedFoo);

// then

});
});
});

class FooWithGetterAndSetter {
constructor(private dependency: Bar) {
}

public sampleMethod(): number {
return 4;
}

public get twoPlusTwo(): number {
return this.dependency.sumTwoNumbers(2, 2);
}

public set twoPlusTwo(value: number) {
this.dependency.sumTwoNumbers(value, 0);
}
}

0 comments on commit fcde94a

Please sign in to comment.