changeset 486:8e40355a172b bgo-commands-codegen

Unit tests OK for preambles, enums and structs in both TS and C++
author bgo-osimis
date Fri, 15 Feb 2019 14:30:26 +0100
parents 772516adcbf6
children f6b7f113cf27
files Resources/CodeGeneration/stonegentool.py Resources/CodeGeneration/stonegentool_test.py Resources/CodeGeneration/test/test1.jsonc
diffstat 3 files changed, 78 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- 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 <string>
 #include <vector>
 #include <map>
+
 """ % (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"]
--- 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<string> someStrings;
     public Array<number> 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 <cstdint>
 #include <string>
 #include <vector>
 #include <map>
-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<string> someStrings;
     std::vector<int32_t> 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()
+
--- 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"
+        }
+      ]
     }
   ]
 }