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, TestClass2, TestClassUse, TestClassLater } = require("./out/build/Release/napitest")
16const { Demo, Test, funcTest, funcTest2, Woman, Child } = require("./out/build/Release/napitest")
17const test = require("./out/build/Release/napitest")
18var assert = require("assert");
19const { consumers } = require("stream");
20
21describe('Class', function () {
22    it('test TestClass1 fun1', function () {
23        let tc1 = new TestClass1();
24        let ret = tc1.fun1(1);
25        assert.strictEqual(ret, 0);
26    });
27
28    it('test TestClass1 fun2', function () {
29        let tc = new TestClass1();
30        let ret = tc.fun2([1, 2, 3], { name: 'haha', age: 20 });
31        let retJson = JSON.stringify(ret);
32        assert.strictEqual(retJson, '{"name":"","age":0}');
33    });
34
35    it('test TestClass1 fun3', function () {
36        let tc = new TestClass1();
37        let ret = tc.fun3(2,'aaa',true);
38        assert.strictEqual(ret, false);
39    });
40
41    it('test TestClass1 fun4', function () {
42        let tc = new test.TestClass1();
43        let ret = tc.fun4({ 'name': 'haha', 'age': '20' });
44        let retJson = JSON.stringify(ret);
45        assert.strictEqual(retJson, '[]');
46        ret = tc.fun4({ 'name': 'haha', 'age': '20' },'aaa');
47        retJson = JSON.stringify(ret);
48        assert.strictEqual(retJson, '[]');
49    });
50
51    /* 测试
52    export class Woman {
53      constructor(name_: string, age_: number, isMarried_: boolean, status_: TestStatus;);
54      w_name: string;
55      w_age: number;
56      w_isMarried: boolean;
57      w_status: TestStatus;
58    }
59    */
60    it('test Woman constructor', function () {
61        let tc = new Woman("haha", 22, true, test.TestStatus.START_ABILITY);
62        console.info("w_name is " + tc.w_name);
63        console.info("w_age is " + tc.w_age);
64        console.info("w_isMarried is " + tc.w_isMarried);
65        console.info("w_status is " + tc.w_status);
66    });
67
68    /* 测试
69    export class Child {
70      constructor(name_: string, age_: number, status_: TestEnumString);
71      w_name: string;
72      w_age: number;
73      w_status: TestEnumString;
74    }
75    */
76    it('test Child constructor', function () {
77        let tc = new Child("xixi", 10, test.TestEnumString.ACTION_SEARCH);
78        console.info("w_name is " + tc.w_name);
79        console.info("w_age is " + tc.w_age);
80        console.info("w_status is " + tc.w_status);
81    });
82});
83
84describe('Class Exception Test', function () {
85    it('test Woman constructor exception', function () {
86        let ret = false;
87        try {
88            let tc = new Woman("hhh", 23, true, 5);
89            console.info("w_name is " + tc.w_name);
90            console.info("w_age is " + tc.w_age);
91            console.info("w_isMarried is " + tc.w_isMarried);
92            console.info("w_status is " + tc.w_status);
93        } catch(err) {
94            ret = true;
95            console.error("error: " + err);
96        }
97        assert.strictEqual(ret, true);
98    });
99
100    it('test Child constructor exception', function () {
101        let ret = false;
102        try {
103            let tc = new Child("xixi", 10, "ggg");
104            console.info("w_name is " + tc.w_name);
105            console.info("w_age is " + tc.w_age);
106            console.info("w_status is " + tc.w_status);
107        } catch(err) {
108            ret = true;
109            console.error("error: " + err);
110        }
111        assert.strictEqual(ret, true);
112    });
113
114});
115
116describe('Class defined later', function () {
117    it('test TestClassUse funceUse', function () {
118        let testLater = new TestClassLater();
119        let tUse = new TestClassUse();
120        let ret = tUse.funceUse(testLater);
121        assert.strictEqual(ret, "");
122    });
123});
124
125describe('Class part2', function () {
126    it('test TestClass1 fun5', function () {
127        let tc = new test.TestClass1();
128        let ret = tc.fun5(
129            [{ name: 'haha', age: 20 }, { name: 'houhou', age: 23 }]);
130        let retJson = JSON.stringify(ret);
131        assert.strictEqual(retJson, '{"name":"","age":0}');
132    });
133
134    it('test TestClass1 fun6', function () {
135        let tc = new test.TestClass1();
136        let ret = tc.fun6(['11','22','33'],{'isExit':true,'isTrue':false});
137        let retJson = JSON.stringify(ret);
138        assert.strictEqual(retJson, '[]');
139    });
140
141    it('test TestClass1 fun8', function () {
142        let tc = new test.TestClass1();
143        let ret = tc.fun8();
144        assert.deepStrictEqual(typeof ret, 'undefined');
145    });
146
147    /* 测试:fun9(manA: Man): string;
148    class Man
149    {
150        name: string;
151        age: number;
152    }
153    */
154    it('test TestClass1 fun9', function () {
155        let tc = new test.TestClass1();
156        let ret = tc.fun9({ name: "testaa", age: 10});
157        assert.strictEqual(ret, '');
158    });
159
160    /* 测试:fun10(v: Image): Image;
161    export class Image {
162        width: number;
163        height: number;
164        toDataURL(type?: string, quality?: number): string;
165    }
166    */
167    it('test TestClass1 fun10', function () {
168        let tc = new test.TestClass1();
169        let ret = tc.fun10({ width: 5, height: 10});
170        assert.strictEqual(ret, '');
171    });
172
173    // 测试:fun11(v: LaunchReason): string;
174    it('test TestClass1 fun11', function () {
175        let tc = new test.TestClass1();
176        let ret = tc.fun11(test.LaunchReason.START_ABILITY);
177        assert.strictEqual(ret, '');
178    });
179
180    /* 测试:fun12(v: TestStatus): number;
181    export enum TestStatus {
182        UNKNOWN = 0,
183        START_ABILITY = 1,
184        CALL = 2,
185        CONTINUATION = 3,
186    }
187    */
188    it('test TestClass1 fun12', function () {
189        let tc = new test.TestClass1();
190        let ret = tc.fun12(test.TestStatus.CALL);
191        assert.strictEqual(ret, '');
192    });
193
194    /* 测试:fun13(v: TestEnumString): number;
195    export enum TestEnumString {
196        ACTION_HOME = 'ohos.want.action.home',
197        ACTION_DIAL = 'ohos.want.action.dial',
198        ACTION_SEARCH = 'ohos.want.action.search',
199        ACTION_WIRELESS_SETTINGS = 'ohos.settings.wireless',
200    }
201    */
202    it('test TestClass1 fun13', function () {
203        let tc = new test.TestClass1();
204        let ret = tc.fun13(test.TestEnumString.ACTION_DIAL);
205        assert.strictEqual(ret, '');
206    });
207});
208
209describe('TestClass2', function () {
210    // 测试:func1(name : string, fp3: {nm: string, age: number}): string;
211    it('test TestClass2 func1', function () {
212        let tc = new TestClass2()
213        let ret = tc.func1("func1p1", {nm:"aaa",age:18,flag:false});
214    });
215
216    // 测试:func2(input: string): { read: number; written: number; flag: boolean };
217    it('test TestClass2 func2', function () {
218        let tc = new TestClass2()
219        let ret = tc.func2("name");
220    });
221
222    // 测试:func3(from: string, to: string): Promise<{result: number, errMsg: string, isT: boolean}>;
223    it('test TestClass2 func3', function () {
224        let tc = new TestClass2()
225        let ret = tc.func3("from", "to");
226    });
227
228    // 测试:func4(from: string, to: string): Promise<{result: number; errMsg: string; isT: boolean}>;
229    it('test TestClass2 func4', function () {
230        let tc = new TestClass2()
231        let ret = tc.func4("responeFrom", "responseTo");
232    });
233
234    // 测试:func5(v1: string, v2: number, v3: boolean);
235    it('test TestClass2 func5', function () {
236      let tc = new TestClass2()
237      tc.func5("func5", 5, false);
238    });
239});
240
241describe('Class Nest', function () {
242  /* 测试
243  class Demo {
244      equals(other: Demo): boolean;
245      handleCallback(): void;
246      intPro: number;
247      strPro: string;
248      boolPro: boolean;
249      inter: aa;
250      type: Type;
251  }
252  */
253  it('test Demo equals', function () {
254      let tc1 = new test.Demo();
255      let ret = tc1.equals({
256          intPro: 1,
257          strPro: "string",
258          boolPro: true,
259          inter: {abc: "abc", def: 7},
260          type: test.Type.typeA,
261      });
262      assert.strictEqual(ret, false);
263  });
264
265  /* 测试
266    class Test {
267        type: Type;
268        func(param: Type): boolean;
269    }
270    */
271    it('test Test func', function () {
272        let tc1 = new test.Test();
273        let ret = tc1.func(test.Type.typeB);
274        assert.strictEqual(ret, false);
275    });
276
277    // 测试:function funcTest(v: Type): boolean;
278    it('test funcTest', function () {
279        let ret = test.funcTest(test.Type.typeA);
280        assert.strictEqual(ret, false);
281    });
282
283    // 测试:function funcTest2(v: Test): boolean;
284    it('test funcTest2', function () {
285        let ret = test.funcTest2({type: test.Type.typeB});
286        assert.strictEqual(ret, false);
287    });
288});
289
290
291