From 996a8a878d6c850280b354a3eff0ad81e6fa84d6 Mon Sep 17 00:00:00 2001 From: Daniel Nieto Date: Wed, 19 Feb 2025 12:34:57 +0100 Subject: [PATCH 1/2] feat: support new-style MOCK_METHOD gmock macro --- package.json | 19 +++++++++++++++---- src/conversions.ts | 12 ++++++++++++ src/extension.ts | 18 +++++++++++++++++- src/gmock_helper.ts | 24 ++++++++++++++++++++++-- 4 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 src/conversions.ts diff --git a/package.json b/package.json index 3331087..1f35eff 100755 --- a/package.json +++ b/package.json @@ -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." } } } @@ -139,4 +150,4 @@ "typescript": "^3.6.4", "vscode-test": "^1.2.2" } -} \ No newline at end of file +} diff --git a/src/conversions.ts b/src/conversions.ts new file mode 100644 index 0000000..2811e75 --- /dev/null +++ b/src/conversions.ts @@ -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}'`); + } +} \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index cb2b7de..40d0ecd 100755 --- a/src/extension.ts +++ b/src/extension.ts @@ -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, @@ -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); @@ -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}.`); @@ -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}.`); diff --git a/src/gmock_helper.ts b/src/gmock_helper.ts index b6e40ab..e1fa48c 100644 --- a/src/gmock_helper.ts +++ b/src/gmock_helper.ts @@ -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, @@ -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 } { From 13ab99011e6520b7911559dba5efd673601725c7 Mon Sep 17 00:00:00 2001 From: Daniel Nieto Date: Wed, 19 Feb 2025 12:45:17 +0100 Subject: [PATCH 2/2] fix: handle non-const methods correctly --- src/gmock_helper.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gmock_helper.ts b/src/gmock_helper.ts index e1fa48c..7aadf61 100644 --- a/src/gmock_helper.ts +++ b/src/gmock_helper.ts @@ -138,8 +138,8 @@ export class GmockHelper { 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}`; + modifier = fun.modifier.indexOf('const') >= 0 ? ', (const)' : ''; + return `${fun.beforeVirtual}MOCK_METHOD(${fun.returnType}, ${fun.functionName}, ${fun.args}${modifier}); ${fun.arfterArgs}`; } }