# HG changeset patch # User bgo-osimis # Date 1550237426 -3600 # Node ID 8e40355a172b2f18471d20479e90ef04381f7a40 # Parent 772516adcbf695d9c547cea448f9d153d71d8d6a Unit tests OK for preambles, enums and structs in both TS and C++ diff -r 772516adcbf6 -r 8e40355a172b Resources/CodeGeneration/stonegentool.py --- a/Resources/CodeGeneration/stonegentool.py Fri Feb 15 12:07:09 2019 +0100 +++ b/Resources/CodeGeneration/stonegentool.py Fri Feb 15 14:30:26 2019 +0100 @@ -22,9 +22,22 @@ 12345678901234567890123456789012345678901234567890123456789012345678901234567890 """ -import json -import re +class GeneratedCode: + def __init__(self): + self.cppPreamble = StringIO() # file-wide preamble (#include directives, comment...) + self.cppEnums = StringIO() + self.cppStructs = StringIO() + self.cppDispatcher = StringIO() + self.cppHandler = StringIO() + self.tsPreamble = StringIO() # file-wide preamble (module directives, comment...) + self.tsEnums = StringIO() + self.tsStructs = StringIO() + self.tsDispatcher = StringIO() + self.tsHandler = StringIO() + + def FlattenToFiles(self,outputDir: str): + raise NotImplementedError() class JsonHelpers: """A set of utilities to perform JSON operations""" @@ -279,10 +292,8 @@ genOrderQueue, structTypes, structTypes[typeName] ) - def ProcessStructType_DepthFirstRecursive( - genOrderQueue: List[str], structTypes: Dict[str, Dict], typeDict: Dict -) -> None: + genOrderQueue: List[str], structTypes: Dict[str, Dict], typeDict: Dict) -> None: # let's generate the code according to the typeName: str = typeDict["name"] if typeDict["kind"] != "struct": @@ -327,9 +338,8 @@ tsText.write("};\n\n") cppText.write("};\n\n") - outputStreams['ts'].write(tsText.getvalue()) - outputStreams['cpp'].write(cppText.getvalue()) - + outputStreams.tsEnums.write(tsText.getvalue()) + outputStreams.cppEnums.write(cppText.getvalue()) def ProcessStructType( outputStreams: GeneratedCode, typeDict) -> None: @@ -353,9 +363,8 @@ tsText.write("};\n\n") cppText.write("};\n\n") - outputStreams['ts'].write(tsText.getvalue()) - outputStreams['cpp'].write(cppText.getvalue()) - + outputStreams.tsStructs.write(tsText.getvalue()) + outputStreams.cppStructs.write(cppText.getvalue()) def WritePreambles(rootName: str, outputStreams: GeneratedCode) -> None: outputStreams.cppPreamble.write("""// autogenerated by stonegentool on %s for module %s @@ -363,28 +372,12 @@ #include #include #include + """ % (time.ctime(),rootName)) outputStreams.tsPreamble.write("""// autogenerated by stonegentool on %s for module %s """ % (time.ctime(),rootName)) -class GeneratedCode: - def __init__(self): - self.cppPreamble = StringIO() # file-wide preamble (#include directives, comment...) - self.cppEnums = StringIO() - self.cppStructs = StringIO() - self.cppDispatcher = StringIO() - self.cppHandler = StringIO() - - self.tsPreamble = StringIO() # file-wide preamble (module directives, comment...) - self.tsEnums = StringIO() - self.tsStructs = StringIO() - self.tsDispatcher = StringIO() - self.tsHandler = StringIO() - - def FlattenToFiles(self,outputDir: str): - raise NotImplementedError() - def ProcessSchema(schema: dict) -> Tuple[str, GeneratedCode, List[str]]: CheckSchemaSchema(schema) rootName: str = schema["root_name"] diff -r 772516adcbf6 -r 8e40355a172b Resources/CodeGeneration/stonegentool_test.py --- a/Resources/CodeGeneration/stonegentool_test.py Fri Feb 15 12:07:09 2019 +0100 +++ b/Resources/CodeGeneration/stonegentool_test.py Fri Feb 15 14:30:26 2019 +0100 @@ -107,8 +107,8 @@ obj = LoadSchema(fn) (_,outputStreams,_) = ProcessSchema(obj) - tsRef = """// autogenerated by stonegentool on Fri Feb 15 07:36:51 2019 -enum MovieType + tsPreambleRef: str = "// autogenerated by stonegentool on Fri Feb 15 07:36:51 2019\n" + tsEnumsRef: str = """enum MovieType { Romcom, Horror, @@ -116,7 +116,17 @@ Vegetables }; -class A +enum CrispType +{ + SaltAndPepper, + CreamAndChives, + Paprika, + Barbecue +}; + +""" + + tsStructsRef: str = """class A { public Array someStrings; public Array someInts2; @@ -135,17 +145,22 @@ }; """ - tsRef = RemoveDateTimeLine(tsRef) - tsActual = RemoveDateTimeLine(outputStreams['ts'].getvalue()) - self.assertEqual(tsActual,tsRef) + tsPreambleRefCastrated: str = RemoveDateTimeLine(tsPreambleRef) + tsPreambleCastrated: str = RemoveDateTimeLine(outputStreams.tsPreamble.getvalue()) + self.assertEqual(tsPreambleRefCastrated,tsPreambleCastrated) + self.assertEqual(tsEnumsRef,outputStreams.tsEnums.getvalue()) + self.assertEqual(tsStructsRef,outputStreams.tsStructs.getvalue()) - cppRef="""// autogenerated by stonegentool on Fri Feb 15 07:36:51 2019 + cppPreambleRef: str = """// autogenerated by stonegentool on Fri Feb 15 07:36:51 2019 #include #include #include #include -enum MovieType + +""" + + cppEnumsRef: str = """enum MovieType { Romcom, Horror, @@ -153,7 +168,16 @@ Vegetables }; -struct A +enum CrispType +{ + SaltAndPepper, + CreamAndChives, + Paprika, + Barbecue +}; + +""" + cppStructsRef: str = """struct A { std::vector someStrings; std::vector someInts2; @@ -172,11 +196,11 @@ }; """ - cppRef = RemoveDateTimeLine(cppRef) - cppActual = RemoveDateTimeLine(outputStreams['cpp'].getvalue()) - - self.assertEqual(cppActual,cppRef) - pass + cppPreambleRefCastrated: str = RemoveDateTimeLine(cppPreambleRef) + cppPreambleCastrated: str = RemoveDateTimeLine(outputStreams.cppPreamble.getvalue()) + self.assertEqual(cppPreambleRefCastrated,cppPreambleCastrated) + self.assertEqual(cppEnumsRef,outputStreams.cppEnums.getvalue()) + self.assertEqual(cppStructsRef,outputStreams.cppStructs.getvalue()) def test_GenerateCppEnumeration(self): pass @@ -208,3 +232,4 @@ if __name__ == '__main__': unittest.main() + diff -r 772516adcbf6 -r 8e40355a172b Resources/CodeGeneration/test/test1.jsonc --- a/Resources/CodeGeneration/test/test1.jsonc Fri Feb 15 12:07:09 2019 +0100 +++ b/Resources/CodeGeneration/test/test1.jsonc Fri Feb 15 14:30:26 2019 +0100 @@ -64,6 +64,24 @@ "name":"Vegetables" } ] + }, + { + "name":"CrispType", + "kind":"enum", + "fields": [ + { + "name":"SaltAndPepper" + }, + { + "name":"CreamAndChives" + }, + { + "name":"Paprika" + }, + { + "name":"Barbecue" + } + ] } ] }