Mercurial > hg > orthanc-stone
annotate Resources/CodeGeneration/stonegentool_test.py @ 491:8e7e151ef472 bgo-commands-codegen
Unit tests pass for enum generation
author | bgo-osimis |
---|---|
date | Wed, 20 Feb 2019 20:51:30 +0100 |
parents | f6b7f113cf27 |
children | 6fbf2eae7c88 |
rev | line source |
---|---|
491 | 1 # |
2 # 1 2 3 4 5 6 7 8 | |
3 # 345678901234567890123456789012345678901234567890123456789012345678901234567890 | |
4 # | |
5 | |
474 | 6 from stonegentool import \ |
491 | 7 EatToken,SplitListOfTypes,ParseTemplateType,ProcessSchema, \ |
8 CheckSchemaSchema,LoadSchema,trim,ComputeRequiredDeclarationOrder, \ | |
9 GetTemplatingDictFromSchemaFilename | |
471 | 10 import unittest |
473 | 11 import os |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
12 import re |
491 | 13 import pprint |
14 from jinja2 import Template | |
15 | |
16 ymlSchema = trim("""rootName: VsolMessages | |
17 | |
18 struct B: | |
19 someAs: vector<A> | |
20 someInts: vector<int32> | |
21 | |
22 struct C: | |
23 someBs: vector<B> | |
24 ddd: vector<string> | |
25 | |
26 struct A: | |
27 someStrings: vector<string> | |
28 someInts2: vector<int32> | |
29 movies: vector<MovieType> | |
30 | |
31 enum MovieType: | |
32 - RomCom | |
33 - Horror | |
34 - ScienceFiction | |
35 - Vegetables | |
36 | |
37 enum CrispType: | |
38 - SaltAndPepper | |
39 - CreamAndChives | |
40 - Paprika | |
41 - Barbecue | |
42 ) | |
43 """) | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
44 |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
45 def RemoveDateTimeLine(s : str): |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
46 # regex are non-multiline by default, and $ does NOT match the end of the line |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
47 s2 = re.sub(r"^// autogenerated by stonegentool on .*\n","",s) |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
48 return s2 |
471 | 49 |
50 class TestStonegentool(unittest.TestCase): | |
51 def test_EatToken_empty(self): | |
52 c = r"" | |
53 a,b = EatToken(c) | |
54 self.assertEqual(a,r"") | |
55 self.assertEqual(b,r"") | |
56 | |
57 def test_EatToken_simpleNonTemplate(self): | |
58 c = r"int32" | |
59 a,b = EatToken(c) | |
60 self.assertEqual(a,r"int32") | |
61 self.assertEqual(b,r"") | |
62 | |
63 def test_EatToken_simpleTemplate(self): | |
64 c = r"vector<string>" | |
65 a,b = EatToken(c) | |
66 self.assertEqual(a,r"vector<string>") | |
67 self.assertEqual(b,r"") | |
68 | |
69 def test_EatToken_complexTemplate(self): | |
70 c = r"vector<map<int64,string>>,vector<map<int32,string>>" | |
71 a,b = EatToken(c) | |
72 self.assertEqual(a,r"vector<map<int64,string>>") | |
73 self.assertEqual(b,r"vector<map<int32,string>>") | |
74 | |
472 | 75 def test_EatToken_complexTemplates(self): |
471 | 76 c = r"vector<map<vector<string>,map<int32,string>>>,map<int32,string>,map<map<int32,string>,string>" |
77 a,b = EatToken(c) | |
78 self.assertEqual(a,r"vector<map<vector<string>,map<int32,string>>>") | |
79 self.assertEqual(b,r"map<int32,string>,map<map<int32,string>,string>") | |
80 a,b = EatToken(b) | |
81 self.assertEqual(a,r"map<int32,string>") | |
82 self.assertEqual(b,r"map<map<int32,string>,string>") | |
83 | |
473 | 84 def test_SplitListOfTypes(self): |
85 c = r"vector<map<vector<string>,map<int32,string>>>,map<int32,string>,map<map<int32,string>,string>" | |
86 lot = SplitListOfTypes(c) | |
87 self.assertEqual(3,len(lot)) | |
88 self.assertEqual("vector<map<vector<string>,map<int32,string>>>",lot[0]) | |
89 self.assertEqual("map<int32,string>",lot[1]) | |
90 self.assertEqual("map<map<int32,string>,string>",lot[2]) | |
91 | |
92 def test_SplitListOfTypes_bogus(self): | |
93 c = r"vector<map<vector<string>,map<int32,string>>,map<int32,string>,map<map<int32,string>,string" | |
94 self.assertRaises(Exception,SplitListOfTypes,c) # the argument c must be passed to assertRaises, not as a normal call of SplitListOfTypes | |
95 | |
96 def test_ParseTemplateType_true(self): | |
97 c = "map<vector<map<int,vector<string>>>,map<vector<int>,vector<string>>>" | |
98 (ok,a,b) = ParseTemplateType(c) | |
99 self.assertEqual(ok,True) | |
100 self.assertEqual(a,"map") | |
101 self.assertEqual(b,["vector<map<int,vector<string>>>","map<vector<int>,vector<string>>"]) | |
102 | |
103 (ok2,a2,b2) = ParseTemplateType(b[0]) | |
104 self.assertEqual(ok2,True) | |
105 self.assertEqual(a2,"vector") | |
106 self.assertEqual(b2,["map<int,vector<string>>"]) | |
107 | |
108 (ok3,a3,b3) = ParseTemplateType(b[1]) | |
109 self.assertEqual(ok3,True) | |
110 self.assertEqual(a3,"map") | |
111 self.assertEqual(b3,["vector<int>","vector<string>"]) | |
112 | |
113 (ok4,a4,b4) = ParseTemplateType(b2[0]) | |
114 self.assertEqual(ok4,True) | |
115 self.assertEqual(a4,"map") | |
116 self.assertEqual(b4,["int","vector<string>"]) | |
117 | |
118 def test_ParseSchema(self): | |
491 | 119 fn = os.path.join(os.path.dirname(__file__), 'test_data', 'test1.yaml') |
473 | 120 obj = LoadSchema(fn) |
491 | 121 # we're happy if it does not crash :) |
473 | 122 CheckSchemaSchema(obj) |
123 | |
491 | 124 # def test_ParseSchema_bogus_json(self): |
125 # fn = os.path.join(os.path.dirname(__file__), 'test', 'test1_bogus_json.jsonc') | |
126 # self.assertRaises(Exception,LoadSchema,fn) | |
127 | |
128 # def test_ParseSchema_bogus_schema(self): | |
129 # fn = os.path.join(os.path.dirname(__file__), 'test', 'test1_bogus_schema.jsonc') | |
130 # obj = LoadSchema(fn) | |
131 # self.assertRaises(Exception,CheckSchemaSchema,obj) | |
473 | 132 |
491 | 133 def test_ComputeRequiredDeclarationOrder(self): |
134 fn = os.path.join(os.path.dirname(__file__), 'test_data', 'test1.yaml') | |
473 | 135 obj = LoadSchema(fn) |
491 | 136 genOrder: str = ComputeRequiredDeclarationOrder(obj) |
137 self.assertEqual(3,len(genOrder)) | |
138 self.assertEqual("A",genOrder[0]) | |
139 self.assertEqual("B",genOrder[1]) | |
140 self.assertEqual("C",genOrder[2]) | |
473 | 141 |
491 | 142 # def test_GeneratePreambleEnumerationAndStructs(self): |
143 # fn = os.path.join(os.path.dirname(__file__), 'test', 'test1.jsonc') | |
144 # obj = LoadSchema(fn) | |
145 # (_,genc,_) = ProcessSchema(obj) | |
146 | |
147 def test_genEnums(self): | |
148 fn = os.path.join(os.path.dirname(__file__), 'test_data', 'test1.yaml') | |
474 | 149 obj = LoadSchema(fn) |
491 | 150 genOrder: str = ComputeRequiredDeclarationOrder(obj) |
151 processedSchema = ProcessSchema(obj, genOrder) | |
152 processedSchemaStr = pprint.pformat(processedSchema,indent=2) | |
153 processedSchemaStrRef = """{ 'enums': [ { 'fields': ['RomCom', 'Horror', 'ScienceFiction', 'Vegetables'], | |
154 'name': 'MovieType'}, | |
155 { 'fields': [ 'SaltAndPepper', | |
156 'CreamAndChives', | |
157 'Paprika', | |
158 'Barbecue'], | |
159 'name': 'CrispType'}], | |
160 'rootName': 'VsolMessages', | |
161 'structs': [ { 'fields': { 'movies': 'vector<MovieType>', | |
162 'someInts2': 'vector<int32>', | |
163 'someStrings': 'vector<string>'}, | |
164 'name': 'A'}, | |
165 { 'fields': {'someAs': 'vector<A>', 'someInts': 'vector<int32>'}, | |
166 'name': 'B'}, | |
167 { 'fields': {'ddd': 'vector<string>', 'someBs': 'vector<B>'}, | |
168 'name': 'C'}]}""" | |
473 | 169 |
491 | 170 self.assertEqual(processedSchemaStrRef,processedSchemaStr) |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
171 |
491 | 172 def test_GenerateTypeScriptEnums(self): |
173 fn = os.path.join(os.path.dirname(__file__), 'test_data', 'test1.yaml') | |
174 tdico = GetTemplatingDictFromSchemaFilename(fn) | |
175 template = Template(""" // end of generic methods | |
176 {% for enum in enums%} export enum {{enum['name']}} { | |
177 {% for key in enum['fields']%} {{key}}, | |
178 {%endfor%} }; | |
179 | |
180 {%endfor%}""") | |
181 renderedCode = template.render(**tdico) | |
182 renderedCodeRef = """ // end of generic methods | |
183 export enum MovieType { | |
184 RomCom, | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
185 Horror, |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
186 ScienceFiction, |
491 | 187 Vegetables, |
188 }; | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
189 |
491 | 190 export enum CrispType { |
486
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
191 SaltAndPepper, |
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
192 CreamAndChives, |
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
193 Paprika, |
491 | 194 Barbecue, |
195 }; | |
486
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
196 |
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
197 """ |
491 | 198 self.assertEqual(renderedCodeRef,renderedCode) |
486
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
199 |
473 | 200 |
474 | 201 def test_GenerateTypeScriptHandlerInterface(self): |
202 pass | |
203 | |
204 def test_GenerateCppHandlerInterface(self): | |
205 pass | |
206 | |
207 def test_GenerateTypeScriptDispatcher(self): | |
208 pass | |
209 | |
210 def test_GenerateCppDispatcher(self): | |
211 pass | |
473 | 212 |
472 | 213 # def test(self): |
471 | 214 # s = 'hello world' |
215 # self.assertEqual(s.split(), ['hello', 'world']) | |
216 # # check that s.split fails when the separator is not a string | |
217 # with self.assertRaises(TypeError): | |
218 # s.split(2) | |
219 | |
220 if __name__ == '__main__': | |
474 | 221 unittest.main() |
486
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
222 |