diff --git a/src/cli/dts2cpp/src/gen/generate/function_async.js b/src/cli/dts2cpp/src/gen/generate/function_async.js index 282169bc49e47988103c4fa64bc024ed768c7927..4cbdfc1886a7296c56cfa3a57be6f462c34d1c92 100644 --- a/src/cli/dts2cpp/src/gen/generate/function_async.js +++ b/src/cli/dts2cpp/src/gen/generate/function_async.js @@ -146,18 +146,7 @@ function generateFunctionAsync(func, data, className, implHCbVariable) { returnGenerate(param.callback, param, data) middleH = replaceValueOut(middleH, param); - if (param.valueCheckout == "") { - middleFunc = replaceAll(middleFunc, "[valueCheckout]", param.valueCheckout) // # 输入参数解析 - } else { - param.valueCheckout = removeEndlineEnter(param.valueCheckout) - middleFunc = replaceAll(middleFunc, "[valueCheckout]", param.valueCheckout) // # 输入参数解析 - } - let optionalCallback = getOptionalCallbackInit(param) - if (optionalCallback == "") { - middleFunc = replaceAll(middleFunc, "[optionalCallbackInit]", optionalCallback) // 可选callback参数初始化 - } else { - middleFunc = replaceAll(middleFunc, "[optionalCallbackInit]", optionalCallback + "\n ") // 可选callback参数初始化 - } + middleFunc = getMiddleFunc(param, middleFunc); middleFunc = replaceAll(middleFunc, "[start_async]", ` napi_value result = pxt->StartAsync(%s_execute, reinterpret_cast(vio), %s_complete, pxt->GetArgc() == %s? pxt->GetArgv(%d) : nullptr);` @@ -186,6 +175,22 @@ function generateFunctionAsync(func, data, className, implHCbVariable) { return [middleFunc, implH, implCpp, middleH] } +function getMiddleFunc(param, middleFunc) { + if (param.valueCheckout === '') { + middleFunc = replaceAll(middleFunc, '[valueCheckout]', param.valueCheckout); // # 输入参数解析 + } else { + param.valueCheckout = removeEndlineEnter(param.valueCheckout); + middleFunc = replaceAll(middleFunc, '[valueCheckout]', param.valueCheckout); // # 输入参数解析 + } + let optionalCallback = getOptionalCallbackInit(param); + if (optionalCallback === '') { + middleFunc = replaceAll(middleFunc, '[optionalCallbackInit]', optionalCallback); // 可选callback参数初始化 + } else { + middleFunc = replaceAll(middleFunc, '[optionalCallbackInit]', optionalCallback + '\n '); // 可选callback参数初始化 + } + return middleFunc; +} + function generateCbInterfaceOutFunc(param, className, prefixArr, implHCbVariable, implCpp, implH) { let cbInterfaceRes = ""; let outInterfaceDefine = param.valueDefine.substring(param.valueDefine.lastIndexOf(",") + 1, diff --git a/src/cli/dts2cpp/src/gen/generate/param_generate.js b/src/cli/dts2cpp/src/gen/generate/param_generate.js index 3697520ac659202cc420dfdb29be3c90b9caab8b..f4e863aff15d9a265b056f789231cb4dff7637a2 100644 --- a/src/cli/dts2cpp/src/gen/generate/param_generate.js +++ b/src/cli/dts2cpp/src/gen/generate/param_generate.js @@ -76,25 +76,7 @@ function jsToC(dest, napiVn, type, enumType = 0, optional) { if (type.indexOf("|") >= 0) { return unionTempleteFunc(dest, napiVn, type, optional) } else if (type === "string") { - let verifyEnumValue = ''; - let strlt = LenIncrease.getAndIncrease() - if (enumType) { - // 对枚举是string的值做校验 - verifyEnumValue = ` - bool isStrValueInMap%s = false; - for (const auto& pair : enumMap%s) { - const char* charPtr = std::any_cast(pair.second); - std::string value = charPtr; - if (value == %s) { - isStrValueInMap%s = true; - break; - } - } - if (!isStrValueInMap%s) { - napi_throw_error(env, nullptr, "enum value is wrong!"); - return nullptr; - }`.format(strlt, enumType, dest, strlt, strlt) - } + let verifyEnumValue = getVeriyEnumValue(enumType, dest); if (napiVn.indexOf("GetValueProperty") >= 0) { let lt = LenIncrease.getAndIncrease() @@ -103,7 +85,7 @@ function jsToC(dest, napiVn, type, enumType = 0, optional) { } else { return "pxt->SwapJs2CUtf8(%s, %s);".format(napiVn, dest) + verifyEnumValue } - } else if (type.substring(type.length - 2) === "[]") { + } else if (type.substring(type.length - 2) === "[]") { return arrTemplete(dest, napiVn, type); } else if (type.substring(0, 12) === "NUMBER_TYPE_") { return numTempleteFunc (enumType, napiVn, type, dest); @@ -129,6 +111,29 @@ function jsToC(dest, napiVn, type, enumType = 0, optional) { } } +function getVeriyEnumValue(enumType, dest) { + let verifyEnumValue = ''; + let strlt = LenIncrease.getAndIncrease(); + if (enumType) { + // 对枚举是string的值做校验 + verifyEnumValue = ` + bool isStrValueInMap%s = false; + for (const auto& pair : enumMap%s) { + const char* charPtr = std::any_cast(pair.second); + std::string value = charPtr; + if (value == %s) { + isStrValueInMap%s = true; + break; + } + } + if (!isStrValueInMap%s) { + napi_throw_error(env, nullptr, "enum value is wrong!"); + return nullptr; + }`.format(strlt, enumType, dest, strlt, strlt); + } + return verifyEnumValue; +} + function interfaceTempleteFunc(type, napiVn, dest) { let tt = ""; let ifl = InterfaceList.getValue(type); diff --git a/src/cli/dts2cpp/src/gen/tools/tool.js b/src/cli/dts2cpp/src/gen/tools/tool.js index 7b65ba2702692474dff3119fbbe14abb4a2b01c5..f2d2a3c7aa2aaf1f771f5cec05d9615b96597ecc 100644 --- a/src/cli/dts2cpp/src/gen/tools/tool.js +++ b/src/cli/dts2cpp/src/gen/tools/tool.js @@ -78,33 +78,43 @@ function checkOutBody(body, off, flag, binside) { for (let i = off + flag[0].length; i < body.length; i++) { if (body[i] === '"') cs1 += 1 if (cs1 % 2 === 0) { - let tb1 = true; - for (let k in csl) { - if (csl[k] !== csr[idx[k]]) { - tb1 = false; - break; - } - } + let tb1 = getTb1(csl, csr, idx); if (tb1 && body.substring(i, i + flag[1].length) === flag[1]) { if (binside) return body.substring(off + flag[0].length, i); return body.substring(off, i + flag[1].length); } - - if (body[i] in csl) { - csl[body[i]] += 1; - if (body[i] in csr) csr[body[i]] += 1; - } - if (body[i] in csr) { - if (!(body[i] === '>' && body[i-1] === '=')) { // 尖括号匹配时忽略关键字 "=>" - csr[body[i]] += 1; - } - } + checkOutBody2(body, i, csl, csr); } } return null; } +function checkOutBody2(body, i, csl, csr) { + if (body[i] in csl) { + csl[body[i]] += 1; + if (body[i] in csr) { + csr[body[i]] += 1; + } + } + if (body[i] in csr) { + if (!(body[i] === '>' && body[i - 1] === '=')) { // 尖括号匹配时忽略关键字 "=>" + csr[body[i]] += 1; + } + } +} + +function getTb1(csl, csr, idx) { + let tb1 = true; + for (let k in csl) { + if (csl[k] !== csr[idx[k]]) { + tb1 = false; + break; + } + } + return tb1; +} + function removeExplains(data) { // 去除 /** */ 类型的注释 while (data.indexOf("/*") >= 0) { diff --git a/src/cli/dts2ets/appCodeGen/src/generate.js b/src/cli/dts2ets/appCodeGen/src/generate.js index f3a710f5234c863c55b1f26c3ef2905283051fdb..2c5187a6ad00d94f3e79141cb7f943b8fbeb7a04 100644 --- a/src/cli/dts2ets/appCodeGen/src/generate.js +++ b/src/cli/dts2ets/appCodeGen/src/generate.js @@ -15,7 +15,6 @@ // 生成BUILD.gn // 生成tool_utility.h,生成tool_utility.cpp const { replaceAll } = require("./tools/tool"); -// const { generateNamespace } = require("./generate/namespace"); const { writeFile } = require("./tools/FileRW"); const re = require("./tools/re"); @@ -79,59 +78,6 @@ Button() { }); ` -// var genFileList = [] -// function deleteFolder(folderPath) { -// if (fs.existsSync(folderPath)) { -// fs.rmSync(folderPath, {"recursive": true}) -// } -// } - -// function createFolder(folderPath) { -// if (!fs.existsSync(folderPath)) { -// fs.mkdirSync(folderPath) -// } -// } - -// function formatCode(destDir) { -// let sysInfo = os.platform() -// let clangFmtName = sysInfo === 'win32' ? "clang-format.exe" : "clang-format" -// let callPath = NapiLog.getCallPath(); -// callPath = callPath.substring(callPath.indexOf("[") + 1, callPath.indexOf("src")); -// let dumyClangFmtFile = path.join(callPath, clangFmtName) -// let dumyFmtCfgFile = path.join(callPath, ".clang-format") - -// if(!fs.existsSync(dumyClangFmtFile)) { -// NapiLog.logInfo("Warning: clang-format does not exist, can not format cpp file."); -// return -// } - -// // 使用pkg打包的napi_generator工具,其中的clang-format程序在运行时是解压到一个名为snapshot的虚拟目录中的,如C:\snapshot\napi_generator\ -// // 虚拟目录中的clang-format程序不能直接运行,必须先将它拷贝到本地硬盘的真实目录下。 -// createFolder(path.resolve("./tmpLocal")) -// let localClangFmtFile = path.resolve("./tmpLocal/" + clangFmtName) // clang-format可执行程序 -// let localFmtCfgFile = path.resolve("./tmpLocal/.clang-format") // clang-format格式化配置文件 -// fs.copyFileSync(dumyClangFmtFile, localClangFmtFile) -// fs.copyFileSync(dumyFmtCfgFile, localFmtCfgFile) - -// let execSync = require("child_process").execSync -// if (sysInfo != 'win32') { -// // linux系统下需要为临时复制的clang-format程序增加可执行权限 -// execSync("chmod +x " + "\"" + localClangFmtFile + "\"") -// } - -// for (let i = 0; i < genFileList.length; ++i) { -// // 文件路径前后要用引号包含,防止因为路径中存在空格而导致命令执行失败 (windows的文件夹允许有空格) -// let cmd = "\"" + localClangFmtFile + "\" -style=file -i \"" + path.resolve(path.join(destDir, genFileList[i])) -// + "\"" -// try { -// execSync(cmd) // C++文件格式化 -// } catch (err) { -// NapiLog.logError("Failed to format code, exception: " + err.stderr) -// } -// } -// // 格式化结束后,删除临时目录文件 -// deleteFolder(path.resolve("./tmpLocal")) -// } let FuncCfgList = [] function analyzeJsonCfg(jsonCfg) { let len = jsonCfg.length; diff --git a/src/cli/h2dtscpp/src/src/napiGen/functionDirect.js b/src/cli/h2dtscpp/src/src/napiGen/functionDirect.js index 960ca0b16421baeb787fa312798e2bf11608fa2e..8235acb07ca14547342a0604e57d15487295a077 100644 --- a/src/cli/h2dtscpp/src/src/napiGen/functionDirect.js +++ b/src/cli/h2dtscpp/src/src/napiGen/functionDirect.js @@ -1,33 +1,33 @@ /* * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); +* Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, +* distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ -const { NapiLog } = require("../tools/NapiLog"); +const { NapiLog } = require('../tools/NapiLog'); const util = require('util'); const path = require('path'); -const fs = require("fs"); -const { writeFile, readFile } = require("../tools/tool"); -const re = require("../tools/re"); +const fs = require('fs'); +const { writeFile, readFile } = require('../tools/tool'); +const re = require('../tools/re'); const LENGTH = 10; const TWO_DECIMAL = 2; function analyzeRetIsObject(retType, objectInfo) { // 去除 * 和 空格 - retType = retType.replace('*', '').replace('struct', '').trim() - let objKeys = Object.keys(objectInfo) + retType = retType.replace('*', '').replace('struct', '').trim(); + let objKeys = Object.keys(objectInfo); for (let i = 0; i < objKeys.length; i++) { - if (retType == objKeys[i]) { + if (retType === objKeys[i]) { return true; } } @@ -36,7 +36,7 @@ function analyzeRetIsObject(retType, objectInfo) { function analyzeRetIsTypeDef(type, info) { - let typedefKeys = Object.keys(info) + let typedefKeys = Object.keys(info); for (let i = 0; i < typedefKeys.length; i++) { if (type === typedefKeys[i]) { return info[type]; @@ -48,133 +48,174 @@ function analyzeRetIsTypeDef(type, info) { //tsFuncName function generateDirectFunction(params, index, tsFuncName, directFuncPath, hFileName) { let funcInfo = { - "name": "", - "params": [], - "retType": "", - } + 'name': '', + 'params': [], + 'retType': '', + }; - let funcName_replace = tsFuncName + let funcNameReplace = tsFuncName; // 方法的注册 - let funcInitPath = path.join(__dirname, directFuncPath.initTempleteDetails.funcInitTemplete); - let funcInitTemplete = readFile(funcInitPath) - let init_replace = replaceAll(funcInitTemplete, '[func_name_replace]', funcName_replace) + let initReplace = genInitFunction(directFuncPath, funcNameReplace); // 分析方法 分析第index个方法 - funcInfo.name = params.functions[index].name - funcInfo.retType = params.functions[index].rtnType - let parseParams = params.functions[index].parameters - for (let i = 0; i < parseParams.length; ++i) { - let param = createParam(parseParams[i]) - funcInfo.params.push(param) - } + analyzeFunction(funcInfo, params, index); // 生成 - let relativeParamGenPath = directFuncPath.cppTempleteDetails.funcBody.funcParamIn.paramGenTemplete - let paramGenTempletePath = path.join(__dirname, relativeParamGenPath); - let paramGenTemplete = readFile(paramGenTempletePath) - let funcParamTypePath = directFuncPath.cppTempleteDetails.funcBody.funcParamIn.funcParamType - let paramGenResult = '' - // napi 获取参数 - for (let i = 0; i < funcInfo.params.length; i++) { - paramGenResult = getParamJs2C(funcInfo, i, paramGenTemplete, funcParamTypePath, paramGenResult, params); - } + let paramGenResult = genParamInfo(directFuncPath, funcInfo, params); // 返回值处理 对于对象要使用循环处理 - let retGenResult = '' + let retGenResult = ''; let retObjInfo = { - "objName": '', - "flag": false - } + 'objName': '', + 'flag': false + }; - let funcRetOutPath = directFuncPath.cppTempleteDetails.funcBody.funcReturnOut + let funcRetOutPath = directFuncPath.cppTempleteDetails.funcBody.funcReturnOut; retGenResult = returnTypeC2Js(funcRetOutPath, funcInfo, params, retGenResult, retObjInfo); - let funcBodyTempletePath = path.join(__dirname, directFuncPath.cppTempleteDetails.funcBody.funcBodyTemplete); - let bodyTemplete = readFile(funcBodyTempletePath); - - let body_replace = replaceAll(bodyTemplete, '[funcName]', funcName_replace) - body_replace = replaceAll(body_replace, '[get_error_msg_tag]', funcName_replace) - body_replace = replaceAll(body_replace, '[file_introduce_replace]', hFileName) - body_replace = replaceAll(body_replace, '[func_introduce_replace]', funcInfo.name) - - let funcInfoParams = '' - let funcInfoParamTemp = '[paramName]: [paramType]; ' - for (let i = 0; i < funcInfo.params.length; i++) { - let funcInfoParamReplace = replaceAll(funcInfoParamTemp, '[paramName]', funcInfo.params[i].name) - funcInfoParamReplace = replaceAll(funcInfoParamReplace, '[paramType]', funcInfo.params[i].type) - funcInfoParams += funcInfoParamReplace - } - body_replace = replaceAll(body_replace, '[input_introduce_replace]', funcInfoParams === ''? 'void': funcInfoParams) - body_replace = replaceAll(body_replace, '[output_introduce_replace]', funcInfo.retType) + let { bodyReplace, funcInfoParams } = getReplaceInfo(directFuncPath, funcInfo, funcNameReplace, hFileName); let funcGetParamTempletePath = path.join(__dirname, directFuncPath.cppTempleteDetails.funcBody.funcParamIn.funcGetParamTemplete); - let funcGetParamTemplete = readFile(funcGetParamTempletePath) - let genParam_replace = replaceAll(funcGetParamTemplete, '[param_length]', "PARAMS" + funcInfo.params.length) - genParam_replace = replaceAll(genParam_replace, '[funcName]', funcName_replace) - genParam_replace = replaceAll(genParam_replace, '[getParam_replace]', paramGenResult) - if (funcInfo.params.length !== 0) { - body_replace = replaceAll(body_replace, '[func_getParam_replace]', genParam_replace) - } else { - body_replace = replaceAll(body_replace, '[func_getParam_replace]', '') - } + let funcGetParamTemplete = readFile(funcGetParamTempletePath); + let genParamReplace = getGenParamReplace(funcGetParamTemplete, funcInfo, funcNameReplace, paramGenResult); + bodyReplace = getBodyReplace2(funcInfo, bodyReplace, genParamReplace); if (funcInfo.retType.replace('*', '').trim() !== 'void') { - let returnType = funcInfo.retType === 'std::string' ? 'const char *' : funcInfo.retType - returnType = returnType === 'size_t' ? 'int64_t' : returnType + let returnType = funcInfo.retType === 'std::string' ? 'const char *' : funcInfo.retType; + returnType = returnType === 'size_t' ? 'int64_t' : returnType; let funcReturnTempletePath = path.join(__dirname, funcRetOutPath.funcReturnTemplete); let funcReturnTemplete = readFile(funcReturnTempletePath); - let = func_return_replace = replaceAll(funcReturnTemplete, '[return_name]', retObjInfo.objName) - func_return_replace = replaceAll(func_return_replace, '[funcName]', funcName_replace) - func_return_replace = replaceAll(func_return_replace, '[return_replace]', retGenResult) - body_replace = replaceAll(body_replace, '[func_return_replace]', func_return_replace) + let funcReturnReplace = replaceAll(funcReturnTemplete, '[return_name]', retObjInfo.objName); + funcReturnReplace = replaceAll(funcReturnReplace, '[funcName]', funcNameReplace); + funcReturnReplace = replaceAll(funcReturnReplace, '[return_replace]', retGenResult); + bodyReplace = replaceAll(bodyReplace, '[func_return_replace]', funcReturnReplace); } else { - body_replace = replaceAll(body_replace, '[func_return_replace]', ' return NULL;\n') + bodyReplace = replaceAll(bodyReplace, '[func_return_replace]', ' return NULL;\n'); } - body_replace = replaceAll(body_replace, '[return_replace]', retGenResult) - - let funcHDeclarePath = path.join(__dirname, directFuncPath.cppTempleteDetails.funcHDeclare.funcHDeclare); - let funcHDeclare = readFile(funcHDeclarePath) - funcHDeclare = replaceAll(funcHDeclare, '[funcName]', funcName_replace) + bodyReplace = replaceAll(bodyReplace, '[return_replace]', retGenResult); - funcHDeclare = replaceAll(funcHDeclare, '[file_introduce_replace]', hFileName) - funcHDeclare = replaceAll(funcHDeclare, '[func_introduce_replace]', funcInfo.name) - funcHDeclare = replaceAll(funcHDeclare, '[input_introduce_replace]', funcInfoParams === ''? 'void': funcInfoParams) - funcHDeclare = replaceAll(funcHDeclare, '[output_introduce_replace]', funcInfo.retType) + let funcHDeclare = genFuncHDeclare(directFuncPath, funcNameReplace, hFileName, funcInfo, funcInfoParams); - return [funcHDeclare, init_replace, body_replace] + return [funcHDeclare, initReplace, bodyReplace]; +} + +function getReplaceInfo(directFuncPath, funcInfo, funcNameReplace, hFileName) { + let funcBodyTempletePath = path.join(__dirname, directFuncPath.cppTempleteDetails.funcBody.funcBodyTemplete); + let bodyTemplete = readFile(funcBodyTempletePath); + let funcInfoParams = genFuncInfoParams(funcInfo); + let bodyReplace = replaceAll(bodyTemplete, '[funcName]', funcNameReplace); + bodyReplace = getBodyReplace(bodyReplace, funcNameReplace, hFileName, funcInfo, funcInfoParams); + return { bodyReplace, funcInfoParams }; +} + +function getBodyReplace(bodyReplace, funcNameReplace, hFileName, funcInfo, funcInfoParams) { + bodyReplace = replaceAll(bodyReplace, '[get_error_msg_tag]', funcNameReplace); + bodyReplace = replaceAll(bodyReplace, '[file_introduce_replace]', hFileName); + bodyReplace = replaceAll(bodyReplace, '[func_introduce_replace]', funcInfo.name); + bodyReplace = replaceAll(bodyReplace, '[input_introduce_replace]', funcInfoParams === '' ? 'void' : funcInfoParams); + bodyReplace = replaceAll(bodyReplace, '[output_introduce_replace]', funcInfo.retType); + return bodyReplace; +} + +function getBodyReplace2(funcInfo, bodyReplace, genParamReplace) { + if (funcInfo.params.length !== 0) { + bodyReplace = replaceAll(bodyReplace, '[func_getParam_replace]', genParamReplace); + } else { + bodyReplace = replaceAll(bodyReplace, '[func_getParam_replace]', ''); + } + return bodyReplace; +} + +function getGenParamReplace(funcGetParamTemplete, funcInfo, funcNameReplace, paramGenResult) { + let genParamReplace = replaceAll(funcGetParamTemplete, '[param_length]', 'PARAMS' + funcInfo.params.length); + genParamReplace = replaceAll(genParamReplace, '[funcName]', funcNameReplace); + genParamReplace = replaceAll(genParamReplace, '[getParam_replace]', paramGenResult); + return genParamReplace; +} + +function genFuncInfoParams(funcInfo) { + let funcInfoParams = ''; + let funcInfoParamTemp = '[paramName]: [paramType]; '; + for (let i = 0; i < funcInfo.params.length; i++) { + let funcInfoParamReplace = replaceAll(funcInfoParamTemp, '[paramName]', funcInfo.params[i].name); + funcInfoParamReplace = replaceAll(funcInfoParamReplace, '[paramType]', funcInfo.params[i].type); + funcInfoParams += funcInfoParamReplace; + } + return funcInfoParams; +} + +function genFuncHDeclare(directFuncPath, funcNameReplace, hFileName, funcInfo, funcInfoParams) { + let funcHDeclarePath = path.join(__dirname, directFuncPath.cppTempleteDetails.funcHDeclare.funcHDeclare); + let funcHDeclare = readFile(funcHDeclarePath); + funcHDeclare = replaceAll(funcHDeclare, '[funcName]', funcNameReplace); + + funcHDeclare = replaceAll(funcHDeclare, '[file_introduce_replace]', hFileName); + funcHDeclare = replaceAll(funcHDeclare, '[func_introduce_replace]', funcInfo.name); + funcHDeclare = replaceAll(funcHDeclare, '[input_introduce_replace]', funcInfoParams === '' ? 'void' : funcInfoParams); + funcHDeclare = replaceAll(funcHDeclare, '[output_introduce_replace]', funcInfo.retType); + return funcHDeclare; +} + +function genParamInfo(directFuncPath, funcInfo, params) { + let relativeParamGenPath = directFuncPath.cppTempleteDetails.funcBody.funcParamIn.paramGenTemplete; + let paramGenTempletePath = path.join(__dirname, relativeParamGenPath); + let paramGenTemplete = readFile(paramGenTempletePath); + let funcParamTypePath = directFuncPath.cppTempleteDetails.funcBody.funcParamIn.funcParamType; + let paramGenResult = ''; + // napi 获取参数 + for (let i = 0; i < funcInfo.params.length; i++) { + paramGenResult = getParamJs2C(funcInfo, i, paramGenTemplete, funcParamTypePath, paramGenResult, params); + } + return paramGenResult; +} + +function analyzeFunction(funcInfo, params, index) { + funcInfo.name = params.functions[index].name; + funcInfo.retType = params.functions[index].rtnType; + let parseParams = params.functions[index].parameters; + for (let i = 0; i < parseParams.length; ++i) { + let param = createParam(parseParams[i]); + funcInfo.params.push(param); + } +} + +function genInitFunction(directFuncPath, funcNameReplace) { + let funcInitPath = path.join(__dirname, directFuncPath.initTempleteDetails.funcInitTemplete); + let funcInitTemplete = readFile(funcInitPath); + let initReplace = replaceAll(funcInitTemplete, '[func_name_replace]', funcNameReplace); + return initReplace; } function getParamJs2C(funcInfo, i, paramGenTemplete, funcParamTypePath, paramGenResult, params) { let paramType = funcInfo.params[i].type === 'size_t' ? 'int64_t' : funcInfo.params[i].type; // 去除const 和 * - paramType = paramType.replace('const', '').replace('*', '').trim() + paramType = paramType.replace('const', '').replace('*', '').trim(); let paramName = funcInfo.params[i].name; - let paramGen = replaceAll(paramGenTemplete, '[param_index_replace]', "PARAMS" + i); + let paramGen = replaceAll(paramGenTemplete, '[param_index_replace]', 'PARAMS' + i); paramGen = replaceAll(paramGen, '[param_name_replace]', paramName); if (paramType === 'double') { - let getParamPath = path.join(__dirname, funcParamTypePath.double) + let getParamPath = path.join(__dirname, funcParamTypePath.double); paramGen = getParamGenCon(getParamPath, i, paramName, paramGen); paramGenResult += paramGen; } else if (paramType === 'uint32_t') { - let getParamPath = path.join(__dirname, funcParamTypePath.uint32_t) + let getParamPath = path.join(__dirname, funcParamTypePath.uint32_t); paramGen = getParamGenCon(getParamPath, i, paramName, paramGen); - paramGenResult += paramGen; } else if (paramType === 'int32_t' || paramType === 'int') { - let getParamPath = path.join(__dirname, funcParamTypePath.int32_t) + let getParamPath = path.join(__dirname, funcParamTypePath.int32_t); paramGen = getParamGenCon(getParamPath, i, paramName, paramGen); paramGenResult += paramGen; } else if (paramType === 'int64_t' || paramType === 'size_t') { - let getParamPath = path.join(__dirname, funcParamTypePath.int64_t) + let getParamPath = path.join(__dirname, funcParamTypePath.int64_t); paramGen = getParamGenCon(getParamPath, i, paramName, paramGen); paramGenResult += paramGen; } else if (paramType === 'bool') { - let getParamPath = path.join(__dirname, funcParamTypePath.bool) + let getParamPath = path.join(__dirname, funcParamTypePath.bool); paramGen = getParamGenCon(getParamPath, i, paramName, paramGen); paramGenResult += paramGen; } else if (paramType === 'std::string' || paramType.indexOf('char') >= 0) { - let getParamPath = path.join(__dirname, funcParamTypePath.string) + let getParamPath = path.join(__dirname, funcParamTypePath.string); paramGen = getParamGenCon(getParamPath, i, paramName, paramGen); paramGenResult += paramGen; } else if (analyzeRetIsTypeDef(paramType, params.typedefs)) { // typedefs @@ -187,73 +228,50 @@ function getParamJs2C(funcInfo, i, paramGenTemplete, funcParamTypePath, paramGen function getParamGenCon(getParamPath, i, paramName, paramGen) { let getParam = readFile(getParamPath); - getParam = replaceAll(getParam, '[param_index_replace]', "PARAMS" + i); + getParam = replaceAll(getParam, '[param_index_replace]', 'PARAMS' + i); getParam = replaceAll(getParam, '[param_name_replace]', paramName); paramGen = replaceAll(paramGen, '[getParam_replace]', getParam); return paramGen; } function returnTypeC2Js(funcRetOutPath, funcInfo, params, retGenResult, retObjInfo) { - let setRetPropertyPath = path.join(__dirname, funcRetOutPath.funcReturnType.returnObj.funcReturnObjectToSet) - let setRetProperty = readFile(setRetPropertyPath) + let setRetPropertyPath = path.join(__dirname, funcRetOutPath.funcReturnType.returnObj.funcReturnObjectToSet); + let setRetProperty = readFile(setRetPropertyPath); let returnName = funcInfo.name; if (!retObjInfo.flag) { - retObjInfo.objName = returnName + retObjInfo.objName = returnName; } if (funcInfo.retType === 'uint32_t') { - let funcReturnTypePath = path.join(__dirname, funcRetOutPath.funcReturnType.uint32_t) + let funcReturnTypePath = path.join(__dirname, funcRetOutPath.funcReturnType.uint32_t); retGenResult = getRetTypeContent(funcReturnTypePath, returnName, retGenResult, retObjInfo, setRetProperty); } else if (funcInfo.retType === 'double') { - let funcReturnTypePath = path.join(__dirname, funcRetOutPath.funcReturnType.double) + let funcReturnTypePath = path.join(__dirname, funcRetOutPath.funcReturnType.double); retGenResult = getRetTypeContent(funcReturnTypePath, returnName, retGenResult, retObjInfo, setRetProperty); } else if (funcInfo.retType === 'int32_t' || funcInfo.retType === 'int') { - let funcReturnTypePath = path.join(__dirname, funcRetOutPath.funcReturnType.int32_t) + let funcReturnTypePath = path.join(__dirname, funcRetOutPath.funcReturnType.int32_t); retGenResult = getRetTypeContent(funcReturnTypePath, returnName, retGenResult, retObjInfo, setRetProperty); } else if (funcInfo.retType === 'int64_t' || funcInfo.retType === 'size_t') { - let funcReturnTypePath = path.join(__dirname, funcRetOutPath.funcReturnType.int64_t) + let funcReturnTypePath = path.join(__dirname, funcRetOutPath.funcReturnType.int64_t); retGenResult = getRetTypeContent(funcReturnTypePath, returnName, retGenResult, retObjInfo, setRetProperty); } else if (funcInfo.retType === 'bool') { - let funcReturnTypePath = path.join(__dirname, funcRetOutPath.funcReturnType.bool) + let funcReturnTypePath = path.join(__dirname, funcRetOutPath.funcReturnType.bool); retGenResult = getRetTypeContent(funcReturnTypePath, returnName, retGenResult, retObjInfo, setRetProperty); - } else if (funcInfo.retType === 'std::string' || funcInfo.retType.substring(0, 10) === 'const char' - || funcInfo.retType === 'char *') { - let funcReturnTypePath = path.join(__dirname, funcRetOutPath.funcReturnType.string) + } else if (funcInfo.retType === 'std::string' || funcInfo.retType.substring(0, 10) === 'const char' || + funcInfo.retType === 'char *') { + let funcReturnTypePath = path.join(__dirname, funcRetOutPath.funcReturnType.string); retGenResult = getRetTypeContent(funcReturnTypePath, returnName, retGenResult, retObjInfo, setRetProperty); } else if (analyzeRetIsObject(funcInfo.retType, params.classes)) { // 返回值是对象 if (!retObjInfo.flag) { retGenResult = getObjRetContent(funcRetOutPath, retGenResult, returnName); - retObjInfo.flag = true - let retType = funcInfo.retType.replace('*', '').trim(); - let objectName = ''; - let objectProperty = []; - - let myObject = params.classes[retType] - objectName = myObject.bare_name; - let myObjectProperty = myObject.properties.public; - for (let j = 0; j < myObjectProperty.length; j++) { - let propertyObj = { - "name": '', - "retType": '' - } - propertyObj.name = myObjectProperty[j].name; - propertyObj.retType = myObjectProperty[j].type; - - objectProperty.push(propertyObj); - } + retObjInfo.flag = true; + let objectProperty = getObjectProperty(funcInfo, params); // 遍历属性 for (let i = 0; i < objectProperty.length; i++) { - let testRes = returnTypeC2Js(funcRetOutPath, objectProperty[i], params, retGenResult, retObjInfo) + let testRes = returnTypeC2Js(funcRetOutPath, objectProperty[i], params, retGenResult, retObjInfo); retGenResult = testRes; } } else { - if (retObjInfo.objName !== '') { - retGenResult = getObjRetContent(funcRetOutPath, retGenResult, returnName); - let setRetPropertyObj = readFile(setRetPropertyPath) - setRetPropertyObj = replaceAll(setRetPropertyObj, '[set_objname_replace]', retObjInfo.objName); - setRetPropertyObj = replaceAll(setRetPropertyObj, '[set_propname_replace]', returnName); - setRetPropertyObj = replaceAll(setRetPropertyObj, '[set_propvalue_replace]', returnName); - retGenResult += setRetPropertyObj; - } + retGenResult = getObjRetGenResult(retObjInfo, retGenResult, funcRetOutPath, returnName, setRetPropertyPath); } } else if (analyzeRetIsTypeDef(funcInfo.retType, params.typedefs)) { // typedefs funcInfo.retType = analyzeRetIsTypeDef(funcInfo.retType, params.typedefs); @@ -262,8 +280,41 @@ function returnTypeC2Js(funcRetOutPath, funcInfo, params, retGenResult, retObjIn return retGenResult; } +function getObjectProperty(funcInfo, params) { + let retType = funcInfo.retType.replace('*', '').trim(); + let objectName = ''; + let objectProperty = []; + + let myObject = params.classes[retType]; + objectName = myObject.bare_name; + let myObjectProperty = myObject.properties.public; + for (let j = 0; j < myObjectProperty.length; j++) { + let propertyObj = { + 'name': '', + 'retType': '' + }; + propertyObj.name = myObjectProperty[j].name; + propertyObj.retType = myObjectProperty[j].type; + + objectProperty.push(propertyObj); + } + return objectProperty; +} + +function getObjRetGenResult(retObjInfo, retGenResult, funcRetOutPath, returnName, setRetPropertyPath) { + if (retObjInfo.objName !== '') { + retGenResult = getObjRetContent(funcRetOutPath, retGenResult, returnName); + let setRetPropertyObj = readFile(setRetPropertyPath); + setRetPropertyObj = replaceAll(setRetPropertyObj, '[set_objname_replace]', retObjInfo.objName); + setRetPropertyObj = replaceAll(setRetPropertyObj, '[set_propname_replace]', returnName); + setRetPropertyObj = replaceAll(setRetPropertyObj, '[set_propvalue_replace]', returnName); + retGenResult += setRetPropertyObj; + } + return retGenResult; +} + function getObjRetContent(funcRetOutPath, retGenResult, returnName) { - let funcReturnTypePath = path.join(__dirname, funcRetOutPath.funcReturnType.returnObj.object) + let funcReturnTypePath = path.join(__dirname, funcRetOutPath.funcReturnType.returnObj.object); let funcReturnType = readFile(funcReturnTypePath); retGenResult += replaceAll(funcReturnType, '[return_name_replace]', returnName); return retGenResult; @@ -284,21 +335,21 @@ function getRetTypeContent(funcReturnTypePath, returnName, retGenResult, retObjI function replaceAll(s, sfrom, sto) { while (s.indexOf(sfrom) >= 0) { - s = s.replace(sfrom, sto) + s = s.replace(sfrom, sto); } return s; } function createParam(parseParamInfo) { let param = { - "name": "", - "type": "" - } - param.name = parseParamInfo.name - param.type = parseParamInfo.type - return param + 'name': '', + 'type': '' + }; + param.name = parseParamInfo.name; + param.type = parseParamInfo.type; + return param; } module.exports = { generateDirectFunction -} \ No newline at end of file +}; \ No newline at end of file diff --git a/src/cli/h2dtscpp/src/src/napiGen/functionDirectTest.js b/src/cli/h2dtscpp/src/src/napiGen/functionDirectTest.js index 78b512938717d988ba24bb34f8d26ff3b0bf9441..f1de13d43976141683719f03649cdbf8421e37d4 100644 --- a/src/cli/h2dtscpp/src/src/napiGen/functionDirectTest.js +++ b/src/cli/h2dtscpp/src/src/napiGen/functionDirectTest.js @@ -63,38 +63,7 @@ function generateFuncTestCase(params, funcIndex, tsFuncName, abilityTestTemplete funcInfo.params.push(param) } funcInfo.retType = params.functions[funcIndex].rtnType - let funcParamDefine = '' - let funcParamUse = '' - let funcInfoParams = '' - let funcInfoParamTemp = '[paramName]: [paramType]; ' - // 判断函数有几个参数,依次给参数赋值 - for(let i = 0; i < funcInfo.params.length; i++) { - let funcInfoParamReplace = replaceAll(funcInfoParamTemp, '[paramName]', funcInfo.params[i].name) - funcInfoParamReplace = replaceAll(funcInfoParamReplace, '[paramType]', funcInfo.params[i].type) - funcInfoParams += funcInfoParamReplace - let testType = getTestType(funcInfo.params[i].type); - if (testType === 'int') { - funcParamDefine += util.format('let %s = %s\n ', funcInfo.params[i].name, generateRandomInteger(0, LENGTH)) - funcParamUse += funcInfo.params[i].name + ', ' - } else if (testType === 'float') { - funcParamDefine += util.format('let %s = %s\n ', funcInfo.params[i].name, generateRandomArbitrary(0, LENGTH, TWO_DECIMAL)) - funcParamUse += funcInfo.params[i].name + ', ' - } else if (testType === 'bool') { - funcParamDefine += util.format('let %s = %s\n ', funcInfo.params[i].name, generateRandomBoolValue()) - funcParamUse += funcInfo.params[i].name + ', ' - } else if (testType === 'string') { - funcParamDefine += util.format('let %s = "%s"\n ', funcInfo.params[i].name, generateRandomString(LENGTH)) - funcParamUse += funcInfo.params[i].name + ', ' - } else if (TypeList.getValue(testType)) { - let typeDefineRes = getTypeDefine(testType, funcParamDefine, funcInfo, i, funcParamUse); - funcParamDefine = typeDefineRes[0] - funcParamUse = typeDefineRes[1] - } else if (InterfaceList.getBody(testType)) { - let interfaceDefineRes = getInterfaceDefine(testType, funcParamDefine, funcInfo, i, funcParamUse); - funcParamDefine = interfaceDefineRes[0] - funcParamUse = interfaceDefineRes[1] - } - } + let { funcParamUse, funcParamDefine, funcInfoParams } = genInitTestfunc(funcInfo); // 去除调用参数的最后一个',' let index = funcParamUse.lastIndexOf(', '); funcParamUse = funcParamUse.substring(0, index); @@ -121,6 +90,42 @@ function generateFuncTestCase(params, funcIndex, tsFuncName, abilityTestTemplete return funcTestContent } +function genInitTestfunc(funcInfo) { + let funcParamDefine = ''; + let funcParamUse = ''; + let funcInfoParams = ''; + let funcInfoParamTemp = '[paramName]: [paramType]; '; + // 判断函数有几个参数,依次给参数赋值 + for (let i = 0; i < funcInfo.params.length; i++) { + let funcInfoParamReplace = replaceAll(funcInfoParamTemp, '[paramName]', funcInfo.params[i].name); + funcInfoParamReplace = replaceAll(funcInfoParamReplace, '[paramType]', funcInfo.params[i].type); + funcInfoParams += funcInfoParamReplace; + let testType = getTestType(funcInfo.params[i].type); + if (testType === 'int') { + funcParamDefine += util.format('let %s = %s\n ', funcInfo.params[i].name, generateRandomInteger(0, LENGTH)); + funcParamUse += funcInfo.params[i].name + ', '; + } else if (testType === 'float') { + funcParamDefine += util.format('let %s = %s\n ', funcInfo.params[i].name, generateRandomArbitrary(0, LENGTH, TWO_DECIMAL)); + funcParamUse += funcInfo.params[i].name + ', '; + } else if (testType === 'bool') { + funcParamDefine += util.format('let %s = %s\n ', funcInfo.params[i].name, generateRandomBoolValue()); + funcParamUse += funcInfo.params[i].name + ', '; + } else if (testType === 'string') { + funcParamDefine += util.format('let %s = "%s"\n ', funcInfo.params[i].name, generateRandomString(LENGTH)); + funcParamUse += funcInfo.params[i].name + ', '; + } else if (TypeList.getValue(testType)) { + let typeDefineRes = getTypeDefine(testType, funcParamDefine, funcInfo, i, funcParamUse); + funcParamDefine = typeDefineRes[0]; + funcParamUse = typeDefineRes[1]; + } else if (InterfaceList.getBody(testType)) { + let interfaceDefineRes = getInterfaceDefine(testType, funcParamDefine, funcInfo, i, funcParamUse); + funcParamDefine = interfaceDefineRes[0]; + funcParamUse = interfaceDefineRes[1]; + } + } + return { funcParamUse, funcParamDefine, funcInfoParams }; +} + function getTypeDefine(testType, funcParamDefine, funcInfo, i, funcParamUse) { let typeDefType = TypeList.getValue(testType); // genType diff --git a/src/cli/h2dtscpp/src/src/tsGen/tsMain.js b/src/cli/h2dtscpp/src/src/tsGen/tsMain.js index e36bbe40df9662a08440912e0a957e403c6cebe9..ad967c30103f7c5c19d07ccafd55934bb53e9b8c 100644 --- a/src/cli/h2dtscpp/src/src/tsGen/tsMain.js +++ b/src/cli/h2dtscpp/src/src/tsGen/tsMain.js @@ -461,27 +461,7 @@ function removeMarco(hFilePath, tempFilePath, macros) { && line.indexOf('#elif') && line.indexOf('#if') < 0 && line.indexOf('#else')) { macros.forEach(macro => { // 去掉使用的宏以及括号() - - let index = line.indexOf(macro) - if (index >= 0) { - let regexPattern = new RegExp(macro + '\\s*\\('); - let isMatch = regexPattern.test(line) - if (isMatch) { - let removeIndexLeft = line.indexOf('(') - line = line.substring(0, index) + line.substring(index + macro.length, removeIndexLeft) - + line.substring(removeIndexLeft + 1, line.length) - let removeIndexRight = line.indexOf(')') - line = line.substring(0, removeIndexRight) + line.substring(removeIndexRight + 1, line.length) - } else { - let tmpLeft = line.substring(0, index) - let indexLeftBracket = tmpLeft.lastIndexOf('(') - tmpLeft = tmpLeft.substring(0, indexLeftBracket) + tmpLeft.substring(indexLeftBracket + 1, tmpLeft.length) - let tmpRight = line.substring(index + macro.length, line.length) - let indexRightBracket = tmpRight.indexOf(')') - tmpRight = tmpRight.substring(0, indexRightBracket) + tmpRight.substring(indexRightBracket + 1, tmpRight.length) - line = tmpLeft + tmpRight - } - } + line = getLineContent(line, macro); }) } processedContent += line + '\n'; @@ -493,6 +473,30 @@ function removeMarco(hFilePath, tempFilePath, macros) { }); } +function getLineContent(line, macro) { + let index = line.indexOf(macro); + if (index >= 0) { + let regexPattern = new RegExp(macro + '\\s*\\('); + let isMatch = regexPattern.test(line); + if (isMatch) { + let removeIndexLeft = line.indexOf('('); + line = line.substring(0, index) + line.substring(index + macro.length, removeIndexLeft) + + line.substring(removeIndexLeft + 1, line.length); + let removeIndexRight = line.indexOf(')'); + line = line.substring(0, removeIndexRight) + line.substring(removeIndexRight + 1, line.length); + } else { + let tmpLeft = line.substring(0, index); + let indexLeftBracket = tmpLeft.lastIndexOf('('); + tmpLeft = tmpLeft.substring(0, indexLeftBracket) + tmpLeft.substring(indexLeftBracket + 1, tmpLeft.length); + let tmpRight = line.substring(index + macro.length, line.length); + let indexRightBracket = tmpRight.indexOf(')'); + tmpRight = tmpRight.substring(0, indexRightBracket) + tmpRight.substring(indexRightBracket + 1, tmpRight.length); + line = tmpLeft + tmpRight; + } + } + return line; +} + // 读取头文件并提取所有的#define宏 function extractMacros(headerFilePath) { return new Promise((resolve, reject) => { diff --git a/test/storytest/test_array_map/test.js b/test/storytest/test_array_map/test.js index df188e653f7d81ec58208ef822863ed3cd9366cd..2b21e9e609a1423e77ac6f2b4bf12f28ac777e1f 100644 --- a/test/storytest/test_array_map/test.js +++ b/test/storytest/test_array_map/test.js @@ -52,22 +52,4 @@ describe('array_map', function () { let ret = fun10([{"age":true}, {"name":false}]); assert.strictEqual(ret, 0); }); - - /* - it('test fun7', function () { - let ret = fun7([{"key":'{"code":0,"data":"code0"}'}, {"name":'{"code":1,"data":"code1"}'}]); - assert.strictEqual(ret, 0); - }); - */ - - /*it('test fun12', function () { - let ret = fun12([{"key1":'code1'}, {"key2":'code2'}]); - assert.strictEqual(ret, 0); - }); - - it('test fun10', function () { - let ret = fun12([{"key1":'{"code":0,"data":"code0"}'}, {"key2":'{"code":1,"data":"code1"}'}]); - assert.strictEqual(ret, 0); - }); - */ }); diff --git a/test/storytest/test_class/test.js b/test/storytest/test_class/test.js index d358ce234665d8693b3029a09e082b93fe48a0a6..90953e5a8173cc788ba387484bf80dc81092c249 100755 --- a/test/storytest/test_class/test.js +++ b/test/storytest/test_class/test.js @@ -211,28 +211,24 @@ describe('TestClass2', function () { it('test TestClass2 func1', function () { let tc = new TestClass2() let ret = tc.func1("func1p1", {nm:"aaa",age:18,flag:false}); - //assert.strictEqual(ret.read, 0); }); // 测试:func2(input: string): { read: number; written: number; flag: boolean }; it('test TestClass2 func2', function () { let tc = new TestClass2() let ret = tc.func2("name"); - //assert.strictEqual(ret.read, 0); }); // 测试:func3(from: string, to: string): Promise<{result: number, errMsg: string, isT: boolean}>; it('test TestClass2 func3', function () { let tc = new TestClass2() let ret = tc.func3("from", "to"); - //assert.strictEqual(ret.read, 0); }); // 测试:func4(from: string, to: string): Promise<{result: number; errMsg: string; isT: boolean}>; it('test TestClass2 func4', function () { let tc = new TestClass2() let ret = tc.func4("responeFrom", "responseTo"); - //assert.strictEqual(ret.read, 0); }); // 测试:func5(v1: string, v2: number, v3: boolean); diff --git a/test/storytest/test_interface_no_name/test.js b/test/storytest/test_interface_no_name/test.js index 1ee445ad5aac7ded67363370ac1a87d9274d9ca3..b7259e43498616d9b747af74dc56d8041222442a 100644 --- a/test/storytest/test_interface_no_name/test.js +++ b/test/storytest/test_interface_no_name/test.js @@ -13,7 +13,7 @@ * limitations under the License. */ const { fun1,fun2, fun3, fun4, fun5, fun6, fun7, fun8, fun9, TestInterfaceBB} = require("./out/build/Release/napitest") -var assert = require("assert"); +let assert = require("assert"); describe('interface_no_name', function () { let fp2 = { @@ -41,13 +41,11 @@ describe('interface_no_name', function () { // 测试:function fun2(fp2: TestInterface): string; it('test interface_no_name fun2', function () { let ret = fun2(fp2); - //assert.strictEqual(ret, ''); }); // 测试:function fun3(name : string, fp3: {nm: string, age: number}): string; it('test interface_no_name fun3', function () { let ret = fun3("fun3p1", {nm:"abc",age:20}); - //assert.strictEqual(ret, ''); }); // 测试:function fun4(input: string): { read: number; written: number }; @@ -75,13 +73,11 @@ describe('interface_no_name', function () { // 测试:function fun8(from: string): Promise<{result: number; errMsg: string; isT: boolean}>; it('test interface_no_name fun8', function () { let ret = fun8("name"); - //assert.strictEqual(ret.read, 0); }); // 测试:function fun9(from: string): Promise<{result: number, errMsg: string, isT: boolean}>; it('test interface_no_name fun9', function () { let ret = fun9("name"); - //assert.strictEqual(ret.read, 0); }); }); @@ -90,28 +86,24 @@ describe('TestInterfaceBB', function () { it('test interface_no_name func1', function () { let tc = new TestInterfaceBB() let ret = tc.func1("func1p1", {nm:"aaa",age:18,flag:false}); - //assert.strictEqual(ret.read, 0); }); // 测试:func2(input: string): { read: number; written: number; flag: boolean }; it('test interface_no_name func2', function () { let tc = new TestInterfaceBB() let ret = tc.func2("name"); - //assert.strictEqual(ret.read, 0); }); // 测试:func3(from: string, to: string): Promise<{result: number, errMsg: string, isT: boolean}>; it('test interface_no_name func3', function () { let tc = new TestInterfaceBB() let ret = tc.func3("from", "to"); - //assert.strictEqual(ret.read, 0); }); // 测试:func4(from: string, to: string): Promise<{result: number; errMsg: string; isT: boolean}>; it('test interface_no_name func4', function () { let tc = new TestInterfaceBB() let ret = tc.func4("responeFrom", "responseTo"); - //assert.strictEqual(ret.read, 0); }); }); diff --git a/test/unittest/extend.test.js b/test/unittest/extend.test.js index 1199f38b9e17f0474fab3fe4ea97d4c2c0b5791a..95290c2aa11d79009ae1b19fd3a66c6a5453c53b 100755 --- a/test/unittest/extend.test.js +++ b/test/unittest/extend.test.js @@ -57,18 +57,10 @@ function partGenerateBase(){ it('test gen/extend/tool_utility generateBase', function () { var fs = require("fs"); if (fs.existsSync('test/unittest/tool_utility.cpp')) { - fs.unlink('test/unittest/tool_utility.cpp', function (err) { - if (err) { - return console.error(err); - } - }); + unlinkCppFile(fs); } if (fs.existsSync('test/unittest/tool_utility.h')) { - fs.unlink('test/unittest/tool_utility.h', function (err) { - if (err) { - return console.error(err); - } - }); + unlinkHFile(fs); } generateBase('test/unittest', '/*\n* Copyright (c) 2022 Shenzhen Kaihong\n*/'); let data = readFile("test/unittest/tool_utility.cpp") @@ -83,3 +75,19 @@ function partGenerateBase(){ }); } +function unlinkHFile(fs) { + fs.unlink('test/unittest/tool_utility.h', function (err) { + if (err) { + return console.error(err); + } + }); +} + +function unlinkCppFile(fs) { + fs.unlink('test/unittest/tool_utility.cpp', function (err) { + if (err) { + return console.error(err); + } + }); +} + diff --git a/test/unittest/generate.test.js b/test/unittest/generate.test.js index de1effa8d983eb3eb9764a40c680b333916a1511..4a8a9fbde7d2d0438707aee2b9ebe9fe093ccba8 100755 --- a/test/unittest/generate.test.js +++ b/test/unittest/generate.test.js @@ -369,37 +369,42 @@ function partOfFuncRegistUnregist() { assert.strictEqual(indexMiddleCpp > 0, true); }); - // registerXXX test Callback - it('test gen/generate/function_onoff generateFunctionOnOff registerXXX', function () { - let ret = funcRegistUnregistAssert('registerTestfunc13', 'Callback', 'void'); - let middleCppRet = ret[0]; - let implHRet = ret[1]; - let implCppRet = ret[2]; - let middleHRet = ret[3]; - let indexImplH = implHRet.indexOf('void Testfunc13Callback(bool &valueIn);'); - assert.strictEqual(indexImplH >= 0, true); - let indexImplCpp = implCppRet.indexOf('ptr->Testfunc13CallbackMiddle(eventName, valueIn);'); - assert.strictEqual(indexImplCpp > 0, true); - let indexMiddleH = middleHRet.indexOf('struct registerTestfunc13_value_struct {'); - assert.strictEqual(indexMiddleH >= 0, true); - let indexMiddleCpp = middleCppRet.indexOf('pxt->RegistOnOffFunc(vio->eventName, pxt->GetArgv(XNapiTool::ZERO));'); - assert.strictEqual(indexMiddleCpp > 0, true); - }); + // registerXXX/unRegisterXXX test Callback + partOfFuncRegistUnregist2(); +} - // unRegisterXXX test Callback - it('test gen/generate/function_onoff generateFunctionOnOff unRegisterXXX', function () { - let ret = funcRegistUnregistAssert('unRegisterTestfunc13', 'Callback', 'void'); - let middleCppRet = ret[0]; - let implHRet = ret[1]; - let implCppRet = ret[2]; - let middleHRet = ret[3]; - assert.strictEqual(JSON.stringify(implHRet), '""'); - assert.strictEqual(JSON.stringify(implCppRet), '""'); - let indexMiddleH = middleHRet.indexOf('struct unRegisterTestfunc13_value_struct {'); - assert.strictEqual(indexMiddleH >= 0, true); - let indexMiddleCpp = middleCppRet.indexOf('pxt->UnregistOnOffFunc(vio->eventName);'); - assert.strictEqual(indexMiddleCpp > 0, true); - }); +function partOfFuncRegistUnregist2() { + // registerXXX test Callback + it('test gen/generate/function_onoff generateFunctionOnOff registerXXX', function () { + let ret = funcRegistUnregistAssert('registerTestfunc13', 'Callback', 'void'); + let middleCppRet = ret[0]; + let implHRet = ret[1]; + let implCppRet = ret[2]; + let middleHRet = ret[3]; + let indexImplH = implHRet.indexOf('void Testfunc13Callback(bool &valueIn);'); + assert.strictEqual(indexImplH >= 0, true); + let indexImplCpp = implCppRet.indexOf('ptr->Testfunc13CallbackMiddle(eventName, valueIn);'); + assert.strictEqual(indexImplCpp > 0, true); + let indexMiddleH = middleHRet.indexOf('struct registerTestfunc13_value_struct {'); + assert.strictEqual(indexMiddleH >= 0, true); + let indexMiddleCpp = middleCppRet.indexOf('pxt->RegistOnOffFunc(vio->eventName, pxt->GetArgv(XNapiTool::ZERO));'); + assert.strictEqual(indexMiddleCpp > 0, true); + }); + + // unRegisterXXX test Callback + it('test gen/generate/function_onoff generateFunctionOnOff unRegisterXXX', function () { + let ret = funcRegistUnregistAssert('unRegisterTestfunc13', 'Callback', 'void'); + let middleCppRet = ret[0]; + let implHRet = ret[1]; + let implCppRet = ret[2]; + let middleHRet = ret[3]; + assert.strictEqual(JSON.stringify(implHRet), '""'); + assert.strictEqual(JSON.stringify(implCppRet), '""'); + let indexMiddleH = middleHRet.indexOf('struct unRegisterTestfunc13_value_struct {'); + assert.strictEqual(indexMiddleH >= 0, true); + let indexMiddleCpp = middleCppRet.indexOf('pxt->UnregistOnOffFunc(vio->eventName);'); + assert.strictEqual(indexMiddleCpp > 0, true); + }); } function partOfInterface() {