Skip to content

Support new-style MOCK_METHOD gmock macro #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,22 @@
"title": "File Generator",
"properties": {
"filegen.templatePath": {
"type": [
"string"
],
"type": "string",
"default": "filegen",
"description": "Specifies the path, where the template files located in."
},
"filegen.gmockStyle": {
"type": "string",
"enum": [
"old",
"new"
],
"enumDescriptions": [
"Use the old-style MOCK_METHODn, MOCK_CONST_METHODn, ... macros.",
"Use the new MOCK_METHOD macro."
],
"default": "old",
"description": "Gmock style to use when generating mock classes from interfaces."
}
}
}
Expand All @@ -139,4 +150,4 @@
"typescript": "^3.6.4",
"vscode-test": "^1.2.2"
}
}
}
12 changes: 12 additions & 0 deletions src/conversions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { GmockStyle } from './gmock_helper';

export function gmockStyleFromStr(str: string) : GmockStyle {
switch(str) {
case "old":
return GmockStyle.Old;
case "new":
return GmockStyle.New;
default:
throw new Error(`Unknown style '${str}'`);
}
}
18 changes: 17 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,24 @@ import { vscode_helper } from "./vscode_helper";
import { dir_helper } from "./dir_helper";
import { template_helper } from './template_helper';
import { GmockHelper } from './gmock_helper';
import { gmockStyleFromStr } from './conversions';

const template = new template_helper();
const gmock = new GmockHelper();
let kConfig: any = {};

function makeGmock(): GmockHelper {
var config = vscode.workspace.getConfiguration('filegen');

var gmockStyleStr = config.get('gmockStyle');
if(gmockStyleStr === undefined) {
throw Error('Coudln\'t find config filegen.gmockStyle');
}

var gmockStyle = gmockStyleFromStr(gmockStyleStr as string);

return new GmockHelper(gmockStyle);
}

async function makeClassGenerator(type: number,
className: string,
location: string,
Expand Down Expand Up @@ -65,6 +78,7 @@ async function handleReplaceGmock(args: any) {
const selection = editor.selection;
const word = doc.getText(selection);
if (word.indexOf("virtual ") >= 0 || word.indexOf("class ") >= 0) {
const gmock = makeGmock();
editor.edit(eb => {
// 文本替换
const mock = gmock.toGmock(word);
Expand All @@ -90,6 +104,7 @@ async function handleExtractGmockEq(root: string, args: any) {
}
const fullText = doc.getText();
const curFileName = path.basename(doc.uri.fsPath);
const gmock = makeGmock();
const result = gmock.extractEqClass(fullText, startTag, lineNumber, curFileName);
if (!result.data) {
vscode.window.showErrorMessage(`FileGen: ${result.error}.`);
Expand Down Expand Up @@ -133,6 +148,7 @@ async function handleExtractGmockClass(root: string, args: any) {
// let f = doc.fileName;
const fullText = doc.getText();
const curFileName = path.basename(doc.uri.fsPath);
const gmock = makeGmock();
const result = gmock.extractMockClass(fullText, startTag, lineNumber, curFileName);
if (!result.data) {
vscode.window.showErrorMessage(`FileGen: ${result.error}.`);
Expand Down
24 changes: 22 additions & 2 deletions src/gmock_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@ interface ExtractResult {
data?: ExtractData;
}

export enum GmockStyle {
Old,
New,
}

export class GmockHelper {
readonly style: GmockStyle;

constructor(style: GmockStyle = GmockStyle.Old) {
this.style = style;
}

private getArgsCount(args: string): number {
// (StreamerLogType type,
Expand Down Expand Up @@ -119,8 +129,18 @@ export class GmockHelper {
if (!fun) {
return text;
}
const modifier = fun.modifier.indexOf('const') >= 0 ? '_CONST' : '';
return `${fun.beforeVirtual}MOCK${modifier}_METHOD${fun.argsCount}(${fun.functionName}, ${fun.returnType}${fun.args});${fun.arfterArgs}`;

var modifier = '';

switch(this.style)
{
case GmockStyle.Old:
modifier = fun.modifier.indexOf('const') >= 0 ? '_CONST' : '';
return `${fun.beforeVirtual}MOCK${modifier}_METHOD${fun.argsCount}(${fun.functionName}, ${fun.returnType}${fun.args});${fun.arfterArgs}`;
case GmockStyle.New:
modifier = fun.modifier.indexOf('const') >= 0 ? ', (const)' : '';
return `${fun.beforeVirtual}MOCK_METHOD(${fun.returnType}, ${fun.functionName}, ${fun.args}${modifier}); ${fun.arfterArgs}`;
}
}

private parseClassDeclare(text: string): { [key: string]: string } {
Expand Down