!34 【napi工具】增加自动化测试用例

Merge pull request !34 from bayanxing/master
This commit is contained in:
openharmony_sig_ci 2022-02-23 06:48:49 +00:00 committed by Gitee
commit e6a8383fef
21 changed files with 1775 additions and 3 deletions

1136
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -7,6 +7,8 @@
"doc": "docs"
},
"dependencies": {
"mocha": "^9.2.1",
"node-gyp": "^8.4.1",
"stdio": "^2.1.1",
"typescript": "^4.5.5"
},

View File

@ -85,7 +85,7 @@ function generateFunction(func) {
}
function formatMiddleInit(inNamespace, name) {
let middleInit
let middleInit = ""
if (inNamespace.length > 0) {
let nsl = inNamespace.split("::")
nsl.pop()

View File

@ -94,10 +94,9 @@ function returnGenerate(type, param) {
else {
print(`\n---- The current version do not support this type return %s ----\n`.format(type))
}
param.valueFill += "%svio->out".format(param.valueFill.length > 0 ? ", " : "")
}
module.exports = {
cToJs,
returnGenerate
}
}

55
test/storytest/README_ZH.md Executable file
View File

@ -0,0 +1,55 @@
# story_test
## 概述
本文主要介绍NAPI框架代码生成工具story功能测试使用指导。
## 目录
```
├── napi_generator # NAPI框架代码生成工具
│ ├── src # 工具源码
│ ├── ... # 其他文件夹
│ ├── test # 测试用例
| | ├── storytest
│ | | ├── test_direct # 同步直接返回函数用例
│ | | ├── test_interface # ts文件输入interface测试用例
│ | | ├── test_namespace # ts文件输入namespace测试用例
│ | | ├── test_number # ts文件输入number测试用例
│ | | ├── test_string # ts文件输入string测试用例
│ | | └── test.py # 用例执行脚本
| | └── unittest # 单元测试用例
```
### code目录源码介绍
此目录为NAPI框架代码生成工具对应的源码开发者可基于此代码进行二次开发。
## 软件环境准备
系统建议Ubuntu 20.04
依赖版本vscode 1.62.0 -- 需要吗?
安装以下依赖:
1.npm i
2.npm i stdio
3.npm i node-gyp
## 使用说明
进入napi_generator/目录:
1.执行所有用例命令为python test/storytest/test.py
2.执行单个用例,如test_interface命令为python test/storytest/test.py test_interface
## 查看结果
红色:用例执行失败
绿色:用例执行成功
## 相关仓
暂无

51
test/storytest/test.py Executable file
View File

@ -0,0 +1,51 @@
# !/usr/bin/env python3
# coding=utf-8
"""
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
"""
import os
import sys
def do_test():
if not os.path.isfile("@ohos.test.d.ts"):
return
if not os.path.exists("out"):
os.mkdir("out")
ret=os.popen("node ../../../src/gen/cmd_gen.js -f @ohos.test.d.ts -o ./out")
print(ret.read())
os.chdir("out")
ret=os.popen("npx node-gyp configure build")
os.chdir("..")
if "COPY Release" not in ret.read():
print("error compile failed")
return
else:
print("compile ok")
os.system("npx mocha test.js")
if __name__ == "__main__":
work_path = os.path.split(sys.argv[0])[0]
os.chdir(work_path)
if len(sys.argv)>=2:
for fn in sys.argv[1:]:
if os.path.isdir(fn):
os.chdir(fn)
do_test()
os.chdir("..")
else:
print("error",fn,"is not exist")
else:
for fn in os.listdir("."):
if os.path.isdir(fn):
os.chdir(fn)
do_test()
os.chdir("..")

30
test/storytest/test_direct/@ohos.test.d.ts vendored Executable file
View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AsyncCallback, Callback } from './basic';
declare namespace napitest {
export interface Result {
code: number;
data: string;
}
interface TestClass1 {
num1: number;
fun1(v: number): number;
fun2(v: string): Result;
}
}
export default napitest;

30
test/storytest/test_direct/basic.d.ts vendored Executable file
View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export interface Callback<T> {
(data: T): void;
}
export interface ErrorCallback<T extends Error = BusinessError> {
(err: T): void;
}
export interface AsyncCallback<T> {
(err: BusinessError, data: T): void;
}
export interface BusinessError extends Error {
code: number;
}

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { TestClass1 } = require("./out/build/Release/napitest")
var assert = require("assert");
describe('Number', function () {
function abc(ret) {
assert.strictEqual(ret, 0);
}
it('test TestClass1 fun1', function () {
let tc1 = new TestClass1();
let ret = tc1.fun1(1);
assert.strictEqual(ret, 0);
});
it('test TestClass1 fun2', function () {
let tc1 = new TestClass1();
let ret = tc1.fun2('aaa');
let retJson = JSON.stringify(ret);
assert.strictEqual(retJson, '{"code":0,"data":""}');
});
});

33
test/storytest/test_interface/@ohos.test.d.ts vendored Executable file
View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AsyncCallback, Callback } from './basic';
declare namespace napitest {
interface Human {
name: string;
age: number;
}
interface TestClass1 {
ahuman: Human;
num1: number;
fun1(v: number): number;
fun2(numcc: Array<number>, mancc: Human): Human;
fun3(v: number):boolean;
fun4(v2: string, mancc: Array<Human>): Human;
}
}
export default napitest;

30
test/storytest/test_interface/basic.d.ts vendored Executable file
View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export interface Callback<T> {
(data: T): void;
}
export interface ErrorCallback<T extends Error = BusinessError> {
(err: T): void;
}
export interface AsyncCallback<T> {
(err: BusinessError, data: T): void;
}
export interface BusinessError extends Error {
code: number;
}

View File

@ -0,0 +1,53 @@
/*
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { TestClass1,TestClass2 } = require("./out/build/Release/napitest")
const test = require("./out/build/Release/napitest")
var assert = require("assert");
const { consumers } = require("stream");
describe('Interface', function () {
it('test TestClass1 fun1', function () {
let tc1 = new TestClass1();
let ret = tc1.fun1(1);
assert.strictEqual(ret, 0);
});
it('test TestClass1 fun2', function () {
let tc = new TestClass1();
let ret = tc.fun2([1,2,3],{name:'haha',age:20});
let retJson=JSON.stringify(ret);
assert.strictEqual(retJson, '{"name":"","age":0}');
});
it('test TestClass1 fun3', function () {
let tc = new TestClass1();
let ret = tc.fun3(2);
let retJson=JSON.stringify(ret);
assert.strictEqual(retJson, '0');
});
it('test TestClass1 fun4', function () {
let tc = new test.TestClass1();
let ret = tc.fun4('aaa',[{name:'haha',age:20},{name:'houhou',age:23}]);
let retJson=JSON.stringify(ret);
assert.strictEqual(retJson, '{"name":"","age":0}');
});
});

32
test/storytest/test_namespace/@ohos.test.d.ts vendored Executable file
View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AsyncCallback, Callback } from './basic';
declare namespace napitest {
namespace Space3 {
function fun3(v3: string): string;
interface Animal {
name: string;
age: number;
}
interface TestClass2 {
animal: Animal;
fix(v1:string):string;
fun2(numcc: Array<number>, animalcc: Animal): Animal;
}
}
}
export default napitest;

30
test/storytest/test_namespace/basic.d.ts vendored Executable file
View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export interface Callback<T> {
(data: T): void;
}
export interface ErrorCallback<T extends Error = BusinessError> {
(err: T): void;
}
export interface AsyncCallback<T> {
(err: BusinessError, data: T): void;
}
export interface BusinessError extends Error {
code: number;
}

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { TestClass2 } = require("./out/build/Release/napitest")
const test = require("./out/build/Release/napitest")
var assert = require("assert");
const { consumers } = require("stream");
describe('Interface', function () {
it('test TestClass2 fun3', function () {
let ret = test.Space3.fun3("kkk");
assert.strictEqual(ret, '');
});
it('test TestClass2 fix', function () {
let tc1 = new test.Space3.TestClass2();
let ret = tc1.fix('ccc');
assert.strictEqual(ret, '');
});
it('test TestClass2 fun4', function () {
let tc1 = new test.Space3.TestClass2();
let ret = tc1.fun2([1,2,3,4],{name:'haha',age:20});
let retJson=JSON.stringify(ret);
assert.strictEqual(retJson, '{"name":"","age":0}');
});
});

29
test/storytest/test_number/@ohos.test.d.ts vendored Executable file
View File

@ -0,0 +1,29 @@
/*
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AsyncCallback, Callback } from './basic';
declare namespace napitest {
interface TestClass1 {
num1: number;
}
function fun1(v: number): number;
function fun2(v: Array<number>, cb: Callback<number>): void;
function fun3(v: TestClass1, cb: AsyncCallback<number>): void;
function fun4(v: string): number;
function fun5(v: number, d: Promise<string>): string;
}
export default napitest;

30
test/storytest/test_number/basic.d.ts vendored Executable file
View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export interface Callback<T> {
(data: T): void;
}
export interface ErrorCallback<T extends Error = BusinessError> {
(err: T): void;
}
export interface AsyncCallback<T> {
(err: BusinessError, data: T): void;
}
export interface BusinessError extends Error {
code: number;
}

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { fun1, fun2, fun3,fun4,fun5 } = require("./out/build/Release/napitest")
var assert = require("assert");
describe('Number', function () {
it('test fun1', function () {
let ret = fun1(1);
assert.strictEqual(ret, 0);
});
function abc(ret) {
assert.strictEqual(ret, 0);
}
it('test fun2', function () {
fun2([1, 2, 3, 4], abc);
});
// it('test fun2', function () {
// fun2([1, 2, 3, 4]).then(abc);
// });
it('test fun3', function () {
fun3({ num1: 3 }).then(abc);
});
it('test fun4', function () {
fun4('aaa',abc);
});
function def(ret) {
assert.strictEqual(ret, '');
}
it('test fun5', function () {
fun5(12,def);
//fun5(12).then(def);
});
});

28
test/storytest/test_string/@ohos.test.d.ts vendored Executable file
View File

@ -0,0 +1,28 @@
/*
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AsyncCallback, Callback } from './basic';
declare namespace napitest {
interface TestClass1 {
string1: string;
}
function fun1(v: string): string;
function fun2(v: Array<string>, cb: Callback<string>): void;
function fun3(v: TestClass1, cb: AsyncCallback<string>): void;
}
export default napitest;

30
test/storytest/test_string/basic.d.ts vendored Executable file
View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export interface Callback<T> {
(data: T): void;
}
export interface ErrorCallback<T extends Error = BusinessError> {
(err: T): void;
}
export interface AsyncCallback<T> {
(err: BusinessError, data: T): void;
}
export interface BusinessError extends Error {
code: number;
}

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { fun1, fun2, fun3 } = require("./out/build/Release/napitest")
var assert = require("assert");
describe('String', function () {
it('test fun1', function () {
let ret = fun1("1");
assert.strictEqual(ret, '');
});
function abc(ret) {
assert.strictEqual(ret, '');
}
it('test fun2', function () {
fun2(['1', '2', '3', '4'], abc);
});
it('test fun3', function () {
fun3({ string1: '3' }).then(abc);
});
});