@darabonba/python-generator 1.2.19 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. package/ChangeLog.md +0 -158
  2. package/lib/builtin.js +818 -0
  3. package/lib/generator.js +2704 -0
  4. package/lib/helper.js +422 -0
  5. package/package.json +6 -5
  6. package/tests/expected/alias/setup.py +79 -0
  7. package/tests/expected/alias/tea_python_tests/__init__.py +1 -0
  8. package/tests/expected/alias/tea_python_tests/client.py +35 -0
  9. package/tests/expected/annotation/tea_python_tests/__init__.py +1 -0
  10. package/tests/expected/annotation/tea_python_tests/client.py +93 -0
  11. package/tests/expected/annotation/tea_python_tests/models.py +37 -0
  12. package/tests/expected/api/tea_python_tests/__init__.py +1 -0
  13. package/tests/expected/api/tea_python_tests/client.py +106 -0
  14. package/tests/expected/builtin/tea_python_tests/client.py +685 -0
  15. package/tests/expected/comment/tea_python_tests/__init__.py +1 -0
  16. package/tests/expected/comment/tea_python_tests/client.py +288 -0
  17. package/tests/expected/comment/tea_python_tests/models.py +195 -0
  18. package/tests/expected/complex/tea_python_tests/__init__.py +1 -0
  19. package/tests/expected/complex/tea_python_tests/client.py +1168 -0
  20. package/tests/expected/complex/tea_python_tests/exceptions.py +39 -0
  21. package/tests/expected/complex/tea_python_tests/models.py +570 -0
  22. package/tests/expected/const/tea_python_tests/__init__.py +1 -0
  23. package/tests/expected/const/tea_python_tests/client.py +14 -0
  24. package/tests/expected/empty/setup.py +74 -0
  25. package/tests/expected/empty/tea_python_tests/__init__.py +1 -0
  26. package/tests/expected/empty/tea_python_tests/client.py +10 -0
  27. package/tests/expected/exec/tea_python_tests/__init__.py +1 -0
  28. package/tests/expected/exec/tea_python_tests/exec_client.py +21 -0
  29. package/tests/expected/function/tea_python_tests/__init__.py +1 -0
  30. package/tests/expected/function/tea_python_tests/client.py +64 -0
  31. package/tests/expected/import/setup.py +78 -0
  32. package/tests/expected/import/tea_python_tests/__init__.py +1 -0
  33. package/tests/expected/import/tea_python_tests/client.py +29 -0
  34. package/tests/expected/map/tea_python_tests/__init__.py +1 -0
  35. package/tests/expected/map/tea_python_tests/client.py +39 -0
  36. package/tests/expected/model/tea_python_tests/__init__.py +1 -0
  37. package/tests/expected/model/tea_python_tests/client.py +10 -0
  38. package/tests/expected/model/tea_python_tests/exceptions.py +28 -0
  39. package/tests/expected/model/tea_python_tests/models.py +780 -0
  40. package/tests/expected/multi/tea_python_tests/api.py +95 -0
  41. package/tests/expected/multi/tea_python_tests/client.py +40 -0
  42. package/tests/expected/multi/tea_python_tests/lib/util.py +19 -0
  43. package/tests/expected/multi/tea_python_tests/model/user.py +23 -0
  44. package/tests/expected/multi/tea_python_tests/model/user_exceptions.py +83 -0
  45. package/tests/expected/multi/tea_python_tests/model/user_models.py +145 -0
  46. package/tests/expected/statements/tea_python_tests/__init__.py +1 -0
  47. package/tests/expected/statements/tea_python_tests/client.py +86 -0
  48. package/tests/expected/super/tea_python_tests/__init__.py +1 -0
  49. package/tests/expected/super/tea_python_tests/client.py +16 -0
  50. package/tests/expected/typedef/setup.py +77 -0
  51. package/tests/expected/typedef/tea_python_tests/__init__.py +1 -0
  52. package/tests/expected/typedef/tea_python_tests/client.py +74 -0
  53. package/tests/expected/typedef/tea_python_tests/models.py +42 -0
  54. package/tests/fixtures/alias/Darafile +11 -0
  55. package/tests/fixtures/alias/libraries/Alias/Darafile +11 -0
  56. package/tests/fixtures/alias/libraries/Alias-Symbol/Darafile +11 -0
  57. package/tests/fixtures/builtin/Darafile +6 -0
  58. package/tests/fixtures/builtin/main.dara +333 -0
  59. package/tests/fixtures/complex/libraries/import.dara +18 -11
  60. package/tests/fixtures/complex/main.dara +451 -103
  61. package/tests/fixtures/import/libraries/Darafile +1 -1
  62. package/tests/fixtures/import/pack/Darafile +3 -0
  63. package/tests/fixtures/map/libraries/Darafile +3 -0
  64. package/tests/fixtures/map/libraries/import.dara +4 -0
  65. package/tests/fixtures/map/main.dara +17 -2
  66. package/tests/fixtures/model/.libraries.json +3 -0
  67. package/tests/fixtures/model/Darafile +5 -2
  68. package/tests/fixtures/model/libraries/Darafile +20 -0
  69. package/tests/fixtures/model/libraries/import.dara +27 -0
  70. package/tests/fixtures/model/main.dara +60 -42
  71. package/tests/fixtures/multi/.libraries.json +3 -0
  72. package/tests/fixtures/multi/Darafile +12 -0
  73. package/tests/fixtures/multi/api.dara +20 -0
  74. package/tests/fixtures/multi/lib/util.dara +5 -0
  75. package/tests/fixtures/multi/libraries/darabonba_Util_0.2.11/Teafile +62 -0
  76. package/tests/fixtures/multi/libraries/darabonba_Util_0.2.11/main.tea +244 -0
  77. package/tests/fixtures/multi/main.dara +32 -0
  78. package/tests/fixtures/multi/model/user.dara +33 -0
  79. package/tests/{python3.tests.js → main.tests.js} +107 -65
  80. package/tests/output/alias/setup.py +79 -0
  81. package/tests/output/alias/tea_python_tests/client.py +35 -0
  82. package/tests/output/annotation/setup.py +77 -0
  83. package/tests/output/annotation/tea_python_tests/client.py +93 -0
  84. package/tests/output/annotation/tea_python_tests/models.py +37 -0
  85. package/tests/output/api/setup.py +77 -0
  86. package/tests/output/api/tea_python_tests/client.py +106 -0
  87. package/tests/output/builtin/setup.py +77 -0
  88. package/tests/output/builtin/tea_python_tests/client.py +685 -0
  89. package/tests/output/comment/setup.py +77 -0
  90. package/tests/output/comment/tea_python_tests/client.py +288 -0
  91. package/tests/output/comment/tea_python_tests/models.py +195 -0
  92. package/tests/output/complex/setup.py +77 -0
  93. package/tests/output/complex/tea_python_tests/client.py +1168 -0
  94. package/tests/output/complex/tea_python_tests/exceptions.py +39 -0
  95. package/tests/output/complex/tea_python_tests/models.py +570 -0
  96. package/tests/output/const/setup.py +77 -0
  97. package/tests/output/const/tea_python_tests/client.py +14 -0
  98. package/tests/output/empty/setup.py +77 -0
  99. package/tests/output/empty/tea_python_tests/client.py +10 -0
  100. package/tests/output/exec/setup.py +77 -0
  101. package/tests/output/exec/tea_python_tests/exec_client.py +21 -0
  102. package/tests/output/function/setup.py +77 -0
  103. package/tests/output/function/tea_python_tests/client.py +64 -0
  104. package/tests/output/import/setup.py +78 -0
  105. package/tests/output/import/tea_python_tests/client.py +29 -0
  106. package/tests/output/map/setup.py +77 -0
  107. package/tests/output/map/tea_python_tests/client.py +39 -0
  108. package/tests/output/model/setup.py +77 -0
  109. package/tests/output/model/tea_python_tests/client.py +10 -0
  110. package/tests/output/model/tea_python_tests/exceptions.py +28 -0
  111. package/tests/output/model/tea_python_tests/models.py +780 -0
  112. package/tests/output/multi/setup.py +77 -0
  113. package/tests/output/multi/tea_python_tests/api.py +95 -0
  114. package/tests/output/multi/tea_python_tests/client.py +40 -0
  115. package/tests/output/multi/tea_python_tests/lib/util.py +19 -0
  116. package/tests/output/multi/tea_python_tests/model/user.py +23 -0
  117. package/tests/output/multi/tea_python_tests/model/user_exceptions.py +83 -0
  118. package/tests/output/multi/tea_python_tests/model/user_models.py +145 -0
  119. package/tests/output/statements/setup.py +77 -0
  120. package/tests/output/statements/tea_python_tests/client.py +86 -0
  121. package/tests/output/super/setup.py +77 -0
  122. package/tests/output/super/tea_python_tests/client.py +16 -0
  123. package/tests/output/typedef/setup.py +78 -0
  124. package/tests/output/typedef/tea_python_tests/client.py +74 -0
  125. package/tests/output/typedef/tea_python_tests/models.py +42 -0
  126. package/tests/common.tests.js +0 -244
  127. package/tests/lib.tests.js +0 -269
  128. package/tests/python2.tests.js +0 -283
  129. package/tests/resolver.tests.js +0 -434
@@ -1,434 +0,0 @@
1
- 'use strict';
2
-
3
- const mm = require('mm');
4
- const expect = require('chai').expect;
5
- require('mocha-sinon');
6
-
7
- const ModelResolver = require('../src/resolver/model');
8
- const ClientResolver = require('../src/resolver/client');
9
-
10
- const {
11
- PropItem,
12
- AnnotationItem,
13
- BehaviorDoAction,
14
- GrammerValue,
15
- FuncItem
16
- } = require('../src/langs/common/items');
17
- const { _deepClone } = require('../src/lib/helper');
18
-
19
- const lang = 'python';
20
- const Combinator = require(`../src/langs/${lang}/combinator.js`);
21
- const config = require(`../src/langs/${lang}/config.js`);
22
-
23
- describe('client resolver should be ok', function () {
24
- beforeEach(function () {
25
- this.sinon.stub(console, 'log');
26
- });
27
-
28
- it('resolve should be ok', function () {
29
- const combinator = new Combinator(Object.assign(_deepClone(config), {
30
- package: 'test', model: { dir: 'Models' }
31
- }), {});
32
- const code = new ClientResolver({ moduleBody: { nodes: [] } }, combinator, {});
33
- mm(code, 'initAnnotation', function () { return; });
34
- mm(code, 'resolveProps', function () { return; });
35
- mm(code.combinator, 'addInclude', function (className) { return className; });
36
- code.config.baseClient = 'BaseClient';
37
- code.resolve();
38
- expect(code.object.extends).to.be.eql(['BaseClient']);
39
-
40
- mm.restore();
41
- });
42
-
43
- it('resolveInitBody should be ok', function () {
44
- const combinator = new Combinator(Object.assign(_deepClone(config), {
45
- package: 'test', model: { dir: 'Models' }
46
- }), {});
47
- const code = new ClientResolver({}, combinator, {});
48
- mm(code.combinator, 'addModelInclude', function (modelName) {
49
- expect(modelName).to.be.eql('modelType');
50
- });
51
- mm(code.combinator, 'addInclude', function (className) {
52
- return className;
53
- });
54
- code.config.baseClient = 'BaseClient';
55
- let init = {
56
- type: 'init', params: {
57
- params: [{
58
- paramType: {
59
- idType: 'model',
60
- lexeme: 'modelType'
61
- },
62
- paramName: {
63
- lexeme: 'modelName'
64
- }
65
- }, {
66
- paramType: {
67
- idType: 'module',
68
- lexeme: 'moduleType'
69
- },
70
- paramName: {
71
- lexeme: 'moduleName'
72
- }
73
- }]
74
- }
75
- };
76
- code.resolveInitBody(init);
77
- mm.restore();
78
- });
79
-
80
- it('resolveFunc should be ok', function () {
81
- const combinator = new Combinator(Object.assign(_deepClone(config), {
82
- package: 'test', model: { dir: 'Models' }
83
- }), {});
84
- const code = new ClientResolver({}, combinator, {});
85
- const funcItem = new FuncItem();
86
-
87
- mm(code.combinator, 'addInclude', function (className) {
88
- return className;
89
- });
90
- code.resolveFunc(funcItem, {
91
- params: { params: [] },
92
- returnType: {
93
- idType: 'module',
94
- lexeme: 'object',
95
- }
96
- }, {});
97
- code.resolveFunc(funcItem, {
98
- params: {
99
- params: [
100
- {
101
- paramType: { type: 'moduleModel', subType: { lexeme: '' }, path: ['a'] },
102
- paramName: { lexeme: 'test' }
103
- }
104
- ]
105
- },
106
- returnType: {
107
- idType: 'module',
108
- lexeme: 'object',
109
- }
110
- }, {});
111
-
112
- mm.restore();
113
- });
114
-
115
- it('code : requestBody should be ok', function () {
116
- const combinator = new Combinator(Object.assign(_deepClone(config), {
117
- package: 'test', model: { dir: 'Models' }
118
- }), {});
119
- const code = new ClientResolver({}, combinator, {});
120
- mm(code, 'visitStmt', function (func) {
121
- expect(func.name).to.be.eql('testFuncName');
122
- });
123
- const funcItem = new FuncItem();
124
- funcItem.name = 'testFuncName';
125
- code.requestBody({}, {
126
- body: {},
127
- }, funcItem);
128
- mm.restore();
129
- mm(code, 'visitStmt', function (func, item) {
130
- expect(item).to.be.eql('https');
131
- });
132
- code.requestBody({}, {
133
- protocol: ['https']
134
- }, funcItem);
135
- });
136
-
137
- it('renderGrammerValue should be ok', function () {
138
- const combinator = new Combinator(Object.assign(_deepClone(config), {
139
- package: 'test', model: { dir: 'Models' }
140
- }), {});
141
- const code = new ClientResolver({}, combinator, {});
142
- expect(function () {
143
- code.renderGrammerValue(null, { type: 'invalid' });
144
- }).to.be.throw('unimpelemented : invalid');
145
-
146
- expect(function () {
147
- code.renderGrammerValue(null, {
148
- type: 'map_access',
149
- accessKey: {
150
- id: '',
151
- value: { lexeme: '' }
152
- }
153
- });
154
- }).to.be.throw('');
155
-
156
- let grammerValue = code.renderGrammerValue(null, {
157
- type: 'map_access',
158
- id: { lexeme: 'test' },
159
- accessKey: {
160
- id: 'test',
161
- value: { lexeme: 'test' }
162
- }
163
- });
164
- expect(grammerValue.value.path.length).to.be.eql(2);
165
-
166
- expect(function () {
167
- code.renderGrammerValue(null, {
168
- type: 'call',
169
- left: {
170
- type: 'invalid'
171
- }
172
- });
173
- }).to.be.throw('');
174
-
175
- expect(function () {
176
- code.renderGrammerValue(null, {
177
- type: 'call',
178
- left: {
179
- type: 'static_call',
180
- id: { type: 'invalid' }
181
- }
182
- });
183
- }).to.be.throw('');
184
-
185
- expect(function () {
186
- code.renderGrammerValue(null, {
187
- type: 'call',
188
- left: {
189
- type: 'instance_call',
190
- id: { type: 'invalid', lexeme: 'test' }
191
- }
192
- });
193
- }).to.be.throw('Unsupported object.left.id.type : invalid');
194
-
195
- grammerValue = code.renderGrammerValue(null, {
196
- type: 'call',
197
- left: {
198
- type: 'instance_call',
199
- id: { lexeme: '@test' }
200
- }
201
- });
202
- expect(grammerValue.value.path.length).to.be.eql(1);
203
-
204
- grammerValue = code.renderGrammerValue(null, {
205
- type: 'call',
206
- left: {
207
- type: 'method_call',
208
- id: { type: 'module', lexeme: 'Util' }
209
- }
210
- });
211
- expect(grammerValue.value.path.length).to.be.eql(2);
212
-
213
- mm(code.combinator, 'addModelInclude', function (modelName) {
214
- return modelName;
215
- });
216
- grammerValue = code.renderGrammerValue(null, {
217
- type: 'construct_model',
218
- aliasId: {
219
- lexeme: 'test'
220
- },
221
- propertyPath: [{ lexeme: 'a' }, { lexeme: 'b' }]
222
- });
223
- expect(grammerValue.value.name).to.be.eql('test.a.b');
224
-
225
- grammerValue = code.renderGrammerValue(null, {
226
- type: 'property_access',
227
- id: {
228
- inferred: 'map',
229
- lexeme: '__module'
230
- },
231
- propertyPath: [{ lexeme: 'test' }],
232
- propertyPathTypes: [{ name: 'test' }],
233
- needCast: true
234
- });
235
- expect(grammerValue.type).to.be.eql('behavior');
236
-
237
- grammerValue = code.renderGrammerValue(null, {
238
- type: 'object',
239
- fields: [
240
- { expr: { type: 'object', fields: [{ expr: { type: 'null' } }] } }
241
- ]
242
- });
243
- expect(grammerValue.type).to.be.eql('array');
244
-
245
- mm.restore();
246
- });
247
-
248
- it('visitStmt should be ok', function () {
249
- const combinator = new Combinator(Object.assign(_deepClone(config), {
250
- package: 'test', model: { dir: 'Models' }
251
- }), {});
252
- const code = new ClientResolver({}, combinator, {});
253
- const obj = new BehaviorDoAction();
254
-
255
- expect(function () {
256
- code.visitStmt(obj, {});
257
- }).to.be.throw('');
258
- expect(console.log.calledWith({})).to.be.true;
259
-
260
- let stmt = { type: 'while', condition: [{}] };
261
- mm(code, 'renderGrammerValue', function () {
262
- return new GrammerValue('string', 'test');
263
- });
264
- code.visitStmt(obj, stmt);
265
- expect(obj.body.length).to.be.eql(1);
266
-
267
- stmt = { type: 'virtualCall' };
268
- code.visitStmt(obj, stmt);
269
- expect(obj.body.length).to.be.eql(2);
270
-
271
- stmt = { type: 'throw', expr: [{}] };
272
- mm(code.combinator, 'addInclude', function (className) {
273
- return className;
274
- });
275
- code.visitStmt(obj, stmt);
276
- expect(obj.body.length).to.be.eql(3);
277
-
278
- stmt = { type: 'elseIfRequestAssign' };
279
- mm(code, 'visitIfElse', function () { return 'visitIfElse'; });
280
- code.visitStmt(obj, stmt);
281
- expect(obj.body.length).to.be.eql(4);
282
-
283
- stmt = {
284
- type: 'requestAssign',
285
- left: {
286
- id: { lexeme: 'test' },
287
- }
288
- };
289
- code.predefined = {
290
- '$Request': {
291
- modelBody: { nodes: [{ fieldName: { lexeme: 'test' }, fieldValue: '' }] }
292
- }
293
- };
294
- code.visitStmt(obj, stmt);
295
- expect(obj.body.length).to.be.eql(5);
296
-
297
- stmt = {
298
- type: 'requestAssign',
299
- left: {
300
- type: 'request_property_assign',
301
- id: { lexeme: 'test' },
302
- propertyPath: [{ lexeme: 'a' }, { lexeme: 'b' }]
303
- }
304
- };
305
- code.visitStmt(obj, stmt);
306
- expect(obj.body.length).to.be.eql(6);
307
- mm.restore();
308
- });
309
-
310
- it('visitIfConfition should be ok', function () {
311
- const combinator = new Combinator(Object.assign(_deepClone(config), {
312
- package: 'test', model: { dir: 'Models' }
313
- }), {});
314
- const code = new ClientResolver({}, combinator, {});
315
- const stmtCondition = { left: {}, type: 'invalid' };
316
- expect(function () {
317
- code.visitIfConfition(stmtCondition);
318
- }).to.be.throw('');
319
- expect(console.log.calledWith(stmtCondition)).to.be.true;
320
-
321
- stmtCondition.type = 'call';
322
- mm(code, 'renderGrammerValue', function () {
323
- return 'render grammer value';
324
- });
325
- expect(code.visitIfConfition(stmtCondition)).to.be.eql('render grammer value');
326
-
327
- stmtCondition.type = 'not';
328
- expect(code.visitIfConfition(stmtCondition).opt).to.be.eql('NOT');
329
- mm.restore();
330
- });
331
-
332
- it('visitIfElse should be ok', function () {
333
- const combinator = new Combinator(Object.assign(_deepClone(config), {
334
- package: 'test', model: { dir: 'Models' }
335
- }), {});
336
- const code = new ClientResolver({}, combinator, {});
337
- expect(function () {
338
- code.visitIfElse({});
339
- }).to.be.throw('');
340
- expect(console.log.calledWith({})).to.be.true;
341
-
342
- let stmt = { stmts: [] };
343
- expect(function () {
344
- code.visitIfElse(stmt);
345
- }).to.be.throw('');
346
- expect(console.log.calledWith(stmt)).to.be.true;
347
-
348
- stmt = {
349
- stmts: { type: 'stmts', stmts: [{ type: 'return', expr: { type: 'null' } }] },
350
- elseAssigns: [{ stmts: { type: 'stmts', stmts: [{ type: 'return', expr: { type: 'null' } }] } }]
351
- };
352
- const grammer = code.visitIfElse(stmt);
353
- expect(grammer.elseItem.length).to.be.eql(1);
354
- });
355
- });
356
-
357
- describe('model resolver should be ok', function () {
358
- it('resolve should be ok', function () {
359
- const combinator = new Combinator(Object.assign(_deepClone(config), {
360
- package: 'test', model: { dir: 'Models' }
361
- }), {});
362
- const model = new ModelResolver({
363
- type: 'model',
364
- modelName: { lexeme: 'test' },
365
- modelBody: { nodes: [] },
366
- tokenRange: [1, 100]
367
- }, combinator, {});
368
- mm(model, 'initAnnotation', function () { return; });
369
- mm(model.combinator, 'addInclude', function (modelName) { return modelName; });
370
- mm(model, 'initProp', function () { return; });
371
- mm(model, 'resolveAnnotations', function () { return [new AnnotationItem()]; });
372
- mm(model, 'getBetweenComments', function () { return; });
373
- model.resolve();
374
- expect(model.object.body.length).to.be.eql(1);
375
- });
376
-
377
- it('initProp should be ok', function () {
378
- const combinator = new Combinator(Object.assign(_deepClone(config), {
379
- package: 'test', model: { dir: 'Models' }
380
- }), {});
381
- const model = new ModelResolver({}, combinator, {});
382
- expect(function () {
383
- model.initProp([{
384
- fieldValue: {
385
- fieldType: null
386
- }
387
- }]);
388
- }).to.be.throw('');
389
-
390
- mm(model.combinator, 'addModelInclude', function (modelName) {
391
- expect(modelName).to.be.eql('a');
392
- });
393
- model.initProp([{
394
- fieldValue: {
395
- type: 'fieldType',
396
- fieldType: { type: 'moduleModel', path: [{ lexeme: 'a' }] }
397
- },
398
- fieldName: {
399
- lexeme: 'test'
400
- },
401
- attrs: [{ attrValue: { value: 'test' }, attrName: { lexeme: 'name' } }]
402
- }]);
403
- mm.restore();
404
- mm(model, 'findSubModelsUsed', function () {
405
- return;
406
- });
407
- model.initProp([{
408
- type: 'modelField',
409
- fieldValue: { type: 'fieldType', fieldType: 'array', fieldItemType: { lexeme: 'string' } },
410
- fieldName: { lexeme: 'test' },
411
- attrs: []
412
- }]);
413
- expect(model.object.body.filter(item => item instanceof PropItem).length).to.be.eql(2);
414
- mm.restore();
415
- });
416
-
417
- it('findSubModelsUsed should be ok', function () {
418
- const combinator = new Combinator(Object.assign(_deepClone(config), {
419
- package: 'test', model: { dir: 'Models' }
420
- }), {});
421
- const model = new ModelResolver({}, combinator, {});
422
- const subModelUsed = [];
423
- model.findSubModelsUsed({
424
- fieldName: { lexeme: 'test' },
425
- fieldValue: {
426
- nodes: [{
427
- fieldValue: { nodes: [] },
428
- fieldName: { lexeme: 'foo' },
429
- }]
430
- }
431
- }, subModelUsed);
432
- expect(subModelUsed.length).to.be.eql(2);
433
- });
434
- });