• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15let genDir = "../../src/gen/"
16const { readFile, writeFile } = require(genDir + "tools/FileRW");
17const { getArrayType, getArrayTypeTwo } = require(genDir + "tools/common");
18const { isEnum, enumIndex, getMapType,checkFileError } = require(genDir + "tools/common");
19const { search, match, removeReg, getReg} = require(genDir + "tools/re");
20const {  replaceAll, all } = require(genDir + "tools/re");
21const { checkOutBody, removeExplains,replaceTab } = require(genDir + "tools/tool");
22const { removeEmptyLine, print,getLicense,removeEmptyLine2 } = require(genDir + "tools/tool");
23const assert = require("assert");
24const rewire = require("rewire");
25const { json } = require("stream/consumers");
26
27var correctResult;
28
29function before(){
30    let data = readFile("test/unittest/result.json")
31        if (data) {
32            correctResult = JSON.parse(data);
33        }
34}
35describe('Tools', function () {
36
37    before(function () {
38        before();
39    });
40
41    it('test gen/tools/re search', function () {
42        let ret = search('@ohos([.a-z_A-Z0-9]+).d.ts','@ohos.input_sample.d.ts');
43        assert.strictEqual(JSON.stringify(ret), `{"regs":[[0,23],[5,18]]}`);
44    });
45
46    it('test gen/tools/re match', function () {
47        let ret = match('@ohos([.a-z_A-Z0-9]+).d.ts','@ohos.input_sample.d.ts');
48        assert.strictEqual(JSON.stringify(ret),  `{"regs":[[0,23],[5,18]]}`);
49    });
50
51    partOfReTest();
52
53    partOfCommonTest();
54
55    partOfToolsTest(correctResult);
56
57    it('test gen/tools/tool removeExplains', function () {
58        let param = correctResult['ParamIn']['removeExplains'];
59        let ret = removeExplains(param);
60        let result = correctResult['tools']['removeExplains'];
61        assert.strictEqual(JSON.stringify(ret), JSON.stringify(result));
62    });
63
64    it('test gen/tools/FileRW readFile', function () {
65        let aaa = Math.random()*10000
66        let content="时间 = %^(^*%*&^*"+aaa;
67        writeFile("test/unittest/testrw.txt",content);
68        let ret = readFile("test/unittest/testrw.txt")
69        assert.strictEqual(ret, content);
70    });
71
72});
73
74function partOfReTest(){
75    it('test gen/tools/re removeReg', function () {
76        let ret = removeReg('export default napitest;',[4, 22]);
77
78        assert.strictEqual(JSON.stringify(ret), `"expot;"`);
79    });
80
81    it('test gen/tools/re getReg', function () {
82        let data = 'declare namespace napitest {function fun6(v2: string): void;}export default napitest;';
83        assert.strictEqual(JSON.stringify(getReg(data,[0, 10])), `"declare na"`);
84    });
85
86    it('test gen/tools/re replaceAll', function () {
87        let ret = replaceAll('\n *//([a-zA-Z .]+)\n','\\.','\\.');
88        let retJson = JSON.stringify(ret)
89        assert.strictEqual(retJson, `"\\n *//([a-zA-Z \\\\.]+)\\n"`);
90    });
91
92    it('test gen/tools/re all', function () {
93        assert.strictEqual(JSON.stringify(all('\\.')), "{}");
94    });
95    }
96
97    function partOfCommonTest() {
98        it('test gen/tools/common getArrayType', function () {
99            let ret = getArrayType('Array<number>');
100            assert.strictEqual(ret, 'number');
101        });
102
103        it('test gen/tools/common getArrayTypeTwo', function () {
104            let ret = getArrayTypeTwo('string[]');
105            assert.strictEqual(ret, 'string');
106        });
107
108        it('test gen/tools/common isEnum', function () {
109            let enumElement = [{name:"STATUS0",value:"0",type:"NUMBER_TYPE_1"}];
110            let data = {
111                class: [],
112                const: [],
113                enum: [{name:"HttpStatus",body:{element:enumElement,function:[],enumValueType:0}}],
114                exports: ['HttpStatus'],
115                function: [],
116                interface: [],
117                namespace: [],
118            }
119            let ret = isEnum('HttpStatus', data);
120            assert.strictEqual(ret, true);
121        });
122
123        it('test gen/tools/common enumIndex', function () {
124            let enumElement = [{name:"STATUS0",value:"0",type:"NUMBER_TYPE_1"}];
125            let data = {
126                class: [],
127                const: [],
128                enum: [{name:"HttpStatus",body:{element:enumElement,function:[],enumValueType:0}}],
129                exports: ['HttpStatus'],
130                function: [],
131                interface: [],
132                namespace: [],
133            }
134            let ret = enumIndex('HttpStatus', data);
135            assert.strictEqual(ret, '0');
136        });
137
138        partOfCommonTwo();
139    }
140
141    function partOfCommonTwo(){
142        it('test gen/tools/common getMapType', function () {
143            let result = [ 'string', 'NUMBER_TYPE_1', undefined, undefined ];
144            let ret = getMapType('{[key:string]:NUMBER_TYPE_1}');
145            assert.strictEqual(JSON.stringify(ret), JSON.stringify(result));
146        });
147
148        it('test gen/tools/common checkFileError', function () {
149            let result = [
150                false,
151                "File 'napi_generator-master/test/unittest/@ohos.input_sample.d.ts' not found.\n" +
152                  '  The file is in the program because:\n' +
153                  '    Root file specified for compilation\n'
154              ];
155            let ret = checkFileError('napi_generator-master\\test\\unittest\\@ohos.input_sample.d.ts');
156            this.timeout(5000)
157            assert.strictEqual(JSON.stringify(ret), JSON.stringify(result));
158        });
159    }
160
161    function partOfToolsTest(correctResult) {
162        it('test gen/tools/tool print', function () {
163            let lib = rewire(genDir+'tools/tool.js');
164            let print = lib.__get__("print");
165            print("tool test print");
166        });
167
168        it('test gen/tools/tool checkOutBody', function () {
169            let body = correctResult['ParamIn']['checkOutBody'];
170            let ret = checkOutBody(body,27,null,true);
171            let result = "function fun1(v1: { [key: string]: Array<string> }):void;";
172            assert.strictEqual(JSON.stringify(ret), JSON.stringify(result));
173        });
174
175        it('test gen/tools/tool removeEmptyLine', function () {
176            param = correctResult['ParamIn']['removeEmptyLine'];
177            let retJson = JSON.stringify(removeEmptyLine(param))
178            let result = 'declare namespace napitest {\nfunction fun1(v1: string):void;\n}\nexport default napitest;\n';
179            assert.strictEqual(retJson, JSON.stringify(result));
180        });
181
182        it('test gen/tools/tool getLicense', function () {
183            param = correctResult['ParamIn']['getLicense'];
184            let retJson = JSON.stringify(getLicense(param))
185            let result = '/*\n* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. \n*/';
186            assert.strictEqual(retJson, JSON.stringify(result));
187        });
188
189        it('test gen/tools/tool replaceTab', function () {
190            param = 'declare namespace napitest {\tfunction fun1():void;}export default napitest;';
191            let retJson = JSON.stringify(replaceTab(param))
192            let result = 'declare namespace napitest {    function fun1():void;}export default napitest;';
193            assert.strictEqual(retJson, JSON.stringify(result));
194        });
195
196        it('test gen/tools/tool replaceAll', function () {
197            param = correctResult['toolsParam']['replaceAll']
198            let ret = replaceAll(JSON.stringify(param),'[funcName]','if_direct');
199            assert.strictEqual(JSON.stringify(ret), correctResult['tools']['replaceAll']);
200        });
201    }