• 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*/
15const { Animal } = require("./out/build/Release/napitest")
16const test = require("./out/build/Release/napitest")
17var assert = require("assert");
18const { consumers } = require("stream");
19
20describe('type function', function () {
21  // 测试:function fun1(v: Plant): number;
22  it('test fun1', function () {
23    let ret = test.fun1({ tomato: 'iutluhkd', tomatoId: 52, isTomato: true });
24    assert.deepStrictEqual(ret, 0);
25  });
26
27  // 测试:function fun2(v: boolean): Plant;
28  it('test fun2', function () {
29    let ret = test.fun2(true);
30    let retJson = JSON.stringify(ret);
31    assert.strictEqual(retJson, '{"tomato":"","tomatoId":0,"isTomato":false}');
32  });
33
34  // 测试:function fun3(v0: MyString, v1: MyNumberType, v2: MyBool): boolean;
35  it('test fun3', function () {
36    let ret = test.fun3('missssss', 24, false);
37    assert.deepStrictEqual(ret, false);
38  });
39
40  // 测试:function fun4(v: string): MyString;
41  it('test fun4', function () {
42    let ret = test.fun4('aaa');
43    assert.deepStrictEqual(ret, '');
44  });
45
46  // 测试:function fun5(v: number): MyNumberType;
47  it('test fun5', function () {
48    let ret = test.fun5(22);
49    assert.deepStrictEqual(ret, 0);
50  });
51
52  // 测试:function fun6(v: boolean): MyBool;
53  it('test fun6', function () {
54    let ret = test.fun6(true);
55    assert.deepStrictEqual(ret, false);
56  });
57
58  // 测试:function fun7(v: MyUnion): number;
59  it('test fun7', function () {
60    let ret = test.fun7(true);
61    assert.deepStrictEqual(ret, 0);
62    let ret2 = test.fun7(16);
63    assert.deepStrictEqual(ret2, 0);
64    let ret3 = test.fun7('fun7');
65    assert.deepStrictEqual(ret3, 0);
66  });
67
68  // 测试:function fun8(v: MyEnumType): string;
69  it('test fun8', function () {
70    let ret = test.fun8('keyup');
71    assert.deepStrictEqual(ret, '');
72    let ret2 = test.fun8('keydown');
73    assert.deepStrictEqual(ret2, '');
74  });
75
76  /* 测试:function fun9(v: Flower): string;
77  type Flower =
78  {
79    name: string;
80    Id: number;
81    isMoreFlower: boolean;
82  }
83  */
84  it('test fun9', function () {
85    let ret = test.fun9({ name: 'mudan', Id: 10, isMoreFlower: true });
86    assert.deepStrictEqual(ret, '');
87    let ret2 = test.fun9({ name: 'gouweicao', Id: 20, isMoreFlower: false });
88    assert.deepStrictEqual(ret2, '');
89  });
90});
91
92describe('Type Function', function () {
93  /* 测试
94    type OptionalTest =
95    {
96      ttt: number;
97      param1?: string;
98      param2?: number;
99      param3?: boolean;
100      param4?: Array<number>;
101      param5?: string[];
102      param6: Array<boolean>;
103      param7?: Map<string, string>;
104      param8?: {[key: string]: number};
105      param9: Map<string, boolean>;
106      param10?: boolean | number | string;
107    }
108    function fun10(v: OptionalTest): string;
109    */
110    it('test fun10', function () {
111        let ret = test.fun10(
112            { ttt: 11, param1: "param", param2: 20, param3: false,
113              param6: [true, false], param9: {'map': false}});
114        assert.deepStrictEqual(ret, '');
115        let ret2 = test.fun10(
116            { ttt: 11, param1: "param", param3: false,
117              param6: [true, false], param9: {'map': false}});
118        assert.deepStrictEqual(ret2, '');
119        let ret3 = test.fun10(
120            { ttt: 11, param1: "param",
121              param6: [true, false], param9: {'map': false}});
122        assert.deepStrictEqual(ret3, '');
123        let ret4 = test.fun10({ ttt: 11, param6: [true, false], param9: {'map': false}});
124        assert.deepStrictEqual(ret4, '');
125        let ret5 = test.fun10(
126            { ttt: 11, param6: [true, false], param9: {'map': false},
127              param4: [1,2,3], param5: ["aa", "bb", "cc"]});
128        assert.deepStrictEqual(ret5, '');
129        let ret6 = test.fun10(
130            { ttt: 11, param6: [true, false], param9: {'map': false},
131              param7: {'map7': 'value7'}, param8: {'map': 8}});
132        assert.deepStrictEqual(ret6, '');
133        let ret7 = test.fun10(
134            { ttt: 11, param6: [true, false], param9: {'map': false},
135              param10: 54});
136        assert.deepStrictEqual(ret7, '');
137    });
138});
139
140describe('Interface', function () {
141  // 测试:catFunc1(v: MyString): MyString;
142  it('test Animal catFunc1', function () {
143      let tc1 = new Animal();
144      let ret = tc1.catFunc1('cat');
145      assert.deepStrictEqual(ret, '');
146  });
147
148  // 测试:catFunc2(v: MyNumberType): MyNumberType;
149  it('test Animal catFunc2', function () {
150      let tc = new Animal();
151      let ret = tc.catFunc2(23);
152      assert.deepStrictEqual(ret, 0);
153  });
154
155  // 测试:catFunc3(v: MyBool): MyBool;
156  it('test Animal catFunc3', function () {
157      let tc = new Animal();
158      let ret = tc.catFunc3(true);
159      assert.deepStrictEqual(ret, false);
160  });
161
162  // 测试:catFunc4(v: Plant): string;
163  it('test Animal catFunc4', function () {
164      let tc = new test.Animal();
165      let ret = tc.catFunc4({ tomato: 'ImageTomato', tomatoId: 12, isTomato: true });
166      assert.deepStrictEqual(ret, '');
167  });
168
169  // 测试:catFunc5(v: number): Plant;
170  it('test Animal catFunc5', function () {
171    let tc = new test.Animal();
172    let ret = tc.catFunc5(66);
173    let retJson = JSON.stringify(ret);
174    assert.strictEqual(retJson, '{"tomato":"","tomatoId":0,"isTomato":false}');
175  });
176});
177
178
179
180
181