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*/
15const { TestClass1 } = require("./out/build/Release/napitest")
16const testObj = require("./out/build/Release/napitest")
17var assert = require("assert");
18
19function onPromiseVoid (ret) {
20    assert.strictEqual(ret, undefined);
21}
22
23function onPromiseNumber (ret) {
24    assert.strictEqual(ret, 0);
25}
26
27function onPromiseBool (ret) {
28    assert.strictEqual(ret, false);
29}
30
31function onPromiseString (ret) {
32    assert.strictEqual(ret, "");
33}
34
35function onPromiseIf (ret) {
36    assert.strictEqual(ret.dataName, "");
37    assert.strictEqual(ret.dataIndex, 0);
38}
39
40function onPromiseArray (ret) {
41    assert.strictEqual(ret, object);
42}
43
44function onAsyncCallString (err, ret) {
45    assert.strictEqual(err.code, 0);
46    assert.strictEqual(ret, "");
47}
48
49function onAsyncCallNumber (err, ret) {
50    assert.strictEqual(err.code, 0);
51    assert.strictEqual(ret, 0);
52}
53
54let testClass = new TestClass1();
55
56describe('Test Promise1', function () {
57    it('return basic type', function () {
58        testObj.fun0().then(onPromiseVoid);
59        testObj.fun1().then(onPromiseNumber);
60        testObj.fun2().then(onPromiseBool);
61        testObj.fun3().then(onPromiseString);
62    });
63
64    it('return basic type(interface method)', function () {
65        testClass.fun10().then(onPromiseVoid);
66        testClass.fun11().then(onPromiseNumber);
67        testClass.fun12().then(onPromiseBool);
68        testClass.fun13().then(onPromiseString);
69    });
70
71    it('return interface obj', function () {
72        testObj.fun4(50).then(onPromiseIf);
73        testClass.fun14(50).then(onPromiseIf);
74    });
75});
76
77describe('Test Promise2', function () {
78    it('return enum value', function () {
79        // enum返回实际是按number处理的
80        testObj.fun5(50).then(onPromiseNumber);
81        testClass.fun15(50).then(onPromiseNumber);
82    });
83
84    it('return array', function () {
85        testObj.fun6(50).then(onPromiseArray);
86        testClass.fun16(50).then(onPromiseArray);
87    });
88});
89
90describe('Test Promise3', function () {
91    it('test AsCallback and Promise', function () {
92        testObj.funX1(50, "60", onAsyncCallString);
93        testObj.funX1(50, "60").then(onPromiseString);
94        testClass.funX11(50, "60", onAsyncCallString);
95        testClass.funX11(50, "60").then(onPromiseString);
96    });
97
98    it('test Promise and AsCallback', function () {
99        testObj.funX2(50, "60", onAsyncCallNumber);
100        testObj.funX2(50, "60").then(onPromiseNumber);
101        testClass.funX12(50, "60", onAsyncCallNumber);
102        testClass.funX12(50, "60").then(onPromiseNumber);
103    });
104});
105
106