• 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 */
15
16type Callback = (() => void) | ((n: number) => void) | ((n: number, n2: number) => void)
17function fn(callback: Callback): void {
18    callback(1, 2)
19}
20
21// test for lambda as param.
22let emptyArg: number = 0;
23let oneArg: number = 0;
24let twoArgs: number = 0;
25fn(() => { emptyArg = emptyArg - 1; })
26fn((n:number) => { oneArg = oneArg + n; })
27fn((n:number, n2: number) => { twoArgs = twoArgs + n + n2; })
28arktest.assertEQ(emptyArg, -1)
29arktest.assertEQ(oneArg, 1)
30arktest.assertEQ(twoArgs, 3)
31
32// test for named function as param.
33emptyArg = 0;
34oneArg = 0;
35twoArgs = 0;
36function foo() { emptyArg = emptyArg - 1; }
37function foo1(n: number) { oneArg = oneArg + n; }
38function foo2(n:number, n2: number) { twoArgs = twoArgs + n + n2; }
39fn(foo);
40fn(foo1);
41fn(foo2);
42arktest.assertEQ(emptyArg, -1)
43arktest.assertEQ(oneArg, 1)
44arktest.assertEQ(twoArgs, 3)
45
46// test for class method and static class method as param
47class A {
48    goo() { emptyArg = emptyArg - 1; }
49    goo1(n: number) { oneArg = oneArg + n; }
50    goo2(n:number, n2: number) { twoArgs = twoArgs + n + n2; }
51
52    static zoo() { emptyArg = emptyArg - 1; }
53    static zoo1(n: number) { oneArg = oneArg + n; }
54    static zoo2(n:number, n2: number) { twoArgs = twoArgs + n + n2; }
55}
56
57let a = new A();
58emptyArg = 0;
59oneArg = 0;
60twoArgs = 0;
61fn(a.goo);
62fn(a.goo1);
63fn(a.goo2);
64arktest.assertEQ(emptyArg, -1)
65arktest.assertEQ(oneArg, 1)
66arktest.assertEQ(twoArgs, 3)
67
68emptyArg = 0;
69oneArg = 0;
70twoArgs = 0;
71fn(A.zoo);
72fn(A.zoo1);
73fn(A.zoo2);
74arktest.assertEQ(emptyArg, -1)
75arktest.assertEQ(oneArg, 1)
76arktest.assertEQ(twoArgs, 3)
77