@darabonba/python-generator 1.2.19 → 2.0.1

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 (152) hide show
  1. package/ChangeLog.md +0 -158
  2. package/lib/builtin.js +818 -0
  3. package/lib/generator.js +2708 -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 +1169 -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/{src/langs/python/files/setup.py.tmpl → tests/expected/empty/setup.py} +11 -11
  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 +26 -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 +452 -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 +68 -0
  76. package/tests/fixtures/multi/libraries/darabonba_Util_0.2.11/main.tea +244 -0
  77. package/tests/fixtures/multi/libraries/darabonba_Util_0.2.11/util.tea +2 -0
  78. package/tests/fixtures/multi/main.dara +32 -0
  79. package/tests/fixtures/multi/model/user.dara +35 -0
  80. package/tests/{python3.tests.js → main.tests.js} +107 -65
  81. package/tests/output/alias/setup.py +79 -0
  82. package/tests/output/alias/tea_python_tests/client.py +35 -0
  83. package/{src/langs/python2/files/setup.py.tmpl → tests/output/annotation/setup.py} +19 -24
  84. package/tests/output/annotation/tea_python_tests/client.py +93 -0
  85. package/tests/output/annotation/tea_python_tests/models.py +37 -0
  86. package/tests/output/api/setup.py +77 -0
  87. package/tests/output/api/tea_python_tests/client.py +106 -0
  88. package/tests/output/builtin/setup.py +77 -0
  89. package/tests/output/builtin/tea_python_tests/client.py +685 -0
  90. package/tests/output/comment/setup.py +77 -0
  91. package/tests/output/comment/tea_python_tests/client.py +288 -0
  92. package/tests/output/comment/tea_python_tests/models.py +195 -0
  93. package/tests/output/complex/setup.py +77 -0
  94. package/tests/output/complex/tea_python_tests/client.py +1169 -0
  95. package/tests/output/complex/tea_python_tests/exceptions.py +39 -0
  96. package/tests/output/complex/tea_python_tests/models.py +570 -0
  97. package/tests/output/const/setup.py +77 -0
  98. package/tests/output/const/tea_python_tests/client.py +14 -0
  99. package/tests/output/empty/setup.py +77 -0
  100. package/tests/output/empty/tea_python_tests/client.py +10 -0
  101. package/tests/output/exec/setup.py +77 -0
  102. package/tests/output/exec/tea_python_tests/exec_client.py +21 -0
  103. package/tests/output/function/setup.py +77 -0
  104. package/tests/output/function/tea_python_tests/client.py +64 -0
  105. package/tests/output/import/setup.py +78 -0
  106. package/tests/output/import/tea_python_tests/client.py +29 -0
  107. package/tests/output/map/setup.py +77 -0
  108. package/tests/output/map/tea_python_tests/client.py +39 -0
  109. package/tests/output/model/setup.py +77 -0
  110. package/tests/output/model/tea_python_tests/client.py +10 -0
  111. package/tests/output/model/tea_python_tests/exceptions.py +28 -0
  112. package/tests/output/model/tea_python_tests/models.py +780 -0
  113. package/tests/output/multi/setup.py +77 -0
  114. package/tests/output/multi/tea_python_tests/api.py +95 -0
  115. package/tests/output/multi/tea_python_tests/client.py +40 -0
  116. package/tests/output/multi/tea_python_tests/lib/util.py +19 -0
  117. package/tests/output/multi/tea_python_tests/model/user.py +26 -0
  118. package/tests/output/multi/tea_python_tests/model/user_exceptions.py +83 -0
  119. package/tests/output/multi/tea_python_tests/model/user_models.py +145 -0
  120. package/tests/output/statements/setup.py +77 -0
  121. package/tests/output/statements/tea_python_tests/client.py +86 -0
  122. package/tests/output/super/setup.py +77 -0
  123. package/tests/output/super/tea_python_tests/client.py +16 -0
  124. package/tests/output/typedef/setup.py +78 -0
  125. package/tests/output/typedef/tea_python_tests/client.py +74 -0
  126. package/tests/output/typedef/tea_python_tests/models.py +42 -0
  127. package/src/generator.js +0 -231
  128. package/src/langs/common/combinator.js +0 -193
  129. package/src/langs/common/config.js +0 -60
  130. package/src/langs/common/enum.js +0 -179
  131. package/src/langs/common/items.js +0 -551
  132. package/src/langs/common/package_info.js +0 -53
  133. package/src/langs/python/combinator.js +0 -1898
  134. package/src/langs/python/config.js +0 -169
  135. package/src/langs/python/files/.gitignore.tmpl +0 -5
  136. package/src/langs/python/files/__init__.py.tmpl +0 -1
  137. package/src/langs/python/package_info.js +0 -78
  138. package/src/langs/python2/combinator.js +0 -1770
  139. package/src/langs/python2/config.js +0 -162
  140. package/src/langs/python2/files/.gitignore.tmpl +0 -5
  141. package/src/langs/python2/files/__init__.py.tmpl +0 -1
  142. package/src/langs/python2/package_info.js +0 -78
  143. package/src/lib/debug.js +0 -55
  144. package/src/lib/emitter.js +0 -95
  145. package/src/lib/helper.js +0 -207
  146. package/src/resolver/base.js +0 -282
  147. package/src/resolver/client.js +0 -1013
  148. package/src/resolver/model.js +0 -159
  149. package/tests/common.tests.js +0 -244
  150. package/tests/lib.tests.js +0 -269
  151. package/tests/python2.tests.js +0 -283
  152. package/tests/resolver.tests.js +0 -434
@@ -1,159 +0,0 @@
1
- 'use strict';
2
-
3
- // eslint-disable-next-line no-unused-vars
4
- const debug = require('../lib/debug');
5
- const assert = require('assert');
6
- const BaseResolver = require('./base');
7
-
8
- const {
9
- _string,
10
- _escape,
11
- _isBasicType,
12
- } = require('../lib/helper');
13
-
14
- const {
15
- ObjectItem,
16
- AnnotationItem,
17
- PropItem,
18
- NoteItem,
19
- } = require('../langs/common/items');
20
-
21
- const {
22
- Modify,
23
- } = require('../langs/common/enum');
24
-
25
- class ModelResolver extends BaseResolver {
26
- constructor(astNode, combinator, globalAst) {
27
- super(astNode, combinator, globalAst);
28
- this.object = new ObjectItem('model');
29
- }
30
-
31
- resolve() {
32
- const object = this.object;
33
- const combinator = this.combinator;
34
- const config = this.config;
35
- const ast = this.ast;
36
-
37
- assert.equal(ast.type, 'model');
38
-
39
- combinator.config.emitType = 'model';
40
-
41
- object.name = ast.modelName.lexeme;
42
- if (config.modelDirName) {
43
- config.model.dir = config.modelDirName;
44
- }
45
- config.layer = config.model.dir;
46
-
47
- if (ast.annotation) {
48
- this.initAnnotation(ast.annotation);
49
- }
50
-
51
- object.topAnnotation.push(new AnnotationItem(
52
- object.index,
53
- 'single',
54
- config.generateFileInfo
55
- ));
56
-
57
- object.addExtends(combinator.addInclude('$Model'));
58
-
59
- this.initProp(ast.modelBody.nodes);
60
- if (ast.modelBody.nodes.length === 0) {
61
- if (ast.tokenRange) {
62
- this.resolveAnnotations(
63
- this.getBetweenComments(ast.tokenRange[0], ast.tokenRange[1]),
64
- object.index
65
- ).forEach(c => {
66
- object.addBodyNode(c);
67
- });
68
- }
69
- }
70
-
71
- object.includeList = this.combinator.includeList;
72
- return object;
73
- }
74
-
75
- initProp(nodes) {
76
- const object = this.object;
77
-
78
- for (let i = 0; i < nodes.length; i++) {
79
- const node = nodes[i];
80
- if (typeof node.fieldValue.fieldType === 'undefined') {
81
- let subModelUsed = [];
82
- this.findSubModelsUsed(node, subModelUsed, object.name);
83
- subModelUsed.forEach(subModel => {
84
- this.combinator.addModelInclude(subModel);
85
- });
86
- } else if (node.fieldValue.fieldType === 'array') {
87
- let name = '';
88
- if (node.fieldValue.fieldItemType.lexeme) {
89
- name = node.fieldValue.fieldItemType.lexeme;
90
- } else if (node.fieldValue.itemType) {
91
- name = node.fieldValue.fieldItemType.lexeme;
92
- }
93
- if (name && !_isBasicType(name)) {
94
- this.combinator.addModelInclude(name);
95
- }
96
- }
97
-
98
- const prop = new PropItem();
99
- prop.belong = object.index;
100
- prop.name = _escape(node.fieldName.lexeme) || _string(node.fieldName);
101
- prop.type = this.resolveType(node.fieldValue, node);
102
-
103
- prop.modify.push(Modify.public());
104
- if (node.required) {
105
- prop.addNote(new NoteItem('required', true));
106
- }
107
- if (node.tokenRange) {
108
- let annotations = this.resolveAnnotations(
109
- this.getFrontComments(node.tokenRange[0]),
110
- object.index
111
- );
112
- annotations.forEach(c => {
113
- object.addBodyNode(c);
114
- });
115
- }
116
- for (let i = 0; i < node.attrs.length; i++) {
117
- const attr = node.attrs[i];
118
- let value;
119
- if (typeof attr.attrValue.string !== 'undefined') {
120
- value = attr.attrValue.string;
121
- } else if (typeof attr.attrValue.value !== 'undefined') {
122
- value = attr.attrValue.value;
123
- } else if (typeof attr.attrValue.lexeme !== 'undefined') {
124
- value = attr.attrValue.lexeme;
125
- }
126
- let note = new NoteItem(
127
- attr.attrName.lexeme,
128
- value
129
- );
130
- prop.addNote(note);
131
- }
132
- object.addBodyNode(prop);
133
- }
134
- if (nodes[nodes.length - 1] && nodes[nodes.length - 1].tokenRange) {
135
- let annotations = this.resolveAnnotations(
136
- this.getBackComments(nodes[nodes.length - 1].tokenRange[1]),
137
- object.index
138
- );
139
- annotations.forEach(c => {
140
- object.addBodyNode(c);
141
- });
142
- }
143
- }
144
-
145
- findSubModelsUsed(node, subModelUsed = [], pre = '') {
146
- let name = _escape(node.fieldName.lexeme) || _string(node.fieldName);
147
- if (pre !== '') {
148
- name = pre + '.' + name;
149
- }
150
- subModelUsed.push(name);
151
- node.fieldValue.nodes.forEach(item => {
152
- if (typeof item.fieldValue.fieldType === 'undefined') {
153
- this.findSubModelsUsed(item, subModelUsed, name);
154
- }
155
- });
156
- }
157
- }
158
-
159
- module.exports = ModelResolver;
@@ -1,244 +0,0 @@
1
- 'use strict';
2
-
3
- const fs = require('fs');
4
- const mm = require('mm');
5
- const expect = require('chai').expect;
6
- require('mocha-sinon');
7
-
8
- const {
9
- Symbol,
10
- Modify
11
- } = require('../src/langs/common/enum');
12
-
13
- const {
14
- Counter,
15
- Grammer,
16
- GrammerCall,
17
- GrammerValue,
18
- GrammerSymbol,
19
- GrammerThrows,
20
- GrammerNewLine,
21
- GrammerCondition,
22
- NoteItem,
23
- ObjectItem,
24
- } = require('../src/langs/common/items');
25
-
26
- const PackageInfo = require('../src/langs/common/package_info');
27
-
28
- const Combinator = require('../src/langs/common/combinator');
29
- const Emitter = require('../src/lib/emitter');
30
-
31
- describe('common tests', function () {
32
- beforeEach(function () {
33
- this.sinon.stub(console, 'log');
34
- });
35
-
36
- it('enum should be ok', function () {
37
- // test Symbol enum
38
- expect(Symbol.eq()).to.be.eql('EQ');
39
- expect(Symbol.not()).to.be.eql('NOT');
40
- expect(Symbol.multi()).to.be.eql('MULTI');
41
- expect(Symbol.div()).to.be.eql('DIV');
42
- expect(Symbol.power()).to.be.eql('POWER');
43
- expect(Symbol.greaterEq()).to.be.eql('GREATER_EQ');
44
- expect(Symbol.less()).to.be.eql('LESS');
45
- expect(Symbol.lessEq()).to.be.eql('LESS_EQ');
46
- expect(Symbol.judge()).to.be.eql('JUDGE');
47
- expect(Symbol.risk()).to.be.eql('RISK');
48
-
49
- // test Modify enum
50
- expect(Modify.private()).to.be.eql('PRIVATE');
51
- expect(Modify.internal()).to.be.eql('INTERNAL');
52
- expect(Modify.final()).to.be.eql('FINAL');
53
- expect(Modify.abstract()).to.be.eql('ABSTRACT');
54
- });
55
-
56
- it('counter should be ok', function () {
57
- const counter = new Counter(-2);
58
- expect(counter.index).to.be.eql(-2);
59
- counter.step(2, 10);
60
- expect(counter.index).to.be.eql(18);
61
- counter.once();
62
- expect(counter.index).to.be.eql(19);
63
- });
64
-
65
- it('items : Item.getItemByIndex&getParent should be ok', function () {
66
- const parentGrammer = new Grammer();
67
- const childGrammer = new GrammerNewLine();
68
- const childGrammer2 = new GrammerSymbol();
69
- childGrammer.belong = parentGrammer.index;
70
-
71
- expect(function () {
72
- childGrammer.getItemByIndex(-100);
73
- }).to.be.throw('Index [-100] not exist in ItemSet');
74
-
75
- expect(childGrammer.getItemByIndex(parentGrammer.index)).to.be.eql(parentGrammer);
76
- expect(childGrammer.getParent()).to.be.eql(parentGrammer);
77
- expect(childGrammer2.getParent()).to.be.eql(parentGrammer);
78
- });
79
-
80
- it('items : GrammerCall.addPath should be ok', function () {
81
- const call = new GrammerCall();
82
- expect(function () {
83
- call.addPath({ type: 'invalid-type' });
84
- }).to.be.throw('invalid-type path.type should be parent|object|object_static|call|call_static|prop|prop_static|map|list');
85
- });
86
-
87
- it('items : NoteItem should be ok', function () {
88
- let noteItem = new NoteItem('', null);
89
- expect(noteItem.type).to.be.eql('null');
90
- expect(function () {
91
- noteItem = new NoteItem('', {});
92
- }).to.be.throw('Not suppoted type : object [number,string,boolean]');
93
- });
94
-
95
- it('items : ObjectItem should be ok', function () {
96
- const objectItem = new ObjectItem('client');
97
- expect(function () {
98
- objectItem.addBodyNode(null);
99
- }).to.be.throw('Only suppoted PropItem | FuncItem | ObjectItem | AnnotationItem | ConstructItem');
100
- objectItem.addModify(Modify.private());
101
- expect(objectItem.modify.indexOf('PRIVATE') !== -1).to.be.true;
102
- });
103
-
104
- it('package_info : render should be ok', function () {
105
- const packageInfo = new PackageInfo();
106
- expect(
107
- packageInfo.render('${key}', { key: 'render-key' })
108
- ).to.be.eql('render-key');
109
- });
110
-
111
- it('package_info : checkParams should be ok', function () {
112
- const packageInfo = new PackageInfo();
113
- const param = { key: 'value' };
114
- packageInfo.checkParams(param);
115
- try {
116
- packageInfo.checkParams(param, ['someRequeiredKey']);
117
- } catch (e) {
118
- expect(e.message).to.be.eql('need config packageInfo.someRequeiredKey');
119
- expect(console.log.calledWith(param)).to.be.true;
120
- }
121
- });
122
-
123
- it('package_info : renderAuto should be ok', function () {
124
- const packageInfo = new PackageInfo();
125
- mm(fs, 'readFileSync', function (filename) {
126
- return '${key}';
127
- });
128
- mm(fs, 'existsSync', function (filename) {
129
- return true;
130
- });
131
- mm(fs, 'writeFileSync', function (filename, content) {
132
- return true;
133
- });
134
- // not throw exception
135
- packageInfo.renderAuto('/tmp/template-file-path.tmpl', '/tmp/target-path.tmp', { key: 'RenderAutoKey' });
136
-
137
- mm(fs, 'existsSync', function (filename) {
138
- return false;
139
- });
140
- mm(fs, 'mkdirSync', function (filename) {
141
- return true;
142
- });
143
- // not throw exception
144
- packageInfo.renderAuto('/tmp/template-file-path.tmpl', '/tmp/target-path.tmp', { key: 'RenderAutoKey' });
145
- });
146
-
147
- it('package_info : resolveOutputDir should be ok', function () {
148
- const packageInfo = new PackageInfo();
149
- packageInfo.config = { dir: '/tmp/' };
150
- expect(packageInfo.resolveOutputDir({ outputDir: null }, './')).to.be.eql('/tmp/');
151
-
152
- mm(fs, 'existsSync', function (filename) {
153
- return false;
154
- });
155
- expect(packageInfo.resolveOutputDir({ outputDir: 'output/' }, './')).to.be.eql('/tmp/output/');
156
- mm.restore();
157
- });
158
-
159
- it('combinator : coreClass should be ok', function () {
160
- const combinator = new Combinator({ tea: {} });
161
- expect(function () {
162
- combinator.coreClass('test');
163
- }).to.be.throw('Unsupported core class name : test');
164
- });
165
-
166
- it('combinator : findThrows should be ok', function () {
167
- const combinator = new Combinator();
168
- const grammer = new GrammerCondition();
169
- expect(combinator.findThrows(grammer).length).to.be.eql(0);
170
- grammer.addBodyNode(new GrammerThrows());
171
- expect(combinator.findThrows(grammer).length).to.be.eql(1);
172
- const childGrammer = new GrammerCondition();
173
- childGrammer.addBodyNode(new GrammerThrows());
174
- grammer.addBodyNode(childGrammer);
175
- expect(combinator.findThrows(grammer).length).to.be.eql(2);
176
- });
177
-
178
- it('combinator : unimpelemented should be ok', function () {
179
- const combinator = new Combinator();
180
- expect(function () {
181
- combinator.init({});
182
- }).to.be.throw('unimpelemented');
183
- expect(function () {
184
- combinator.addModelInclude('foo');
185
- }).to.be.throw('unimpelemented');
186
- expect(function () {
187
- combinator.addInclude('foo');
188
- }).to.be.throw('unimpelemented');
189
- });
190
-
191
- it('combinator : systemfunc should be ok', function () {
192
- const combinator = new Combinator();
193
- const emitter = new Emitter();
194
- const gram = new GrammerCall();
195
- expect(function () {
196
- combinator.systemfunc(emitter, gram);
197
- }).to.be.throw('Invalid path. path list cannot be empty.');
198
- gram.addPath({ type: 'call', name: 'test' });
199
- expect(function () {
200
- combinator.systemfunc(emitter, gram);
201
- }).to.be.throw('unimpelemented sysTest(emitter, gram){} method\n');
202
-
203
- combinator.sysTest = function () {
204
- console.log('test call combinator.sysTest');
205
- return true;
206
- };
207
- combinator.systemfunc(emitter, gram);
208
- expect(console.log.calledWith('test call combinator.sysTest')).to.be.true;
209
- });
210
-
211
- it('combinator : grammerNewLine should be ok', function () {
212
- const combinator = new Combinator();
213
- const emitter = new Emitter();
214
- emitter.output = '';
215
- const grammerNewLine = new GrammerNewLine(2);
216
- combinator.grammerNewLine(emitter, grammerNewLine);
217
- expect(emitter.output).to.be.eql(emitter.eol + emitter.eol);
218
- });
219
-
220
- it('combinator : emitGrammerValue should be ok', function () {
221
- const combinator = new Combinator();
222
- combinator.grammerValue = function (emitter) {
223
- emitter.emit('test');
224
- };
225
- const grammerValue = new GrammerValue();
226
- expect(combinator.emitGrammerValue(grammerValue)).to.be.eql('test');
227
- });
228
-
229
- it('combinator : grammer should be ok', function () {
230
- const combinator = new Combinator();
231
- const emitter = new Emitter();
232
-
233
- emitter.output = '';
234
- const obj = new ObjectItem('client');
235
- expect(function () {
236
- combinator.grammer(emitter, obj);
237
- }).to.be.throw('Unsupported');
238
- expect(console.log.calledWith(obj)).to.be.true;
239
-
240
- expect(function () {
241
- combinator.grammer(emitter, new GrammerCondition());
242
- }).to.be.throw('Unimpelemented : grammerCondition');
243
- });
244
- });
@@ -1,269 +0,0 @@
1
- 'use strict';
2
-
3
- const fs = require('fs');
4
- const debug = require('../src/lib/debug');
5
- const mm = require('mm');
6
- const expect = require('chai').expect;
7
- const Emitter = require('../src/lib/emitter');
8
- require('mocha-sinon');
9
-
10
- const {
11
- _camelCase,
12
- _subModelName,
13
- _string,
14
- _upperFirst,
15
- _config,
16
- _avoidKeywords,
17
- _avoidFuncKeywords,
18
- _avoidClassKeywords,
19
- _avoidVarKeywords,
20
- _modify,
21
- _symbol,
22
- _toSnakeCase,
23
- _toCamelCase,
24
- _isSnakeCase
25
- } = require('../src/lib/helper');
26
-
27
- describe('debug should be ok', function () {
28
- beforeEach(function () {
29
- this.sinon.stub(console, 'log');
30
- this.sinon.stub(process, 'exit');
31
- this.sinon.stub(fs, 'mkdirSync');
32
- this.sinon.stub(fs, 'writeFileSync');
33
- this.sinon.stub(fs, 'appendFileSync');
34
- this.sinon.stub(fs, 'existsSync');
35
- });
36
-
37
- it('dump should be ok', function () {
38
- debug.dump(null, 1, '1', 1.11111);
39
- expect(console.log.calledWith('foo')).to.be.false;
40
- expect(console.log.calledWith(null)).to.be.true;
41
- expect(console.log.calledWith(1)).to.be.true;
42
- expect(console.log.calledWith('1')).to.be.true;
43
- expect(console.log.calledWith(1.11111)).to.be.true;
44
- expect(process.exit.calledWith(-1)).to.be.false;
45
- });
46
-
47
- it('halt should be ok', function () {
48
- debug.halt(null, 1, '1', 1.11111);
49
- expect(console.log.calledWith('foo')).to.be.false;
50
- expect(console.log.calledWith(null)).to.be.true;
51
- expect(console.log.calledWith(1)).to.be.true;
52
- expect(console.log.calledWith('1')).to.be.true;
53
- expect(console.log.calledWith(1.11111)).to.be.true;
54
- expect(process.exit.calledWith(-1)).to.be.true;
55
- });
56
-
57
- it('jump should be ok', function () {
58
- for (let i = 0; i < 3; i++) {
59
- debug.jump(1, `jump output : ${i}`);
60
- }
61
- expect(console.log.calledWith('jump output : 0')).to.be.false;
62
- expect(console.log.calledWith('jump output : 1')).to.be.true;
63
- expect(console.log.calledWith('jump output : 2')).to.be.false;
64
- expect(process.exit.calledWith(-1)).to.be.true;
65
- });
66
-
67
- it('stack should be ok', function () {
68
- try {
69
- debug.stack('called debug stack with message', 'data1', 'data2');
70
- } catch (e) {
71
- expect(e.message).to.be.eql('called debug stack with message');
72
- expect(console.log.calledWith('called debug stack with message')).to.be.false;
73
- expect(console.log.calledWith('data1')).to.be.true;
74
- expect(console.log.calledWith('data2')).to.be.true;
75
- expect(process.exit.calledWith(-1)).to.be.false;
76
- }
77
-
78
- try {
79
- debug.stack(1, 'called debug stack without message', 'some data');
80
- } catch (e) {
81
- expect(e.message).to.be.eql('');
82
- expect(console.log.calledWith(1)).to.be.true;
83
- expect(console.log.calledWith('called debug stack without message')).to.be.true;
84
- expect(console.log.calledWith('some data')).to.be.true;
85
- expect(process.exit.calledWith(-1)).to.be.false;
86
- }
87
- });
88
- });
89
-
90
- describe('emitter should be ok', function () {
91
- beforeEach(function () {
92
- this.sinon.stub(console, 'log');
93
- this.sinon.stub(process, 'exit');
94
- this.sinon.stub(fs, 'mkdirSync');
95
- this.sinon.stub(fs, 'writeFileSync');
96
- this.sinon.stub(fs, 'appendFileSync');
97
- this.sinon.stub(fs, 'existsSync');
98
- });
99
-
100
- it('indent should be ok', function () {
101
- const emitter = new Emitter();
102
- expect(emitter.indent(1)).to.be.eql(' ');
103
- });
104
-
105
- it('emit should be ok', function () {
106
- const emitter = new Emitter();
107
- emitter.emit('emit some string');
108
- expect(emitter.output).to.be.eql('emit some string');
109
- });
110
-
111
- it('emitln should be ok', function () {
112
- const emitter = new Emitter();
113
- emitter.emitln('emitln some string');
114
- expect(emitter.output).to.be.eql('emitln some string' + emitter.eol);
115
- });
116
-
117
- it('emits should be ok', function () {
118
- const emitter = new Emitter();
119
- emitter.emits(1);
120
- expect(emitter.output).to.be.eql('');
121
- emitter.emits(0, 'row1', 'row2');
122
- expect(emitter.output).to.be.eql('row1' + emitter.eol + 'row2' + emitter.eol);
123
- });
124
-
125
- it('erase should be ok', function () {
126
- const emitter = new Emitter();
127
- emitter.emit('full string');
128
- emitter.erase(3);
129
- expect(emitter.output).to.be.eql('full str');
130
- });
131
-
132
- it('savePath should be ok', function () {
133
- const emitter = new Emitter({
134
- dir: '/tmp/',
135
- layer: 'a.b.c',
136
- filename: 'filename',
137
- ext: '.tmp'
138
- });
139
- expect(emitter.savePath()).to.be.eql('/tmp/a/b/c/filename.tmp');
140
- });
141
-
142
- it('save should be ok', function () {
143
- const emitter = new Emitter();
144
- try {
145
- emitter.save();
146
- } catch (e) {
147
- expect(e.message).to.be.eql('`option.dir` should not be empty');
148
- }
149
- emitter.config.dir = '/tmp/';
150
- try {
151
- emitter.save();
152
- } catch (e) {
153
- expect(e.message).to.be.eql('filename cannot be empty');
154
- }
155
- emitter.config.filename = 'filename';
156
- emitter.config.ext = '.tmp';
157
-
158
- emitter.emit('test');
159
-
160
- emitter.save();
161
- emitter.emit();
162
- mm(fs, 'existsSync', function (filename) {
163
- return true;
164
- });
165
- emitter.save(true);
166
- mm.restore();
167
-
168
- // show emit info
169
- emitter.config.showInfo = true;
170
- emitter.emit('test');
171
- emitter.save();
172
- const filename = emitter.savePath();
173
-
174
- expect(filename).to.be.eql('/tmp/filename.tmp');
175
- });
176
- });
177
-
178
- describe('helper tests', function () {
179
- it('_upperFirst should be ok', function () {
180
- expect(_upperFirst(null)).to.be.eql('');
181
- });
182
-
183
- it('_camelCase should be ok', function () {
184
- expect(_camelCase('test_camel_case')).to.be.eql('testCamelCase');
185
- });
186
-
187
- it('_subModelName should be ok', function () {
188
- expect(_subModelName('test.model.name')).to.be.eql('TestModelName');
189
- });
190
-
191
- it('_string should be ok', function () {
192
- const obj = {
193
- string: 'test'
194
- };
195
- expect(_string(obj)).to.be.eql('test');
196
- });
197
-
198
- it('_avoidKeywords should be ok', function () {
199
- _config({
200
- keywords: {
201
- general: 'key',
202
- function: ['function'],
203
- class: ['class'],
204
- param_variables: ['var']
205
- }
206
- });
207
- expect(_avoidKeywords('key')).to.be.eql('key_');
208
- expect(_avoidFuncKeywords('key')).to.be.eql('key_');
209
- expect(_avoidFuncKeywords('function')).to.be.eql('function_');
210
- expect(_avoidClassKeywords('key')).to.be.eql('key_');
211
- expect(_avoidClassKeywords('class')).to.be.eql('class_');
212
- expect(_avoidVarKeywords('key')).to.be.eql('key_');
213
- expect(_avoidVarKeywords('var')).to.be.eql('var_');
214
- });
215
-
216
- it('_modify should be ok', function () {
217
- _config({
218
- modifyOrder: [
219
- 'PRIVATE',
220
- 'PROTECTED',
221
- 'PUBLIC',
222
- 'FINAL',
223
- 'ABSTRACT',
224
- 'STATIC'
225
- ]
226
- });
227
- expect(_modify('PRIVATE')).to.be.eql('private');
228
- });
229
-
230
- it('_symbol should be ok', function () {
231
- _config({ symbolMap: { 'ASSIGN': '=' } });
232
- expect(function () {
233
- _symbol('InvalidSymbol');
234
- }).to.be.throw('Unsupported symbol : InvalidSymbol');
235
-
236
- expect(_symbol('ASSIGN')).to.be.eql('=');
237
- });
238
-
239
- it('_isSnakeCase should be ok', function () {
240
- expect(_isSnakeCase('tea_rpc')).to.be.eql(true);
241
- expect(_isSnakeCase('tea')).to.be.eql(true);
242
- expect(_isSnakeCase('tearpc')).to.be.eql(true);
243
- expect(_isSnakeCase('tearpc01')).to.be.eql(true);
244
- expect(_isSnakeCase('_tearpc')).to.be.eql(true);
245
- expect(_isSnakeCase('teaRpc')).to.be.eql(false);
246
- expect(_isSnakeCase('tea-rpc')).to.be.eql(false);
247
- expect(_isSnakeCase('tea-Rpc')).to.be.eql(false);
248
- expect(_isSnakeCase('tea rpc')).to.be.eql(false);
249
- expect(_isSnakeCase('01tearpc')).to.be.eql(false);
250
- });
251
-
252
- it('_toSnakeCase should be ok', function () {
253
- expect(_toSnakeCase('TestABC')).to.be.eql('test_abc');
254
- expect(_toSnakeCase(null)).to.be.eql('');
255
- expect(_toSnakeCase('SLS')).to.be.eql('sls');
256
- expect(_toSnakeCase('_runtime')).to.be.eql('_runtime');
257
- expect(_toSnakeCase('TT123')).to.be.eql('tt123');
258
- expect(_toSnakeCase('fooBar')).to.be.eql('foo_bar');
259
- });
260
-
261
- it('_toCamelCase should be ok', function () {
262
- expect(_toCamelCase('test_abc')).to.be.eql('TestAbc');
263
- expect(_toCamelCase(null)).to.be.eql('');
264
- expect(_toCamelCase('sls')).to.be.eql('Sls');
265
- expect(_toCamelCase('_runtime')).to.be.eql('Runtime');
266
- expect(_toCamelCase('tt_123')).to.be.eql('Tt123');
267
- expect(_toCamelCase('foo_bar')).to.be.eql('FooBar');
268
- });
269
- });