From e37461af690353ea02add1174a748f9d4213da66 Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 8 Apr 2025 16:51:54 +0800 Subject: [PATCH 1/3] add class, union, struct, func testcases Signed-off-by: sunlian --- .../ohosgen/src/main/java/gen/GenCppFile.java | 103 +++- .../src/main/java/grammar/ParamObj.java | 2 +- .../src/main/java/grammar/StructObj.java | 29 + .../src/main/java/grammar/UnionObj.java | 29 + .../src/test/java/gen/GenCppFileTest.java | 520 +++++++++++++++++- 5 files changed, 653 insertions(+), 30 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java index 6798221e..9dc93473 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java @@ -38,7 +38,11 @@ public class GenCppFile extends GeneratorBase { private static final String CPP_CLASS_TOKEN = "class"; private static final String CPP_STRUCT_TOKEN = "struct"; private static final String CPP_UNION_TOKEN = "union"; + private static final String CPP_TEMPLATE_TOKEN = "template"; + private static final String CPP_TYPE_NAME_TOKEN = "typename"; + private static final String CPP_STAR_TOKEN = "*"; private static final String CPP_CHAR_START_TOKEN = "char*"; + private static final String CPP_AUTO_TOKEN = "auto"; private static final String CPP_EXPORT_TOKEN = "export"; private static final String CPP_IMPLEMENCPP_TOKEN = "implements"; private static final String CPP_EXTENDS_TOKEN = "extends"; @@ -114,7 +118,8 @@ public class GenCppFile extends GeneratorBase { Map.entry("boolean", "bool"), Map.entry("string", "char*"), Map.entry("number", "int"), - Map.entry("void", "void") + Map.entry("void", "void"), + Map.entry("[]", "*") ); private final Map tsTokenMap = Map.ofEntries( @@ -139,12 +144,17 @@ public class GenCppFile extends GeneratorBase { * @return ts key */ private String ts2CppKey(String cppKey) { + if (cppKey == null) { + return ""; + } String retKey = cppKey; for (Map.Entry entry : ts2cppMap.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); int ret = cppKey.indexOf(key); - if (ret >= 0) { + if (ret >= 0 && value.contains(CPP_STAR_TOKEN)) { + return cppKey.substring(0, ret) + value; + } else if (ret >= 0) { return value; } } @@ -360,6 +370,15 @@ public class GenCppFile extends GeneratorBase { for (ClassObj co : col) { String className = co.getName(); className = !className.isEmpty() ? className : co.getAlias(); + + String templateStr = !co.getTempList().isEmpty() ? + CPP_TEMPLATE_TOKEN + CPP_BLANK_SPACE + CPP_LEFT_ANGLE_BRACKET : ""; + for (String teStr : co.getTempList()) { + templateStr += CPP_TYPE_NAME_TOKEN + CPP_BLANK_SPACE + teStr + CPP_COMMA + CPP_BLANK_SPACE; + } + templateStr = templateStr.length() > 1 ? + StringUtils.removeLastCharacter(templateStr, 2) + CPP_RIGHT_ANGLE_BRACKET + CPP_BLANK_SPACE : ""; + List hnList = co.getHeritageNameList(); String htStr = hnList.size() > 0 ? CPP_BLANK_SPACE + CPP_COLON + CPP_BLANK_SPACE : ""; for (String hName : hnList) { @@ -367,8 +386,15 @@ public class GenCppFile extends GeneratorBase { } htStr = htStr.length() > 1 ? StringUtils.removeLastCharacter(htStr, 2) : htStr; - resContent += CPP_NEW_LINE + CPP_CLASS_TOKEN + - CPP_BLANK_SPACE + className + htStr + CPP_BLANK_SPACE + CPP_LEFT_BRACE; + List htempList = co.getHeritageTemplateList(); + String htempStr = htempList.size() > 0 ? CPP_LEFT_ANGLE_BRACKET : ""; + for (String tempStr : htempList) { + htempStr += tempStr + CPP_COMMA + CPP_BLANK_SPACE; + } + htempStr = htempList.size() > 0 ? + StringUtils.removeLastCharacter(htempStr, 2) + CPP_RIGHT_ANGLE_BRACKET : ""; + resContent += CPP_NEW_LINE + templateStr + CPP_CLASS_TOKEN + + CPP_BLANK_SPACE + className + htStr + htempStr + CPP_BLANK_SPACE + CPP_LEFT_BRACE; List paList = co.getParamList(); for (ParamObj paItem : paList) { String paType = paItem.getType(); @@ -392,10 +418,10 @@ public class GenCppFile extends GeneratorBase { replaceTsToken(funcItem.getName()) + CPP_LEFT_PARENTHESES; List pol = funcItem.getParamList(); for (ParamObj poItem : pol) { - String retType = ts2CppKey(poItem.getType()); + String retType = ts2CppKey(poItem.getType()).isEmpty() ? + CPP_AUTO_TOKEN : ts2CppKey(poItem.getType()); resContent += poItem.getName() == null ? retType + CPP_COMMA + CPP_BLANK_SPACE : - retType + CPP_BLANK_SPACE + replaceTsToken(poItem.getName()) + - CPP_COMMA + CPP_BLANK_SPACE; + retType + CPP_BLANK_SPACE + replaceTsToken(poItem.getName()) + CPP_COMMA + CPP_BLANK_SPACE; } if (!pol.isEmpty()) { resContent = StringUtils.removeLastCharacter(resContent, 2); @@ -420,20 +446,30 @@ public class GenCppFile extends GeneratorBase { for (FuncObj fo : fol) { String funcName = fo.getName(); funcName = !funcName.isEmpty() ? funcName : fo.getAlias(); + List tempList = fo.getTempList(); + String tempStr = tempList.isEmpty() ? "" : CPP_TEMPLATE_TOKEN + CPP_LEFT_ANGLE_BRACKET; + for (String teStr : tempList) { + tempStr += CPP_TYPE_NAME_TOKEN + CPP_BLANK_SPACE + teStr + CPP_COMMA + CPP_BLANK_SPACE; + } + tempStr = tempList.isEmpty() ? "" : + StringUtils.removeLastCharacter(tempStr, 2) + CPP_RIGHT_ANGLE_BRACKET + CPP_BLANK_SPACE; List paList = fo.getParamList(); - String retValue = fo.getRetValue(); - resContent += CPP_NEW_LINE + ts2CppKey(retValue) + - CPP_BLANK_SPACE + replaceTsToken(funcName) + CPP_LEFT_PARENTHESES; + String retValue = ts2CppKey(fo.getRetValue()).isEmpty() ? + "" : ts2CppKey(fo.getRetValue()) + CPP_BLANK_SPACE; + resContent += CPP_NEW_LINE + tempStr + retValue + + replaceTsToken(funcName) + CPP_LEFT_PARENTHESES; for (ParamObj poItem : paList) { - String paType = ts2CppKey(poItem.getType()); + String paType = ts2CppKey(poItem.getType()).isEmpty() ? + CPP_AUTO_TOKEN + CPP_BLANK_SPACE : ts2CppKey(poItem.getType()) + CPP_BLANK_SPACE; String paName = poItem.getName(); - - resContent += !paName.isEmpty() ? paType + CPP_BLANK_SPACE + replaceTsToken(paName) + - CPP_COMMA + CPP_BLANK_SPACE : + String defaultVal = poItem.getStrValue(0); + defaultVal = defaultVal.isEmpty() ? "" : CPP_EQUAL + defaultVal; + resContent += !paName.isEmpty() ? paType + replaceTsToken(paName) + + defaultVal + CPP_COMMA + CPP_BLANK_SPACE : paType + CPP_COMMA + CPP_BLANK_SPACE; } - if (paList.size() > 0) { + if (!paList.isEmpty()) { resContent = StringUtils.removeLastCharacter(resContent, 2); } resContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; @@ -456,12 +492,20 @@ public class GenCppFile extends GeneratorBase { String structName = so.getName(); structName = !structName.isEmpty() ? structName : so.getAlias(); + String templateStr = !so.getTemplateList().isEmpty() ? + CPP_TEMPLATE_TOKEN + CPP_BLANK_SPACE + CPP_LEFT_ANGLE_BRACKET : ""; + for (String teStr : so.getTemplateList()) { + templateStr += CPP_TYPE_NAME_TOKEN + CPP_BLANK_SPACE + teStr + CPP_COMMA + CPP_BLANK_SPACE; + } + templateStr = templateStr.length() > 1 ? + StringUtils.removeLastCharacter(templateStr, 2) + CPP_RIGHT_ANGLE_BRACKET + CPP_BLANK_SPACE : ""; + List paList = so.getMemberList(); - resContent += CPP_NEW_LINE + CPP_STRUCT_TOKEN + + resContent += CPP_NEW_LINE + templateStr + CPP_STRUCT_TOKEN + CPP_BLANK_SPACE + structName + CPP_BLANK_SPACE + CPP_LEFT_BRACE; for (ParamObj paItem : paList) { - String paType = paItem.getType(); + String paType = paItem.getType().isEmpty() ? CPP_AUTO_TOKEN : paItem.getType(); resContent += CPP_NEW_LINE + CPP_TAB_SPACE + ts2CppKey(paType) + CPP_BLANK_SPACE + paItem.getName(); ; @@ -476,12 +520,13 @@ public class GenCppFile extends GeneratorBase { List funcList = so.getFuncList(); for (FuncObj funcItem : funcList) { - String retValue = funcItem.getRetValue(); - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + ts2CppKey(retValue) + - CPP_BLANK_SPACE + replaceTsToken(funcItem.getName()) + CPP_LEFT_PARENTHESES; + String retValue = ts2CppKey(funcItem.getRetValue()).isEmpty() ? "" : + ts2CppKey(funcItem.getRetValue()) + CPP_BLANK_SPACE; + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + retValue + + replaceTsToken(funcItem.getName()) + CPP_LEFT_PARENTHESES; List pol = funcItem.getParamList(); for (ParamObj poItem : pol) { - String retType = ts2CppKey(poItem.getType()); + String retType = ts2CppKey(poItem.getType()).isEmpty() ? CPP_AUTO_TOKEN : ts2CppKey(poItem.getType()); resContent += retType + CPP_BLANK_SPACE + replaceTsToken(poItem.getName()) + CPP_COMMA + CPP_BLANK_SPACE; } @@ -521,18 +566,24 @@ public class GenCppFile extends GeneratorBase { for (UnionObj uo : uol) { String unionName = uo.getName(); unionName = !unionName.isEmpty() ? unionName : uo.getAlias(); - List paList = uo.getMemList(); - int i = 0; - resContent += CPP_NEW_LINE + CPP_UNION_TOKEN + + + String templateStr = !uo.getTemplateList().isEmpty() ? + CPP_TEMPLATE_TOKEN + CPP_BLANK_SPACE + CPP_LEFT_ANGLE_BRACKET : ""; + for (String teStr : uo.getTemplateList()) { + templateStr += CPP_TYPE_NAME_TOKEN + CPP_BLANK_SPACE + teStr + CPP_COMMA + CPP_BLANK_SPACE; + } + templateStr = templateStr.length() > 1 ? + StringUtils.removeLastCharacter(templateStr, 2) + CPP_RIGHT_ANGLE_BRACKET + CPP_BLANK_SPACE : ""; + + resContent += CPP_NEW_LINE + templateStr + CPP_UNION_TOKEN + CPP_BLANK_SPACE + unionName + CPP_LEFT_BRACE; + List paList = uo.getMemList(); for (ParamObj paItem : paList) { String paType = paItem.getType(); String paName = paItem.getName(); resContent += CPP_NEW_LINE + CPP_TAB_SPACE + ts2CppKey(paType) + CPP_BLANK_SPACE + paName + CPP_SEMICOLON; - - i++; } resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE; resContent += CPP_SEMICOLON + CPP_NEW_LINE; diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java index 2f16d2c2..5dfeea44 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/ParamObj.java @@ -213,7 +213,7 @@ public class ParamObj extends GBaseObject { * @return 返回参数值 */ public String getStrValue(int i) { - return vList.get(i); + return vList.isEmpty() ? "" : vList.get(i); } /** diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/StructObj.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/StructObj.java index dd6f1eec..efa406b4 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/StructObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/StructObj.java @@ -32,6 +32,7 @@ public class StructObj extends GBaseObject { private String alias; private List memberList; private List funcList; + private List templateList; /** * 构造函数 @@ -39,6 +40,7 @@ public class StructObj extends GBaseObject { public StructObj() { this.memberList = new CopyOnWriteArrayList<>(); this.funcList = new CopyOnWriteArrayList<>(); + this.templateList = new CopyOnWriteArrayList<>(); } /** @@ -129,6 +131,24 @@ public class StructObj extends GBaseObject { this.memberList = memberList; } + /** + * 获取模板类列表 + * + * @return 模板类列表 + */ + public List getTemplateList() { + return templateList; + } + + /** + * 设置模板类列表 + * + * @param templateList 模板类列表 + */ + public void setTemplateList(List templateList) { + this.templateList = templateList; + } + /** * 增加属性 * @@ -174,4 +194,13 @@ public class StructObj extends GBaseObject { fo.setParamList(pol); this.funcList.add(fo); } + + /** + * 增加模板 + * + * @param name 增加模板名字 + */ + public void addTemplate(String name) { + this.templateList.add(name); + } } diff --git a/src/intellij_plugin/ohosgen/src/main/java/grammar/UnionObj.java b/src/intellij_plugin/ohosgen/src/main/java/grammar/UnionObj.java index a2567ecd..41852a8f 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/grammar/UnionObj.java +++ b/src/intellij_plugin/ohosgen/src/main/java/grammar/UnionObj.java @@ -35,12 +35,14 @@ public class UnionObj extends GBaseObject { private String alias; private List memList; private List funcList; + private List templateList; /** * 构造函数 */ public UnionObj() { memList = new CopyOnWriteArrayList<>(); + templateList = new CopyOnWriteArrayList<>(); } /** @@ -128,6 +130,24 @@ public class UnionObj extends GBaseObject { this.funcList = funcList; } + /** + * 获取模板列表 + * + * @return 模板列表 + */ + public List getTemplateList() { + return templateList; + } + + /** + * 设置模板列表 + * + * @param templateList 模板列表 + */ + public void setTemplateList(List templateList) { + this.templateList = templateList; + } + /** * 增加属性 * @@ -173,4 +193,13 @@ public class UnionObj extends GBaseObject { fo.setParamList(pol); this.funcList.add(fo); } + + /** + * 增加模板 + * + * @param name 模板名称 + */ + public void addTemplate(String name) { + this.templateList.add(name); + } } diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java index 9b68ebe3..e1705f58 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java @@ -234,7 +234,218 @@ class GenCppFileTest { } @Test - void getFuncContent() { + void getClassContent3() { + ClassObj co = new ClassObj(); + co.setName("Employee"); + List hList = new CopyOnWriteArrayList<>(); + hList.add("Person"); + co.setHeritageNameList(hList); + + ParamObj pa = new ParamObj(); + pa.setName("empCode"); + pa.setType("number"); + co.addParam(pa); + + ParamObj pa1 = new ParamObj(); + pa1.setName("currentUser"); + pa1.setType("any"); + co.addParam(pa1); + + ParamObj pa2 = new ParamObj(); + pa2.setName("pi"); + pa2.setType("number"); + pa2.setQualifier("static"); + pa2.setStrValue("3.14"); + co.addParam(pa2); + + List poList = new CopyOnWriteArrayList<>(); + ParamObj p1 = new ParamObj(); + p1.setName("empcode"); + p1.setType("number"); + ParamObj p2 = new ParamObj(); + p2.setName("name"); + p2.setType("string"); + co.addFunc("constructor", "", poList); + List poList1 = new CopyOnWriteArrayList<>(); + co.addFunc("displayName", "void", poList1); + + List col = new CopyOnWriteArrayList<>(); + col.add(co); + + ParseObj po = new ParseObj(); + po.setClassList(col); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenCppFile gdf) { + String classContent = gdf.getClassContent(); + System.out.println("genClass: " + classContent); + String expect = "\nclass Employee : public Person {\n" + + "\tint empCode;\n" + + "\tauto currentUser;\n" + + "\tstatic int pi = 3.14;\n" + + "\tconstructor();\n" + + "\tvoid displayName();\n" + + "};\n"; + assertEquals(expect, classContent); + } + } + + @Test + void getClassContent4() { + ClassObj co = new ClassObj(); + co.setName("myClass"); + + List poList1 = new CopyOnWriteArrayList<>(); + FuncObj fo = new FuncObj(); + fo.setName("foo"); + fo.setRetValue("Promise"); + fo.setAccessor("public"); + fo.setType("async"); + fo.setParamList(poList1); + co.addFunc(fo); + List col = new CopyOnWriteArrayList<>(); + col.add(co); + + ParseObj po = new ParseObj(); + po.setClassList(col); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenCppFile gdf) { + String classContent = gdf.getClassContent(); + System.out.println("genClass: " + classContent); + String expect = "\nclass myClass {\n" + + "\tauto foo();\n" + + "};\n"; + assertEquals(expect, classContent); + } + } + + @Test + void getClassContent5() { + ClassObj co = new ClassObj(); + co.setName("KeyValuePair"); + List pol = new CopyOnWriteArrayList<>(); + ParamObj pa = new ParamObj(); + pa.setName("key"); + pa.setType("T"); + pa.setQualifier("private"); + pol.add(pa); + ParamObj po1 = new ParamObj(); + po1.setName("val"); + po1.setType("U"); + po1.setQualifier("private"); + pol.add(po1); + co.setParamList(pol); + + List tmpList = new CopyOnWriteArrayList<>(); + tmpList.add("T"); + tmpList.add("U"); + co.setTempList(tmpList); + + List poList1 = new CopyOnWriteArrayList<>(); + FuncObj fo = new FuncObj(); + fo.setName("setKeyValue"); + fo.setRetValue("void"); + fo.addParam("key", "T"); + fo.addParam("val", "U"); + co.addFunc(fo); + List col = new CopyOnWriteArrayList<>(); + col.add(co); + + ParseObj po = new ParseObj(); + po.setClassList(col); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenCppFile gdf) { + String classContent = gdf.getClassContent(); + System.out.println("genClass: " + classContent); + String expect = "\ntemplate class KeyValuePair {\n" + + "\tprivate T key;\n" + + "\tprivate U val;\n" + + "\tvoid setKeyValue(T key, U val);\n" + + "};\n"; + assertEquals(expect, classContent); + } + } + + @Test + void getClassContent6() { + ClassObj co = new ClassObj(); + co.setName("kvProcessor"); + List tmpList = new CopyOnWriteArrayList<>(); + tmpList.add("T"); + tmpList.add("U"); + co.setTempList(tmpList); + List htList = new CopyOnWriteArrayList<>(); + htList.add("implements"); + co.setHeritageTypeList(htList); + List hnList = new CopyOnWriteArrayList<>(); + hnList.add("IKeyValueProcessor"); + co.setHeritageNameList(hnList); + List htempList = new CopyOnWriteArrayList<>(); + htempList.add("T"); + htempList.add("U"); + co.setHeritageTemplateList(htempList); + + List poList1 = new CopyOnWriteArrayList<>(); + FuncObj fo = new FuncObj(); + fo.setName("process"); + fo.setRetValue("void"); + fo.addParam("key", "T"); + fo.addParam("val", "U"); + co.addFunc(fo); + List col = new CopyOnWriteArrayList<>(); + col.add(co); + + ParseObj po = new ParseObj(); + po.setClassList(col); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenCppFile gdf) { + String classContent = gdf.getClassContent(); + System.out.println("genClass: " + classContent); + String expect = "\ntemplate class kvProcessor : public IKeyValueProcessor {\n" + + "\tvoid process(T key, U val);\n" + + "};\n"; + assertEquals(expect, classContent); + } + } + + @Test + void getClassContent7() { + ClassObj co = new ClassObj(); + co.setName("Shape"); + + FuncObj fo = new FuncObj(); + fo.setName("process"); + fo.setRetValue("void"); + fo.addParam("key", ""); + fo.addParam("val", ""); + co.addFunc(fo); + List col = new CopyOnWriteArrayList<>(); + col.add(co); + + ParseObj po = new ParseObj(); + po.setClassList(col); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenCppFile gdf) { + String classContent = gdf.getClassContent(); + System.out.println("genClass: " + classContent); + String expect = "\nclass Shape {\n" + + "\tvoid process(auto key, auto val);\n" + + "};\n"; + assertEquals(expect, classContent); + } + } + + @Test + void getFuncContent1() { FuncObj fo = new FuncObj(); fo.setName("TestFunc"); fo.setRetValue("void"); @@ -256,7 +467,178 @@ class GenCppFileTest { } @Test - void getStructContent() { + void getFuncContent2() { + FuncObj fo = new FuncObj(); + fo.setName("ToCapital"); + fo.setRetValue("string"); + fo.addParam("str", "string"); + ParamObj pa = new ParamObj(); + pa.setName("length"); + pa.setType("number"); + pa.setStrValue("0"); + fo.addParam(pa); + List fol = new CopyOnWriteArrayList<>(); + fol.add(fo); + ParseObj po = new ParseObj(); + po.setFuncList(fol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nchar* ToCapital(char* str, int length = 0);"; + assertEquals(expect, funcContent); + } + } + + @Test + void getFuncContent3() { + FuncObj fo = new FuncObj(); + fo.setName("Nemw"); + fo.setRetValue("string"); + ParamObj pa1 = new ParamObj(); + pa1.setName("str"); + pa1.setType("string"); + pa1.setStrValue("\"joke\""); + fo.addParam(pa1); + ParamObj pa2 = new ParamObj(); + pa2.setName("length"); + pa2.setType("number"); + pa2.setStrValue("0"); + fo.addParam(pa2); + List fol = new CopyOnWriteArrayList<>(); + fol.add(fo); + ParseObj po = new ParseObj(); + po.setFuncList(fol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nchar* Nemw(char* str = \"joke\", int length = 0);"; + assertEquals(expect, funcContent); + } + } + + @Test + void getFuncContent4() { + FuncObj fo = new FuncObj(); + fo.setName("Nemw"); + fo.setRetValue("string"); + ParamObj pa1 = new ParamObj(); + pa1.setName("str"); + fo.addParam(pa1); + ParamObj pa2 = new ParamObj(); + pa2.setName("length"); + fo.addParam(pa2); + List fol = new CopyOnWriteArrayList<>(); + fol.add(fo); + ParseObj po = new ParseObj(); + po.setFuncList(fol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nchar* Nemw(auto str, auto length);"; + assertEquals(expect, funcContent); + } + } + + @Test + void getFuncContent5() { + FuncObj fo = new FuncObj(); + fo.setName("Nemw"); + fo.setRetValue(""); + ParamObj pa1 = new ParamObj(); + pa1.setName("str"); + fo.addParam(pa1); + ParamObj pa2 = new ParamObj(); + pa2.setName("length"); + fo.addParam(pa2); + List fol = new CopyOnWriteArrayList<>(); + fol.add(fo); + ParseObj po = new ParseObj(); + po.setFuncList(fol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nNemw(auto str, auto length);"; + assertEquals(expect, funcContent); + } + } + + @Test + void getFuncContent6() { + FuncObj fo = new FuncObj(); + fo.setName("getArray"); + fo.setRetValue("T[]"); + + List tempList = new CopyOnWriteArrayList<>(); + tempList.add("T"); + fo.setTempList(tempList); + ParamObj pa1 = new ParamObj(); + pa1.setName("items"); + pa1.setType("T[]"); + fo.addParam(pa1); + + List fol = new CopyOnWriteArrayList<>(); + fol.add(fo); + ParseObj po = new ParseObj(); + po.setFuncList(fol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\ntemplate T* getArray(T* items);"; + assertEquals(expect, funcContent); + } + } + + @Test + void getFuncContent7() { + FuncObj fo = new FuncObj(); + fo.setName("displayType"); + fo.setRetValue("void"); + + List tempList = new CopyOnWriteArrayList<>(); + tempList.add("T"); + tempList.add("U"); + fo.setTempList(tempList); + ParamObj pa1 = new ParamObj(); + pa1.setName("id"); + pa1.setType("T"); + fo.addParam(pa1); + ParamObj pa2 = new ParamObj(); + pa2.setName("name"); + pa2.setType("U"); + fo.addParam(pa2); + + List fol = new CopyOnWriteArrayList<>(); + fol.add(fo); + ParseObj po = new ParseObj(); + po.setFuncList(fol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\ntemplate void displayType(T id, U name);"; + assertEquals(expect, funcContent); + } + } + + @Test + void getStructContent1() { StructObj so = new StructObj(); so.setName("TestStruct"); @@ -295,12 +677,115 @@ class GenCppFileTest { } } + @Test + void getStructContent2() { + StructObj so = new StructObj(); + so.setName("TestStruct"); + so.addMember("name", "T"); + so.addMember("age", "U"); + so.addTemplate("T"); + so.addTemplate("U"); + + List poList = new CopyOnWriteArrayList<>(); + ParamObj poItem = new ParamObj(); + poItem.setName("a"); + poItem.setType("T"); + poList.add(poItem); + ParamObj poItem2 = new ParamObj(); + poItem2.setName("b"); + poItem2.setType("U"); + poList.add(poItem2); + + so.addFunc("add", "number", poList); + + List sol = new CopyOnWriteArrayList<>(); + sol.add(so); + ParseObj po = new ParseObj(); + po.setStructList(sol); + + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenCppFile gdf) { + String structContent = gdf.getStructContent(); + System.out.println("genStruct: " + structContent); + String expect = "\ntemplate struct TestStruct {\n" + + "\tT name;\n" + + "\tU age;\n" + + "\tint add(T a, U b);\n" + + "};\n"; + assertEquals(expect, structContent); + } + } + + @Test + void getStructContent3() { + StructObj so = new StructObj(); + so.setName("TestStruct"); + + so.addMember("name", ""); + so.addMember("age", ""); + + List poList = new CopyOnWriteArrayList<>(); + ParamObj poItem = new ParamObj(); + poItem.setName("a"); + poItem.setType(""); + poList.add(poItem); + ParamObj poItem2 = new ParamObj(); + poItem2.setName("b"); + poItem2.setType(""); + poList.add(poItem2); + + so.addFunc("add", "", poList); + + List sol = new CopyOnWriteArrayList<>(); + sol.add(so); + ParseObj po = new ParseObj(); + po.setStructList(sol); + + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenCppFile gdf) { + String structContent = gdf.getStructContent(); + System.out.println("genStruct: " + structContent); + String expect = "\nstruct TestStruct {\n" + + "\tauto name;\n" + + "\tauto age;\n" + + "\tadd(auto a, auto b);\n" + + "};\n"; + assertEquals(expect, structContent); + } + } + + @Test + void getStructContent4() { + StructObj so = new StructObj(); + so.setName("TestStruct"); + + List sol = new CopyOnWriteArrayList<>(); + sol.add(so); + ParseObj po = new ParseObj(); + po.setStructList(sol); + + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenCppFile gdf) { + String structContent = gdf.getStructContent(); + System.out.println("genStruct: " + structContent); + String expect = "\nstruct TestStruct {\n" + + "};\n"; + assertEquals(expect, structContent); + } + } + @Test void getTypeContent() { } @Test - void getUnionContent() { + void getUnionContent1() { UnionObj uo = new UnionObj(); uo.setName("TestUnion"); @@ -325,6 +810,35 @@ class GenCppFileTest { } } + @Test + void getUnionContent2() { + UnionObj uo = new UnionObj(); + uo.setName("TestUnion"); + uo.addMember("name", "T"); + uo.addMember("age", "U"); + + uo.addTemplate("T"); + uo.addTemplate("U"); + + List uol = new CopyOnWriteArrayList<>(); + uol.add(uo); + ParseObj po = new ParseObj(); + po.setUnionList(uol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genUnionList(po.getUnionList()); + + if (gb instanceof GenCppFile gdf) { + String unionContent = gdf.getUnionContent(); + System.out.println("genUnion: " + unionContent); + String expect = "\ntemplate union TestUnion{\n" + + "\tT name;\n" + + "\tU age;\n" + + "};\n"; + assertEquals(expect, unionContent); + } + } + + @Test void getConstContent() { ParseObj po = new ParseObj(); -- Gitee From 5335811db1cf72acf35a9317fccc9554e249c5fd Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 8 Apr 2025 17:21:03 +0800 Subject: [PATCH 2/3] fix code check Signed-off-by: sunlian --- .../ohosgen/src/main/java/gen/GenCppFile.java | 52 ++++++++++--------- .../src/test/java/gen/GenCppFileTest.java | 3 +- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java index 9dc93473..632fadfe 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java @@ -357,6 +357,26 @@ public class GenCppFile extends GeneratorBase { this.enumContent = resContent; }; + private String setClassFunc(List funcList, String resContent) { + for (FuncObj funcItem : funcList) { + String retValue = funcItem.getRetValue(); + retValue = retValue.isEmpty() ? "" : ts2CppKey(retValue) + CPP_BLANK_SPACE; + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + retValue + + replaceTsToken(funcItem.getName()) + CPP_LEFT_PARENTHESES; + List pol = funcItem.getParamList(); + for (ParamObj poItem : pol) { + String retType = ts2CppKey(poItem.getType()).isEmpty() ? + CPP_AUTO_TOKEN : ts2CppKey(poItem.getType()); + resContent += poItem.getName() == null ? retType + CPP_COMMA + CPP_BLANK_SPACE : + retType + CPP_BLANK_SPACE + replaceTsToken(poItem.getName()) + CPP_COMMA + CPP_BLANK_SPACE; + } + if (!pol.isEmpty()) { + resContent = StringUtils.removeLastCharacter(resContent, 2); + } + resContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; + } + return resContent; + } /** * 生成输出内容 * @@ -374,7 +394,7 @@ public class GenCppFile extends GeneratorBase { String templateStr = !co.getTempList().isEmpty() ? CPP_TEMPLATE_TOKEN + CPP_BLANK_SPACE + CPP_LEFT_ANGLE_BRACKET : ""; for (String teStr : co.getTempList()) { - templateStr += CPP_TYPE_NAME_TOKEN + CPP_BLANK_SPACE + teStr + CPP_COMMA + CPP_BLANK_SPACE; + templateStr += CPP_TYPE_NAME_TOKEN + CPP_BLANK_SPACE + teStr + CPP_COMMA + CPP_BLANK_SPACE; } templateStr = templateStr.length() > 1 ? StringUtils.removeLastCharacter(templateStr, 2) + CPP_RIGHT_ANGLE_BRACKET + CPP_BLANK_SPACE : ""; @@ -410,24 +430,7 @@ public class GenCppFile extends GeneratorBase { } } - List funcList = co.getFuncList(); - for (FuncObj funcItem : funcList) { - String retValue = funcItem.getRetValue(); - retValue = retValue.isEmpty() ? "" : ts2CppKey(retValue) + CPP_BLANK_SPACE; - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + retValue + - replaceTsToken(funcItem.getName()) + CPP_LEFT_PARENTHESES; - List pol = funcItem.getParamList(); - for (ParamObj poItem : pol) { - String retType = ts2CppKey(poItem.getType()).isEmpty() ? - CPP_AUTO_TOKEN : ts2CppKey(poItem.getType()); - resContent += poItem.getName() == null ? retType + CPP_COMMA + CPP_BLANK_SPACE : - retType + CPP_BLANK_SPACE + replaceTsToken(poItem.getName()) + CPP_COMMA + CPP_BLANK_SPACE; - } - if (!pol.isEmpty()) { - resContent = StringUtils.removeLastCharacter(resContent, 2); - } - resContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; - } + resContent = setClassFunc(co.getFuncList(), resContent); resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; } @@ -495,7 +498,7 @@ public class GenCppFile extends GeneratorBase { String templateStr = !so.getTemplateList().isEmpty() ? CPP_TEMPLATE_TOKEN + CPP_BLANK_SPACE + CPP_LEFT_ANGLE_BRACKET : ""; for (String teStr : so.getTemplateList()) { - templateStr += CPP_TYPE_NAME_TOKEN + CPP_BLANK_SPACE + teStr + CPP_COMMA + CPP_BLANK_SPACE; + templateStr += CPP_TYPE_NAME_TOKEN + CPP_BLANK_SPACE + teStr + CPP_COMMA + CPP_BLANK_SPACE; } templateStr = templateStr.length() > 1 ? StringUtils.removeLastCharacter(templateStr, 2) + CPP_RIGHT_ANGLE_BRACKET + CPP_BLANK_SPACE : ""; @@ -521,12 +524,13 @@ public class GenCppFile extends GeneratorBase { List funcList = so.getFuncList(); for (FuncObj funcItem : funcList) { String retValue = ts2CppKey(funcItem.getRetValue()).isEmpty() ? "" : - ts2CppKey(funcItem.getRetValue()) + CPP_BLANK_SPACE; + ts2CppKey(funcItem.getRetValue()) + CPP_BLANK_SPACE; resContent += CPP_NEW_LINE + CPP_TAB_SPACE + retValue + - replaceTsToken(funcItem.getName()) + CPP_LEFT_PARENTHESES; + replaceTsToken(funcItem.getName()) + CPP_LEFT_PARENTHESES; List pol = funcItem.getParamList(); for (ParamObj poItem : pol) { - String retType = ts2CppKey(poItem.getType()).isEmpty() ? CPP_AUTO_TOKEN : ts2CppKey(poItem.getType()); + String retType = ts2CppKey(poItem.getType()).isEmpty() ? + CPP_AUTO_TOKEN : ts2CppKey(poItem.getType()); resContent += retType + CPP_BLANK_SPACE + replaceTsToken(poItem.getName()) + CPP_COMMA + CPP_BLANK_SPACE; } @@ -570,7 +574,7 @@ public class GenCppFile extends GeneratorBase { String templateStr = !uo.getTemplateList().isEmpty() ? CPP_TEMPLATE_TOKEN + CPP_BLANK_SPACE + CPP_LEFT_ANGLE_BRACKET : ""; for (String teStr : uo.getTemplateList()) { - templateStr += CPP_TYPE_NAME_TOKEN + CPP_BLANK_SPACE + teStr + CPP_COMMA + CPP_BLANK_SPACE; + templateStr += CPP_TYPE_NAME_TOKEN + CPP_BLANK_SPACE + teStr + CPP_COMMA + CPP_BLANK_SPACE; } templateStr = templateStr.length() > 1 ? StringUtils.removeLastCharacter(templateStr, 2) + CPP_RIGHT_ANGLE_BRACKET + CPP_BLANK_SPACE : ""; diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java index e1705f58..9efb0b8f 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java @@ -408,7 +408,8 @@ class GenCppFileTest { if (gb instanceof GenCppFile gdf) { String classContent = gdf.getClassContent(); System.out.println("genClass: " + classContent); - String expect = "\ntemplate class kvProcessor : public IKeyValueProcessor {\n" + + String expect = "\ntemplate class kvProcessor : " + + "public IKeyValueProcessor {\n" + "\tvoid process(T key, U val);\n" + "};\n"; assertEquals(expect, classContent); -- Gitee From 7856b416f5d095564b1a998b1f72873114a87cf5 Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 8 Apr 2025 17:35:56 +0800 Subject: [PATCH 3/3] fix code check Signed-off-by: sunlian --- .../ohosgen/src/main/java/gen/GenCppFile.java | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java index 632fadfe..0b9c0439 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java @@ -357,26 +357,28 @@ public class GenCppFile extends GeneratorBase { this.enumContent = resContent; }; - private String setClassFunc(List funcList, String resContent) { + private String setClassFunc(List funcList, String content) { + String tempResContent = content; for (FuncObj funcItem : funcList) { String retValue = funcItem.getRetValue(); retValue = retValue.isEmpty() ? "" : ts2CppKey(retValue) + CPP_BLANK_SPACE; - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + retValue + + tempResContent += CPP_NEW_LINE + CPP_TAB_SPACE + retValue + replaceTsToken(funcItem.getName()) + CPP_LEFT_PARENTHESES; List pol = funcItem.getParamList(); for (ParamObj poItem : pol) { String retType = ts2CppKey(poItem.getType()).isEmpty() ? CPP_AUTO_TOKEN : ts2CppKey(poItem.getType()); - resContent += poItem.getName() == null ? retType + CPP_COMMA + CPP_BLANK_SPACE : + tempResContent += (poItem.getName() == null) ? retType + CPP_COMMA + CPP_BLANK_SPACE : retType + CPP_BLANK_SPACE + replaceTsToken(poItem.getName()) + CPP_COMMA + CPP_BLANK_SPACE; } if (!pol.isEmpty()) { - resContent = StringUtils.removeLastCharacter(resContent, 2); + tempResContent = StringUtils.removeLastCharacter(tempResContent, 2); } - resContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; + tempResContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; } - return resContent; + return tempResContent; } + /** * 生成输出内容 * @@ -513,8 +515,7 @@ public class GenCppFile extends GeneratorBase { CPP_BLANK_SPACE + paItem.getName(); ; List initVList = paItem.getvList(); - int vaSize = initVList.size(); - if (vaSize > 0) { + if (initVList.size() > 0) { resContent += CPP_EQUAL + initVList.get(0) + CPP_SEMICOLON; } else { resContent += CPP_SEMICOLON; @@ -534,10 +535,7 @@ public class GenCppFile extends GeneratorBase { resContent += retType + CPP_BLANK_SPACE + replaceTsToken(poItem.getName()) + CPP_COMMA + CPP_BLANK_SPACE; } - if (!pol.isEmpty()) { - resContent = StringUtils.removeLastCharacter(resContent, 2); - } - + resContent = !pol.isEmpty() ? StringUtils.removeLastCharacter(resContent, 2) : resContent; resContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; } -- Gitee