Mercurial > hg > orthanc-stone
annotate Resources/CodeGeneration/stonegentool_test.py @ 1290:7def6ab2929f bugs/2020-02-invisible-slice
Removal of debugging logs
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Fri, 21 Feb 2020 15:20:29 +0100 |
parents | 342f3e04bfa9 |
children |
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, \ | |
628
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
9 GetTemplatingDictFromSchemaFilename,MakeTemplate,MakeTemplateFromFile,LoadSchemaFromString,GetTemplatingDictFromSchema |
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 | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
16 def RemoveDateTimeLine(s : str): |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
17 # 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
|
18 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
|
19 return s2 |
471 | 20 |
21 class TestStonegentool(unittest.TestCase): | |
22 def test_EatToken_empty(self): | |
23 c = r"" | |
24 a,b = EatToken(c) | |
25 self.assertEqual(a,r"") | |
26 self.assertEqual(b,r"") | |
27 | |
28 def test_EatToken_simpleNonTemplate(self): | |
29 c = r"int32" | |
30 a,b = EatToken(c) | |
31 self.assertEqual(a,r"int32") | |
32 self.assertEqual(b,r"") | |
33 | |
34 def test_EatToken_simpleTemplate(self): | |
35 c = r"vector<string>" | |
36 a,b = EatToken(c) | |
37 self.assertEqual(a,r"vector<string>") | |
38 self.assertEqual(b,r"") | |
39 | |
40 def test_EatToken_complexTemplate(self): | |
41 c = r"vector<map<int64,string>>,vector<map<int32,string>>" | |
42 a,b = EatToken(c) | |
43 self.assertEqual(a,r"vector<map<int64,string>>") | |
44 self.assertEqual(b,r"vector<map<int32,string>>") | |
45 | |
472 | 46 def test_EatToken_complexTemplates(self): |
471 | 47 c = r"vector<map<vector<string>,map<int32,string>>>,map<int32,string>,map<map<int32,string>,string>" |
48 a,b = EatToken(c) | |
49 self.assertEqual(a,r"vector<map<vector<string>,map<int32,string>>>") | |
50 self.assertEqual(b,r"map<int32,string>,map<map<int32,string>,string>") | |
51 a,b = EatToken(b) | |
52 self.assertEqual(a,r"map<int32,string>") | |
53 self.assertEqual(b,r"map<map<int32,string>,string>") | |
54 | |
473 | 55 def test_SplitListOfTypes(self): |
56 c = r"vector<map<vector<string>,map<int32,string>>>,map<int32,string>,map<map<int32,string>,string>" | |
57 lot = SplitListOfTypes(c) | |
58 self.assertEqual(3,len(lot)) | |
59 self.assertEqual("vector<map<vector<string>,map<int32,string>>>",lot[0]) | |
60 self.assertEqual("map<int32,string>",lot[1]) | |
61 self.assertEqual("map<map<int32,string>,string>",lot[2]) | |
62 | |
63 def test_SplitListOfTypes_bogus(self): | |
64 c = r"vector<map<vector<string>,map<int32,string>>,map<int32,string>,map<map<int32,string>,string" | |
65 self.assertRaises(Exception,SplitListOfTypes,c) # the argument c must be passed to assertRaises, not as a normal call of SplitListOfTypes | |
66 | |
67 def test_ParseTemplateType_true(self): | |
68 c = "map<vector<map<int,vector<string>>>,map<vector<int>,vector<string>>>" | |
69 (ok,a,b) = ParseTemplateType(c) | |
70 self.assertEqual(ok,True) | |
71 self.assertEqual(a,"map") | |
72 self.assertEqual(b,["vector<map<int,vector<string>>>","map<vector<int>,vector<string>>"]) | |
73 | |
74 (ok2,a2,b2) = ParseTemplateType(b[0]) | |
75 self.assertEqual(ok2,True) | |
76 self.assertEqual(a2,"vector") | |
77 self.assertEqual(b2,["map<int,vector<string>>"]) | |
78 | |
79 (ok3,a3,b3) = ParseTemplateType(b[1]) | |
80 self.assertEqual(ok3,True) | |
81 self.assertEqual(a3,"map") | |
82 self.assertEqual(b3,["vector<int>","vector<string>"]) | |
83 | |
84 (ok4,a4,b4) = ParseTemplateType(b2[0]) | |
85 self.assertEqual(ok4,True) | |
86 self.assertEqual(a4,"map") | |
87 self.assertEqual(b4,["int","vector<string>"]) | |
88 | |
89 def test_ParseSchema(self): | |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
90 fn = os.path.join(os.path.dirname(__file__), 'test_data', 'testTestStoneCodeGen.yaml') |
473 | 91 obj = LoadSchema(fn) |
491 | 92 # we're happy if it does not crash :) |
473 | 93 CheckSchemaSchema(obj) |
94 | |
491 | 95 def test_ComputeRequiredDeclarationOrder(self): |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
96 fn = os.path.join(os.path.dirname(__file__), 'test_data', 'testTestStoneCodeGen.yaml') |
473 | 97 obj = LoadSchema(fn) |
491 | 98 genOrder: str = ComputeRequiredDeclarationOrder(obj) |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
99 self.assertEqual(5,len(genOrder)) |
491 | 100 self.assertEqual("A",genOrder[0]) |
101 self.assertEqual("B",genOrder[1]) | |
102 self.assertEqual("C",genOrder[2]) | |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
103 self.assertEqual("Message1",genOrder[3]) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
104 self.assertEqual("Message2",genOrder[4]) |
473 | 105 |
491 | 106 # def test_GeneratePreambleEnumerationAndStructs(self): |
107 # fn = os.path.join(os.path.dirname(__file__), 'test', 'test1.jsonc') | |
108 # obj = LoadSchema(fn) | |
109 # (_,genc,_) = ProcessSchema(obj) | |
110 | |
111 def test_genEnums(self): | |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
112 self.maxDiff = None |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
113 fn = os.path.join(os.path.dirname(__file__), 'test_data', 'testTestStoneCodeGen.yaml') |
474 | 114 obj = LoadSchema(fn) |
491 | 115 genOrder: str = ComputeRequiredDeclarationOrder(obj) |
116 processedSchema = ProcessSchema(obj, genOrder) | |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
117 self.assertTrue('rootName' in processedSchema) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
118 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
119 structs = {} |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
120 for v in processedSchema['structs']: |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
121 structs[v['name']] = v |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
122 enums = {} |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
123 for v in processedSchema['enums']: |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
124 enums[v['name']] = v |
473 | 125 |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
126 self.assertTrue('C' in structs) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
127 self.assertTrue('someBs' in structs['C']['fields']) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
128 self.assertTrue('CrispType' in enums) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
129 self.assertTrue('Message1' in structs) |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
130 self.assertEqual('int32', structs['Message1']['fields']['memberInt32'].type) |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
131 self.assertEqual('string', structs['Message1']['fields']['memberString'].type) |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
132 self.assertEqual('EnumMonth0', structs['Message1']['fields']['memberEnumMonth'].type) |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
133 self.assertEqual('bool', structs['Message1']['fields']['memberBool'].type) |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
134 self.assertEqual('float32', structs['Message1']['fields']['memberFloat32'].type) |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
135 self.assertEqual('float64', structs['Message1']['fields']['memberFloat64'].type) |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
136 |
491 | 137 def test_GenerateTypeScriptEnums(self): |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
138 fn = os.path.join(os.path.dirname(__file__), 'test_data', 'testTestStoneCodeGen.yaml') |
491 | 139 tdico = GetTemplatingDictFromSchemaFilename(fn) |
140 template = Template(""" // end of generic methods | |
141 {% for enum in enums%} export enum {{enum['name']}} { | |
142 {% for key in enum['fields']%} {{key}}, | |
143 {%endfor%} }; | |
144 | |
145 {%endfor%}""") | |
146 renderedCode = template.render(**tdico) | |
147 renderedCodeRef = """ // end of generic methods | |
148 export enum MovieType { | |
149 RomCom, | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
150 Horror, |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
151 ScienceFiction, |
491 | 152 Vegetables, |
153 }; | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
154 |
491 | 155 export enum CrispType { |
486
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
156 SaltAndPepper, |
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
157 CreamAndChives, |
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
158 Paprika, |
491 | 159 Barbecue, |
160 }; | |
486
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
161 |
627 | 162 export enum EnumMonth0 { |
163 January, | |
164 February, | |
165 March, | |
166 }; | |
167 | |
486
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
168 """ |
491 | 169 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
|
170 |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
171 def test_GenerateCplusplusEnums(self): |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
172 fn = os.path.join(os.path.dirname(__file__), 'test_data', 'testTestStoneCodeGen.yaml') |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
173 tdico = GetTemplatingDictFromSchemaFilename(fn) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
174 template = Template(""" // end of generic methods |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
175 {% for enum in enums%} enum {{enum['name']}} { |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
176 {% for key in enum['fields']%} {{key}}, |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
177 {%endfor%} }; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
178 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
179 {%endfor%}""") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
180 renderedCode = template.render(**tdico) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
181 renderedCodeRef = """ // end of generic methods |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
182 enum MovieType { |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
183 RomCom, |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
184 Horror, |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
185 ScienceFiction, |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
186 Vegetables, |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
187 }; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
188 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
189 enum CrispType { |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
190 SaltAndPepper, |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
191 CreamAndChives, |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
192 Paprika, |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
193 Barbecue, |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
194 }; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
195 |
627 | 196 enum EnumMonth0 { |
197 January, | |
198 February, | |
199 March, | |
200 }; | |
201 | |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
202 """ |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
203 self.assertEqual(renderedCodeRef,renderedCode) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
204 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
205 def test_generateTsStructType(self): |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
206 fn = os.path.join(os.path.dirname(__file__), 'test_data', 'testTestStoneCodeGen.yaml') |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
207 tdico = GetTemplatingDictFromSchemaFilename(fn) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
208 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
209 # template = MakeTemplate(""" // end of generic methods |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
210 # {% for struct in struct%} export class {{struct['name']}} { |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
211 # {% for key in struct['fields']%} {{key}}:{{struct['fields'][key]}}, |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
212 # {% endfor %} |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
213 # constructor() { |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
214 # {% for key in struct['fields']%} |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
215 # {% if NeedsConstruction(struct['fields']['key'])} |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
216 # {{key}} = new {{CanonToTs(struct['fields']['key'])}}; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
217 # {% end if %} |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
218 # {% endfor %} |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
219 # } |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
220 # {% endfor %} |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
221 # public StoneSerialize(): string { |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
222 # let container: object = {}; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
223 # container['type'] = '{{rootName}}.{{struct['name']}}'; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
224 # container['value'] = this; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
225 # return JSON.stringify(container); |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
226 # } };""") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
227 template = MakeTemplate(""" // end of generic methods |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
228 {% for struct in structs%} export class {{struct['name']}} { |
628
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
229 {% for key in struct['fields']%} {{key}}:{{CanonToTs(struct['fields'][key]['type'])}}; |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
230 {% endfor %} |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
231 constructor() { |
628
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
232 {% for key in struct['fields']%} this.{{key}} = new {{CanonToTs(struct['fields'][key]['type'])}}(); |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
233 {% endfor %} } |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
234 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
235 public StoneSerialize(): string { |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
236 let container: object = {}; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
237 container['type'] = '{{rootName}}.{{struct['name']}}'; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
238 container['value'] = this; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
239 return JSON.stringify(container); |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
240 } |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
241 }; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
242 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
243 {% endfor %}""") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
244 renderedCode = template.render(**tdico) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
245 renderedCodeRef = """ // end of generic methods |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
246 export class A { |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
247 someStrings:Array<string>; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
248 someInts2:Array<number>; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
249 movies:Array<MovieType>; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
250 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
251 constructor() { |
627 | 252 this.someStrings = new Array<string>(); |
253 this.someInts2 = new Array<number>(); | |
254 this.movies = new Array<MovieType>(); | |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
255 } |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
256 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
257 public StoneSerialize(): string { |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
258 let container: object = {}; |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
259 container['type'] = 'TestStoneCodeGen.A'; |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
260 container['value'] = this; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
261 return JSON.stringify(container); |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
262 } |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
263 }; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
264 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
265 export class B { |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
266 someAs:Array<A>; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
267 someInts:Array<number>; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
268 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
269 constructor() { |
627 | 270 this.someAs = new Array<A>(); |
271 this.someInts = new Array<number>(); | |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
272 } |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
273 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
274 public StoneSerialize(): string { |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
275 let container: object = {}; |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
276 container['type'] = 'TestStoneCodeGen.B'; |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
277 container['value'] = this; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
278 return JSON.stringify(container); |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
279 } |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
280 }; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
281 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
282 export class C { |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
283 someBs:Array<B>; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
284 ddd:Array<string>; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
285 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
286 constructor() { |
627 | 287 this.someBs = new Array<B>(); |
288 this.ddd = new Array<string>(); | |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
289 } |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
290 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
291 public StoneSerialize(): string { |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
292 let container: object = {}; |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
293 container['type'] = 'TestStoneCodeGen.C'; |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
294 container['value'] = this; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
295 return JSON.stringify(container); |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
296 } |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
297 }; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
298 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
299 export class Message1 { |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
300 memberInt32:number; |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
301 memberString:string; |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
302 memberEnumMonth:EnumMonth0; |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
303 memberBool:boolean; |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
304 memberFloat32:number; |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
305 memberFloat64:number; |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
306 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
307 constructor() { |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
308 this.memberInt32 = new number(); |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
309 this.memberString = new string(); |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
310 this.memberEnumMonth = new EnumMonth0(); |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
311 this.memberBool = new boolean(); |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
312 this.memberFloat32 = new number(); |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
313 this.memberFloat64 = new number(); |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
314 } |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
315 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
316 public StoneSerialize(): string { |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
317 let container: object = {}; |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
318 container['type'] = 'TestStoneCodeGen.Message1'; |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
319 container['value'] = this; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
320 return JSON.stringify(container); |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
321 } |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
322 }; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
323 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
324 export class Message2 { |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
325 memberString:string; |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
326 memberStringWithDefault:string; |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
327 memberVectorOfMessage1:Array<Message1>; |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
328 memberVectorOfString:Array<string>; |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
329 memberMapStringString:Map<string, string>; |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
330 memberMapStringStruct:Map<string, Message1>; |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
331 memberMapEnumFloat:Map<CrispType, number>; |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
332 memberEnumMovieType:MovieType; |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
333 memberJson:Object; |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
334 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
335 constructor() { |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
336 this.memberString = new string(); |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
337 this.memberStringWithDefault = new string(); |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
338 this.memberVectorOfMessage1 = new Array<Message1>(); |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
339 this.memberVectorOfString = new Array<string>(); |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
340 this.memberMapStringString = new Map<string, string>(); |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
341 this.memberMapStringStruct = new Map<string, Message1>(); |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
342 this.memberMapEnumFloat = new Map<CrispType, number>(); |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
343 this.memberEnumMovieType = new MovieType(); |
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
344 this.memberJson = new Object(); |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
345 } |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
346 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
347 public StoneSerialize(): string { |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
348 let container: object = {}; |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
349 container['type'] = 'TestStoneCodeGen.Message2'; |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
350 container['value'] = this; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
351 return JSON.stringify(container); |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
352 } |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
353 }; |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
354 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
355 """ |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
356 # print(renderedCode) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
357 self.maxDiff = None |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
358 self.assertEqual(renderedCodeRef, renderedCode) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
359 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
360 def test_generateWholeTsFile(self): |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
361 schemaFile = \ |
687
342f3e04bfa9
CodeGen: test cleanup + all working again + using same yaml and stimuli files
Alain Mazy <alain@mazy.be>
parents:
686
diff
changeset
|
362 os.path.join(os.path.dirname(__file__), 'test_data', 'testTestStoneCodeGen.yaml') |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
363 tdico = GetTemplatingDictFromSchemaFilename(schemaFile) |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
364 tsTemplateFile = \ |
515
1dbf2d9ed1e4
Added .j2 extension to the Jinja2 template files to allow for a better syntax highlighting experience (a.o. in vscode)
Benjamin Golinvaux <bgo@osimis.io>
parents:
494
diff
changeset
|
365 os.path.join(os.path.dirname(__file__), 'template.in.ts.j2') |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
366 template = MakeTemplateFromFile(tsTemplateFile) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
367 renderedCode = template.render(**tdico) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
368 print(renderedCode) |
473 | 369 |
474 | 370 def test_GenerateTypeScriptHandlerInterface(self): |
371 pass | |
372 | |
373 def test_GenerateCppHandlerInterface(self): | |
374 pass | |
375 | |
376 def test_GenerateTypeScriptDispatcher(self): | |
377 pass | |
378 | |
379 def test_GenerateCppDispatcher(self): | |
380 pass | |
473 | 381 |
628
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
382 def test_StringDefaultValueInTs(self): |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
383 schema = LoadSchemaFromString(""" |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
384 rootName: MyTest |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
385 struct Toto: |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
386 withoutDefault: string |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
387 withDefault: string = \"tutu\" |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
388 """) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
389 tdico = GetTemplatingDictFromSchema(schema) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
390 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
391 tsTemplateFile = os.path.join(os.path.dirname(__file__), 'template.in.ts.j2') |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
392 template = MakeTemplateFromFile(tsTemplateFile) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
393 renderedCode = template.render(**tdico) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
394 self.assertIn("withDefault = \"tutu\"", renderedCode) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
395 # print(renderedCode) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
396 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
397 cppTemplateFile = os.path.join(os.path.dirname(__file__), 'template.in.h.j2') |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
398 template = MakeTemplateFromFile(cppTemplateFile) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
399 renderedCode = template.render(**tdico) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
400 print(renderedCode) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
401 self.assertIn("withDefault = \"tutu\"", renderedCode) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
402 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
403 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
404 def test_EnumDefaultValue(self): |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
405 schema = LoadSchemaFromString(""" |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
406 rootName: MyTest |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
407 enum MyEnum: |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
408 - Toto |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
409 - Tutu |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
410 struct Toto: |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
411 withoutDefault: MyEnum |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
412 withDefault: MyEnum = Toto |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
413 """) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
414 tdico = GetTemplatingDictFromSchema(schema) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
415 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
416 tsTemplateFile = os.path.join(os.path.dirname(__file__), 'template.in.ts.j2') |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
417 template = MakeTemplateFromFile(tsTemplateFile) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
418 renderedCode = template.render(**tdico) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
419 # print(renderedCode) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
420 self.assertIn("withDefault = MyEnum.Toto", renderedCode) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
421 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
422 tsTemplateFile = os.path.join(os.path.dirname(__file__), 'template.in.h.j2') |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
423 template = MakeTemplateFromFile(tsTemplateFile) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
424 renderedCode = template.render(**tdico) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
425 self.assertIn("withDefault = MyTest::MyEnum_Toto", renderedCode) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
426 # print(renderedCode) |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
427 |
84af39146e76
CodeGeneration: support default values
Alain Mazy <alain@mazy.be>
parents:
627
diff
changeset
|
428 |
472 | 429 # def test(self): |
471 | 430 # s = 'hello world' |
431 # self.assertEqual(s.split(), ['hello', 'world']) | |
432 # # check that s.split fails when the separator is not a string | |
433 # with self.assertRaises(TypeError): | |
434 # s.split(2) | |
435 | |
436 if __name__ == '__main__': | |
474 | 437 unittest.main() |
486
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
438 |