• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device 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 */
15import {BusinessError} from "@ohos.base";
16import * as FuncTest from "function_test";
17
18loadLibrary("ani_function");
19
20const NUMBER_TWENTY: int = 20;
21
22function test_func_no_input_no_output() {
23    let foo = FuncTest.makeFoo();
24    foo.bar();
25}
26
27function test_func_int_input_no_output() {
28    let foo = FuncTest.makeFoo();
29    foo.bar(12);
30}
31
32function test_func_string_input_no_output() {
33    let foo = FuncTest.makeFoo();
34    foo.bar("zhangsan");
35}
36
37function test_func_union_str() {
38    let foo = FuncTest.makeFoo();
39    foo.bar("lisi", "myunionStr");
40}
41
42function test_func_union_number() {
43    let foo = FuncTest.makeFoo();
44    foo.bar("lisi", 18);
45}
46
47function test_func_optional_srting() {
48    let foo = FuncTest.makeFoo();
49    foo.bar("lisi", 18);
50}
51
52function test_void_input_void_output() {
53    let foo = FuncTest.makeFoo();
54    foo.test_cb_v(() => {
55        console.log("void input void output callback!");
56    });
57}
58
59function test_int_input_void_output() {
60    let foo = FuncTest.makeFoo();
61    foo.test_cb_i((a) => {
62        assertEQ(a, 1);
63    });
64}
65
66function test_string_boolean_input() {
67    let foo = FuncTest.makeFoo();
68    foo.test_cb_s((a, b) => {
69        if (b) {
70            assertEQ(a, "hello");
71        }
72    });
73}
74
75function test_string_return_value() {
76    let foo = FuncTest.makeFoo();
77    let result = foo.test_cb_rs((a) => {
78        return a * 2;
79    });
80    assertEQ(result, "888");
81}
82
83function test_from_callback() {
84    let foo = FuncTest.makeFoo();
85    let p = new Promise<int>((resolve, reject) => {
86        foo.add(10, NUMBER_TWENTY, (error: BusinessError, data?: int) => {
87            if (error.message) {
88                reject(error);
89            } else {
90                resolve(data!);
91            }
92        });
93    });
94    let res: int;
95    let err: BusinessError = new BusinessError();
96    try {
97        res = await p;
98    } catch (e) {
99        err = e as BusinessError;
100    }
101    assertEQ(res, 30);
102}
103
104function test_static_func_add() {
105    const result = FuncTest.FooCls.static_func_add(20, 10);
106    assertEQ(result, 30);
107}
108
109function test_static_func_sub() {
110    const result = FuncTest.FooCls.static_func_sub(20, 10);
111    assertEQ(result, 10);
112}
113
114function test_static_property() {
115    assertEQ(FuncTest.FooCls.static_property, 0);
116    FuncTest.FooCls.static_property = 123;
117    assertEQ(FuncTest.FooCls.static_property, 123);
118}
119
120function test_new_class() {
121    let test = new FuncTest.FooCls("hello");
122    console.log("test_new_class test is:", test.get());
123    assertEQ(test.get(), "zhangsan");
124}
125
126function test_new_class_with_two_args() {
127    let test = new FuncTest.FooCls("hello", "test");
128    console.log("test_new_class_with_two_args test is:", test.get());
129    assertEQ(test.get(), "zhangsan");
130}
131
132function main() {
133    console.log("##############start#############");
134    const suite = new ArkTestsuite("FuncTest Test");
135
136    suite.addTest("test func no input no output", test_func_no_input_no_output);
137    suite.addTest(
138        "test func int input no output", test_func_int_input_no_output);
139    suite.addTest(
140        "test func string input no output", test_func_string_input_no_output);
141    suite.addTest("test func union string", test_func_union_str);
142    suite.addTest("test func union number", test_func_union_number);
143    suite.addTest("test func optional srting", test_func_optional_srting);
144    suite.addTest(
145        "test callback no input no output", test_void_input_void_output);
146    suite.addTest(
147        "test callback int input no output", test_int_input_void_output);
148    suite.addTest(
149        "test callback string boolean input", test_string_boolean_input);
150    suite.addTest("test callback", test_string_return_value);
151    suite.addTest("test from callback", test_from_callback);
152    suite.addTest("test static func add", test_static_func_add);
153    suite.addTest("test static func sub", test_static_func_sub);
154    suite.addTest("test new class", test_new_class);
155    suite.addTest("test new class with two args", test_new_class_with_two_args);
156
157    suite.addTest("test static property", test_static_property);
158    exit(suite.run());
159}
160