From 68a5673a235da2b096c59335183eb5cac41e285c Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 15 Apr 2025 15:20:50 +0800 Subject: [PATCH 01/14] delete cpp file test Signed-off-by: sunlian --- .../src/test/java/gen/GenCppFileTest.java | 1288 ----------------- 1 file changed, 1288 deletions(-) delete mode 100644 src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java deleted file mode 100644 index 565240fa..00000000 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppFileTest.java +++ /dev/null @@ -1,1288 +0,0 @@ -/* - * 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 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"); - 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 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"); - - 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 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 getUnionContent1() { - 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 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 getVarContent1() { - ParamObj paObj = new ParamObj(); - paObj.setName("employeeName"); - paObj.setStrValue("\"John\""); - - List pol = new CopyOnWriteArrayList<>(); - pol.add(paObj); - ParseObj po = new ParseObj(); - po.setVarList(pol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); - gb.genVarList(po.getVarList()); - - if (gb instanceof GenCppFile gdf) { - String constContent = gdf.getConstContent(); - System.out.println("getVar: " + constContent); - String expect = "\nextends const auto employeeName = \"John\";\n"; - assertEquals(expect, constContent); - } - } - - @Test - void getVarContent2() { - ParamObj paObj = new ParamObj(); - paObj.setName("employeeName"); - paObj.setType("string"); - paObj.setStrValue("\"John\""); - - List pol = new CopyOnWriteArrayList<>(); - pol.add(paObj); - ParseObj po = new ParseObj(); - po.setVarList(pol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); - gb.genVarList(po.getVarList()); - - if (gb instanceof GenCppFile gdf) { - String constContent = gdf.getConstContent(); - System.out.println("getVar: " + constContent); - String expect = "\nextends const char* employeeName = \"John\";\n"; - assertEquals(expect, constContent); - } - } - - @Test - void getVarContent3() { - ParamObj paObj = new ParamObj(); - paObj.setName("num1"); - paObj.setType("number"); - paObj.setStrValue("1"); - - List pol = new CopyOnWriteArrayList<>(); - pol.add(paObj); - ParseObj po = new ParseObj(); - po.setVarList(pol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); - gb.genVarList(po.getVarList()); - - if (gb instanceof GenCppFile gdf) { - String constContent = gdf.getConstContent(); - System.out.println("getVar: " + constContent); - String expect = "\nextends const int num1 = 1;\n"; - assertEquals(expect, constContent); - } - } - - @Test - void getVarContent4() { - ParamObj paObj = new ParamObj(); - paObj.setName("playerCodes"); - - ParamObj paItem1 = new ParamObj(); - paItem1.setName("player1"); - paItem1.setStrValue("9"); - paObj.addParam(paItem1); - ParamObj paItem2 = new ParamObj(); - paItem2.setName("player2"); - paItem2.setStrValue("10"); - paObj.addParam(paItem2); - - List pol = new CopyOnWriteArrayList<>(); - pol.add(paObj); - ParseObj po = new ParseObj(); - po.setVarList(pol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); - gb.genVarList(po.getVarList()); - - if (gb instanceof GenCppFile gdf) { - String constContent = gdf.getConstContent(); - System.out.println("getVar: " + constContent); - String expect = "\nextends const std::map pol = new CopyOnWriteArrayList<>(); - pol.add(paObj); - ParseObj po = new ParseObj(); - po.setVarList(pol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); - gb.genVarList(po.getVarList()); - - if (gb instanceof GenCppFile gdf) { - String constContent = gdf.getConstContent(); - System.out.println("getVar: " + constContent); - String expect = "\nextends const auto playerCodes.player2 = 11;\n"; - assertEquals(expect, constContent); - } - } - - @Test - void getVarContent6() { - ParamObj paObj = new ParamObj(); - paObj.setName("ROUTES"); - paObj.setType("any[]"); - - ParamObj paListItem1 = new ParamObj(); - ParamObj paItem1 = new ParamObj(); - paItem1.setName("path"); - paItem1.setStrValue("'/dashboard'"); - paListItem1.addParam(paItem1); - - ParamObj paItem3 = new ParamObj(); - paItem3.setName("allowAnonymous"); - paItem3.setStrValue("false"); - paListItem1.addParam(paItem3); - paObj.addParam(paListItem1); - - ParamObj paListItem2 = new ParamObj(); - ParamObj paItem21 = new ParamObj(); - paItem21.setName("path"); - paItem21.setStrValue("'/deals'"); - paListItem2.addParam(paItem21); - - ParamObj paItem23 = new ParamObj(); - paItem23.setName("allowAnonymous"); - paItem23.setStrValue("true"); - paListItem2.addParam(paItem23); - paObj.addParam(paListItem2); - - List pol = new CopyOnWriteArrayList<>(); - pol.add(paObj); - ParseObj po = new ParseObj(); - po.setVarList(pol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); - gb.genVarList(po.getVarList()); - - if (gb instanceof GenCppFile gdf) { - String constContent = gdf.getConstContent(); - System.out.println("getVar: " + constContent); - String expect = "\nstruct ROUTESST {\n" + - "\tstd::string path;\n" + - "\tboolean allowAnonymous;\n" + - "};\n" + - "\n" + - "const std::vector ROUTES = {\n" + - "\t{'/dashboard', false},\n" + - "\t{'/deals', true},\n" + - "};\n"; - assertEquals(expect, constContent); - } - } - - @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 = "\nextends const 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 = "\nextends const 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 = "\nextends const 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() { - TypeObj to = new TypeObj(); - } - - @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 = "\nextends const int TestParam = 100;\n"; - assertEquals(expect, varContent); - } - } -} \ No newline at end of file -- Gitee From 90afb513b46425149b2b2253eb51e0a0d4966e8e Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 15 Apr 2025 15:21:35 +0800 Subject: [PATCH 02/14] mod gen cpp file Signed-off-by: sunlian --- src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 ada58cbe..029f407e 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppFile.java @@ -297,7 +297,9 @@ public class GenCppFile extends GeneratorBase { String outFileName = filePath + File.separator + CPP_FILE_PREFIX + fileName.replace(".", "_") + CPP_FILE_H_SUFFIX; System.out.println("outFileName : " + outFileName); - + if (this.genMode.equals(GeneratorBase.GEN_REPLACE)) { + FileUtils.deleteFile(outFileName); + } FileUtils.createFile(outFileName); FileUtils.appendText(outFileName, this.genFileHeader(filePath + File.separator + fileName)); -- Gitee From d5e3e53386b49f8fcf42f5f686067d95f65a079d Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 15 Apr 2025 15:21:55 +0800 Subject: [PATCH 03/14] mod gen dts file Signed-off-by: sunlian --- src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java index 2354fd2d..cb744412 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenDtsFile.java @@ -283,7 +283,9 @@ public class GenDtsFile extends GeneratorBase { String outFileName = filePath + File.separator + TS_FILE_PREFIX + fileName.replace(".", "_") + TS_FILE_SUFFIX; System.out.println("outFileName : " + outFileName); - + if (this.genMode.equals(GeneratorBase.GEN_REPLACE)) { + FileUtils.deleteFile(outFileName); + } FileUtils.createFile(outFileName); FileUtils.appendText(outFileName, this.genFileHeader(filePath + File.separator + fileName)); -- Gitee From 0890af83629a9f0e5c1020bef2e8cb31fe96bde8 Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 15 Apr 2025 15:22:20 +0800 Subject: [PATCH 04/14] mod gen base file Signed-off-by: sunlian --- .../ohosgen/src/main/java/gen/GeneratorBase.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java index 3129574f..cd6734f8 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java @@ -17,6 +17,7 @@ package gen; import grammar.*; import utils.Constants; +import utils.FileUtils; import java.util.List; @@ -31,6 +32,13 @@ import java.util.List; */ public class GeneratorBase { private final String headerFormat = "// Generated from %s by KaiHong ohgen %s-PLUGIN"; + public static final String GEN_APPEND = "APPEND"; + public static final String GEN_REPLACE = "REPLACE"; + private final String GEN_NEW = "NEW"; + + protected String genFileName = ""; + protected String genMode = GEN_REPLACE; + /** * 生成文件头内容 @@ -63,6 +71,10 @@ public class GeneratorBase { System.out.println("GeneratorBase: path is " + filePath + ", file is " + fileName); } + public void setGenMode(String genMode) { + this.genMode = genMode; + } + /** * 生成接口 * -- Gitee From ce1b81fa0d985bd6330a6e7c085f3eccaf440873 Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 15 Apr 2025 15:23:06 +0800 Subject: [PATCH 05/14] mod file utils Signed-off-by: sunlian --- src/intellij_plugin/ohosgen/src/main/java/utils/FileUtils.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/intellij_plugin/ohosgen/src/main/java/utils/FileUtils.java b/src/intellij_plugin/ohosgen/src/main/java/utils/FileUtils.java index f0a48213..6b3bfe41 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/utils/FileUtils.java +++ b/src/intellij_plugin/ohosgen/src/main/java/utils/FileUtils.java @@ -18,6 +18,8 @@ package utils; import java.io.File; import java.io.IOException; import java.io.*; +import java.nio.file.Files; +import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.List; -- Gitee From 604f647ed2767af9393c880087e090fbf966921a Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 15 Apr 2025 15:24:15 +0800 Subject: [PATCH 06/14] gen aki cpp Signed-off-by: sunlian --- .../src/main/java/gen/GenAkiCppFile.java | 853 ++++++++++++++++++ 1 file changed, 853 insertions(+) create mode 100644 src/intellij_plugin/ohosgen/src/main/java/gen/GenAkiCppFile.java diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenAkiCppFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenAkiCppFile.java new file mode 100644 index 00000000..10e6332b --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenAkiCppFile.java @@ -0,0 +1,853 @@ +/* + * 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 utils.FileUtils; +import utils.StringUtils; + +import java.io.File; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +/** + *

类名:该类用于xxx

+ * description ${description} + * + * @author ${USER} + * date 2025-02-28 + * @since 2025-02-28 + * @version 1.0 + */ +public class GenAkiCppFile extends GeneratorBase { + private static final String AKI_ENUM_TOKEN = "enum"; + private static final String AKI_CLASS_TOKEN = "class"; + private static final String AKI_STRUCT_TOKEN = "struct"; + private static final String AKI_UNION_TOKEN = "union"; + private static final String AKI_TEMPLATE_TOKEN = "template"; + private static final String AKI_TYPE_NAME_TOKEN = "typename"; + private static final String AKI_STAR_TOKEN = "*"; + private static final String AKI_CHAR_START_TOKEN = "char*"; + private static final String AKI_AUTO_TOKEN = "auto"; + private static final String AKI_EXPORT_TOKEN = "export"; + private static final String AKI_IMPLEMENAKI_TOKEN = "implements"; + private static final String AKI_EXTENDS_TOKEN = "extends"; + private static final String AKI_CONST_TOKEN = "const"; + private static final String AKI_PRIVATE_TOKEN = "private"; + private static final String AKI_PUBLIC_TOKEN = "public"; + private static final String AKI_INTERFACE_TOKEN = "interface"; + private static final String AKI_PROTECTED_TOKEN = "protected"; + private static final String AKI_STATIC_TOKEN = "static"; + private static final String AKI_ANY_TOKEN = "any"; + private static final String AKI_NUMBER_TOKEN = "number"; + private static final String AKI_NEVER_TOKEN = "never"; + private static final String AKI_BOOLEAN_TOKEN = "boolean"; + private static final String AKI_STRING_TOKEN = "string"; + private static final String AKI_UNIQUE_TOKEN = "unique"; + private static final String AKI_SYMBOL_TOKEN = "symbol"; + private static final String AKI_UNDEFINED_TOKEN = "undefined"; + private static final String AKI_OBJECT_TOKEN = "object"; + private static final String AKI_OF_TOKEN = "of"; + private static final String AKI_KEYOF_TOKEN = "keyof"; + private static final String AKI_TYPE_TOKEN = "type"; + private static final String AKI_CONSTRUCTOR_TOKEN = "constructor"; + private static final String AKI_NAMESPACE_TOKEN = "namespace"; + private static final String AKI_REQUIRE_TOKEN = "require"; + private static final String AKI_MODULE_TOKEN = "module"; + private static final String AKI_DECLARE_TOKEN = "declare"; + private static final String AKI_ABSTRACT_TOKEN = "abstract"; + private static final String AKI_DEBUGGER_TOKEN = "debugger"; + private static final String AKI_FUNCTION_TOKEN = "function"; + private static final String AKI_THIS_TOKEN = "this"; + private static final String AKI_WITH_TOKEN = "with"; + private static final String AKI_DEFAULT_TOKEN = "default"; + private static final String AKI_READONLY_TOKEN = "readonly"; + private static final String AKI_ASYNC_TOKEN = "async"; + private static final String AKI_AWAIT_TOKEN = "await"; + private static final String AKI_YIELD_TOKEN = "yield"; + private static final String AKI_ARROW_TOKEN = "=>"; + private static final String AKI_NEW_LINE = "\n"; + private static final String AKI_TAB_SPACE = "\t"; + private static final String AKI_BLANK_SPACE = " "; + private static final String AKI_SPLIT = " | "; + private static final String AKI_EQUAL = " = "; + private static final String AKI_COMMA = ","; + private static final String AKI_DOUBLE_QUOTATION = "\""; + private static final String AKI_UNDER_LINE = "_"; + private static final String AKI_SEMICOLON = ";"; + private static final String AKI_COLON = ":"; + private static final String AKI_ELLIPSIS = "..."; + private static final String AKI_DOT = "."; + private static final String AKI_LEFT_BRACE = "{"; + private static final String AKI_RIGHT_BRACE = "}"; + private static final String AKI_LEFT_PARENTHESES = "("; + private static final String AKI_RIGHT_PARENTHESES = ")"; + private static final String AKI_LEFT_SQUARE_BRACKET = "["; + private static final String AKI_RIGHT_SQUARE_BRACKET = "]"; + private static final String AKI_LEFT_ANGLE_BRACKET = "<"; + private static final String AKI_RIGHT_ANGLE_BRACKET = ">"; + + private static final String AKI_STD_STRING = "std::string"; + private static final String AKI_STD_VECTOR = "std::vector"; + private static final String AKI_STD_LIST = "std::list"; + private static final String AKI_STD_ARRAY = "std::array"; + private static final String AKI_STD_STACK = "std::stack"; + private static final String AKI_STD_QUEUE = "std::queue"; + private static final String AKI_STD_PAIR = "std::pair"; + private static final String AKI_STD_MAP = "std::map"; + private static final String AKI_STD_SET = "std::set"; + private static final String AKI_STD_DEQUE = "std::deque"; + private static final String AKI_STD_MULTIMAP = "std::multimap"; + private static final String AKI_STD_MULTISET = "std::multiset"; + + private static final String AKI_RET_EXPRESS = "AKI_RET_EXPRESS"; + private static final String AKI_ARGUMENT_EXPRESS = "AKI_ARGUMENT_EXPRESS"; + private static final String AKI_CALLBACK_DECLARE = "aki::SafetyCallback "; + + private static final String AKI_STR_SUFFIX = "STR"; + private static final String AKI_FILE_PREFIX = "ag_aki"; + private static final String AKI_FILE_H_SUFFIX = ".h"; + private static final String AKI_FILE_AKI_SUFFIX = ".cpp"; + private static final String AKI_FILE_C_SUFFIX = ".c"; + private static final String AKI_STRUCT_SUFFIX = "ST"; + + + private static final String AKI_ENUM_NAME = "AKI_ENUM_NAME"; + private static final String AKI_ENUM_ITEM_NANE = "AKI_ENUM_ITEM_NANE"; + private static final String AKI_ENUM_ITEM_DECLARE = "\n\tJSBIND_ENUM_VALUE(AKI_ENUM_ITEM_NANE);"; + private static final String AKI_ENUM_EXPRESSION = "JSBIND_ENUM_EXPRESSION"; + private static final String AKI_ENUM_DECLARE = "\nJSBIND_ENUM(AKI_ENUM_NAME) {" + + "JSBIND_ENUM_EXPRESSION\n" + + "};\n"; + + private static final String AKI_CLASS_NAME = "AKI_CLASS_NAME"; + private static final String AKI_CONSTRUCTOR_EXPRESSION = "AKI_CONSTRUCTOR_EXPRESSION"; + private static final String AKI_CONSTRUCTOR_DECLARE = "\n\tJSBIND_CONSTRUCTOR();"; + private static final String AKI_CONSTRUCTOR_PARAMS = "AKI_CONSTRUCTOR_PARAMS"; + private static final String AKI_METHOD_DECLARE = "\n\tJSBIND_METHOD(AKI_METHOD_NAME, \"AKI_METHOD_NAME\");" + + "\n\tJSBIND_PMETHOD(AKI_METHOD_NAME, \"AKI_METHOD_NAMEPromise\");"; + private static final String AKI_METHOD_EXPRESSION = "AKI_METHOD_EXPRESSION"; + private static final String AKI_METHOD_NAME = "AKI_METHOD_NAME"; + private static final String AKI_PMETHOD_DECLARE = "\n\tJSBIND_PMETHOD(AKI_PMETHOD_NAME);"; + private static final String AKI_PMETHOD_EXPRESSION = "AKI_PMETHOD_EXPRESSION"; + private static final String AKI_PMETHOD_NAME = "AKI_PMETHOD_NAME"; + private static final String AKI_PROPERTY_DECLARE = "\n\tJSBIND_PROPERTY(AKI_PROPERTY_NAME);"; + private static final String AKI_PROPERTY_EXPRESSION = "AKI_PROPERTY_EXPRESSION"; + private static final String AKI_PROPERTY_NAME = "AKI_PROPERTY_NAME"; + private static final String AKI_CLASS_DECLARE = "\nJSBIND_CLASS(AKI_CLASS_NAME)\n" + + "{" + + "AKI_CONSTRUCTOR_EXPRESSION" + + "AKI_METHOD_EXPRESSION" + + "AKI_PMETHOD_EXPRESSION" + + "AKI_PROPERTY_EXPRESSION" + + "\n};\n"; + + private static final String AKI_FUNCTION_ITEM_DECLARE = + "\n\tJSBIND_FUNCTION(AKI_FUNCTION_NAME, \"AKI_FUNCTION_NAME\");" + + "\n\tJSBIND_PFUNCTION(AKI_FUNCTION_NAME, \"AKI_FUNCTION_NAMEPromise\");"; + private static final String AKI_FUNCTION_EXPRESSION = "AKI_FUNCTION_EXPRESSION"; + private static final String AKI_FUNCTION_NAME = "AKI_FUNCTION_NAME"; + private static final String AKI_FUNCTION_DECLARE = "\nJSBIND_GLOBAL()\n" + + "{" + + "AKI_FUNCTION_EXPRESSION\n" + + "};\n"; + + private static final String AKI_ADDON_NAME = "AKI_ADDON_NAME"; + private static final String AKI_ADDON_DECLARE = "\n#include \n" + + "#include \n" + + "\n" + + "JSBIND_ADDON(AKI_ADDON_NAME)\n"; + + private String interfaceContent = ""; + private String enumContent = ""; + private String classContent = ""; + private String funcContent = ""; + private String structContent = ""; + private String typeContent = ""; + private String unionContent = ""; + private String constContent = ""; + + private final Map ts2cppMap = Map.ofEntries( + Map.entry("any", "auto"), + Map.entry("boolean", "bool"), + Map.entry("string", "char*"), + Map.entry("number", "int"), + Map.entry("void", "void"), + Map.entry("[]", "*") + ); + + private final Map tsTokenMap = Map.ofEntries( + Map.entry("\"", ""), + Map.entry("*", ""), + Map.entry("&", ""), + Map.entry("(", ""), + Map.entry(")", "") + ); + + /** + * 构造函数 + */ + GenAkiCppFile() { + + } + + /** + * 将 cpp key 转换成 ts key + * + * @param cppKey 枚举对象列表 + * @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 && value.contains(AKI_STAR_TOKEN)) { + return cppKey.substring(0, ret) + value; + } else if (ret >= 0) { + return value; + } + } + return retKey; + } + + /** + * 将cpp token 替换成对应的dts token + * + * @param cppKey 语言关键字 + * @return 替换后字符串 + */ + private String replaceTsToken(String cppKey) { + String retKey = cppKey; + for (Map.Entry entry : tsTokenMap.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + int ret = retKey.indexOf(key); + if (ret >= 0) { + retKey = retKey.replace(key, value); + } + } + return retKey; + } + + /** + * 获得接口内容 + * + * @return 接口内容 + */ + public String getInterfaceContent() { + return interfaceContent; + } + + /** + * 获得枚举内容 + * + * @return 枚举内容 + */ + public String getEnumContent() { + return enumContent; + } + + /** + * 获得类内容 + * + * @return 类内容 + */ + public String getClassContent() { + return classContent; + } + + /** + * 获得方法内容 + * + * @return 方法内容 + */ + public String getFuncContent() { + return funcContent; + } + + /** + * 获得结构体内容 + * + * @return 结构体内容 + */ + public String getStructContent() { + return structContent; + } + + /** + * 获得type内容 + * + * @return type内容 + */ + public String getTypeContent() { + return typeContent; + } + + /** + * 获得联合体内容 + * + * @return 联合体内容 + */ + public String getUnionContent() { + return unionContent; + } + + /** + * 获得常量内容 + * + * @return 常量内容 + */ + public String getConstContent() { + return constContent; + } + + /** + * 生成输出内容 + * + * @param po 解析类 + */ + @Override + public void genContent(ParseObj po) { + genInterfaceList(po.getInterfaceList()); + genEnumList(po.getEnumList()); + genClassList(po.getClassList()); + genFuncList(po.getFuncList()); + genStructList(po.getStructList()); + genTypeList(po.getTypeList()); + genUnionList(po.getUnionList()); + genVarList(po.getVarList()); + } + + /** + * 生成文件 + * + * @param fileName 文件名 + * @param filePath 文件路径 + */ + @Override + public void genFile(String filePath, String fileName) { + System.out.println("genFile : " + filePath + fileName); + String outFileName = filePath + File.separator + AKI_FILE_PREFIX + + fileName.replace(".", "_") + AKI_FILE_AKI_SUFFIX; + this.genFileName = outFileName; + System.out.println("outFileName : " + outFileName); + + if (this.genMode.equals(GeneratorBase.GEN_REPLACE)) { + FileUtils.deleteFile(outFileName); + } + FileUtils.createFile(outFileName); + + FileUtils.appendText(outFileName, this.genFileHeader(filePath + File.separator + fileName)); + FileUtils.appendText(outFileName, + AKI_ADDON_DECLARE.replace(AKI_ADDON_NAME, fileName.replace(".", ""))); + FileUtils.appendText(outFileName, this.constContent); + FileUtils.appendText(outFileName, this.enumContent); + FileUtils.appendText(outFileName, this.typeContent); + FileUtils.appendText(outFileName, this.interfaceContent); + FileUtils.appendText(outFileName, this.unionContent); + FileUtils.appendText(outFileName, this.funcContent); + FileUtils.appendText(outFileName, this.structContent); + FileUtils.appendText(outFileName, this.classContent); + + } + + /** + * 生成输出内容 + * + * @param iol 接口列表 + */ + @Override + public void genInterfaceList(List iol) { + System.out.println("genInterfaceList" + iol.toString()); + }; + + private String genCppEnumContent(EnumObj eo) { + String resContent = ""; + String enumName = eo.getName(); + enumName = enumName != null && !enumName.isEmpty() ? enumName : eo.getAlias(); + List memList = eo.getMemberList(); + List vaList = eo.getValueList(); + int i = 0; + resContent += AKI_NEW_LINE + AKI_ENUM_TOKEN + + AKI_BLANK_SPACE + enumName + AKI_BLANK_SPACE + AKI_LEFT_BRACE; + for (String memItem : memList) { + resContent += AKI_NEW_LINE + AKI_TAB_SPACE + memItem; + if (vaList.size() > i && !vaList.get(i).isEmpty()) { + resContent += AKI_EQUAL + replaceTsToken(vaList.get(i)) + AKI_COMMA; + } else { + resContent += AKI_COMMA; + } + i++; + } + + resContent = StringUtils.removeLastSpace(resContent); + resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE + AKI_SEMICOLON + AKI_NEW_LINE; + + i = 0; + if (vaList.size() > i && !vaList.get(i).isEmpty() && + vaList.get(i).contains("\"")) { + resContent += AKI_NEW_LINE + AKI_CHAR_START_TOKEN + AKI_BLANK_SPACE + + enumName.toLowerCase(Locale.ROOT) + AKI_UNDER_LINE + AKI_STR_SUFFIX + + AKI_LEFT_SQUARE_BRACKET + AKI_RIGHT_SQUARE_BRACKET + AKI_EQUAL + AKI_LEFT_BRACE; + for (String val : vaList) { + resContent += AKI_NEW_LINE + AKI_TAB_SPACE + AKI_LEFT_SQUARE_BRACKET + + memList.get(i) + AKI_RIGHT_SQUARE_BRACKET + AKI_EQUAL + val + AKI_COMMA; + i++; + } + resContent = StringUtils.removeLastCharacter(resContent, 1); + resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE + AKI_SEMICOLON + AKI_NEW_LINE; + + } + return resContent; + } + + private String genAkiEnumContent(EnumObj eo) { + String resContent = ""; + String enumName = eo.getName(); + enumName = !enumName.isEmpty() ? enumName : eo.getAlias(); + List memList = eo.getMemberList(); + + String enumDeclare = AKI_ENUM_DECLARE.replace(AKI_ENUM_NAME, enumName); + String enumItemDeclare = ""; + for (String memItem : memList) { + enumItemDeclare += AKI_ENUM_ITEM_DECLARE.replace(AKI_ENUM_ITEM_NANE, memItem); + } + + resContent = enumDeclare.replace(AKI_ENUM_EXPRESSION, enumItemDeclare); + return resContent; + } + + /** + * 生成输出内容 + * + * @param eol 枚举列表 + */ + @Override + public void genEnumList(List eol) { + System.out.println("genEnumList" + eol.toString()); + + String resContent = ""; + for (EnumObj eo : eol) { + resContent += genCppEnumContent(eo); + resContent += genAkiEnumContent(eo); + } + + this.enumContent = resContent; + }; + + private String setClassFunc(List funcList, String content) { + String tempResContent = content; + for (FuncObj funcItem : funcList) { + String retValue = funcItem.getRetValue(); + retValue = retValue.isEmpty() ? "" : ts2CppKey(retValue) + AKI_BLANK_SPACE; + tempResContent += AKI_NEW_LINE + AKI_TAB_SPACE + retValue + + replaceTsToken(funcItem.getName()) + AKI_LEFT_PARENTHESES; + List pol = funcItem.getParamList(); + for (ParamObj poItem : pol) { + String retType = ts2CppKey(poItem.getType()).isEmpty() ? + AKI_AUTO_TOKEN : ts2CppKey(poItem.getType()); + tempResContent += (poItem.getName() == null) ? retType + AKI_COMMA + AKI_BLANK_SPACE : + retType + AKI_BLANK_SPACE + replaceTsToken(poItem.getName()) + AKI_COMMA + AKI_BLANK_SPACE; + } + if (!pol.isEmpty()) { + tempResContent = StringUtils.removeLastCharacter(tempResContent, 2); + } + tempResContent += AKI_RIGHT_PARENTHESES + AKI_SEMICOLON; + } + return tempResContent; + } + + private String genCppClassContent(ClassObj co) { + String resContent = ""; + String className = co.getName(); + className = !className.isEmpty() ? className : co.getAlias(); + + String templateStr = !co.getTempList().isEmpty() ? + AKI_TEMPLATE_TOKEN + AKI_BLANK_SPACE + AKI_LEFT_ANGLE_BRACKET : ""; + for (String teStr : co.getTempList()) { + templateStr += AKI_TYPE_NAME_TOKEN + AKI_BLANK_SPACE + teStr + AKI_COMMA + AKI_BLANK_SPACE; + } + templateStr = templateStr.length() > 1 ? + StringUtils.removeLastCharacter(templateStr, 2) + AKI_RIGHT_ANGLE_BRACKET + AKI_BLANK_SPACE : ""; + + List hnList = co.getHeritageNameList(); + String htStr = hnList.size() > 0 ? AKI_BLANK_SPACE + AKI_COLON + AKI_BLANK_SPACE : ""; + for (String hName : hnList) { + htStr += AKI_PUBLIC_TOKEN + AKI_BLANK_SPACE + hName + AKI_COMMA + AKI_BLANK_SPACE; + } + htStr = htStr.length() > 1 ? StringUtils.removeLastCharacter(htStr, 2) : htStr; + + List htempList = co.getHeritageTemplateList(); + String htempStr = htempList.size() > 0 ? AKI_LEFT_ANGLE_BRACKET : ""; + for (String tempStr : htempList) { + htempStr += tempStr + AKI_COMMA + AKI_BLANK_SPACE; + } + htempStr = htempList.size() > 0 ? + StringUtils.removeLastCharacter(htempStr, 2) + AKI_RIGHT_ANGLE_BRACKET : ""; + resContent += AKI_NEW_LINE + templateStr + AKI_CLASS_TOKEN + + AKI_BLANK_SPACE + className + htStr + htempStr + AKI_BLANK_SPACE + AKI_LEFT_BRACE; + List paList = co.getParamList(); + for (ParamObj paItem : paList) { + String paType = paItem.getType(); + String qualifyStr = paItem.getQualifier() == null || paItem.getQualifier().isEmpty() ? + "" : paItem.getQualifier() + AKI_BLANK_SPACE; + resContent += AKI_NEW_LINE + AKI_TAB_SPACE + qualifyStr + ts2CppKey(paType) + + AKI_BLANK_SPACE + replaceTsToken(paItem.getName()); + List initVList = paItem.getvList(); + if (!initVList.isEmpty()) { + resContent += AKI_EQUAL + initVList.get(0) + AKI_SEMICOLON; + } else { + resContent += AKI_SEMICOLON; + } + } + + resContent = setClassFunc(co.getFuncList(), resContent); + resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE + AKI_SEMICOLON + AKI_NEW_LINE; + return resContent; + } + + private String genAkiCppClassContent(ClassObj co) { + String resContent = ""; + String className = co.getName(); + className = !className.isEmpty() ? className : co.getAlias(); + + String classDeclare = AKI_CLASS_DECLARE.replace(AKI_CLASS_NAME, className); + String classConstructDeclare = ""; + String classPropertyDeclare = ""; + String classFunctionDeclare = ""; + String classPFunctionDeclare = ""; + + List paList = co.getParamList(); + for (ParamObj paItem : paList) { + classPropertyDeclare += AKI_PROPERTY_DECLARE.replace(AKI_PROPERTY_NAME, paItem.getName()); + } + classDeclare = classDeclare.replace(AKI_PROPERTY_EXPRESSION, classPropertyDeclare); + + List foList = co.getFuncList(); + for (FuncObj foItem : foList) { + if (foItem.getName().equals("constructor")) { + String paramStr = ""; + for (ParamObj poItem : foItem.getParamList()) { + paramStr += poItem.getType() + AKI_COMMA + AKI_BLANK_SPACE; + } + paramStr = StringUtils.removeLastCharacter(paramStr, 2); + classConstructDeclare += AKI_CONSTRUCTOR_DECLARE.replace( + AKI_CONSTRUCTOR_PARAMS, paramStr); + } else { + classFunctionDeclare += AKI_METHOD_DECLARE.replace(AKI_METHOD_NAME, foItem.getName()); + } + } + classDeclare = classDeclare.replace(AKI_METHOD_EXPRESSION, classFunctionDeclare); + classDeclare = classDeclare.replace(AKI_CONSTRUCTOR_EXPRESSION, classConstructDeclare); + classDeclare = classDeclare.replace(AKI_PMETHOD_EXPRESSION, classPFunctionDeclare); + + resContent = classDeclare; + return resContent; + } + + /** + * 生成输出内容 + * + * @param col 类列表 + */ + @Override + public void genClassList(List col) { + System.out.println("genClassList" + col.toString()); + + String resContent = ""; + for (ClassObj co : col) { + resContent += genCppClassContent(co); + resContent += genAkiCppClassContent(co); + } + this.classContent = resContent; + }; + + private String genCppFuncContent(FuncObj fo) { + String resContent = ""; + String funcName = fo.getName(); + funcName = !funcName.isEmpty() ? funcName : fo.getAlias(); + List tempList = fo.getTempList(); + String tempStr = tempList.isEmpty() ? "" : AKI_TEMPLATE_TOKEN + AKI_LEFT_ANGLE_BRACKET; + for (String teStr : tempList) { + tempStr += AKI_TYPE_NAME_TOKEN + AKI_BLANK_SPACE + teStr + AKI_COMMA + AKI_BLANK_SPACE; + } + tempStr = tempList.isEmpty() ? "" : + StringUtils.removeLastCharacter(tempStr, 2) + AKI_RIGHT_ANGLE_BRACKET + AKI_BLANK_SPACE; + List paList = fo.getParamList(); + String retValue = ts2CppKey(fo.getRetValue()).isEmpty() ? + "" : ts2CppKey(fo.getRetValue()) + AKI_BLANK_SPACE; + resContent += AKI_NEW_LINE + tempStr + retValue + + replaceTsToken(funcName) + AKI_LEFT_PARENTHESES; + + for (ParamObj poItem : paList) { + String rawType = poItem.getType(); + String paType = ""; + System.out.println("rawType: " + rawType); + if (rawType != null && rawType.contains(AKI_ARROW_TOKEN)) { + FuncObj cbFoItem = poItem.getFoList().get(0); + String cbFoRetVal = cbFoItem.getRetValue(); + List pal = cbFoItem.getParamList(); + String cbFoParams = ""; + for (ParamObj cbFunPa : pal) { + cbFoParams += ts2CppKey(cbFunPa.getType()).isEmpty() ? AKI_AUTO_TOKEN : ts2CppKey(rawType); + cbFoParams += AKI_COMMA + AKI_BLANK_SPACE; + } + cbFoParams = StringUtils.removeLastCharacter(cbFoParams, 2); + paType = AKI_CALLBACK_DECLARE.replace(AKI_RET_EXPRESS, cbFoRetVal); + paType = paType.replace(AKI_ARGUMENT_EXPRESS, cbFoParams); + } else { + paType = ts2CppKey(rawType).isEmpty() ? + AKI_AUTO_TOKEN + AKI_BLANK_SPACE : ts2CppKey(rawType) + AKI_BLANK_SPACE; + } + + String paName = poItem.getName(); + String defaultVal = poItem.getStrValue(0); + defaultVal = defaultVal.isEmpty() ? "" : AKI_EQUAL + defaultVal; + resContent += !paName.isEmpty() ? paType + replaceTsToken(paName) + + defaultVal + AKI_COMMA + AKI_BLANK_SPACE : + paType + AKI_COMMA + AKI_BLANK_SPACE; + } + if (!paList.isEmpty()) { + resContent = StringUtils.removeLastCharacter(resContent, 2); + } + resContent += AKI_RIGHT_PARENTHESES + AKI_SEMICOLON + AKI_NEW_LINE; + return resContent; + } + + private String genAkiCppFuncContent(List fol) { + String resContent = ""; + + String funcItemDeclare = ""; + for (FuncObj fo : fol) { + String funcName = fo.getName(); + funcName = !funcName.isEmpty() ? funcName : fo.getAlias(); + funcItemDeclare += AKI_FUNCTION_ITEM_DECLARE.replace(AKI_FUNCTION_NAME, funcName); + } + String funcDeclare = AKI_FUNCTION_DECLARE.replace(AKI_FUNCTION_EXPRESSION, funcItemDeclare); + resContent += funcDeclare; + return resContent; + } + + /** + * 生成输出内容 + * + * @param fol 方法列表 + */ + @Override + public void genFuncList(List fol) { + System.out.println("genFuncList : " + fol.toString()); + String resContent = ""; + for (FuncObj fo : fol) { + resContent += genCppFuncContent(fo); + } + resContent += genAkiCppFuncContent(fol); + this.funcContent = resContent; + System.out.println("genFuncList : " + resContent); + }; + + /** + * 生成输出内容 + * + * @param sol 结构体列表 + */ + @Override + public void genStructList(List sol) { + System.out.println("genStructList" + sol.toString()); + + String resContent = ""; + for (StructObj so : sol) { + String structName = so.getName(); + structName = !structName.isEmpty() ? structName : so.getAlias(); + + String templateStr = !so.getTemplateList().isEmpty() ? + AKI_TEMPLATE_TOKEN + AKI_BLANK_SPACE + AKI_LEFT_ANGLE_BRACKET : ""; + for (String teStr : so.getTemplateList()) { + templateStr += AKI_TYPE_NAME_TOKEN + AKI_BLANK_SPACE + teStr + AKI_COMMA + AKI_BLANK_SPACE; + } + templateStr = templateStr.length() > 1 ? + StringUtils.removeLastCharacter(templateStr, 2) + AKI_RIGHT_ANGLE_BRACKET + AKI_BLANK_SPACE : ""; + + List paList = so.getMemberList(); + resContent += AKI_NEW_LINE + templateStr + AKI_STRUCT_TOKEN + + AKI_BLANK_SPACE + structName + AKI_BLANK_SPACE + AKI_LEFT_BRACE; + + for (ParamObj paItem : paList) { + String paType = paItem.getType().isEmpty() ? AKI_AUTO_TOKEN : paItem.getType(); + resContent += AKI_NEW_LINE + AKI_TAB_SPACE + ts2CppKey(paType) + + AKI_BLANK_SPACE + paItem.getName(); + ; + List initVList = paItem.getvList(); + if (initVList.size() > 0) { + resContent += AKI_EQUAL + initVList.get(0) + AKI_SEMICOLON; + } else { + resContent += AKI_SEMICOLON; + } + } + + List funcList = so.getFuncList(); + for (FuncObj funcItem : funcList) { + String retValue = ts2CppKey(funcItem.getRetValue()).isEmpty() ? "" : + ts2CppKey(funcItem.getRetValue()) + AKI_BLANK_SPACE; + resContent += AKI_NEW_LINE + AKI_TAB_SPACE + retValue + + replaceTsToken(funcItem.getName()) + AKI_LEFT_PARENTHESES; + List pol = funcItem.getParamList(); + for (ParamObj poItem : pol) { + String retType = ts2CppKey(poItem.getType()).isEmpty() ? + AKI_AUTO_TOKEN : ts2CppKey(poItem.getType()); + resContent += retType + AKI_BLANK_SPACE + replaceTsToken(poItem.getName()) + + AKI_COMMA + AKI_BLANK_SPACE; + } + resContent = !pol.isEmpty() ? StringUtils.removeLastCharacter(resContent, 2) : resContent; + resContent += AKI_RIGHT_PARENTHESES + AKI_SEMICOLON; + } + + resContent = StringUtils.removeLastSpace(resContent); + resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE + AKI_SEMICOLON + AKI_NEW_LINE; + } + this.structContent = resContent; + }; + + /** + * 生成输出内容 + * + * @param tol 类型列表 + */ + @Override + public void genTypeList(List tol) { + System.out.println("genTypeList : " + tol.toString()); + }; + + /** + * 生成输出内容 + * + * @param uol 联合体列表 + */ + @Override + public void genUnionList(List uol) { + System.out.println("genUnionList : " + uol.toString()); + + String resContent = ""; + for (UnionObj uo : uol) { + String unionName = uo.getName(); + unionName = !unionName.isEmpty() ? unionName : uo.getAlias(); + + String templateStr = !uo.getTemplateList().isEmpty() ? + AKI_TEMPLATE_TOKEN + AKI_BLANK_SPACE + AKI_LEFT_ANGLE_BRACKET : ""; + for (String teStr : uo.getTemplateList()) { + templateStr += AKI_TYPE_NAME_TOKEN + AKI_BLANK_SPACE + teStr + AKI_COMMA + AKI_BLANK_SPACE; + } + templateStr = templateStr.length() > 1 ? + StringUtils.removeLastCharacter(templateStr, 2) + AKI_RIGHT_ANGLE_BRACKET + AKI_BLANK_SPACE : ""; + + resContent += AKI_NEW_LINE + templateStr + AKI_UNION_TOKEN + + AKI_BLANK_SPACE + unionName + AKI_LEFT_BRACE; + + List paList = uo.getMemList(); + for (ParamObj paItem : paList) { + String paType = paItem.getType(); + String paName = paItem.getName(); + resContent += AKI_NEW_LINE + AKI_TAB_SPACE + ts2CppKey(paType) + + AKI_BLANK_SPACE + paName + AKI_SEMICOLON; + } + resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE; + resContent += AKI_SEMICOLON + AKI_NEW_LINE; + } + this.unionContent = resContent; + }; + + private String genVarArrayList(String tmpContent, String paName, List paList) { + String resContent = tmpContent; + resContent += AKI_NEW_LINE + AKI_STRUCT_TOKEN + AKI_BLANK_SPACE + paName + + AKI_STRUCT_SUFFIX + AKI_BLANK_SPACE + AKI_LEFT_BRACE; + List paramList = paList.get(0).getPaList(); + for (ParamObj paItem : paramList) { + String paStr = paItem.getName(); + String paVal = paItem.getStrValue(0); + String typeStr = StringUtils.isAllDigits(paVal) ? + AKI_NUMBER_TOKEN : AKI_STD_STRING; + typeStr = StringUtils.isBoolean(paVal) ? AKI_BOOLEAN_TOKEN : typeStr; + resContent += AKI_NEW_LINE + AKI_TAB_SPACE + typeStr + AKI_BLANK_SPACE + paStr + AKI_SEMICOLON; + } + resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE + AKI_SEMICOLON + AKI_NEW_LINE; + + resContent += AKI_NEW_LINE + AKI_CONST_TOKEN + AKI_BLANK_SPACE + AKI_STD_VECTOR + + AKI_LEFT_ANGLE_BRACKET + paName + AKI_STRUCT_SUFFIX + AKI_RIGHT_ANGLE_BRACKET + + AKI_BLANK_SPACE + paName + AKI_EQUAL + AKI_LEFT_BRACE; + for (ParamObj paramListItem : paList) { + List subParamList = paramListItem.getPaList(); + resContent += AKI_NEW_LINE + AKI_TAB_SPACE + AKI_LEFT_BRACE; + for (ParamObj paItem : subParamList) { + String paVal = paItem.getStrValue(0); + resContent += paVal + AKI_COMMA + AKI_BLANK_SPACE; + } + resContent = StringUtils.removeLastCharacter(resContent, 2); + resContent += AKI_RIGHT_BRACE + AKI_COMMA; + } + resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE + AKI_SEMICOLON + AKI_NEW_LINE; + return resContent; + }; + + /** + * 生成输出内容 + * + * @param pol 常量列表 + */ + @Override + public void genVarList(List pol) { + System.out.println("genVarList : " + pol.toString()); + + String resContent = ""; + for (ParamObj po : pol) { + String paName = po.getName(); + String paType = ts2CppKey(po.getType()).isEmpty() ? AKI_AUTO_TOKEN : ts2CppKey(po.getType()); + String paValue = po.getStrValue(0); + List paList = po.getPaList(); + if (paList.isEmpty()) { + resContent += AKI_NEW_LINE + AKI_EXTENDS_TOKEN + AKI_BLANK_SPACE + AKI_CONST_TOKEN + + AKI_BLANK_SPACE + paType + AKI_BLANK_SPACE + paName + AKI_EQUAL + paValue; + + resContent += AKI_SEMICOLON + AKI_NEW_LINE; + } else if (paList.get(0).getPaList().isEmpty()) { + String valType = StringUtils.isAllDigits(paList.get(0).getStrValue(0)) ? + AKI_NUMBER_TOKEN : AKI_STD_STRING; + resContent += AKI_NEW_LINE + AKI_EXTENDS_TOKEN + AKI_BLANK_SPACE + AKI_CONST_TOKEN + + AKI_BLANK_SPACE + AKI_STD_MAP + AKI_LEFT_ANGLE_BRACKET + AKI_STD_STRING + + AKI_COMMA + AKI_BLANK_SPACE + valType + AKI_RIGHT_BRACE + AKI_BLANK_SPACE + + paName + AKI_EQUAL + AKI_LEFT_BRACE; + for (ParamObj paItem : paList) { + String pName = paItem.getName(); + String pVal = paItem.getStrValue(0); + resContent += AKI_NEW_LINE + AKI_TAB_SPACE + AKI_LEFT_BRACE + AKI_DOUBLE_QUOTATION + + pName + AKI_DOUBLE_QUOTATION + AKI_COMMA + AKI_BLANK_SPACE + pVal + + AKI_RIGHT_BRACE + AKI_COMMA; + } + resContent = StringUtils.removeLastCharacter(resContent, 1); + resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE + AKI_SEMICOLON + AKI_NEW_LINE; + } else if (!(paList.get(0).getPaList().isEmpty())) { + resContent = genVarArrayList(resContent, paName, paList); + } + + } + this.constContent = resContent; + System.out.println("genVarList : " + resContent); + } +} -- Gitee From f214c71c2829c23911061deb11600027ea1d565d Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 15 Apr 2025 15:24:39 +0800 Subject: [PATCH 07/14] gen c h cpp Signed-off-by: sunlian --- .../ohosgen/src/main/java/gen/GenCHFile.java | 691 ++++++++++++++++++ 1 file changed, 691 insertions(+) create mode 100644 src/intellij_plugin/ohosgen/src/main/java/gen/GenCHFile.java diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenCHFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCHFile.java new file mode 100644 index 00000000..d3c893ec --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCHFile.java @@ -0,0 +1,691 @@ +/* + * 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 utils.FileUtils; +import utils.StringUtils; + +import java.io.File; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +/** + *

类名:该类用于xxx

+ * description ${description} + * + * @author ${USER} + * date 2025-02-28 + * @since 2025-02-28 + * @version 1.0 + */ +public class GenCHFile extends GeneratorBase { + private static final String C_ENUM_TOKEN = "enum"; + private static final String C_CLASS_TOKEN = "class"; + private static final String C_STRUCT_TOKEN = "struct"; + private static final String C_UNION_TOKEN = "union"; + private static final String C_TEMPLATE_TOKEN = "template"; + private static final String C_TYPE_NAME_TOKEN = "typename"; + private static final String C_STAR_TOKEN = "*"; + private static final String C_CHAR_START_TOKEN = "char*"; + private static final String C_AUTO_TOKEN = "auto"; + private static final String C_EXPORT_TOKEN = "export"; + private static final String C_IMPLEMENC_TOKEN = "implements"; + private static final String C_EXTENDS_TOKEN = "extends"; + private static final String C_CONST_TOKEN = "const"; + private static final String C_PRIVATE_TOKEN = "private"; + private static final String C_PUBLIC_TOKEN = "public"; + private static final String C_INTERFACE_TOKEN = "interface"; + private static final String C_PROTECTED_TOKEN = "protected"; + private static final String C_STATIC_TOKEN = "static"; + private static final String C_ANY_TOKEN = "any"; + private static final String C_NUMBER_TOKEN = "number"; + private static final String C_NEVER_TOKEN = "never"; + private static final String C_BOOLEAN_TOKEN = "boolean"; + private static final String C_STRING_TOKEN = "string"; + private static final String C_UNIQUE_TOKEN = "unique"; + private static final String C_SYMBOL_TOKEN = "symbol"; + private static final String C_UNDEFINED_TOKEN = "undefined"; + private static final String C_OBJECT_TOKEN = "object"; + private static final String C_OF_TOKEN = "of"; + private static final String C_KEYOF_TOKEN = "keyof"; + private static final String C_TYPE_TOKEN = "type"; + private static final String C_CONSTRUCTOR_TOKEN = "constructor"; + private static final String C_NAMESPACE_TOKEN = "namespace"; + private static final String C_REQUIRE_TOKEN = "require"; + private static final String C_MODULE_TOKEN = "module"; + private static final String C_DECLARE_TOKEN = "declare"; + private static final String C_ABSTRACT_TOKEN = "abstract"; + private static final String C_DEBUGGER_TOKEN = "debugger"; + private static final String C_FUNCTION_TOKEN = "function"; + private static final String C_THIS_TOKEN = "this"; + private static final String C_WITH_TOKEN = "with"; + private static final String C_DEFAULT_TOKEN = "default"; + private static final String C_READONLY_TOKEN = "readonly"; + private static final String C_ASYNC_TOKEN = "async"; + private static final String C_AWAIT_TOKEN = "await"; + private static final String C_YIELD_TOKEN = "yield"; + private static final String C_NEW_LINE = "\n"; + private static final String C_TAB_SPACE = "\t"; + private static final String C_BLANK_SPACE = " "; + private static final String C_SPLIT = " | "; + private static final String C_EQUAL = " = "; + private static final String C_COMMA = ","; + private static final String C_DOUBLE_QUOTATION = "\""; + private static final String C_UNDER_LINE = "_"; + private static final String C_SEMICOLON = ";"; + private static final String C_COLON = ":"; + private static final String C_ELLIPSIS = "..."; + private static final String C_DOT = "."; + private static final String C_LEFT_BRACE = "{"; + private static final String C_RIGHT_BRACE = "}"; + private static final String C_LEFT_PARENTHESES = "("; + private static final String C_RIGHT_PARENTHESES = ")"; + private static final String C_LEFT_SQUARE_BRACKET = "["; + private static final String C_RIGHT_SQUARE_BRACKET = "]"; + private static final String C_LEFT_ANGLE_BRACKET = "<"; + private static final String C_RIGHT_ANGLE_BRACKET = ">"; + + private static final String C_STD_STRING = "std::string"; + private static final String C_STD_VECTOR = "std::vector"; + private static final String C_STD_LIST = "std::list"; + private static final String C_STD_ARRAY = "std::array"; + private static final String C_STD_STACK = "std::stack"; + private static final String C_STD_QUEUE = "std::queue"; + private static final String C_STD_PAIR = "std::pair"; + private static final String C_STD_MAP = "std::map"; + private static final String C_STD_SET = "std::set"; + private static final String C_STD_DEQUE = "std::deque"; + private static final String C_STD_MULTIMAP = "std::multimap"; + private static final String C_STD_MULTISET = "std::multiset"; + + private static final String C_STR_SUFFIX = "STR"; + private static final String C_FILE_PREFIX = "ag_"; + private static final String C_FILE_H_SUFFIX = ".h"; + private static final String C_FILE_CPP_SUFFIX = ".cpp"; + private static final String C_FILE_C_SUFFIX = ".c"; + private static final String C_STRUCT_SUFFIX = "ST"; + + private String interfaceContent = ""; + private String enumContent = ""; + private String classContent = ""; + private String funcContent = ""; + private String structContent = ""; + private String typeContent = ""; + private String unionContent = ""; + private String constContent = ""; + + private final Map ts2cppMap = Map.ofEntries( + Map.entry("any", "auto"), + Map.entry("boolean", "bool"), + Map.entry("string", "char*"), + Map.entry("number", "int"), + Map.entry("void", "void"), + Map.entry("[]", "*") + ); + + private final Map tsTokenMap = Map.ofEntries( + Map.entry("\"", ""), + Map.entry("*", ""), + Map.entry("&", ""), + Map.entry("(", ""), + Map.entry(")", "") + ); + + /** + * 构造函数 + */ + GenCHFile() { + + } + + /** + * 将 cpp key 转换成 ts key + * + * @param cppKey 枚举对象列表 + * @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 && value.contains(C_STAR_TOKEN)) { + return cppKey.substring(0, ret) + value; + } else if (ret >= 0) { + return value; + } + } + return retKey; + } + + /** + * 将cpp token 替换成对应的dts token + * + * @param cppKey 语言关键字 + * @return 替换后字符串 + */ + private String replaceTsToken(String cppKey) { + String retKey = cppKey; + for (Map.Entry entry : tsTokenMap.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + int ret = retKey.indexOf(key); + if (ret >= 0) { + retKey = retKey.replace(key, value); + } + } + return retKey; + } + + /** + * 获得接口内容 + * + * @return 接口内容 + */ + public String getInterfaceContent() { + return interfaceContent; + } + + /** + * 获得枚举内容 + * + * @return 枚举内容 + */ + public String getEnumContent() { + return enumContent; + } + + /** + * 获得类内容 + * + * @return 类内容 + */ + public String getClassContent() { + return classContent; + } + + /** + * 获得方法内容 + * + * @return 方法内容 + */ + public String getFuncContent() { + return funcContent; + } + + /** + * 获得结构体内容 + * + * @return 结构体内容 + */ + public String getStructContent() { + return structContent; + } + + /** + * 获得type内容 + * + * @return type内容 + */ + public String getTypeContent() { + return typeContent; + } + + /** + * 获得联合体内容 + * + * @return 联合体内容 + */ + public String getUnionContent() { + return unionContent; + } + + /** + * 获得常量内容 + * + * @return 常量内容 + */ + public String getConstContent() { + return constContent; + } + + /** + * 生成输出内容 + * + * @param po 解析类 + */ + @Override + public void genContent(ParseObj po) { + genInterfaceList(po.getInterfaceList()); + genEnumList(po.getEnumList()); + genClassList(po.getClassList()); + genFuncList(po.getFuncList()); + genStructList(po.getStructList()); + genTypeList(po.getTypeList()); + genUnionList(po.getUnionList()); + genVarList(po.getVarList()); + } + + /** + * 生成文件 + * + * @param fileName 文件名 + * @param filePath 文件路径 + */ + @Override + public void genFile(String filePath, String fileName) { + System.out.println("genFile : " + filePath + fileName); + String outFileName = filePath + File.separator + C_FILE_PREFIX + + fileName.replace(".", "_") + C_FILE_H_SUFFIX; + System.out.println("outFileName : " + outFileName); + + if (this.genMode.equals(GeneratorBase.GEN_REPLACE)) { + FileUtils.deleteFile(outFileName); + } + FileUtils.createFile(outFileName); + + FileUtils.appendText(outFileName, this.genFileHeader(filePath + File.separator + fileName)); + FileUtils.appendText(outFileName, this.constContent); + FileUtils.appendText(outFileName, this.enumContent); + FileUtils.appendText(outFileName, this.typeContent); + FileUtils.appendText(outFileName, this.interfaceContent); + FileUtils.appendText(outFileName, this.unionContent); + FileUtils.appendText(outFileName, this.funcContent); + FileUtils.appendText(outFileName, this.structContent); + FileUtils.appendText(outFileName, this.classContent); + + } + + /** + * 生成输出内容 + * + * @param iol 接口列表 + */ + @Override + public void genInterfaceList(List iol) { + System.out.println("genInterfaceList" + iol.toString()); + }; + + /** + * 生成输出内容 + * + * @param eol 枚举列表 + */ + @Override + public void genEnumList(List eol) { + System.out.println("genEnumList" + eol.toString()); + + String resContent = ""; + for (EnumObj eo : eol) { + String enumName = eo.getName(); + enumName = !enumName.isEmpty() ? enumName : eo.getAlias(); + List memList = eo.getMemberList(); + List vaList = eo.getValueList(); + int i = 0; + resContent += C_NEW_LINE + C_ENUM_TOKEN + + C_BLANK_SPACE + enumName + C_BLANK_SPACE + C_LEFT_BRACE; + for (String memItem : memList) { + resContent += C_NEW_LINE + C_TAB_SPACE + memItem; + if (vaList.size() > i && !vaList.get(i).isEmpty()) { + resContent += C_EQUAL + replaceTsToken(vaList.get(i)) + C_COMMA; + } else { + resContent += C_COMMA; + } + i++; + } + + resContent = StringUtils.removeLastSpace(resContent); + resContent += C_NEW_LINE + C_RIGHT_BRACE + C_SEMICOLON + C_NEW_LINE; + + i = 0; + if (vaList.size() > i && !vaList.get(i).isEmpty() && + vaList.get(i).contains("\"")) { + resContent += C_NEW_LINE + C_CHAR_START_TOKEN + C_BLANK_SPACE + + enumName.toLowerCase(Locale.ROOT) + C_UNDER_LINE + C_STR_SUFFIX + + C_LEFT_SQUARE_BRACKET + C_RIGHT_SQUARE_BRACKET + C_EQUAL + C_LEFT_BRACE; + for (String val : vaList) { + resContent += C_NEW_LINE + C_TAB_SPACE + C_LEFT_SQUARE_BRACKET + + memList.get(i) + C_RIGHT_SQUARE_BRACKET + C_EQUAL + val + C_COMMA; + i++; + } + resContent = StringUtils.removeLastCharacter(resContent, 1); + resContent += C_NEW_LINE + C_RIGHT_BRACE + C_SEMICOLON + C_NEW_LINE; + + } + } + + this.enumContent = resContent; + }; + + private String setClassFunc(List funcList, String content) { + String tempResContent = content; + for (FuncObj funcItem : funcList) { + String retValue = funcItem.getRetValue(); + retValue = retValue.isEmpty() ? "" : ts2CppKey(retValue) + C_BLANK_SPACE; + tempResContent += C_NEW_LINE + C_TAB_SPACE + retValue + + replaceTsToken(funcItem.getName()) + C_LEFT_PARENTHESES; + List pol = funcItem.getParamList(); + for (ParamObj poItem : pol) { + String retType = ts2CppKey(poItem.getType()).isEmpty() ? + C_AUTO_TOKEN : ts2CppKey(poItem.getType()); + tempResContent += (poItem.getName() == null) ? retType + C_COMMA + C_BLANK_SPACE : + retType + C_BLANK_SPACE + replaceTsToken(poItem.getName()) + C_COMMA + C_BLANK_SPACE; + } + if (!pol.isEmpty()) { + tempResContent = StringUtils.removeLastCharacter(tempResContent, 2); + } + tempResContent += C_RIGHT_PARENTHESES + C_SEMICOLON; + } + return tempResContent; + } + + /** + * 生成输出内容 + * + * @param col 类列表 + */ + @Override + public void genClassList(List col) { + System.out.println("genClassList" + col.toString()); + + String resContent = ""; + for (ClassObj co : col) { + String className = co.getName(); + className = !className.isEmpty() ? className : co.getAlias(); + + String templateStr = !co.getTempList().isEmpty() ? + C_TEMPLATE_TOKEN + C_BLANK_SPACE + C_LEFT_ANGLE_BRACKET : ""; + for (String teStr : co.getTempList()) { + templateStr += C_TYPE_NAME_TOKEN + C_BLANK_SPACE + teStr + C_COMMA + C_BLANK_SPACE; + } + templateStr = templateStr.length() > 1 ? + StringUtils.removeLastCharacter(templateStr, 2) + C_RIGHT_ANGLE_BRACKET + C_BLANK_SPACE : ""; + + List hnList = co.getHeritageNameList(); + String htStr = hnList.size() > 0 ? C_BLANK_SPACE + C_COLON + C_BLANK_SPACE : ""; + for (String hName : hnList) { + htStr += C_PUBLIC_TOKEN + C_BLANK_SPACE + hName + C_COMMA + C_BLANK_SPACE; + } + htStr = htStr.length() > 1 ? StringUtils.removeLastCharacter(htStr, 2) : htStr; + + List htempList = co.getHeritageTemplateList(); + String htempStr = htempList.size() > 0 ? C_LEFT_ANGLE_BRACKET : ""; + for (String tempStr : htempList) { + htempStr += tempStr + C_COMMA + C_BLANK_SPACE; + } + htempStr = htempList.size() > 0 ? + StringUtils.removeLastCharacter(htempStr, 2) + C_RIGHT_ANGLE_BRACKET : ""; + resContent += C_NEW_LINE + templateStr + C_CLASS_TOKEN + + C_BLANK_SPACE + className + htStr + htempStr + C_BLANK_SPACE + C_LEFT_BRACE; + List paList = co.getParamList(); + for (ParamObj paItem : paList) { + String paType = paItem.getType(); + String qualifyStr = paItem.getQualifier() == null || paItem.getQualifier().isEmpty() ? + "" : paItem.getQualifier() + C_BLANK_SPACE; + resContent += C_NEW_LINE + C_TAB_SPACE + qualifyStr + ts2CppKey(paType) + + C_BLANK_SPACE + replaceTsToken(paItem.getName()); + List initVList = paItem.getvList(); + if (!initVList.isEmpty()) { + resContent += C_EQUAL + initVList.get(0) + C_SEMICOLON; + } else { + resContent += C_SEMICOLON; + } + } + + resContent = setClassFunc(co.getFuncList(), resContent); + + resContent += C_NEW_LINE + C_RIGHT_BRACE + C_SEMICOLON + C_NEW_LINE; + } + this.classContent = resContent; + }; + + /** + * 生成输出内容 + * + * @param fol 方法列表 + */ + @Override + public void genFuncList(List fol) { + System.out.println("genFuncList : " + fol.toString()); + String resContent = ""; + for (FuncObj fo : fol) { + String funcName = fo.getName(); + funcName = !funcName.isEmpty() ? funcName : fo.getAlias(); + List tempList = fo.getTempList(); + String tempStr = tempList.isEmpty() ? "" : C_TEMPLATE_TOKEN + C_LEFT_ANGLE_BRACKET; + for (String teStr : tempList) { + tempStr += C_TYPE_NAME_TOKEN + C_BLANK_SPACE + teStr + C_COMMA + C_BLANK_SPACE; + } + tempStr = tempList.isEmpty() ? "" : + StringUtils.removeLastCharacter(tempStr, 2) + C_RIGHT_ANGLE_BRACKET + C_BLANK_SPACE; + List paList = fo.getParamList(); + String retValue = ts2CppKey(fo.getRetValue()).isEmpty() ? + "" : ts2CppKey(fo.getRetValue()) + C_BLANK_SPACE; + resContent += C_NEW_LINE + tempStr + retValue + + replaceTsToken(funcName) + C_LEFT_PARENTHESES; + + for (ParamObj poItem : paList) { + String paType = ts2CppKey(poItem.getType()).isEmpty() ? + C_AUTO_TOKEN + C_BLANK_SPACE : ts2CppKey(poItem.getType()) + C_BLANK_SPACE; + String paName = poItem.getName(); + String defaultVal = poItem.getStrValue(0); + defaultVal = defaultVal.isEmpty() ? "" : C_EQUAL + defaultVal; + resContent += !paName.isEmpty() ? paType + replaceTsToken(paName) + + defaultVal + C_COMMA + C_BLANK_SPACE : + paType + C_COMMA + C_BLANK_SPACE; + } + if (!paList.isEmpty()) { + resContent = StringUtils.removeLastCharacter(resContent, 2); + } + resContent += C_RIGHT_PARENTHESES + C_SEMICOLON; + } + this.funcContent = resContent; + System.out.println("genFuncList : " + resContent); + }; + + /** + * 生成输出内容 + * + * @param sol 结构体列表 + */ + @Override + public void genStructList(List sol) { + System.out.println("genStructList" + sol.toString()); + + String resContent = ""; + for (StructObj so : sol) { + String structName = so.getName(); + structName = !structName.isEmpty() ? structName : so.getAlias(); + + String templateStr = !so.getTemplateList().isEmpty() ? + C_TEMPLATE_TOKEN + C_BLANK_SPACE + C_LEFT_ANGLE_BRACKET : ""; + for (String teStr : so.getTemplateList()) { + templateStr += C_TYPE_NAME_TOKEN + C_BLANK_SPACE + teStr + C_COMMA + C_BLANK_SPACE; + } + templateStr = templateStr.length() > 1 ? + StringUtils.removeLastCharacter(templateStr, 2) + C_RIGHT_ANGLE_BRACKET + C_BLANK_SPACE : ""; + + List paList = so.getMemberList(); + resContent += C_NEW_LINE + templateStr + C_STRUCT_TOKEN + + C_BLANK_SPACE + structName + C_BLANK_SPACE + C_LEFT_BRACE; + + for (ParamObj paItem : paList) { + String paType = paItem.getType().isEmpty() ? C_AUTO_TOKEN : paItem.getType(); + resContent += C_NEW_LINE + C_TAB_SPACE + ts2CppKey(paType) + + C_BLANK_SPACE + paItem.getName(); + ; + List initVList = paItem.getvList(); + if (initVList.size() > 0) { + resContent += C_EQUAL + initVList.get(0) + C_SEMICOLON; + } else { + resContent += C_SEMICOLON; + } + } + + List funcList = so.getFuncList(); + for (FuncObj funcItem : funcList) { + String retValue = ts2CppKey(funcItem.getRetValue()).isEmpty() ? "" : + ts2CppKey(funcItem.getRetValue()) + C_BLANK_SPACE; + resContent += C_NEW_LINE + C_TAB_SPACE + retValue + + replaceTsToken(funcItem.getName()) + C_LEFT_PARENTHESES; + List pol = funcItem.getParamList(); + for (ParamObj poItem : pol) { + String retType = ts2CppKey(poItem.getType()).isEmpty() ? + C_AUTO_TOKEN : ts2CppKey(poItem.getType()); + resContent += retType + C_BLANK_SPACE + replaceTsToken(poItem.getName()) + + C_COMMA + C_BLANK_SPACE; + } + resContent = !pol.isEmpty() ? StringUtils.removeLastCharacter(resContent, 2) : resContent; + resContent += C_RIGHT_PARENTHESES + C_SEMICOLON; + } + + resContent = StringUtils.removeLastSpace(resContent); + resContent += C_NEW_LINE + C_RIGHT_BRACE + C_SEMICOLON + C_NEW_LINE; + } + this.structContent = resContent; + }; + + /** + * 生成输出内容 + * + * @param tol 类型列表 + */ + @Override + public void genTypeList(List tol) { + System.out.println("genTypeList : " + tol.toString()); + }; + + /** + * 生成输出内容 + * + * @param uol 联合体列表 + */ + @Override + public void genUnionList(List uol) { + System.out.println("genUnionList : " + uol.toString()); + + String resContent = ""; + for (UnionObj uo : uol) { + String unionName = uo.getName(); + unionName = !unionName.isEmpty() ? unionName : uo.getAlias(); + + String templateStr = !uo.getTemplateList().isEmpty() ? + C_TEMPLATE_TOKEN + C_BLANK_SPACE + C_LEFT_ANGLE_BRACKET : ""; + for (String teStr : uo.getTemplateList()) { + templateStr += C_TYPE_NAME_TOKEN + C_BLANK_SPACE + teStr + C_COMMA + C_BLANK_SPACE; + } + templateStr = templateStr.length() > 1 ? + StringUtils.removeLastCharacter(templateStr, 2) + C_RIGHT_ANGLE_BRACKET + C_BLANK_SPACE : ""; + + resContent += C_NEW_LINE + templateStr + C_UNION_TOKEN + + C_BLANK_SPACE + unionName + C_LEFT_BRACE; + + List paList = uo.getMemList(); + for (ParamObj paItem : paList) { + String paType = paItem.getType(); + String paName = paItem.getName(); + resContent += C_NEW_LINE + C_TAB_SPACE + ts2CppKey(paType) + + C_BLANK_SPACE + paName + C_SEMICOLON; + } + resContent += C_NEW_LINE + C_RIGHT_BRACE; + resContent += C_SEMICOLON + C_NEW_LINE; + } + this.unionContent = resContent; + }; + + private String genVarArrayList(String tmpContent, String paName, List paList) { + String resContent = tmpContent; + resContent += C_NEW_LINE + C_STRUCT_TOKEN + C_BLANK_SPACE + paName + + C_STRUCT_SUFFIX + C_BLANK_SPACE + C_LEFT_BRACE; + List paramList = paList.get(0).getPaList(); + for (ParamObj paItem : paramList) { + String paStr = paItem.getName(); + String paVal = paItem.getStrValue(0); + String typeStr = StringUtils.isAllDigits(paVal) ? + C_NUMBER_TOKEN : C_STD_STRING; + typeStr = StringUtils.isBoolean(paVal) ? C_BOOLEAN_TOKEN : typeStr; + resContent += C_NEW_LINE + C_TAB_SPACE + typeStr + C_BLANK_SPACE + paStr + C_SEMICOLON; + } + resContent += C_NEW_LINE + C_RIGHT_BRACE + C_SEMICOLON + C_NEW_LINE; + + resContent += C_NEW_LINE + C_CONST_TOKEN + C_BLANK_SPACE + C_STD_VECTOR + + C_LEFT_ANGLE_BRACKET + paName + C_STRUCT_SUFFIX + C_RIGHT_ANGLE_BRACKET + + C_BLANK_SPACE + paName + C_EQUAL + C_LEFT_BRACE; + for (ParamObj paramListItem : paList) { + List subParamList = paramListItem.getPaList(); + resContent += C_NEW_LINE + C_TAB_SPACE + C_LEFT_BRACE; + for (ParamObj paItem : subParamList) { + String paVal = paItem.getStrValue(0); + resContent += paVal + C_COMMA + C_BLANK_SPACE; + } + resContent = StringUtils.removeLastCharacter(resContent, 2); + resContent += C_RIGHT_BRACE + C_COMMA; + } + resContent += C_NEW_LINE + C_RIGHT_BRACE + C_SEMICOLON + C_NEW_LINE; + return resContent; + }; + + /** + * 生成输出内容 + * + * @param pol 常量列表 + */ + @Override + public void genVarList(List pol) { + System.out.println("genVarList : " + pol.toString()); + + String resContent = ""; + for (ParamObj po : pol) { + String paName = po.getName(); + String paType = ts2CppKey(po.getType()).isEmpty() ? C_AUTO_TOKEN : ts2CppKey(po.getType()); + String paValue = po.getStrValue(0); + List paList = po.getPaList(); + if (paList.isEmpty()) { + resContent += C_NEW_LINE + C_EXTENDS_TOKEN + C_BLANK_SPACE + C_CONST_TOKEN + + C_BLANK_SPACE + paType + C_BLANK_SPACE + paName + C_EQUAL + paValue; + + resContent += C_SEMICOLON + C_NEW_LINE; + } else if (paList.get(0).getPaList().isEmpty()) { + String valType = StringUtils.isAllDigits(paList.get(0).getStrValue(0)) ? + C_NUMBER_TOKEN : C_STD_STRING; + resContent += C_NEW_LINE + C_EXTENDS_TOKEN + C_BLANK_SPACE + C_CONST_TOKEN + + C_BLANK_SPACE + C_STD_MAP + C_LEFT_ANGLE_BRACKET + C_STD_STRING + + C_COMMA + C_BLANK_SPACE + valType + C_RIGHT_BRACE + C_BLANK_SPACE + + paName + C_EQUAL + C_LEFT_BRACE; + for (ParamObj paItem : paList) { + String pName = paItem.getName(); + String pVal = paItem.getStrValue(0); + resContent += C_NEW_LINE + C_TAB_SPACE + C_LEFT_BRACE + C_DOUBLE_QUOTATION + + pName + C_DOUBLE_QUOTATION + C_COMMA + C_BLANK_SPACE + pVal + + C_RIGHT_BRACE + C_COMMA; + } + resContent = StringUtils.removeLastCharacter(resContent, 1); + resContent += C_NEW_LINE + C_RIGHT_BRACE + C_SEMICOLON + C_NEW_LINE; + } else if (!(paList.get(0).getPaList().isEmpty())) { + resContent = genVarArrayList(resContent, paName, paList); + } + + } + this.constContent = resContent; + System.out.println("genVarList : " + resContent); + } +} -- Gitee From 4688b193cd7d64e8fe98e1de2c6f6780fe62953f Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 15 Apr 2025 15:25:06 +0800 Subject: [PATCH 08/14] gen cpp h Signed-off-by: sunlian --- .../src/main/java/gen/GenCppHFile.java | 691 ++++++++++++++++++ 1 file changed, 691 insertions(+) create mode 100644 src/intellij_plugin/ohosgen/src/main/java/gen/GenCppHFile.java diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppHFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppHFile.java new file mode 100644 index 00000000..bb2b415b --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenCppHFile.java @@ -0,0 +1,691 @@ +/* + * 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 utils.FileUtils; +import utils.StringUtils; + +import java.io.File; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +/** + *

类名:该类用于xxx

+ * description ${description} + * + * @author ${USER} + * date 2025-02-28 + * @since 2025-02-28 + * @version 1.0 + */ +public class GenCppHFile 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_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"; + private static final String CPP_CONST_TOKEN = "const"; + private static final String CPP_PRIVATE_TOKEN = "private"; + private static final String CPP_PUBLIC_TOKEN = "public"; + private static final String CPP_INTERFACE_TOKEN = "interface"; + private static final String CPP_PROTECTED_TOKEN = "protected"; + private static final String CPP_STATIC_TOKEN = "static"; + private static final String CPP_ANY_TOKEN = "any"; + private static final String CPP_NUMBER_TOKEN = "number"; + private static final String CPP_NEVER_TOKEN = "never"; + private static final String CPP_BOOLEAN_TOKEN = "boolean"; + private static final String CPP_STRING_TOKEN = "string"; + private static final String CPP_UNIQUE_TOKEN = "unique"; + private static final String CPP_SYMBOL_TOKEN = "symbol"; + private static final String CPP_UNDEFINED_TOKEN = "undefined"; + private static final String CPP_OBJECT_TOKEN = "object"; + private static final String CPP_OF_TOKEN = "of"; + private static final String CPP_KEYOF_TOKEN = "keyof"; + private static final String CPP_TYPE_TOKEN = "type"; + private static final String CPP_CONSTRUCTOR_TOKEN = "constructor"; + private static final String CPP_NAMESPACE_TOKEN = "namespace"; + private static final String CPP_REQUIRE_TOKEN = "require"; + private static final String CPP_MODULE_TOKEN = "module"; + private static final String CPP_DECLARE_TOKEN = "declare"; + private static final String CPP_ABSTRACT_TOKEN = "abstract"; + private static final String CPP_DEBUGGER_TOKEN = "debugger"; + private static final String CPP_FUNCTION_TOKEN = "function"; + private static final String CPP_THIS_TOKEN = "this"; + private static final String CPP_WITH_TOKEN = "with"; + private static final String CPP_DEFAULT_TOKEN = "default"; + private static final String CPP_READONLY_TOKEN = "readonly"; + private static final String CPP_ASYNC_TOKEN = "async"; + private static final String CPP_AWAIT_TOKEN = "await"; + private static final String CPP_YIELD_TOKEN = "yield"; + private static final String CPP_NEW_LINE = "\n"; + private static final String CPP_TAB_SPACE = "\t"; + private static final String CPP_BLANK_SPACE = " "; + 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_ELLIPSIS = "..."; + private static final String CPP_DOT = "."; + private static final String CPP_LEFT_BRACE = "{"; + private static final String CPP_RIGHT_BRACE = "}"; + private static final String CPP_LEFT_PARENTHESES = "("; + private static final String CPP_RIGHT_PARENTHESES = ")"; + private static final String CPP_LEFT_SQUARE_BRACKET = "["; + private static final String CPP_RIGHT_SQUARE_BRACKET = "]"; + private static final String CPP_LEFT_ANGLE_BRACKET = "<"; + private static final String CPP_RIGHT_ANGLE_BRACKET = ">"; + + private static final String CPP_STD_STRING = "std::string"; + private static final String CPP_STD_VECTOR = "std::vector"; + private static final String CPP_STD_LIST = "std::list"; + private static final String CPP_STD_ARRAY = "std::array"; + private static final String CPP_STD_STACK = "std::stack"; + private static final String CPP_STD_QUEUE = "std::queue"; + private static final String CPP_STD_PAIR = "std::pair"; + private static final String CPP_STD_MAP = "std::map"; + private static final String CPP_STD_SET = "std::set"; + private static final String CPP_STD_DEQUE = "std::deque"; + private static final String CPP_STD_MULTIMAP = "std::multimap"; + private static final String CPP_STD_MULTISET = "std::multiset"; + private static final String CPP_STD_FUNCTION = "std::function"; + + 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"; + private static final String CPP_FILE_C_SUFFIX = ".c"; + private static final String CPP_STRUCT_SUFFIX = "ST"; + + private String interfaceContent = ""; + private String enumContent = ""; + private String classContent = ""; + private String funcContent = ""; + private String structContent = ""; + private String typeContent = ""; + private String unionContent = ""; + private String constContent = ""; + + private final Map ts2cppMap = Map.ofEntries( + Map.entry("any", "auto"), + Map.entry("boolean", "bool"), + Map.entry("string", "std::string"), + Map.entry("number", "int"), + Map.entry("void", "void"), + Map.entry("[]", "*") + ); + + private final Map tsTokenMap = Map.ofEntries( + Map.entry("\"", ""), + Map.entry("*", ""), + Map.entry("&", ""), + Map.entry("(", ""), + Map.entry(")", "") + ); + + /** + * 构造函数 + */ + GenCppHFile() { + + } + + /** + * 将 cpp key 转换成 ts key + * + * @param cppKey 枚举对象列表 + * @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 && value.contains(CPP_STAR_TOKEN)) { + return cppKey.substring(0, ret) + value; + } else if (ret >= 0) { + return value; + } + } + return retKey; + } + + /** + * 将cpp token 替换成对应的dts token + * + * @param cppKey 语言关键字 + * @return 替换后字符串 + */ + private String replaceTsToken(String cppKey) { + String retKey = cppKey; + for (Map.Entry entry : tsTokenMap.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + int ret = retKey.indexOf(key); + if (ret >= 0) { + retKey = retKey.replace(key, value); + } + } + return retKey; + } + + /** + * 获得接口内容 + * + * @return 接口内容 + */ + public String getInterfaceContent() { + return interfaceContent; + } + + /** + * 获得枚举内容 + * + * @return 枚举内容 + */ + public String getEnumContent() { + return enumContent; + } + + /** + * 获得类内容 + * + * @return 类内容 + */ + public String getClassContent() { + return classContent; + } + + /** + * 获得方法内容 + * + * @return 方法内容 + */ + public String getFuncContent() { + return funcContent; + } + + /** + * 获得结构体内容 + * + * @return 结构体内容 + */ + public String getStructContent() { + return structContent; + } + + /** + * 获得type内容 + * + * @return type内容 + */ + public String getTypeContent() { + return typeContent; + } + + /** + * 获得联合体内容 + * + * @return 联合体内容 + */ + public String getUnionContent() { + return unionContent; + } + + /** + * 获得常量内容 + * + * @return 常量内容 + */ + public String getConstContent() { + return constContent; + } + + /** + * 生成输出内容 + * + * @param po 解析类 + */ + @Override + public void genContent(ParseObj po) { + genInterfaceList(po.getInterfaceList()); + genEnumList(po.getEnumList()); + genClassList(po.getClassList()); + genFuncList(po.getFuncList()); + genStructList(po.getStructList()); + genTypeList(po.getTypeList()); + genUnionList(po.getUnionList()); + genVarList(po.getVarList()); + } + + /** + * 生成文件 + * + * @param fileName 文件名 + * @param filePath 文件路径 + */ + @Override + public void genFile(String filePath, String fileName) { + System.out.println("genFile : " + filePath + fileName); + String outFileName = filePath + File.separator + CPP_FILE_PREFIX + + fileName.replace(".", "_") + CPP_FILE_H_SUFFIX; + System.out.println("outFileName : " + outFileName); + if (this.genMode.equals(GeneratorBase.GEN_REPLACE)) { + FileUtils.deleteFile(outFileName); + } + FileUtils.createFile(outFileName); + + FileUtils.appendText(outFileName, this.genFileHeader(filePath + File.separator + fileName)); + FileUtils.appendText(outFileName, this.constContent); + FileUtils.appendText(outFileName, this.enumContent); + FileUtils.appendText(outFileName, this.typeContent); + FileUtils.appendText(outFileName, this.interfaceContent); + FileUtils.appendText(outFileName, this.unionContent); + FileUtils.appendText(outFileName, this.funcContent); + FileUtils.appendText(outFileName, this.structContent); + FileUtils.appendText(outFileName, this.classContent); + + } + + /** + * 生成输出内容 + * + * @param iol 接口列表 + */ + @Override + public void genInterfaceList(List iol) { + System.out.println("genInterfaceList" + iol.toString()); + }; + + /** + * 生成输出内容 + * + * @param eol 枚举列表 + */ + @Override + public void genEnumList(List eol) { + System.out.println("genEnumList" + eol.toString()); + + String resContent = ""; + for (EnumObj eo : eol) { + String enumName = eo.getName(); + enumName = !enumName.isEmpty() ? enumName : eo.getAlias(); + List memList = eo.getMemberList(); + List vaList = eo.getValueList(); + int i = 0; + resContent += CPP_NEW_LINE + CPP_ENUM_TOKEN + + CPP_BLANK_SPACE + enumName + CPP_BLANK_SPACE + CPP_LEFT_BRACE; + for (String memItem : memList) { + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + memItem; + if (vaList.size() > i && !vaList.get(i).isEmpty()) { + 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; + }; + + 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; + 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()); + 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()) { + tempResContent = StringUtils.removeLastCharacter(tempResContent, 2); + } + tempResContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; + } + return tempResContent; + } + + /** + * 生成输出内容 + * + * @param col 类列表 + */ + @Override + public void genClassList(List col) { + System.out.println("genClassList" + col.toString()); + + String resContent = ""; + 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) { + htStr += CPP_PUBLIC_TOKEN + CPP_BLANK_SPACE + hName + CPP_COMMA + CPP_BLANK_SPACE; + } + htStr = htStr.length() > 1 ? StringUtils.removeLastCharacter(htStr, 2) : htStr; + + 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(); + 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(); + if (!initVList.isEmpty()) { + resContent += CPP_EQUAL + initVList.get(0) + CPP_SEMICOLON; + } else { + resContent += CPP_SEMICOLON; + } + } + + resContent = setClassFunc(co.getFuncList(), resContent); + + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + } + this.classContent = resContent; + }; + + /** + * 生成输出内容 + * + * @param fol 方法列表 + */ + @Override + public void genFuncList(List fol) { + System.out.println("genFuncList : " + fol.toString()); + String resContent = ""; + 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 = 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()).isEmpty() ? + CPP_AUTO_TOKEN + CPP_BLANK_SPACE : ts2CppKey(poItem.getType()) + CPP_BLANK_SPACE; + String paName = poItem.getName(); + 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.isEmpty()) { + resContent = StringUtils.removeLastCharacter(resContent, 2); + } + resContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; + } + this.funcContent = resContent; + System.out.println("genFuncList : " + resContent); + }; + + /** + * 生成输出内容 + * + * @param sol 结构体列表 + */ + @Override + public void genStructList(List sol) { + System.out.println("genStructList" + sol.toString()); + + String resContent = ""; + for (StructObj so : sol) { + 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 + templateStr + CPP_STRUCT_TOKEN + + CPP_BLANK_SPACE + structName + CPP_BLANK_SPACE + CPP_LEFT_BRACE; + + for (ParamObj paItem : paList) { + String paType = paItem.getType().isEmpty() ? CPP_AUTO_TOKEN : paItem.getType(); + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + ts2CppKey(paType) + + CPP_BLANK_SPACE + paItem.getName(); + ; + List initVList = paItem.getvList(); + if (initVList.size() > 0) { + resContent += CPP_EQUAL + initVList.get(0) + CPP_SEMICOLON; + } else { + resContent += CPP_SEMICOLON; + } + } + + List funcList = so.getFuncList(); + for (FuncObj funcItem : funcList) { + 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()).isEmpty() ? + CPP_AUTO_TOKEN : ts2CppKey(poItem.getType()); + resContent += retType + CPP_BLANK_SPACE + replaceTsToken(poItem.getName()) + + CPP_COMMA + CPP_BLANK_SPACE; + } + resContent = !pol.isEmpty() ? StringUtils.removeLastCharacter(resContent, 2) : resContent; + resContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; + } + + resContent = StringUtils.removeLastSpace(resContent); + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + } + this.structContent = resContent; + }; + + /** + * 生成输出内容 + * + * @param tol 类型列表 + */ + @Override + public void genTypeList(List tol) { + System.out.println("genTypeList : " + tol.toString()); + }; + + /** + * 生成输出内容 + * + * @param uol 联合体列表 + */ + @Override + public void genUnionList(List uol) { + System.out.println("genUnionList : " + uol.toString()); + + String resContent = ""; + for (UnionObj uo : uol) { + String unionName = uo.getName(); + unionName = !unionName.isEmpty() ? unionName : uo.getAlias(); + + 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; + } + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE; + resContent += CPP_SEMICOLON + CPP_NEW_LINE; + } + this.unionContent = resContent; + }; + + private String genVarArrayList(String tmpContent, String paName, List paList) { + String resContent = tmpContent; + resContent += CPP_NEW_LINE + CPP_STRUCT_TOKEN + CPP_BLANK_SPACE + paName + + CPP_STRUCT_SUFFIX + CPP_BLANK_SPACE + CPP_LEFT_BRACE; + List paramList = paList.get(0).getPaList(); + for (ParamObj paItem : paramList) { + String paStr = paItem.getName(); + String paVal = paItem.getStrValue(0); + String typeStr = StringUtils.isAllDigits(paVal) ? + CPP_NUMBER_TOKEN : CPP_STD_STRING; + typeStr = StringUtils.isBoolean(paVal) ? CPP_BOOLEAN_TOKEN : typeStr; + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + typeStr + CPP_BLANK_SPACE + paStr + CPP_SEMICOLON; + } + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + + resContent += CPP_NEW_LINE + CPP_CONST_TOKEN + CPP_BLANK_SPACE + CPP_STD_VECTOR + + CPP_LEFT_ANGLE_BRACKET + paName + CPP_STRUCT_SUFFIX + CPP_RIGHT_ANGLE_BRACKET + + CPP_BLANK_SPACE + paName + CPP_EQUAL + CPP_LEFT_BRACE; + for (ParamObj paramListItem : paList) { + List subParamList = paramListItem.getPaList(); + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + CPP_LEFT_BRACE; + for (ParamObj paItem : subParamList) { + String paVal = paItem.getStrValue(0); + resContent += paVal + CPP_COMMA + CPP_BLANK_SPACE; + } + resContent = StringUtils.removeLastCharacter(resContent, 2); + resContent += CPP_RIGHT_BRACE + CPP_COMMA; + } + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + return resContent; + }; + + /** + * 生成输出内容 + * + * @param pol 常量列表 + */ + @Override + public void genVarList(List pol) { + System.out.println("genVarList : " + pol.toString()); + + String resContent = ""; + for (ParamObj po : pol) { + String paName = po.getName(); + String paType = ts2CppKey(po.getType()).isEmpty() ? CPP_AUTO_TOKEN : ts2CppKey(po.getType()); + String paValue = po.getStrValue(0); + List paList = po.getPaList(); + if (paList.isEmpty()) { + resContent += CPP_NEW_LINE + CPP_EXTENDS_TOKEN + CPP_BLANK_SPACE + CPP_CONST_TOKEN + + CPP_BLANK_SPACE + paType + CPP_BLANK_SPACE + paName + CPP_EQUAL + paValue; + + resContent += CPP_SEMICOLON + CPP_NEW_LINE; + } else if (paList.get(0).getPaList().isEmpty()) { + String valType = StringUtils.isAllDigits(paList.get(0).getStrValue(0)) ? + CPP_NUMBER_TOKEN : CPP_STD_STRING; + resContent += CPP_NEW_LINE + CPP_EXTENDS_TOKEN + CPP_BLANK_SPACE + CPP_CONST_TOKEN + + CPP_BLANK_SPACE + CPP_STD_MAP + CPP_LEFT_ANGLE_BRACKET + CPP_STD_STRING + + CPP_COMMA + CPP_BLANK_SPACE + valType + CPP_RIGHT_BRACE + CPP_BLANK_SPACE + + paName + CPP_EQUAL + CPP_LEFT_BRACE; + for (ParamObj paItem : paList) { + String pName = paItem.getName(); + String pVal = paItem.getStrValue(0); + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + CPP_LEFT_BRACE + CPP_DOUBLE_QUOTATION + + pName + CPP_DOUBLE_QUOTATION + CPP_COMMA + CPP_BLANK_SPACE + pVal + + CPP_RIGHT_BRACE + CPP_COMMA; + } + resContent = StringUtils.removeLastCharacter(resContent, 1); + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + } else if (!(paList.get(0).getPaList().isEmpty())) { + resContent = genVarArrayList(resContent, paName, paList); + } + + } + this.constContent = resContent; + System.out.println("genVarList : " + resContent); + } +} -- Gitee From 8894af45483aff5a74d97432261a0d2260daf20e Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 15 Apr 2025 15:25:40 +0800 Subject: [PATCH 09/14] gen napi cpp Signed-off-by: sunlian --- .../src/main/java/gen/GenNapiCppFile.java | 688 ++++++++++++++++++ 1 file changed, 688 insertions(+) create mode 100644 src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java new file mode 100644 index 00000000..c707a9c2 --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java @@ -0,0 +1,688 @@ +/* + * 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 utils.FileUtils; +import utils.StringUtils; + +import java.io.File; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +/** + *

类名:该类用于xxx

+ * description ${description} + * + * @author ${USER} + * date 2025-02-28 + * @since 2025-02-28 + * @version 1.0 + */ +public class GenNapiCppFile 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_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"; + private static final String CPP_CONST_TOKEN = "const"; + private static final String CPP_PRIVATE_TOKEN = "private"; + private static final String CPP_PUBLIC_TOKEN = "public"; + private static final String CPP_INTERFACE_TOKEN = "interface"; + private static final String CPP_PROTECTED_TOKEN = "protected"; + private static final String CPP_STATIC_TOKEN = "static"; + private static final String CPP_ANY_TOKEN = "any"; + private static final String CPP_NUMBER_TOKEN = "number"; + private static final String CPP_NEVER_TOKEN = "never"; + private static final String CPP_BOOLEAN_TOKEN = "boolean"; + private static final String CPP_STRING_TOKEN = "string"; + private static final String CPP_UNIQUE_TOKEN = "unique"; + private static final String CPP_SYMBOL_TOKEN = "symbol"; + private static final String CPP_UNDEFINED_TOKEN = "undefined"; + private static final String CPP_OBJECT_TOKEN = "object"; + private static final String CPP_OF_TOKEN = "of"; + private static final String CPP_KEYOF_TOKEN = "keyof"; + private static final String CPP_TYPE_TOKEN = "type"; + private static final String CPP_CONSTRUCTOR_TOKEN = "constructor"; + private static final String CPP_NAMESPACE_TOKEN = "namespace"; + private static final String CPP_REQUIRE_TOKEN = "require"; + private static final String CPP_MODULE_TOKEN = "module"; + private static final String CPP_DECLARE_TOKEN = "declare"; + private static final String CPP_ABSTRACT_TOKEN = "abstract"; + private static final String CPP_DEBUGGER_TOKEN = "debugger"; + private static final String CPP_FUNCTION_TOKEN = "function"; + private static final String CPP_THIS_TOKEN = "this"; + private static final String CPP_WITH_TOKEN = "with"; + private static final String CPP_DEFAULT_TOKEN = "default"; + private static final String CPP_READONLY_TOKEN = "readonly"; + private static final String CPP_ASYNC_TOKEN = "async"; + private static final String CPP_AWAIT_TOKEN = "await"; + private static final String CPP_YIELD_TOKEN = "yield"; + private static final String CPP_NEW_LINE = "\n"; + private static final String CPP_TAB_SPACE = "\t"; + private static final String CPP_BLANK_SPACE = " "; + 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_ELLIPSIS = "..."; + private static final String CPP_DOT = "."; + private static final String CPP_LEFT_BRACE = "{"; + private static final String CPP_RIGHT_BRACE = "}"; + private static final String CPP_LEFT_PARENTHESES = "("; + private static final String CPP_RIGHT_PARENTHESES = ")"; + private static final String CPP_LEFT_SQUARE_BRACKET = "["; + private static final String CPP_RIGHT_SQUARE_BRACKET = "]"; + private static final String CPP_LEFT_ANGLE_BRACKET = "<"; + private static final String CPP_RIGHT_ANGLE_BRACKET = ">"; + + private static final String CPP_STD_STRING = "std::string"; + private static final String CPP_STD_VECTOR = "std::vector"; + private static final String CPP_STD_LIST = "std::list"; + private static final String CPP_STD_ARRAY = "std::array"; + private static final String CPP_STD_STACK = "std::stack"; + private static final String CPP_STD_QUEUE = "std::queue"; + private static final String CPP_STD_PAIR = "std::pair"; + private static final String CPP_STD_MAP = "std::map"; + private static final String CPP_STD_SET = "std::set"; + private static final String CPP_STD_DEQUE = "std::deque"; + private static final String CPP_STD_MULTIMAP = "std::multimap"; + private static final String CPP_STD_MULTISET = "std::multiset"; + + 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"; + private static final String CPP_FILE_C_SUFFIX = ".c"; + private static final String CPP_STRUCT_SUFFIX = "ST"; + + private String interfaceContent = ""; + private String enumContent = ""; + private String classContent = ""; + private String funcContent = ""; + private String structContent = ""; + private String typeContent = ""; + private String unionContent = ""; + private String constContent = ""; + + private final Map ts2cppMap = Map.ofEntries( + Map.entry("any", "auto"), + Map.entry("boolean", "bool"), + Map.entry("string", "char*"), + Map.entry("number", "int"), + Map.entry("void", "void"), + Map.entry("[]", "*") + ); + + private final Map tsTokenMap = Map.ofEntries( + Map.entry("\"", ""), + Map.entry("*", ""), + Map.entry("&", ""), + Map.entry("(", ""), + Map.entry(")", "") + ); + + /** + * 构造函数 + */ + GenNapiCppFile() { + + } + + /** + * 将 cpp key 转换成 ts key + * + * @param cppKey 枚举对象列表 + * @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 && value.contains(CPP_STAR_TOKEN)) { + return cppKey.substring(0, ret) + value; + } else if (ret >= 0) { + return value; + } + } + return retKey; + } + + /** + * 将cpp token 替换成对应的dts token + * + * @param cppKey 语言关键字 + * @return 替换后字符串 + */ + private String replaceTsToken(String cppKey) { + String retKey = cppKey; + for (Map.Entry entry : tsTokenMap.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + int ret = retKey.indexOf(key); + if (ret >= 0) { + retKey = retKey.replace(key, value); + } + } + return retKey; + } + + /** + * 获得接口内容 + * + * @return 接口内容 + */ + public String getInterfaceContent() { + return interfaceContent; + } + + /** + * 获得枚举内容 + * + * @return 枚举内容 + */ + public String getEnumContent() { + return enumContent; + } + + /** + * 获得类内容 + * + * @return 类内容 + */ + public String getClassContent() { + return classContent; + } + + /** + * 获得方法内容 + * + * @return 方法内容 + */ + public String getFuncContent() { + return funcContent; + } + + /** + * 获得结构体内容 + * + * @return 结构体内容 + */ + public String getStructContent() { + return structContent; + } + + /** + * 获得type内容 + * + * @return type内容 + */ + public String getTypeContent() { + return typeContent; + } + + /** + * 获得联合体内容 + * + * @return 联合体内容 + */ + public String getUnionContent() { + return unionContent; + } + + /** + * 获得常量内容 + * + * @return 常量内容 + */ + public String getConstContent() { + return constContent; + } + + /** + * 生成输出内容 + * + * @param po 解析类 + */ + @Override + public void genContent(ParseObj po) { + genInterfaceList(po.getInterfaceList()); + genEnumList(po.getEnumList()); + genClassList(po.getClassList()); + genFuncList(po.getFuncList()); + genStructList(po.getStructList()); + genTypeList(po.getTypeList()); + genUnionList(po.getUnionList()); + genVarList(po.getVarList()); + } + + /** + * 生成文件 + * + * @param fileName 文件名 + * @param filePath 文件路径 + */ + @Override + public void genFile(String filePath, String fileName) { + System.out.println("genFile : " + filePath + fileName); + String outFileName = filePath + File.separator + CPP_FILE_PREFIX + + fileName.replace(".", "_") + CPP_FILE_H_SUFFIX; + System.out.println("outFileName : " + outFileName); + + FileUtils.createFile(outFileName); + + FileUtils.appendText(outFileName, this.genFileHeader(filePath + File.separator + fileName)); + FileUtils.appendText(outFileName, this.constContent); + FileUtils.appendText(outFileName, this.enumContent); + FileUtils.appendText(outFileName, this.typeContent); + FileUtils.appendText(outFileName, this.interfaceContent); + FileUtils.appendText(outFileName, this.unionContent); + FileUtils.appendText(outFileName, this.funcContent); + FileUtils.appendText(outFileName, this.structContent); + FileUtils.appendText(outFileName, this.classContent); + + } + + /** + * 生成输出内容 + * + * @param iol 接口列表 + */ + @Override + public void genInterfaceList(List iol) { + System.out.println("genInterfaceList" + iol.toString()); + }; + + /** + * 生成输出内容 + * + * @param eol 枚举列表 + */ + @Override + public void genEnumList(List eol) { + System.out.println("genEnumList" + eol.toString()); + + String resContent = ""; + for (EnumObj eo : eol) { + String enumName = eo.getName(); + enumName = !enumName.isEmpty() ? enumName : eo.getAlias(); + List memList = eo.getMemberList(); + List vaList = eo.getValueList(); + int i = 0; + resContent += CPP_NEW_LINE + CPP_ENUM_TOKEN + + CPP_BLANK_SPACE + enumName + CPP_BLANK_SPACE + CPP_LEFT_BRACE; + for (String memItem : memList) { + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + memItem; + if (vaList.size() > i && !vaList.get(i).isEmpty()) { + 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; + }; + + 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; + 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()); + 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()) { + tempResContent = StringUtils.removeLastCharacter(tempResContent, 2); + } + tempResContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; + } + return tempResContent; + } + + /** + * 生成输出内容 + * + * @param col 类列表 + */ + @Override + public void genClassList(List col) { + System.out.println("genClassList" + col.toString()); + + String resContent = ""; + 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) { + htStr += CPP_PUBLIC_TOKEN + CPP_BLANK_SPACE + hName + CPP_COMMA + CPP_BLANK_SPACE; + } + htStr = htStr.length() > 1 ? StringUtils.removeLastCharacter(htStr, 2) : htStr; + + 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(); + 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(); + if (!initVList.isEmpty()) { + resContent += CPP_EQUAL + initVList.get(0) + CPP_SEMICOLON; + } else { + resContent += CPP_SEMICOLON; + } + } + + resContent = setClassFunc(co.getFuncList(), resContent); + + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + } + this.classContent = resContent; + }; + + /** + * 生成输出内容 + * + * @param fol 方法列表 + */ + @Override + public void genFuncList(List fol) { + System.out.println("genFuncList : " + fol.toString()); + String resContent = ""; + 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 = 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()).isEmpty() ? + CPP_AUTO_TOKEN + CPP_BLANK_SPACE : ts2CppKey(poItem.getType()) + CPP_BLANK_SPACE; + String paName = poItem.getName(); + 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.isEmpty()) { + resContent = StringUtils.removeLastCharacter(resContent, 2); + } + resContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; + } + this.funcContent = resContent; + System.out.println("genFuncList : " + resContent); + }; + + /** + * 生成输出内容 + * + * @param sol 结构体列表 + */ + @Override + public void genStructList(List sol) { + System.out.println("genStructList" + sol.toString()); + + String resContent = ""; + for (StructObj so : sol) { + 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 + templateStr + CPP_STRUCT_TOKEN + + CPP_BLANK_SPACE + structName + CPP_BLANK_SPACE + CPP_LEFT_BRACE; + + for (ParamObj paItem : paList) { + String paType = paItem.getType().isEmpty() ? CPP_AUTO_TOKEN : paItem.getType(); + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + ts2CppKey(paType) + + CPP_BLANK_SPACE + paItem.getName(); + ; + List initVList = paItem.getvList(); + if (initVList.size() > 0) { + resContent += CPP_EQUAL + initVList.get(0) + CPP_SEMICOLON; + } else { + resContent += CPP_SEMICOLON; + } + } + + List funcList = so.getFuncList(); + for (FuncObj funcItem : funcList) { + 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()).isEmpty() ? + CPP_AUTO_TOKEN : ts2CppKey(poItem.getType()); + resContent += retType + CPP_BLANK_SPACE + replaceTsToken(poItem.getName()) + + CPP_COMMA + CPP_BLANK_SPACE; + } + resContent = !pol.isEmpty() ? StringUtils.removeLastCharacter(resContent, 2) : resContent; + resContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; + } + + resContent = StringUtils.removeLastSpace(resContent); + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + } + this.structContent = resContent; + }; + + /** + * 生成输出内容 + * + * @param tol 类型列表 + */ + @Override + public void genTypeList(List tol) { + System.out.println("genTypeList : " + tol.toString()); + }; + + /** + * 生成输出内容 + * + * @param uol 联合体列表 + */ + @Override + public void genUnionList(List uol) { + System.out.println("genUnionList : " + uol.toString()); + + String resContent = ""; + for (UnionObj uo : uol) { + String unionName = uo.getName(); + unionName = !unionName.isEmpty() ? unionName : uo.getAlias(); + + 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; + } + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE; + resContent += CPP_SEMICOLON + CPP_NEW_LINE; + } + this.unionContent = resContent; + }; + + private String genVarArrayList(String tmpContent, String paName, List paList) { + String resContent = tmpContent; + resContent += CPP_NEW_LINE + CPP_STRUCT_TOKEN + CPP_BLANK_SPACE + paName + + CPP_STRUCT_SUFFIX + CPP_BLANK_SPACE + CPP_LEFT_BRACE; + List paramList = paList.get(0).getPaList(); + for (ParamObj paItem : paramList) { + String paStr = paItem.getName(); + String paVal = paItem.getStrValue(0); + String typeStr = StringUtils.isAllDigits(paVal) ? + CPP_NUMBER_TOKEN : CPP_STD_STRING; + typeStr = StringUtils.isBoolean(paVal) ? CPP_BOOLEAN_TOKEN : typeStr; + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + typeStr + CPP_BLANK_SPACE + paStr + CPP_SEMICOLON; + } + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + + resContent += CPP_NEW_LINE + CPP_CONST_TOKEN + CPP_BLANK_SPACE + CPP_STD_VECTOR + + CPP_LEFT_ANGLE_BRACKET + paName + CPP_STRUCT_SUFFIX + CPP_RIGHT_ANGLE_BRACKET + + CPP_BLANK_SPACE + paName + CPP_EQUAL + CPP_LEFT_BRACE; + for (ParamObj paramListItem : paList) { + List subParamList = paramListItem.getPaList(); + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + CPP_LEFT_BRACE; + for (ParamObj paItem : subParamList) { + String paVal = paItem.getStrValue(0); + resContent += paVal + CPP_COMMA + CPP_BLANK_SPACE; + } + resContent = StringUtils.removeLastCharacter(resContent, 2); + resContent += CPP_RIGHT_BRACE + CPP_COMMA; + } + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + return resContent; + }; + + /** + * 生成输出内容 + * + * @param pol 常量列表 + */ + @Override + public void genVarList(List pol) { + System.out.println("genVarList : " + pol.toString()); + + String resContent = ""; + for (ParamObj po : pol) { + String paName = po.getName(); + String paType = ts2CppKey(po.getType()).isEmpty() ? CPP_AUTO_TOKEN : ts2CppKey(po.getType()); + String paValue = po.getStrValue(0); + List paList = po.getPaList(); + if (paList.isEmpty()) { + resContent += CPP_NEW_LINE + CPP_EXTENDS_TOKEN + CPP_BLANK_SPACE + CPP_CONST_TOKEN + + CPP_BLANK_SPACE + paType + CPP_BLANK_SPACE + paName + CPP_EQUAL + paValue; + + resContent += CPP_SEMICOLON + CPP_NEW_LINE; + } else if (paList.get(0).getPaList().isEmpty()) { + String valType = StringUtils.isAllDigits(paList.get(0).getStrValue(0)) ? + CPP_NUMBER_TOKEN : CPP_STD_STRING; + resContent += CPP_NEW_LINE + CPP_EXTENDS_TOKEN + CPP_BLANK_SPACE + CPP_CONST_TOKEN + + CPP_BLANK_SPACE + CPP_STD_MAP + CPP_LEFT_ANGLE_BRACKET + CPP_STD_STRING + + CPP_COMMA + CPP_BLANK_SPACE + valType + CPP_RIGHT_BRACE + CPP_BLANK_SPACE + + paName + CPP_EQUAL + CPP_LEFT_BRACE; + for (ParamObj paItem : paList) { + String pName = paItem.getName(); + String pVal = paItem.getStrValue(0); + resContent += CPP_NEW_LINE + CPP_TAB_SPACE + CPP_LEFT_BRACE + CPP_DOUBLE_QUOTATION + + pName + CPP_DOUBLE_QUOTATION + CPP_COMMA + CPP_BLANK_SPACE + pVal + + CPP_RIGHT_BRACE + CPP_COMMA; + } + resContent = StringUtils.removeLastCharacter(resContent, 1); + resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + } else if (!(paList.get(0).getPaList().isEmpty())) { + resContent = genVarArrayList(resContent, paName, paList); + } + + } + this.constContent = resContent; + System.out.println("genVarList : " + resContent); + } +} -- Gitee From 5e95d400b199340021767100e6330619d341a499 Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 15 Apr 2025 15:26:19 +0800 Subject: [PATCH 10/14] test aki cpp Signed-off-by: sunlian --- .../src/test/java/gen/GenAkiCppFileTest.java | 1609 +++++++++++++++++ 1 file changed, 1609 insertions(+) create mode 100644 src/intellij_plugin/ohosgen/src/test/java/gen/GenAkiCppFileTest.java diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenAkiCppFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenAkiCppFileTest.java new file mode 100644 index 00000000..43e7e71f --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenAkiCppFileTest.java @@ -0,0 +1,1609 @@ +/* + * 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 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.assertEquals; +import static utils.FileUtils.readText; + +/** + *

类名:该类用于xxx

+ * description + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +class GenAkiCppFileTest { + + @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("AKICPP"); + gb.genEnumList(po.getEnumList()); + + if (gb instanceof GenAkiCppFile gdf) { + String enumContent = gdf.getEnumContent(); + System.out.println("genEnum: " + enumContent); + String expect = "\nenum TestEnum {\n" + + "\tONE,\n" + + "\tTWO,\n" + + "};\n" + + "\n" + + "JSBIND_ENUM(TestEnum) {\n" + + "\tJSBIND_ENUM_VALUE(ONE);\n" + + "\tJSBIND_ENUM_VALUE(TWO);\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("AKICPP"); + gb.genEnumList(po.getEnumList()); + + if (gb instanceof GenAkiCppFile 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" + + "JSBIND_ENUM(Colors) {\n" + + "\tJSBIND_ENUM_VALUE(Red);\n" + + "\tJSBIND_ENUM_VALUE(Green);\n" + + "\tJSBIND_ENUM_VALUE(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("AKICPP"); + gb.genEnumList(po.getEnumList()); + + if (gb instanceof GenAkiCppFile 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" + + "\n" + + "JSBIND_ENUM(Colors) {\n" + + "\tJSBIND_ENUM_VALUE(Red);\n" + + "\tJSBIND_ENUM_VALUE(Green);\n" + + "\tJSBIND_ENUM_VALUE(Blue);\n" + + "};\n"; + assertEquals(expect, enumContent); + } + } + + @Test + void getEnumContent4() { + EnumObj eo = new EnumObj(); + eo.setName("TestEnum"); + List ml = new CopyOnWriteArrayList<>(); + ml.add("ONE"); + ml.add("TWO"); + eo.setMemberList(ml); + + EnumObj eo1 = new EnumObj(); + eo1.setName("Colors"); + List ml1 = new CopyOnWriteArrayList<>(); + ml1.add("BLACK"); + ml1.add("WHITE"); + eo1.setMemberList(ml1); + + List eol = new CopyOnWriteArrayList<>(); + eol.add(eo); + eol.add(eo1); + + ParseObj po = new ParseObj(); + po.setEnumList(eol); + GeneratorBase gb = GenerateFactory.getGenerator("AKICPP"); + gb.genEnumList(po.getEnumList()); + + if (gb instanceof GenAkiCppFile gdf) { + String enumContent = gdf.getEnumContent(); + System.out.println("genEnum: " + enumContent); + String expect = "\nenum TestEnum {\n" + + "\tONE,\n" + + "\tTWO,\n" + + "};\n" + + "\n" + + "JSBIND_ENUM(TestEnum) {\n" + + "\tJSBIND_ENUM_VALUE(ONE);\n" + + "\tJSBIND_ENUM_VALUE(TWO);\n" + + "};\n" + + "\n" + + "enum Colors {\n" + + "\tBLACK,\n" + + "\tWHITE,\n" + + "};\n" + + "\n" + + "JSBIND_ENUM(Colors) {\n" + + "\tJSBIND_ENUM_VALUE(BLACK);\n" + + "\tJSBIND_ENUM_VALUE(WHITE);\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("AKICPP"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenAkiCppFile 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" + + "\n" + + "JSBIND_CLASS(TestClass)\n" + + "{\n" + + "\tJSBIND_METHOD(add, \"add\");\n" + + "\tJSBIND_PMETHOD(add, \"addPromise\");\n" + + "\tJSBIND_PROPERTY(name);\n" + + "\tJSBIND_PROPERTY(age);\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("AKICPP"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenAkiCppFile 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" + + "\n" + + "JSBIND_CLASS(TestClass)\n" + + "{\n" + + "\tJSBIND_CONSTRUCTOR<>();\n" + + "\tJSBIND_PROPERTY(name);\n" + + "\tJSBIND_PROPERTY(age);\n" + + "\tJSBIND_PROPERTY(no);\n" + + "\tJSBIND_PROPERTY(addr);\n" + + "};\n"; + assertEquals(expect, classContent); + } + } + + @Test + 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("AKICPP"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenAkiCppFile 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" + + "\n" + + "JSBIND_CLASS(Employee)\n" + + "{\n" + + "\tJSBIND_CONSTRUCTOR<>();\n" + + "\tJSBIND_METHOD(displayName, \"displayName\");\n" + + "\tJSBIND_PMETHOD(displayName, \"displayNamePromise\");\n" + + "\tJSBIND_PROPERTY(empCode);\n" + + "\tJSBIND_PROPERTY(currentUser);\n" + + "\tJSBIND_PROPERTY(pi);\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("AKICPP"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenAkiCppFile gdf) { + String classContent = gdf.getClassContent(); + System.out.println("genClass: " + classContent); + String expect = "\nclass myClass {\n" + + "\tauto foo();\n" + + "};\n" + + "\n" + + "JSBIND_CLASS(myClass)\n" + + "{\n" + + "\tJSBIND_METHOD(foo, \"foo\");\n" + + "\tJSBIND_PMETHOD(foo, \"fooPromise\");\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("AKICPP"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenAkiCppFile 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" + + "\n" + + "JSBIND_CLASS(KeyValuePair)\n" + + "{\n" + + "\tJSBIND_METHOD(setKeyValue, \"setKeyValue\");\n" + + "\tJSBIND_PMETHOD(setKeyValue, \"setKeyValuePromise\");\n" + + "\tJSBIND_PROPERTY(key);\n" + + "\tJSBIND_PROPERTY(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("AKICPP"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenAkiCppFile 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" + + "\n" + + "JSBIND_CLASS(kvProcessor)\n" + + "{\n" + + "\tJSBIND_METHOD(process, \"process\");\n" + + "\tJSBIND_PMETHOD(process, \"processPromise\");\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("AKICPP"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenAkiCppFile gdf) { + String classContent = gdf.getClassContent(); + System.out.println("genClass: " + classContent); + String expect = "\nclass Shape {\n" + + "\tvoid process(auto key, auto val);\n" + + "};\n" + + "\n" + + "JSBIND_CLASS(Shape)\n" + + "{\n" + + "\tJSBIND_METHOD(process, \"process\");\n" + + "\tJSBIND_PMETHOD(process, \"processPromise\");\n" + + "};\n"; + assertEquals(expect, classContent); + } + } + + @Test + void getClassContent8() { + 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); + + ClassObj co1 = new ClassObj(); + co1.setName("myClass2"); + + List col = new CopyOnWriteArrayList<>(); + col.add(co); + col.add(co1); + + ParseObj po = new ParseObj(); + po.setClassList(col); + GeneratorBase gb = GenerateFactory.getGenerator("AKICPP"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenAkiCppFile gdf) { + String classContent = gdf.getClassContent(); + System.out.println("genClass: " + classContent); + String expect = "\nclass myClass {\n" + + "\tauto foo();\n" + + "};\n" + + "\n" + + "JSBIND_CLASS(myClass)\n" + + "{\n" + + "\tJSBIND_METHOD(foo, \"foo\");\n" + + "\tJSBIND_PMETHOD(foo, \"fooPromise\");\n" + + "};\n" + + "\n" + + "class myClass2 {\n" + + "};\n" + + "\n" + + "JSBIND_CLASS(myClass2)\n" + + "{\n" + + "};\n"; + assertEquals(expect, classContent); + } + } + + @Test + void getFuncContent1() { + 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("AKICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenAkiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nvoid TestFunc(char* name, int age);\n" + + "\n" + + "JSBIND_GLOBAL()\n" + + "{\n" + + "\tJSBIND_FUNCTION(TestFunc, \"TestFunc\");\n" + + "\tJSBIND_PFUNCTION(TestFunc, \"TestFuncPromise\");\n" + + "};\n"; + assertEquals(expect, funcContent); + } + } + + @Test + 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("AKICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenAkiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nchar* ToCapital(char* str, int length = 0);\n" + + "\n" + + "JSBIND_GLOBAL()\n" + + "{\n" + + "\tJSBIND_FUNCTION(ToCapital, \"ToCapital\");\n" + + "\tJSBIND_PFUNCTION(ToCapital, \"ToCapitalPromise\");\n" + + "};\n"; + 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("AKICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenAkiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nchar* Nemw(char* str = \"joke\", int length = 0);\n" + + "\n" + + "JSBIND_GLOBAL()\n" + + "{\n" + + "\tJSBIND_FUNCTION(Nemw, \"Nemw\");\n" + + "\tJSBIND_PFUNCTION(Nemw, \"NemwPromise\");\n" + + "};\n"; + 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("AKICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenAkiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nchar* Nemw(auto str, auto length);\n" + + "\n" + + "JSBIND_GLOBAL()\n" + + "{\n" + + "\tJSBIND_FUNCTION(Nemw, \"Nemw\");\n" + + "\tJSBIND_PFUNCTION(Nemw, \"NemwPromise\");\n" + + "};\n"; + 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("AKICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenAkiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nNemw(auto str, auto length);\n" + + "\n" + + "JSBIND_GLOBAL()\n" + + "{\n" + + "\tJSBIND_FUNCTION(Nemw, \"Nemw\");\n" + + "\tJSBIND_PFUNCTION(Nemw, \"NemwPromise\");\n" + + "};\n"; + 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("AKICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenAkiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\ntemplate T* getArray(T* items);\n" + + "\n" + + "JSBIND_GLOBAL()\n" + + "{\n" + + "\tJSBIND_FUNCTION(getArray, \"getArray\");\n" + + "\tJSBIND_PFUNCTION(getArray, \"getArrayPromise\");\n" + + "};\n"; + 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("AKICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenAkiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\ntemplate void displayType(T id, U name);\n" + + "\n" + + "JSBIND_GLOBAL()\n" + + "{\n" + + "\tJSBIND_FUNCTION(displayType, \"displayType\");\n" + + "\tJSBIND_PFUNCTION(displayType, \"displayTypePromise\");\n" + + "};\n"; + assertEquals(expect, funcContent); + } + } + + @Test + void getFuncContent8() { + 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); + + FuncObj fo1 = new FuncObj(); + fo1.setName("getCnt"); + fo1.setRetValue("int"); + + List fol = new CopyOnWriteArrayList<>(); + fol.add(fo); + fol.add(fo1); + ParseObj po = new ParseObj(); + po.setFuncList(fol); + GeneratorBase gb = GenerateFactory.getGenerator("AKICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenAkiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nchar* Nemw(auto str, auto length);\n" + + "\n" + + "int getCnt();\n" + + "\n" + + "JSBIND_GLOBAL()\n" + + "{\n" + + "\tJSBIND_FUNCTION(Nemw, \"Nemw\");\n" + + "\tJSBIND_PFUNCTION(Nemw, \"NemwPromise\");\n" + + "\tJSBIND_FUNCTION(getCnt, \"getCnt\");\n" + + "\tJSBIND_PFUNCTION(getCnt, \"getCntPromise\");\n" + + "};\n"; + assertEquals(expect, funcContent); + } + } + + @Test + void getFuncContent9() { + FuncObj fo = new FuncObj(); + fo.setName("TestFunc"); + fo.setRetValue("void"); + fo.addParam("name", "string"); + + FuncObj foItem = new FuncObj(); + foItem.setRetValue("boolean"); + foItem.addParam("value", "boolean"); + + ParamObj paItem = new ParamObj(); + paItem.setName("func"); + paItem.setType("(value:boolean)=>boolean"); + paItem.addFunc(foItem); + + fo.addParam(paItem); + + List fol = new CopyOnWriteArrayList<>(); + fol.add(fo); + ParseObj po = new ParseObj(); + po.setFuncList(fol); + GeneratorBase gb = GenerateFactory.getGenerator("AKICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenAkiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nvoid TestFunc(char* name, aki::SafetyCallback func);\n" + + "\n" + + "JSBIND_GLOBAL()\n" + + "{\n" + + "\tJSBIND_FUNCTION(TestFunc, \"TestFunc\");\n" + + "\tJSBIND_PFUNCTION(TestFunc, \"TestFuncPromise\");\n" + + "};\n"; + assertEquals(expect, funcContent); + } + } + + @Test + void getStructContent1() { + 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("AKICPP"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenAkiCppFile 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 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("AKICPP"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenAkiCppFile 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("AKICPP"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenAkiCppFile 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("AKICPP"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenAkiCppFile gdf) { + String structContent = gdf.getStructContent(); + System.out.println("genStruct: " + structContent); + String expect = "\nstruct TestStruct {\n" + + "};\n"; + assertEquals(expect, structContent); + } + } + + @Test + void getTypeContent() { + } + + @Test + void getUnionContent1() { + 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("AKICPP"); + gb.genUnionList(po.getUnionList()); + + if (gb instanceof GenAkiCppFile 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 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("AKICPP"); + gb.genUnionList(po.getUnionList()); + + if (gb instanceof GenAkiCppFile 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 getVarContent1() { + ParamObj paObj = new ParamObj(); + paObj.setName("employeeName"); + paObj.setStrValue("\"John\""); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("AKICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenAkiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const auto employeeName = \"John\";\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent2() { + ParamObj paObj = new ParamObj(); + paObj.setName("employeeName"); + paObj.setType("string"); + paObj.setStrValue("\"John\""); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("AKICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenAkiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const char* employeeName = \"John\";\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent3() { + ParamObj paObj = new ParamObj(); + paObj.setName("num1"); + paObj.setType("number"); + paObj.setStrValue("1"); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("AKICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenAkiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const int num1 = 1;\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent4() { + ParamObj paObj = new ParamObj(); + paObj.setName("playerCodes"); + + ParamObj paItem1 = new ParamObj(); + paItem1.setName("player1"); + paItem1.setStrValue("9"); + paObj.addParam(paItem1); + ParamObj paItem2 = new ParamObj(); + paItem2.setName("player2"); + paItem2.setStrValue("10"); + paObj.addParam(paItem2); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("AKICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenAkiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const std::map pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("AKICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenAkiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const auto playerCodes.player2 = 11;\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent6() { + ParamObj paObj = new ParamObj(); + paObj.setName("ROUTES"); + paObj.setType("any[]"); + + ParamObj paListItem1 = new ParamObj(); + ParamObj paItem1 = new ParamObj(); + paItem1.setName("path"); + paItem1.setStrValue("'/dashboard'"); + paListItem1.addParam(paItem1); + + ParamObj paItem3 = new ParamObj(); + paItem3.setName("allowAnonymous"); + paItem3.setStrValue("false"); + paListItem1.addParam(paItem3); + paObj.addParam(paListItem1); + + ParamObj paListItem2 = new ParamObj(); + ParamObj paItem21 = new ParamObj(); + paItem21.setName("path"); + paItem21.setStrValue("'/deals'"); + paListItem2.addParam(paItem21); + + ParamObj paItem23 = new ParamObj(); + paItem23.setName("allowAnonymous"); + paItem23.setStrValue("true"); + paListItem2.addParam(paItem23); + paObj.addParam(paListItem2); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("AKICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenAkiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nstruct ROUTESST {\n" + + "\tstd::string path;\n" + + "\tboolean allowAnonymous;\n" + + "};\n" + + "\n" + + "const std::vector ROUTES = {\n" + + "\t{'/dashboard', false},\n" + + "\t{'/deals', true},\n" + + "};\n"; + assertEquals(expect, constContent); + } + } + + @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("AKICPP"); + gb.genVarList(pol); + + if (gb instanceof GenAkiCppFile gdf) { + String varContent = gdf.getConstContent(); + System.out.println("genVar: " + varContent); + String expect = "\nextends const 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("AKICPP"); + gb.genContent(po); + + if (gb instanceof GenAkiCppFile gdf) { + String varContent = gdf.getConstContent(); + System.out.println("genVar: " + varContent); + String expect = "\nextends const 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("AKICPP"); + gb.genContent(po); + gb.genFile("./", "testGenFile.h"); + + File file = new File("./ag_akitestGenFile_h.cpp"); + assertEquals(true, file.exists()); + assertEquals(false, file.isDirectory()); + + List fcList = readText("./ag_akitestGenFile_h.cpp"); + + assertEquals("// Generated from ./\\testGenFile.h by KaiHong ohgen 1.0.0-PLUGIN", + fcList.get(0)); + + assertEquals("#include ", + fcList.get(1)); + assertEquals("#include ", + fcList.get(2)); + assertEquals("", + fcList.get(3)); + assertEquals("JSBIND_ADDON(testGenFileh)", + fcList.get(4)); + assertEquals("extends const int TestParam = 100;", + fcList.get(6)); + + if (gb instanceof GenAkiCppFile gdf) { + String varContent = gdf.getConstContent(); + System.out.println("genVar: " + varContent); + String expect = "\nextends const 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("AKICPP"); + gb.genEnumList(po.getEnumList()); + + if (gb instanceof GenAkiCppFile gdf) { + String enumContent = gdf.getEnumContent(); + System.out.println("genEnum: " + enumContent); + String expect = "\nenum TestEnum {\n" + + "\tONE = 1,\n" + + "\tTWO = 2,\n" + + "};\n" + + "\n" + + "JSBIND_ENUM(TestEnum) {\n" + + "\tJSBIND_ENUM_VALUE(ONE);\n" + + "\tJSBIND_ENUM_VALUE(TWO);\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("AKICPP"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenAkiCppFile 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" + + "\n" + + "JSBIND_CLASS(TestClass)\n" + + "{\n" + + "\tJSBIND_METHOD(add, \"add\");\n" + + "\tJSBIND_PMETHOD(add, \"addPromise\");\n" + + "\tJSBIND_METHOD(delete, \"delete\");\n" + + "\tJSBIND_PMETHOD(delete, \"deletePromise\");\n" + + "\tJSBIND_PROPERTY(name);\n" + + "\tJSBIND_PROPERTY(age);\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("AKICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenAkiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nvoid TestFunc(char* name, int age);\n" + + "\n" + + "JSBIND_GLOBAL()\n" + + "{\n" + + "\tJSBIND_FUNCTION(TestFunc, \"TestFunc\");\n" + + "\tJSBIND_PFUNCTION(TestFunc, \"TestFuncPromise\");\n" + + "};\n"; + 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("AKICPP"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenAkiCppFile 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() { + TypeObj to = new TypeObj(); + } + + @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("AKICPP"); + gb.genUnionList(po.getUnionList()); + + if (gb instanceof GenAkiCppFile 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("AKICPP"); + gb.genVarList(pol); + + if (gb instanceof GenAkiCppFile gdf) { + String varContent = gdf.getConstContent(); + System.out.println("genVar: " + varContent); + String expect = "\nextends const int TestParam = 100;\n"; + assertEquals(expect, varContent); + } + } +} \ No newline at end of file -- Gitee From 914efb780f558845c4e54fa14f0174b26f7b4602 Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 15 Apr 2025 15:26:43 +0800 Subject: [PATCH 11/14] test c h Signed-off-by: sunlian --- .../src/test/java/gen/GenCHFileTest.java | 1287 +++++++++++++++++ 1 file changed, 1287 insertions(+) create mode 100644 src/intellij_plugin/ohosgen/src/test/java/gen/GenCHFileTest.java diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenCHFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenCHFileTest.java new file mode 100644 index 00000000..1d4fa2fc --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenCHFileTest.java @@ -0,0 +1,1287 @@ +/* + * 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 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.assertEquals; +import static utils.FileUtils.readText; + +/** + *

类名:该类用于xxx

+ * description + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +class GenCHFileTest { + + @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 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"); + 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 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"); + + 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 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 getUnionContent1() { + 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 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 getVarContent1() { + ParamObj paObj = new ParamObj(); + paObj.setName("employeeName"); + paObj.setStrValue("\"John\""); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const auto employeeName = \"John\";\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent2() { + ParamObj paObj = new ParamObj(); + paObj.setName("employeeName"); + paObj.setType("string"); + paObj.setStrValue("\"John\""); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const char* employeeName = \"John\";\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent3() { + ParamObj paObj = new ParamObj(); + paObj.setName("num1"); + paObj.setType("number"); + paObj.setStrValue("1"); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const int num1 = 1;\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent4() { + ParamObj paObj = new ParamObj(); + paObj.setName("playerCodes"); + + ParamObj paItem1 = new ParamObj(); + paItem1.setName("player1"); + paItem1.setStrValue("9"); + paObj.addParam(paItem1); + ParamObj paItem2 = new ParamObj(); + paItem2.setName("player2"); + paItem2.setStrValue("10"); + paObj.addParam(paItem2); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const std::map pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const auto playerCodes.player2 = 11;\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent6() { + ParamObj paObj = new ParamObj(); + paObj.setName("ROUTES"); + paObj.setType("any[]"); + + ParamObj paListItem1 = new ParamObj(); + ParamObj paItem1 = new ParamObj(); + paItem1.setName("path"); + paItem1.setStrValue("'/dashboard'"); + paListItem1.addParam(paItem1); + + ParamObj paItem3 = new ParamObj(); + paItem3.setName("allowAnonymous"); + paItem3.setStrValue("false"); + paListItem1.addParam(paItem3); + paObj.addParam(paListItem1); + + ParamObj paListItem2 = new ParamObj(); + ParamObj paItem21 = new ParamObj(); + paItem21.setName("path"); + paItem21.setStrValue("'/deals'"); + paListItem2.addParam(paItem21); + + ParamObj paItem23 = new ParamObj(); + paItem23.setName("allowAnonymous"); + paItem23.setStrValue("true"); + paListItem2.addParam(paItem23); + paObj.addParam(paListItem2); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nstruct ROUTESST {\n" + + "\tstd::string path;\n" + + "\tboolean allowAnonymous;\n" + + "};\n" + + "\n" + + "const std::vector ROUTES = {\n" + + "\t{'/dashboard', false},\n" + + "\t{'/deals', true},\n" + + "};\n"; + assertEquals(expect, constContent); + } + } + + @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 = "\nextends const 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 = "\nextends const 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 = "\nextends const 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() { + TypeObj to = new TypeObj(); + } + + @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 = "\nextends const int TestParam = 100;\n"; + assertEquals(expect, varContent); + } + } +} \ No newline at end of file -- Gitee From d8eb444e11ef528fe6d9fa8eb294cf4cc0c43ada Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 15 Apr 2025 15:27:24 +0800 Subject: [PATCH 12/14] gen cpp h Signed-off-by: sunlian --- .../src/test/java/gen/GenCppHFileTest.java | 1287 +++++++++++++++++ 1 file changed, 1287 insertions(+) create mode 100644 src/intellij_plugin/ohosgen/src/test/java/gen/GenCppHFileTest.java diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppHFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppHFileTest.java new file mode 100644 index 00000000..59b8405e --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenCppHFileTest.java @@ -0,0 +1,1287 @@ +/* + * 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 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.assertEquals; +import static utils.FileUtils.readText; + +/** + *

类名:该类用于xxx

+ * description + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +class GenCppHFileTest { + + @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("CPPH"); + gb.genEnumList(po.getEnumList()); + + if (gb instanceof GenCppHFile 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("CPPH"); + gb.genEnumList(po.getEnumList()); + + if (gb instanceof GenCppHFile 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("CPPH"); + gb.genEnumList(po.getEnumList()); + + if (gb instanceof GenCppHFile 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("CPPH"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenCppHFile gdf) { + String classContent = gdf.getClassContent(); + System.out.println("genClass: " + classContent); + String expect = "\nclass TestClass {\n" + + "\tstd::string 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("CPPH"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenCppHFile gdf) { + String classContent = gdf.getClassContent(); + System.out.println("genClass: " + classContent); + String expect = "\nclass TestClass : public IPerson {\n" + + "\tpublic std::string name;\n" + + "\tprivate int age;\n" + + "\tprotected std::string no;\n" + + "\treadonly std::string addr;\n" + + "\tconstructor();\n" + + "};\n"; + assertEquals(expect, classContent); + } + } + + @Test + 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("CPPH"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenCppHFile 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("CPPH"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenCppHFile 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("CPPH"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenCppHFile 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("CPPH"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenCppHFile 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("CPPH"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenCppHFile 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"); + 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("CPPH"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenCppHFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nvoid TestFunc(std::string name, int age);"; + assertEquals(expect, funcContent); + } + } + + @Test + 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("CPPH"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenCppHFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nstd::string ToCapital(std::string 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("CPPH"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenCppHFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nstd::string Nemw(std::string 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("CPPH"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenCppHFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nstd::string 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("CPPH"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenCppHFile 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("CPPH"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenCppHFile 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("CPPH"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenCppHFile 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"); + + 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("CPPH"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenCppHFile gdf) { + String structContent = gdf.getStructContent(); + System.out.println("genStruct: " + structContent); + String expect = "\nstruct TestStruct {\n" + + "\tstd::string name;\n" + + "\tbool age;\n" + + "\tint add(bool a, bool b);\n" + + "};\n"; + assertEquals(expect, structContent); + } + } + + @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("CPPH"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenCppHFile 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("CPPH"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenCppHFile 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("CPPH"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenCppHFile gdf) { + String structContent = gdf.getStructContent(); + System.out.println("genStruct: " + structContent); + String expect = "\nstruct TestStruct {\n" + + "};\n"; + assertEquals(expect, structContent); + } + } + + @Test + void getTypeContent() { + } + + @Test + void getUnionContent1() { + 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("CPPH"); + gb.genUnionList(po.getUnionList()); + + if (gb instanceof GenCppHFile gdf) { + String unionContent = gdf.getUnionContent(); + System.out.println("genUnion: " + unionContent); + String expect = "\nunion TestUnion{\n" + + "\tstd::string name;\n" + + "\tint age;\n" + + "};\n"; + assertEquals(expect, unionContent); + } + } + + @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("CPPH"); + gb.genUnionList(po.getUnionList()); + + if (gb instanceof GenCppHFile 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 getVarContent1() { + ParamObj paObj = new ParamObj(); + paObj.setName("employeeName"); + paObj.setStrValue("\"John\""); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPPH"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppHFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const auto employeeName = \"John\";\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent2() { + ParamObj paObj = new ParamObj(); + paObj.setName("employeeName"); + paObj.setType("string"); + paObj.setStrValue("\"John\""); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPPH"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppHFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const std::string employeeName = \"John\";\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent3() { + ParamObj paObj = new ParamObj(); + paObj.setName("num1"); + paObj.setType("number"); + paObj.setStrValue("1"); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPPH"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppHFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const int num1 = 1;\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent4() { + ParamObj paObj = new ParamObj(); + paObj.setName("playerCodes"); + + ParamObj paItem1 = new ParamObj(); + paItem1.setName("player1"); + paItem1.setStrValue("9"); + paObj.addParam(paItem1); + ParamObj paItem2 = new ParamObj(); + paItem2.setName("player2"); + paItem2.setStrValue("10"); + paObj.addParam(paItem2); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPPH"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppHFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const std::map pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPPH"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppHFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const auto playerCodes.player2 = 11;\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent6() { + ParamObj paObj = new ParamObj(); + paObj.setName("ROUTES"); + paObj.setType("any[]"); + + ParamObj paListItem1 = new ParamObj(); + ParamObj paItem1 = new ParamObj(); + paItem1.setName("path"); + paItem1.setStrValue("'/dashboard'"); + paListItem1.addParam(paItem1); + + ParamObj paItem3 = new ParamObj(); + paItem3.setName("allowAnonymous"); + paItem3.setStrValue("false"); + paListItem1.addParam(paItem3); + paObj.addParam(paListItem1); + + ParamObj paListItem2 = new ParamObj(); + ParamObj paItem21 = new ParamObj(); + paItem21.setName("path"); + paItem21.setStrValue("'/deals'"); + paListItem2.addParam(paItem21); + + ParamObj paItem23 = new ParamObj(); + paItem23.setName("allowAnonymous"); + paItem23.setStrValue("true"); + paListItem2.addParam(paItem23); + paObj.addParam(paListItem2); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPPH"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppHFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nstruct ROUTESST {\n" + + "\tstd::string path;\n" + + "\tboolean allowAnonymous;\n" + + "};\n" + + "\n" + + "const std::vector ROUTES = {\n" + + "\t{'/dashboard', false},\n" + + "\t{'/deals', true},\n" + + "};\n"; + assertEquals(expect, constContent); + } + } + + @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("CPPH"); + gb.genVarList(pol); + + if (gb instanceof GenCppHFile gdf) { + String varContent = gdf.getConstContent(); + System.out.println("genVar: " + varContent); + String expect = "\nextends const 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("CPPH"); + gb.genContent(po); + + if (gb instanceof GenCppHFile gdf) { + String varContent = gdf.getConstContent(); + System.out.println("genVar: " + varContent); + String expect = "\nextends const 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("CPPH"); + 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 GenCppHFile gdf) { + String varContent = gdf.getConstContent(); + System.out.println("genVar: " + varContent); + String expect = "\nextends const 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("CPPH"); + gb.genEnumList(po.getEnumList()); + + if (gb instanceof GenCppHFile 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("CPPH"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenCppHFile gdf) { + String classContent = gdf.getClassContent(); + System.out.println("genClass: " + classContent); + String expect = "\nclass TestClass {\n" + + "\tstd::string 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("CPPH"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenCppHFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nvoid TestFunc(std::string 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("CPPH"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenCppHFile gdf) { + String structContent = gdf.getStructContent(); + System.out.println("genStruct: " + structContent); + String expect = "\nstruct TestStruct {\n" + + "\tstd::string name;\n" + + "\tint age;\n" + + "\tint add(int a, int b);\n" + + "};\n"; + assertEquals(expect, structContent); + } + } + + @Test + void genTypeList() { + TypeObj to = new TypeObj(); + } + + @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("CPPH"); + gb.genUnionList(po.getUnionList()); + + if (gb instanceof GenCppHFile 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("CPPH"); + gb.genVarList(pol); + + if (gb instanceof GenCppHFile gdf) { + String varContent = gdf.getConstContent(); + System.out.println("genVar: " + varContent); + String expect = "\nextends const int TestParam = 100;\n"; + assertEquals(expect, varContent); + } + } +} \ No newline at end of file -- Gitee From 6db8807448e6d0d5d64af465e883b351e63bdbb0 Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 15 Apr 2025 15:28:32 +0800 Subject: [PATCH 13/14] test napi cpp Signed-off-by: sunlian --- .../src/test/java/gen/GenNapiCppFileTest.java | 1287 +++++++++++++++++ 1 file changed, 1287 insertions(+) create mode 100644 src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java new file mode 100644 index 00000000..4b23a2b8 --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java @@ -0,0 +1,1287 @@ +/* + * 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 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.assertEquals; +import static utils.FileUtils.readText; + +/** + *

类名:该类用于xxx

+ * description + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +class GenNapiCppFileTest { + + @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 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"); + 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 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"); + + 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 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 getUnionContent1() { + 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 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 getVarContent1() { + ParamObj paObj = new ParamObj(); + paObj.setName("employeeName"); + paObj.setStrValue("\"John\""); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const auto employeeName = \"John\";\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent2() { + ParamObj paObj = new ParamObj(); + paObj.setName("employeeName"); + paObj.setType("string"); + paObj.setStrValue("\"John\""); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const char* employeeName = \"John\";\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent3() { + ParamObj paObj = new ParamObj(); + paObj.setName("num1"); + paObj.setType("number"); + paObj.setStrValue("1"); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const int num1 = 1;\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent4() { + ParamObj paObj = new ParamObj(); + paObj.setName("playerCodes"); + + ParamObj paItem1 = new ParamObj(); + paItem1.setName("player1"); + paItem1.setStrValue("9"); + paObj.addParam(paItem1); + ParamObj paItem2 = new ParamObj(); + paItem2.setName("player2"); + paItem2.setStrValue("10"); + paObj.addParam(paItem2); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const std::map pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const auto playerCodes.player2 = 11;\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent6() { + ParamObj paObj = new ParamObj(); + paObj.setName("ROUTES"); + paObj.setType("any[]"); + + ParamObj paListItem1 = new ParamObj(); + ParamObj paItem1 = new ParamObj(); + paItem1.setName("path"); + paItem1.setStrValue("'/dashboard'"); + paListItem1.addParam(paItem1); + + ParamObj paItem3 = new ParamObj(); + paItem3.setName("allowAnonymous"); + paItem3.setStrValue("false"); + paListItem1.addParam(paItem3); + paObj.addParam(paListItem1); + + ParamObj paListItem2 = new ParamObj(); + ParamObj paItem21 = new ParamObj(); + paItem21.setName("path"); + paItem21.setStrValue("'/deals'"); + paListItem2.addParam(paItem21); + + ParamObj paItem23 = new ParamObj(); + paItem23.setName("allowAnonymous"); + paItem23.setStrValue("true"); + paListItem2.addParam(paItem23); + paObj.addParam(paListItem2); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nstruct ROUTESST {\n" + + "\tstd::string path;\n" + + "\tboolean allowAnonymous;\n" + + "};\n" + + "\n" + + "const std::vector ROUTES = {\n" + + "\t{'/dashboard', false},\n" + + "\t{'/deals', true},\n" + + "};\n"; + assertEquals(expect, constContent); + } + } + + @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 = "\nextends const 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 = "\nextends const 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 = "\nextends const 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() { + TypeObj to = new TypeObj(); + } + + @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 = "\nextends const int TestParam = 100;\n"; + assertEquals(expect, varContent); + } + } +} \ No newline at end of file -- Gitee From 768548ccf67824ad583063791a4e6d619fc2cf7d Mon Sep 17 00:00:00 2001 From: sunlian Date: Tue, 15 Apr 2025 16:14:18 +0800 Subject: [PATCH 14/14] fix code check Signed-off-by: sunlian --- .../src/main/java/gen/GenAkiCppFile.java | 15 ++-- .../src/main/java/gen/GeneratorBase.java | 18 ++++- .../src/main/java/utils/FileUtils.java | 2 - .../src/test/java/gen/GenAkiCppFileTest.java | 72 ++++++++++--------- 4 files changed, 60 insertions(+), 47 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenAkiCppFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenAkiCppFile.java index 10e6332b..7e6676c9 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenAkiCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenAkiCppFile.java @@ -477,7 +477,6 @@ public class GenAkiCppFile extends GeneratorBase { } private String genCppClassContent(ClassObj co) { - String resContent = ""; String className = co.getName(); className = !className.isEmpty() ? className : co.getAlias(); @@ -503,6 +502,7 @@ public class GenAkiCppFile extends GeneratorBase { } htempStr = htempList.size() > 0 ? StringUtils.removeLastCharacter(htempStr, 2) + AKI_RIGHT_ANGLE_BRACKET : ""; + String resContent = ""; resContent += AKI_NEW_LINE + templateStr + AKI_CLASS_TOKEN + AKI_BLANK_SPACE + className + htStr + htempStr + AKI_BLANK_SPACE + AKI_LEFT_BRACE; List paList = co.getParamList(); @@ -526,15 +526,11 @@ public class GenAkiCppFile extends GeneratorBase { } private String genAkiCppClassContent(ClassObj co) { - String resContent = ""; String className = co.getName(); className = !className.isEmpty() ? className : co.getAlias(); String classDeclare = AKI_CLASS_DECLARE.replace(AKI_CLASS_NAME, className); - String classConstructDeclare = ""; String classPropertyDeclare = ""; - String classFunctionDeclare = ""; - String classPFunctionDeclare = ""; List paList = co.getParamList(); for (ParamObj paItem : paList) { @@ -543,6 +539,8 @@ public class GenAkiCppFile extends GeneratorBase { classDeclare = classDeclare.replace(AKI_PROPERTY_EXPRESSION, classPropertyDeclare); List foList = co.getFuncList(); + String classFunctionDeclare = ""; + String classConstructDeclare = ""; for (FuncObj foItem : foList) { if (foItem.getName().equals("constructor")) { String paramStr = ""; @@ -556,12 +554,13 @@ public class GenAkiCppFile extends GeneratorBase { classFunctionDeclare += AKI_METHOD_DECLARE.replace(AKI_METHOD_NAME, foItem.getName()); } } + + String classPFunctionDeclare = ""; classDeclare = classDeclare.replace(AKI_METHOD_EXPRESSION, classFunctionDeclare); classDeclare = classDeclare.replace(AKI_CONSTRUCTOR_EXPRESSION, classConstructDeclare); classDeclare = classDeclare.replace(AKI_PMETHOD_EXPRESSION, classPFunctionDeclare); - resContent = classDeclare; - return resContent; + return classDeclare; } /** @@ -582,7 +581,6 @@ public class GenAkiCppFile extends GeneratorBase { }; private String genCppFuncContent(FuncObj fo) { - String resContent = ""; String funcName = fo.getName(); funcName = !funcName.isEmpty() ? funcName : fo.getAlias(); List tempList = fo.getTempList(); @@ -595,6 +593,7 @@ public class GenAkiCppFile extends GeneratorBase { List paList = fo.getParamList(); String retValue = ts2CppKey(fo.getRetValue()).isEmpty() ? "" : ts2CppKey(fo.getRetValue()) + AKI_BLANK_SPACE; + String resContent = ""; resContent += AKI_NEW_LINE + tempStr + retValue + replaceTsToken(funcName) + AKI_LEFT_PARENTHESES; diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java index cd6734f8..414b6c78 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GeneratorBase.java @@ -17,7 +17,6 @@ package gen; import grammar.*; import utils.Constants; -import utils.FileUtils; import java.util.List; @@ -31,14 +30,27 @@ import java.util.List; * @since 2025-02-28 */ public class GeneratorBase { - private final String headerFormat = "// Generated from %s by KaiHong ohgen %s-PLUGIN"; + /** + * 生成文件时追加内容 + */ public static final String GEN_APPEND = "APPEND"; + + /** + * 生成文件时覆盖内容 + */ public static final String GEN_REPLACE = "REPLACE"; - private final String GEN_NEW = "NEW"; + /** + * 生成文件名 + */ protected String genFileName = ""; + + /** + * 生成文件模式 + */ protected String genMode = GEN_REPLACE; + private final String headerFormat = "// Generated from %s by KaiHong ohgen %s-PLUGIN"; /** * 生成文件头内容 diff --git a/src/intellij_plugin/ohosgen/src/main/java/utils/FileUtils.java b/src/intellij_plugin/ohosgen/src/main/java/utils/FileUtils.java index 6b3bfe41..f0a48213 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/utils/FileUtils.java +++ b/src/intellij_plugin/ohosgen/src/main/java/utils/FileUtils.java @@ -18,8 +18,6 @@ package utils; import java.io.File; import java.io.IOException; import java.io.*; -import java.nio.file.Files; -import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.List; diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenAkiCppFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenAkiCppFileTest.java index 43e7e71f..741327a3 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenAkiCppFileTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenAkiCppFileTest.java @@ -35,6 +35,40 @@ import static utils.FileUtils.readText; * @since 2025-02-28 */ class GenAkiCppFileTest { + private String classContentExpect2 = "\nclass TestClass : public IPerson {\n" + + "\tpublic char* name;\n" + + "\tprivate int age;\n" + + "\tprotected char* no;\n" + + "\treadonly char* addr;\n" + + "\tconstructor();\n" + + "};\n" + + "\n" + + "JSBIND_CLASS(TestClass)\n" + + "{\n" + + "\tJSBIND_CONSTRUCTOR<>();\n" + + "\tJSBIND_PROPERTY(name);\n" + + "\tJSBIND_PROPERTY(age);\n" + + "\tJSBIND_PROPERTY(no);\n" + + "\tJSBIND_PROPERTY(addr);\n" + + "};\n"; + + private String classContentExpect3 = "\nclass Employee : public Person {\n" + + "\tint empCode;\n" + + "\tauto currentUser;\n" + + "\tstatic int pi = 3.14;\n" + + "\tconstructor();\n" + + "\tvoid displayName();\n" + + "};\n" + + "\n" + + "JSBIND_CLASS(Employee)\n" + + "{\n" + + "\tJSBIND_CONSTRUCTOR<>();\n" + + "\tJSBIND_METHOD(displayName, \"displayName\");\n" + + "\tJSBIND_PMETHOD(displayName, \"displayNamePromise\");\n" + + "\tJSBIND_PROPERTY(empCode);\n" + + "\tJSBIND_PROPERTY(currentUser);\n" + + "\tJSBIND_PROPERTY(pi);\n" + + "};\n"; @Test void getInterfaceContent() { @@ -297,22 +331,7 @@ class GenAkiCppFileTest { if (gb instanceof GenAkiCppFile 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" + - "\n" + - "JSBIND_CLASS(TestClass)\n" + - "{\n" + - "\tJSBIND_CONSTRUCTOR<>();\n" + - "\tJSBIND_PROPERTY(name);\n" + - "\tJSBIND_PROPERTY(age);\n" + - "\tJSBIND_PROPERTY(no);\n" + - "\tJSBIND_PROPERTY(addr);\n" + - "};\n"; + String expect = classContentExpect2; assertEquals(expect, classContent); } } @@ -364,23 +383,7 @@ class GenAkiCppFileTest { if (gb instanceof GenAkiCppFile 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" + - "\n" + - "JSBIND_CLASS(Employee)\n" + - "{\n" + - "\tJSBIND_CONSTRUCTOR<>();\n" + - "\tJSBIND_METHOD(displayName, \"displayName\");\n" + - "\tJSBIND_PMETHOD(displayName, \"displayNamePromise\");\n" + - "\tJSBIND_PROPERTY(empCode);\n" + - "\tJSBIND_PROPERTY(currentUser);\n" + - "\tJSBIND_PROPERTY(pi);\n" + - "};\n"; + String expect = classContentExpect3; assertEquals(expect, classContent); } } @@ -516,7 +519,8 @@ class GenAkiCppFileTest { if (gb instanceof GenAkiCppFile 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" + "\n" + -- Gitee