Skip to content

Commit

Permalink
Merge pull request #22 from uittorio/#20-handle-method-without-return…
Browse files Browse the repository at this point in the history
…-value

make sure methods without return value will not fail the compilation
  • Loading branch information
uittorio authored Apr 6, 2019
2 parents c8daaa5 + 86c1be5 commit 0e21af2
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion config/karma/karma.config.transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ module.exports = function(config) {
const karmaConfig = karmaBaseConfig(config, '../../test/transformer/**/*.test.ts');

config.set(karmaConfig);
};
};
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export { MockMarker } from "./src/mockMarker/mockMarker";
export { MockMethod } from "./src/mockMethod/mockMethod";
export { On, AutoMockExtensionHandler } from "./src/framework/framework";
export { createMock } from "./src/transformer/create-mock";
export {method} from "./src/framework/methodExtension";
export { method } from "./src/framework/methodExtension";
2 changes: 1 addition & 1 deletion src/transformer/descriptor/descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,4 @@ export function GetDescriptor(node: ts.Node): ts.Expression {
console.log("NOT IMPLEMENTED "+ ts.SyntaxKind[node.kind]);
return GetNullDescriptor();
}
}
}
2 changes: 1 addition & 1 deletion src/transformer/descriptor/method/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ export function GetMethodDescriptor(propertyName: ts.PropertyName, returnValue:

const parenthesizedExpression = ts.createParen(functionExpression);
return ts.createCall(parenthesizedExpression, [], []);
}
}
2 changes: 1 addition & 1 deletion src/transformer/descriptor/method/methodDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ export function GetMethodDeclarationDescriptor(node: ts.MethodDeclaration): ts.E
}

return GetMethodDescriptor(node.name, returnType);
}
}
18 changes: 12 additions & 6 deletions src/transformer/descriptor/method/methodSignature.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import * as ts from "typescript";
import {GetDescriptor} from "../descriptor";
import {GetMethodDescriptor} from "./method";
import { GetNullDescriptor } from "../null/null";
import { GetDescriptor } from "../descriptor";

type Methods = ts.MethodSignature | ts.MethodDeclaration;

export function GetMethodSignatureDescriptor(node: Methods): ts.Expression {
const returnType: ts.Expression = GetDescriptor(node.type);
export function GetMethodSignatureDescriptor(node: ts.MethodSignature): ts.Expression {
let returnType: ts.Expression;

if (node.type) {
returnType = GetDescriptor(node.type);
} else {
returnType = GetNullDescriptor();
}

return GetMethodDescriptor(node.name, returnType);
}
}
2 changes: 1 addition & 1 deletion src/transformer/descriptor/property/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ export class PropertySignatureCache {
public get(): ts.PropertyName {
return this._cache || TypescriptHelper.createEmptyProperty().name;
}
}
}
2 changes: 1 addition & 1 deletion src/transformer/typeValidator/typeValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ export function isTypeReusable(node: ts.Node): boolean {

function hasTypeArguments(node: ts.Node): boolean {
return ts.isTypeReferenceNode(node) && !!node.typeArguments && node.typeArguments.length > 0;
}
}
2 changes: 1 addition & 1 deletion test/framework/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ MockFactory.instance.registerFactory((name: string, value: any) => {
});

const frameworkContext = require.context('./', true, /\.test\.ts$/);
frameworkContext.keys().map(frameworkContext);
frameworkContext.keys().map(frameworkContext);
26 changes: 19 additions & 7 deletions test/transformer/descriptor/methods/methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ describe('for methods', () => {
interface InterfaceReturnMethod {
a: string;
}

interface Interface {
interface Interface {
a(): void;
b(): number;
c(): string;
Expand All @@ -25,14 +25,14 @@ describe('for methods', () => {
describe('for interface declaration', () => {
interface Interface {
method: () => number
}

}
it('should set the functions', () => {
const properties: Interface = createMock<Interface>();
expect(properties.method()).toBe(0);
});
});

describe('for declaration', () => {
class MyClass {
method(): number {
Expand All @@ -44,7 +44,7 @@ describe('for methods', () => {
const properties: MyClass = createMock<MyClass>();
expect(properties.method()).toBe(0);
});
});
});

describe('for undeclared return value', () => {
class MyClass {
Expand All @@ -66,4 +66,16 @@ describe('for methods', () => {
expect(properties()).toBe("");
});
});
});

describe('for an interface without return value', () => {
interface Interface {
method();
}

it('should return null', () => {
const a: Interface = createMock<Interface>();

expect(a.method()).toBeNull();
});
})
});

0 comments on commit 0e21af2

Please sign in to comment.