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 onCallback (ret) {
20    assert.strictEqual(ret, 0);
21}
22
23function onVoidCallback (ret) {
24    assert.strictEqual(ret, undefined);
25}
26
27function onAsyncCallback (err, ret) {
28    assert.strictEqual(err.code, 0);
29    assert.strictEqual(ret, 0);
30}
31
32function onVoidAsyncCallback (err, ret) {
33    assert.strictEqual(err.code, 0);
34    assert.strictEqual(ret, undefined);
35}
36
37function onCallbackNumStr(num) {
38    assert.strictEqual(num, 0);
39    return '' + num
40}
41
42function onCallbackVoid () {
43
44}
45
46// 测试:cb: (wid: boolean, str: string, tc2:number) => string
47function onCallbackfun9(wid, str, tc2) {
48    return 'wid' + 'str' + 'tc2'
49}
50
51// 测试:cb: (wid: boolean) => string
52function onCallbackfun10nm(wid) {
53    return 'fun10nm'
54}
55
56function onCallbackBooleanVStr (isOK) {
57    let str
58    if (isOK) {
59        str = 'a' + 'b'
60    }
61}
62
63function onCallbackBooleanVStrRet (isOK) {
64    let str = ''
65    if (isOK) {
66        str = 'onCallbackBooleanVStr' + 'a' + 'b' + 'isOK'
67    }
68    return str
69}
70
71function onCallbackfun22nm(isOk) {
72    let flag = 0
73    if(isOk) {
74        flag = 1
75    }
76    return flag
77}
78
79function onCallbackfun21nm(isOk) {
80    let flag = false
81    if(isOk) {
82        flag = true
83    }
84    return flag
85}
86
87describe('Test interface callback', function () {
88    // 测试:fun11(cb: Callback<number>): void;
89    it('test callback in interface fun11', function () {
90        let testClass = new TestClass1();
91        testClass.fun11(onCallback);
92    });
93
94    // 测试:fun12(cb: Callback<void>): void;
95    it('test callback in interface fun12', function () {
96        let testClass = new TestClass1();
97        testClass.fun12(onVoidCallback);
98    });
99
100    // 测试:fun13(cb: AsyncCallback<number>): void;
101    it('test callback in interface fun13', function () {
102        let testClass = new TestClass1();
103        testClass.fun13(onAsyncCallback);
104    });
105
106    // 测试:fun14(cb: AsyncCallback<void>): void;
107    it('test callback in interface fun14', function () {
108        let testClass = new TestClass1();
109        testClass.fun14(onVoidAsyncCallback);
110    });
111
112    // 测试:function fun15(cb: Callback<number>): string;
113    it('test common func callback fun15', function () {
114        let testClass = new TestClass1();
115        testClass.fun15(onCallbackNumStr);
116    });
117
118    // 测试:fun16(tt: function): void;
119    it('test common func callback fun16', function () {
120        let testClass = new TestClass1();
121        testClass.fun16(onCallbackVoid);
122    });
123
124    // 测试:fun17(tt: Function): string;
125    it('test common func callback fun17', function () {
126        let testClass = new TestClass1();
127        let ret = testClass.fun17(onCallbackVoid);
128            assert.strictEqual(ret, '');
129    });
130
131    // 测试:fun110(cb: (wid: boolean) => string): string;
132    it('test common func callback fun110', function () {
133        let testClass = new TestClass1();
134        let ret = testClass.fun110(onCallbackBooleanVStrRet);
135            assert.strictEqual(ret, '');
136    });
137
138    // 测试:fun111(cb: (wid: boolean) => string): boolean;
139    it('test common func callback fun111', function () {
140        let testClass = new TestClass1();
141        let ret = testClass.fun111(onCallbackBooleanVStrRet);
142            assert.strictEqual(ret, false);
143    });
144
145    // 测试:fun112(cb: (wid: boolean) => string): number;
146    it('test common func callback fun112', function () {
147        let testClass = new TestClass1();
148        let ret = testClass.fun112(onCallbackBooleanVStrRet);
149            assert.strictEqual(ret, 0);
150    });
151
152    // 待补充用例
153    // 测试:fun210(cb: (wid: boolean) => string): boolean;
154    // 测试:fun211(cb: (wid: boolean) => boolean): string;
155    // 测试:fun212(cb: (wid: boolean) => number): string;
156});
157
158describe('Test callback', function () {
159    // 测试:function fun1(cb: Callback<number>): void;
160    it('test common func callback fun1', function () {
161        testObj.fun1(onCallback);
162    });
163
164    // 测试:function fun2(cb: Callback<void>): void;
165    it('test common func callback fun2', function () {
166        testObj.fun2(onVoidCallback);
167    });
168
169    // 测试:function fun3(cb: AsyncCallback<number>): void;
170    it('test common func callback fun3', function () {
171        testObj.fun3(onAsyncCallback);
172    });
173
174    // 测试:function fun4(cb: AsyncCallback<void>): void;
175    it('test common func callback fun4', function () {
176        testObj.fun4(onVoidAsyncCallback);
177    });
178
179    // 测试:function fun5(cb: Callback<number>): string;
180    it('test common func callback fun5', function () {
181        testObj.fun5(onCallbackNumStr);
182    });
183
184    // 测试:function fun6(tt: function): void;
185    it('test common func callback fun6', function () {
186        testObj.fun5(onCallbackVoid);
187    });
188
189    // 测试:fun7(tt: Function): string;
190    it('test common func callback fun7', function () {
191       let ret = testObj.fun7(onCallbackVoid);
192        assert.strictEqual(ret, '');
193    });
194});
195
196describe('Test namespace arrow callback ', function () {
197    // 测试:function fun8(cb: (wid: boolean) => void): string;
198it('test common func callback fun8', function () {
199let ret = ''
200ret = testObj.fun8(onCallbackBooleanVStr);
201assert.strictEqual(ret, '');
202});
203
204// 测试:function fun9(cb: (wid: boolean, str: string, tc2:number) => string): string;
205it('test common func callback fun9', function () {
206let ret = ''
207ret = testObj.fun9(onCallbackfun9);
208assert.strictEqual(ret, '');
209});
210
211// 测试:function fun10nm(cb: (wid: boolean) => string): string;
212it('test common func callback fun10nm', function () {
213let ret = ''
214ret = testObj.fun10nm(onCallbackfun10nm);
215assert.strictEqual(ret, '');
216});
217
218// 测试:function fun11nm(cb: (wid: boolean) => string): boolean;
219it('test common func callback fun11nm', function () {
220let ret = ''
221ret = testObj.fun11nm(onCallbackfun10nm);
222assert.strictEqual(ret, false);
223});
224
225// 测试:function fun12nm(cb: (wid: boolean) => string): number;
226it('test common func callback fun12nm', function () {
227let ret = ''
228ret = testObj.fun12nm(onCallbackfun10nm);
229assert.strictEqual(ret, 0);
230});
231
232// 测试:function fun20nm(cb: (wid: boolean) => string): boolean;
233it('test common func callback fun20nm', function () {
234let ret = ''
235ret = testObj.fun20nm(onCallbackBooleanVStrRet);
236assert.strictEqual(ret, false);
237});
238
239// 测试:function fun21nm(cb: (wid: boolean) => boolean): string;
240it('test common func callback fun21nm', function () {
241let ret = ''
242ret = testObj.fun21nm(onCallbackfun21nm);
243assert.strictEqual(ret, '');
244});
245
246// 测试:function fun22nm(cb: (wid: boolean) => number): string;
247it('test common func callback fun22nm', function () {
248let ret = ''
249ret = testObj.fun22nm(onCallbackfun22nm);
250assert.strictEqual(ret, '');
251});
252
253});