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 1ed4a8cfb1fa754fa7b4d3dc3651c16114bf3648..6798221e10add23145dc9e2c6269343c1194b5e0 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java @@ -21,6 +21,7 @@ import utils.StringUtils; import java.io.File; import java.util.List; +import java.util.Locale; import java.util.Map; /** @@ -35,6 +36,9 @@ import java.util.Map; public class GenCppFile extends GeneratorBase { private static final String CPP_ENUM_TOKEN = "enum"; 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_CHAR_START_TOKEN = "char*"; private static final String CPP_EXPORT_TOKEN = "export"; private static final String CPP_IMPLEMENCPP_TOKEN = "implements"; private static final String CPP_EXTENDS_TOKEN = "extends"; @@ -77,6 +81,8 @@ public class GenCppFile extends GeneratorBase { private static final String CPP_SPLIT = " | "; private static final String CPP_EQUAL = " = "; private static final String CPP_COMMA = ","; + private static final String CPP_DOUBLE_QUOTATION = "\""; + private static final String CPP_UNDER_LINE = "_"; private static final String CPP_SEMICOLON = ";"; private static final String CPP_COLON = ":"; private static final String CPP_LEFT_BRACE = "{"; @@ -88,6 +94,7 @@ public class GenCppFile extends GeneratorBase { private static final String CPP_LEFT_ANGLE_BRACKET = "<"; private static final String CPP_RIGHT_ANGLE_BRACKET = ">"; + private static final String CPP_STR_SUFFIX = "STR"; private static final String CPP_FILE_PREFIX = "ag_"; private static final String CPP_FILE_H_SUFFIX = ".h"; private static final String CPP_FILE_CPP_SUFFIX = ".cpp"; @@ -111,6 +118,7 @@ public class GenCppFile extends GeneratorBase { ); private final Map tsTokenMap = Map.ofEntries( + Map.entry("\"", ""), Map.entry("*", ""), Map.entry("&", ""), Map.entry("(", ""), @@ -248,6 +256,7 @@ public class GenCppFile extends GeneratorBase { genStructList(po.getStructList()); genTypeList(po.getTypeList()); genUnionList(po.getUnionList()); + genVarList(po.getVarList()); } /** @@ -308,15 +317,33 @@ public class GenCppFile extends GeneratorBase { for (String memItem : memList) { resContent += CPP_NEW_LINE + CPP_TAB_SPACE + memItem; if (vaList.size() > i && !vaList.get(i).isEmpty()) { - resContent += CPP_EQUAL + vaList.get(i) + CPP_COMMA; + resContent += CPP_EQUAL + replaceTsToken(vaList.get(i)) + CPP_COMMA; } else { resContent += CPP_COMMA; } i++; } + resContent = StringUtils.removeLastSpace(resContent); resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + + i = 0; + if (vaList.size() > i && !vaList.get(i).isEmpty() && + vaList.get(i).contains("\"")) { + resContent += CPP_NEW_LINE + CPP_CHAR_START_TOKEN + CPP_BLANK_SPACE + + enumName.toLowerCase(Locale.ROOT) + CPP_UNDER_LINE + CPP_STR_SUFFIX + + CPP_LEFT_SQUARE_BRACKET + CPP_RIGHT_SQUARE_BRACKET + CPP_EQUAL + CPP_LEFT_BRACE; + for (String val : vaList) { + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + CPP_LEFT_SQUARE_BRACKET + + memList.get(i) + CPP_RIGHT_SQUARE_BRACKET + CPP_EQUAL + val + CPP_COMMA; + i++; + } + resContent = StringUtils.removeLastCharacter(resContent, 1); + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + + } } + this.enumContent = resContent; }; @@ -333,18 +360,24 @@ public class GenCppFile extends GeneratorBase { for (ClassObj co : col) { String className = co.getName(); className = !className.isEmpty() ? className : co.getAlias(); + List hnList = co.getHeritageNameList(); + String htStr = hnList.size() > 0 ? CPP_BLANK_SPACE + CPP_COLON + CPP_BLANK_SPACE : ""; + for (String hName : hnList) { + htStr += CPP_PUBLIC_TOKEN + CPP_BLANK_SPACE + hName + CPP_COMMA + CPP_BLANK_SPACE; + } + htStr = htStr.length() > 1 ? StringUtils.removeLastCharacter(htStr, 2) : htStr; - List paList = co.getParamList(); resContent += CPP_NEW_LINE + CPP_CLASS_TOKEN + - CPP_BLANK_SPACE + className + CPP_BLANK_SPACE + CPP_LEFT_BRACE; - + CPP_BLANK_SPACE + className + htStr + CPP_BLANK_SPACE + CPP_LEFT_BRACE; + List paList = co.getParamList(); for (ParamObj paItem : paList) { String paType = paItem.getType(); - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + replaceTsToken(paItem.getName()) + - CPP_COLON + CPP_BLANK_SPACE + ts2CppKey(paType); + String qualifyStr = paItem.getQualifier() == null || paItem.getQualifier().isEmpty() ? + "" : paItem.getQualifier() + CPP_BLANK_SPACE; + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + qualifyStr + ts2CppKey(paType) + + CPP_BLANK_SPACE + replaceTsToken(paItem.getName()); List initVList = paItem.getvList(); - int vaSize = initVList.size(); - if (vaSize > 0) { + if (!initVList.isEmpty()) { resContent += CPP_EQUAL + initVList.get(0) + CPP_SEMICOLON; } else { resContent += CPP_SEMICOLON; @@ -353,24 +386,23 @@ public class GenCppFile extends GeneratorBase { List funcList = co.getFuncList(); for (FuncObj funcItem : funcList) { - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + replaceTsToken(funcItem.getName()) + - CPP_LEFT_PARENTHESES; + 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()); - resContent += replaceTsToken(poItem.getName()) + CPP_COLON + - CPP_BLANK_SPACE + retType + CPP_COMMA + CPP_BLANK_SPACE; + resContent += poItem.getName() == null ? retType + CPP_COMMA + CPP_BLANK_SPACE : + retType + CPP_BLANK_SPACE + replaceTsToken(poItem.getName()) + + CPP_COMMA + CPP_BLANK_SPACE; } - if (pol.size() > 0) { + if (!pol.isEmpty()) { resContent = StringUtils.removeLastCharacter(resContent, 2); } - - String retValue = funcItem.getRetValue(); - resContent += CPP_RIGHT_PARENTHESES + CPP_BLANK_SPACE + CPP_COLON + - CPP_BLANK_SPACE + ts2CppKey(retValue) + CPP_SEMICOLON; + resContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; } - resContent = StringUtils.removeLastSpace(resContent); resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; } this.classContent = resContent; @@ -389,25 +421,22 @@ public class GenCppFile extends GeneratorBase { String funcName = fo.getName(); funcName = !funcName.isEmpty() ? funcName : fo.getAlias(); List paList = fo.getParamList(); - int i = 0; - resContent += CPP_NEW_LINE + CPP_FUNCTION_TOKEN + + String retValue = fo.getRetValue(); + resContent += CPP_NEW_LINE + ts2CppKey(retValue) + CPP_BLANK_SPACE + replaceTsToken(funcName) + CPP_LEFT_PARENTHESES; for (ParamObj poItem : paList) { String paType = ts2CppKey(poItem.getType()); String paName = poItem.getName(); - resContent += !paName.isEmpty() ? replaceTsToken(paName) + CPP_COLON + - CPP_BLANK_SPACE + paType + CPP_COMMA + CPP_BLANK_SPACE : + resContent += !paName.isEmpty() ? paType + CPP_BLANK_SPACE + replaceTsToken(paName) + + CPP_COMMA + CPP_BLANK_SPACE : paType + CPP_COMMA + CPP_BLANK_SPACE; } if (paList.size() > 0) { resContent = StringUtils.removeLastCharacter(resContent, 2); } - - String retValue = fo.getRetValue(); - resContent += CPP_RIGHT_PARENTHESES + CPP_BLANK_SPACE + CPP_COLON + - CPP_BLANK_SPACE + ts2CppKey(retValue) + CPP_SEMICOLON; + resContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; } this.funcContent = resContent; System.out.println("genFuncList : " + resContent); @@ -428,13 +457,14 @@ public class GenCppFile extends GeneratorBase { structName = !structName.isEmpty() ? structName : so.getAlias(); List paList = so.getMemberList(); - resContent += CPP_NEW_LINE + CPP_EXPORT_TOKEN + CPP_BLANK_SPACE + CPP_CLASS_TOKEN + + resContent += CPP_NEW_LINE + CPP_STRUCT_TOKEN + CPP_BLANK_SPACE + structName + CPP_BLANK_SPACE + CPP_LEFT_BRACE; for (ParamObj paItem : paList) { String paType = paItem.getType(); - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + paItem.getName() + - CPP_COLON + CPP_BLANK_SPACE + ts2CppKey(paType); + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + ts2CppKey(paType) + + CPP_BLANK_SPACE + paItem.getName(); + ; List initVList = paItem.getvList(); int vaSize = initVList.size(); if (vaSize > 0) { @@ -446,20 +476,20 @@ public class GenCppFile extends GeneratorBase { List funcList = so.getFuncList(); for (FuncObj funcItem : funcList) { - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + replaceTsToken(funcItem.getName()) + CPP_LEFT_PARENTHESES; + String retValue = funcItem.getRetValue(); + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + ts2CppKey(retValue) + + CPP_BLANK_SPACE + replaceTsToken(funcItem.getName()) + CPP_LEFT_PARENTHESES; List pol = funcItem.getParamList(); for (ParamObj poItem : pol) { String retType = ts2CppKey(poItem.getType()); - resContent += replaceTsToken(poItem.getName()) + CPP_COLON + - CPP_BLANK_SPACE + retType + CPP_COMMA + CPP_BLANK_SPACE; + resContent += retType + CPP_BLANK_SPACE + replaceTsToken(poItem.getName()) + + CPP_COMMA + CPP_BLANK_SPACE; } if (!pol.isEmpty()) { resContent = StringUtils.removeLastCharacter(resContent, 2); } - String retValue = funcItem.getRetValue(); - resContent += CPP_RIGHT_PARENTHESES + CPP_BLANK_SPACE + CPP_COLON + - CPP_BLANK_SPACE + ts2CppKey(retValue) + CPP_SEMICOLON; + resContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; } resContent = StringUtils.removeLastSpace(resContent); @@ -493,17 +523,18 @@ public class GenCppFile extends GeneratorBase { unionName = !unionName.isEmpty() ? unionName : uo.getAlias(); List paList = uo.getMemList(); int i = 0; - resContent += CPP_NEW_LINE + CPP_EXPORT_TOKEN + CPP_BLANK_SPACE + CPP_TYPE_TOKEN + - CPP_BLANK_SPACE + unionName + CPP_EQUAL; + resContent += CPP_NEW_LINE + CPP_UNION_TOKEN + + CPP_BLANK_SPACE + unionName + CPP_LEFT_BRACE; for (ParamObj paItem : paList) { String paType = paItem.getType(); - resContent += ts2CppKey(paType) + CPP_SPLIT; + String paName = paItem.getName(); + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + ts2CppKey(paType) + + CPP_BLANK_SPACE + paName + CPP_SEMICOLON; i++; } - - resContent = StringUtils.removeLastCharacter(resContent, 3); + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE; resContent += CPP_SEMICOLON + CPP_NEW_LINE; } this.unionContent = resContent; @@ -525,8 +556,8 @@ public class GenCppFile extends GeneratorBase { String paValue = po.getStrValue(0); int i = 0; resContent += CPP_NEW_LINE + CPP_CONST_TOKEN + - CPP_BLANK_SPACE + paName + CPP_BLANK_SPACE + CPP_COLON + CPP_BLANK_SPACE + - paType + CPP_EQUAL + paValue; + CPP_BLANK_SPACE + paType + CPP_BLANK_SPACE + paName + + CPP_EQUAL + paValue; resContent += CPP_SEMICOLON + CPP_NEW_LINE; } diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9b68ebe308c35acb71067b709e49387541f470e3 --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java @@ -0,0 +1,602 @@ +/* + * Copyright (c) 2025 Shenzhen Kaihong Digital. + * 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, + * 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. + */ + +package gen; + +import grammar.*; +import it.unimi.dsi.fastutil.ints.P; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +import static org.junit.jupiter.api.Assertions.*; +import static utils.FileUtils.readText; + +/** + *

类名:该类用于xxx

+ * description + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +class GenCppFileTest { + + @Test + void getInterfaceContent() { + } + + @Test + void getEnumContent1() { + EnumObj eo = new EnumObj(); + eo.setName("TestEnum"); + List ml = new CopyOnWriteArrayList<>(); + ml.add("ONE"); + ml.add("TWO"); + eo.setMemberList(ml); + List eol = new CopyOnWriteArrayList<>(); + eol.add(eo); + ParseObj po = new ParseObj(); + po.setEnumList(eol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genEnumList(po.getEnumList()); + + if (gb instanceof GenCppFile gdf) { + String enumContent = gdf.getEnumContent(); + System.out.println("genEnum: " + enumContent); + String expect = "\nenum TestEnum {\n" + + "\tONE,\n" + + "\tTWO,\n" + + "};\n"; + assertEquals(expect, enumContent); + } + } + + @Test + void getEnumContent2() { + EnumObj eo = new EnumObj(); + eo.setName("Colors"); + List ml = new CopyOnWriteArrayList<>(); + ml.add("Red"); + ml.add("Green"); + ml.add("Blue"); + eo.setMemberList(ml); + List vl = new CopyOnWriteArrayList<>(); + vl.add("RED"); + vl.add("GREEN"); + vl.add("BLUE"); + eo.setValueList(vl); + List eol = new CopyOnWriteArrayList<>(); + eol.add(eo); + ParseObj po = new ParseObj(); + po.setEnumList(eol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genEnumList(po.getEnumList()); + + if (gb instanceof GenCppFile gdf) { + String enumContent = gdf.getEnumContent(); + System.out.println("genEnum: " + enumContent); + String expect = "\nenum Colors {\n" + + "\tRed = RED,\n" + + "\tGreen = GREEN,\n" + + "\tBlue = BLUE,\n" + + "};\n"; + assertEquals(expect, enumContent); + } + } + + @Test + void getEnumContent3() { + EnumObj eo = new EnumObj(); + eo.setName("Colors"); + List ml = new CopyOnWriteArrayList<>(); + ml.add("Red"); + ml.add("Green"); + ml.add("Blue"); + eo.setMemberList(ml); + List vl = new CopyOnWriteArrayList<>(); + vl.add("\"RED\""); + vl.add("\"GREEN\""); + vl.add("\"BLUE\""); + eo.setValueList(vl); + List eol = new CopyOnWriteArrayList<>(); + eol.add(eo); + ParseObj po = new ParseObj(); + po.setEnumList(eol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genEnumList(po.getEnumList()); + + if (gb instanceof GenCppFile gdf) { + String enumContent = gdf.getEnumContent(); + System.out.println("genEnum: " + enumContent); + String expect = "\nenum Colors {\n" + + "\tRed = RED,\n" + + "\tGreen = GREEN,\n" + + "\tBlue = BLUE,\n" + + "};\n" + + "\n" + + "char* colors_STR[] = {\n" + + "\t[Red] = \"RED\",\n" + + "\t[Green] = \"GREEN\",\n" + + "\t[Blue] = \"BLUE\"\n" + + "};\n"; + assertEquals(expect, enumContent); + } + } + + @Test + void getClassContent1() { + ClassObj co = new ClassObj(); + co.setName("TestClass"); + + co.addParam("name", "string"); + co.addParam("age", "number"); + + List poList = new CopyOnWriteArrayList<>(); + ParamObj poItem = new ParamObj(); + poItem.setName("a"); + poItem.setType("number"); + poList.add(poItem); + ParamObj poItem2 = new ParamObj(); + poItem2.setName("b"); + poItem2.setType("number"); + poList.add(poItem2); + + co.addFunc("add", "number", poList); + + 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 TestClass {\n" + + "\tchar* name;\n" + + "\tint age;\n" + + "\tint add(int a, int b);\n" + + "};\n"; + assertEquals(expect, classContent); + } + } + + @Test + void getClassContent2() { + ClassObj co = new ClassObj(); + co.setName("TestClass"); + List hList = new CopyOnWriteArrayList<>(); + hList.add("IPerson"); + co.setHeritageNameList(hList); + + ParamObj pa = new ParamObj(); + pa.setName("name"); + pa.setType("string"); + pa.setQualifier("public"); + co.addParam(pa); + ParamObj pa1 = new ParamObj(); + pa1.setName("age"); + pa1.setType("number"); + pa1.setQualifier("private"); + co.addParam(pa1); + ParamObj pa2 = new ParamObj(); + pa2.setName("no"); + pa2.setType("string"); + pa2.setQualifier("protected"); + co.addParam(pa2); + ParamObj pa3 = new ParamObj(); + pa3.setName("addr"); + pa3.setType("string"); + pa3.setQualifier("readonly"); + co.addParam(pa3); + + List poList = new CopyOnWriteArrayList<>(); + co.addFunc("constructor", "", poList); + + 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 TestClass : public IPerson {\n" + + "\tpublic char* name;\n" + + "\tprivate int age;\n" + + "\tprotected char* no;\n" + + "\treadonly char* addr;\n" + + "\tconstructor();\n" + + "};\n"; + assertEquals(expect, classContent); + } + } + + @Test + void getFuncContent() { + FuncObj fo = new FuncObj(); + fo.setName("TestFunc"); + fo.setRetValue("void"); + fo.addParam("name", "string"); + fo.addParam("age", "number"); + 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 = "\nvoid TestFunc(char* name, int age);"; + assertEquals(expect, funcContent); + } + } + + @Test + void getStructContent() { + StructObj so = new StructObj(); + so.setName("TestStruct"); + + so.addMember("name", "string"); + so.addMember("age", "boolean"); + + List poList = new CopyOnWriteArrayList<>(); + ParamObj poItem = new ParamObj(); + poItem.setName("a"); + poItem.setType("boolean"); + poList.add(poItem); + ParamObj poItem2 = new ParamObj(); + poItem2.setName("b"); + poItem2.setType("boolean"); + 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 = "\nstruct TestStruct {\n" + + "\tchar* name;\n" + + "\tbool age;\n" + + "\tint add(bool a, bool b);\n" + + "};\n"; + assertEquals(expect, structContent); + } + } + + @Test + void getTypeContent() { + } + + @Test + void getUnionContent() { + UnionObj uo = new UnionObj(); + uo.setName("TestUnion"); + + uo.addMember("name", "string"); + uo.addMember("age", "number"); + + 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 = "\nunion TestUnion{\n" + + "\tchar* name;\n" + + "\tint age;\n" + + "};\n"; + assertEquals(expect, unionContent); + } + } + + @Test + void getConstContent() { + ParseObj po = new ParseObj(); + ParamObj pao = new ParamObj(); + pao.setName("TestParam"); + pao.setType("int"); + pao.setStrValue("100"); + List pol = new CopyOnWriteArrayList<>(); + pol.add(pao); + po.setVarList(pol); + + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(pol); + + if (gb instanceof GenCppFile gdf) { + String varContent = gdf.getConstContent(); + System.out.println("genVar: " + varContent); + String expect = "\nconst int TestParam = 100;\n"; + assertEquals(expect, varContent); + } + } + + @Test + void genContent() { + ParseObj po = new ParseObj(); + ParamObj pao = new ParamObj(); + pao.setName("TestParam"); + pao.setType("int"); + pao.setStrValue("100"); + List pol = new CopyOnWriteArrayList<>(); + pol.add(pao); + po.setVarList(pol); + + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genContent(po); + + if (gb instanceof GenCppFile gdf) { + String varContent = gdf.getConstContent(); + System.out.println("genVar: " + varContent); + String expect = "\nconst int TestParam = 100;\n"; + assertEquals(expect, varContent); + } + } + + @Test + void genFile() { + ParamObj pao = new ParamObj(); + pao.setName("TestParam"); + pao.setType("int"); + pao.setStrValue("100"); + List pol = new CopyOnWriteArrayList<>(); + pol.add(pao); + + ParseObj po = new ParseObj(); + po.setVarList(pol); + + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genContent(po); + gb.genFile("./", "testGenFile.d.ts"); + + File file = new File("./ag_testGenFile_d_ts.h"); + assertEquals(true, file.exists()); + assertEquals(false, file.isDirectory()); + + List fcList = readText("./ag_testGenFile_d_ts.h"); + + assertEquals("// Generated from ./\\testGenFile.d.ts by KaiHong ohgen 1.0.0-PLUGIN", + fcList.get(0)); + assertEquals("const int TestParam = 100;", + fcList.get(1)); + + if (gb instanceof GenCppFile gdf) { + String varContent = gdf.getConstContent(); + System.out.println("genVar: " + varContent); + String expect = "\nconst int TestParam = 100;\n"; + assertEquals(expect, varContent); + } + } + + @Test + void genInterfaceList() { + } + + @Test + void genEnumList() { + EnumObj eo = new EnumObj(); + eo.setName("TestEnum"); + List ml = new CopyOnWriteArrayList<>(); + ml.add("ONE"); + ml.add("TWO"); + eo.setMemberList(ml); + List vl = new CopyOnWriteArrayList<>(); + vl.add("1"); + vl.add("2"); + eo.setValueList(vl); + List eol = new CopyOnWriteArrayList<>(); + eol.add(eo); + ParseObj po = new ParseObj(); + po.setEnumList(eol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genEnumList(po.getEnumList()); + + if (gb instanceof GenCppFile gdf) { + String enumContent = gdf.getEnumContent(); + System.out.println("genEnum: " + enumContent); + String expect = "\nenum TestEnum {\n" + + "\tONE = 1,\n" + + "\tTWO = 2,\n" + + "};\n"; + assertEquals(expect, enumContent); + } + } + + @Test + void genClassList() { + ClassObj co = new ClassObj(); + co.setName("TestClass"); + + co.addParam("name", "string"); + co.addParam("age", "number"); + + List poList = new CopyOnWriteArrayList<>(); + ParamObj poItem = new ParamObj(); + poItem.setName("a"); + poItem.setType("number"); + poList.add(poItem); + ParamObj poItem2 = new ParamObj(); + poItem2.setName("b"); + poItem2.setType("number"); + poList.add(poItem2); + + co.addFunc("add", "number", poList); + + poList = new CopyOnWriteArrayList<>(); + poItem = new ParamObj(); + poItem.setType("number"); + poList.add(poItem); + + co.addFunc("delete", "number", poList); + + 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 TestClass {\n" + + "\tchar* name;\n" + + "\tint age;\n" + + "\tint add(int a, int b);\n" + + "\tint delete(int);\n" + + "};\n"; + assertEquals(expect, classContent); + } + } + + @Test + void genFuncList() { + FuncObj fo = new FuncObj(); + fo.setName("TestFunc"); + fo.setRetValue("void"); + fo.addParam("name", "string"); + fo.addParam("age", "number"); + 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 = "\nvoid TestFunc(char* name, int age);"; + assertEquals(expect, funcContent); + } + } + + @Test + void genStructList() { + StructObj so = new StructObj(); + so.setName("TestStruct"); + + so.addMember("name", "string"); + so.addMember("age", "number"); + + List poList = new CopyOnWriteArrayList<>(); + ParamObj poItem = new ParamObj(); + poItem.setName("a"); + poItem.setType("int"); + poList.add(poItem); + ParamObj poItem2 = new ParamObj(); + poItem2.setName("b"); + poItem2.setType("int"); + poList.add(poItem2); + + so.addFunc("add", "int", 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" + + "\tchar* name;\n" + + "\tint age;\n" + + "\tint add(int a, int b);\n" + + "};\n"; + assertEquals(expect, structContent); + } + } + + @Test + void genTypeList() { + } + + @Test + void genUnionList() { + UnionObj uo = new UnionObj(); + uo.setName("TestUnion"); + + uo.addMember("name", "any"); + uo.addMember("age", "number"); + + 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 = "\nunion TestUnion{\n" + + "\tauto name;\n" + + "\tint age;\n" + + "};\n"; + assertEquals(expect, unionContent); + } + } + + @Test + void genVarList() { + ParseObj po = new ParseObj(); + ParamObj pao = new ParamObj(); + pao.setName("TestParam"); + pao.setType("number"); + pao.setStrValue("100"); + List pol = new CopyOnWriteArrayList<>(); + pol.add(pao); + po.setVarList(pol); + + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(pol); + + if (gb instanceof GenCppFile gdf) { + String varContent = gdf.getConstContent(); + System.out.println("genVar: " + varContent); + String expect = "\nconst int TestParam = 100;\n"; + assertEquals(expect, varContent); + } + } +} \ No newline at end of file diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenDtsFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenDtsFileTest.java index d4a7629359de93a914465d890ad5f1ac6a4c13ea..64bb25175879dcffbf0d7f228db7a99b82f079b7 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenDtsFileTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenDtsFileTest.java @@ -109,7 +109,7 @@ class GenDtsFileTest { } @Test - void genEnumList() { + void genEnumList1() { ParseObj po = new ParseObj(); EnumObj eo = new EnumObj(); eo.setName("TestEnum"); @@ -134,6 +134,39 @@ class GenDtsFileTest { } } + @Test + void genEnumList2() { + EnumObj eo = new EnumObj(); + eo.setName("Colors"); + List ml = new CopyOnWriteArrayList<>(); + ml.add("Red"); + ml.add("Green"); + ml.add("Blue"); + eo.setMemberList(ml); + List vl = new CopyOnWriteArrayList<>(); + vl.add("RED"); + vl.add("GREEN"); + vl.add("BLUE"); + eo.setValueList(vl); + List eol = new CopyOnWriteArrayList<>(); + eol.add(eo); + ParseObj po = new ParseObj(); + po.setEnumList(eol); + GeneratorBase gb = GenerateFactory.getGenerator("DTS"); + gb.genEnumList(po.getEnumList()); + + if (gb instanceof GenDtsFile gdf) { + String enumContent = gdf.getEnumContent(); + System.out.println("genEnum: " + enumContent); + String expect = "\nexport enum Colors {\n" + + "\tRed = RED,\n" + + "\tGreen = GREEN,\n" + + "\tBlue = BLUE,\n" + + "};\n"; + assertEquals(expect, enumContent); + } + } + @Test void genClassList() { ClassObj co = new ClassObj(); @@ -176,40 +209,24 @@ class GenDtsFileTest { @Test void genFuncList() { - ClassObj co = new ClassObj(); - co.setName("TestClass"); + FuncObj fo = new FuncObj(); + fo.setName("TestFunc"); - co.addParam("name", "char*"); - co.addParam("age", "int"); + fo.addParam("name", "char*"); + fo.addParam("age", "int"); - List poList = new CopyOnWriteArrayList<>(); - ParamObj poItem = new ParamObj(); - poItem.setName("a"); - poItem.setType("int"); - poList.add(poItem); - ParamObj poItem2 = new ParamObj(); - poItem2.setName("b"); - poItem2.setType("int"); - poList.add(poItem2); - - co.addFunc("add", "int", poList); - - List col = new CopyOnWriteArrayList<>(); - col.add(co); + List fol = new CopyOnWriteArrayList<>(); + fol.add(fo); ParseObj po = new ParseObj(); - po.setClassList(col); + po.setFuncList(fol); GeneratorBase gb = GenerateFactory.getGenerator("DTS"); - gb.genClassList(po.getClassList()); + gb.genFuncList(po.getFuncList()); if (gb instanceof GenDtsFile gdf) { - String classContent = gdf.getClassContent(); - System.out.println("genClass: " + classContent); - String expect = "\nexport class TestClass {\n" + - "\tname: string;\n" + - "\tage: number;\n" + - "\tadd(a: number, b: number) : number;\n" + - "};\n"; - assertEquals(expect, classContent); + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nexport function TestFunc(name: string, age: number) : void;"; + assertEquals(expect, funcContent); } }