• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024-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
16interface Base {
17    f: int
18}
19
20interface InnerValue {
21    v: int
22}
23
24interface I extends Base {
25    x: Int
26    s: String
27    iv: InnerValue
28}
29
30function returnI(): I {
31    return {x: 99, f: 44, s: "sR", iv: {v: 77}} // return statement
32}
33
34function test(i: int, f: int, x: int, s: String, ivv: int) {} // should not prevent calling the next func
35
36function test(i: I = {f: -1, x: -2, s: "default", iv: {v: -3}},
37              f: int = -1,
38              x: int = -2,
39              s: String = "default",
40              ivv: int = -3) {
41    assertEQ(i.f, f, "\"f\" is not equal \"" + i.f + "\" != \"" + f + "\"")
42    assertEQ(i.x, x, "\"x\" is not equal \"" + i.x + "\" != \"" + x + "\"")
43    assertEQ(i.s, s, "\"s\" is not equal \"" + i.s + "\" != \"" + s + "\"")
44    assertEQ(i.iv.v, ivv, "\"innervalue.v\" is not equal \"" + i.iv.v + "\" != \"" + ivv + "\"")
45}
46
47function main(): int {
48    let i: I = { // variable definition
49        f : 1,
50        "x": 2,
51        s: "s1",
52        iv: { v: 3}
53    };
54    test(i, 1, 2, "s1", 3)
55
56    let i2 = { // as construction
57        f: 4,
58        x: 5,
59        s: "s2",
60        iv: { v: 6}
61    } as I;
62    test(i2, 4, 5, "s2", 6)
63
64    i = {f: 7, x: 8, s: "s3", iv : { v: 9}} // assignment
65    test(i, 7, 8, "s3", 9)
66
67    test({  // function argument
68        f: 10,
69        x: 11,
70        s: "s3",
71        iv: {
72            v: 12
73        }}, 10, 11, "s3", 12)
74
75    test(returnI(), 44, 99, "sR", 77)
76
77    let ia: I[] = [{f: 20, x: 21, s: "first", iv: {v: 22}}, {f: 30, x: 31, s: "second", iv: {v: 32}}] // array elements
78    test(ia[1], 30, 31, "second", 32)
79
80    test() // optional parameter
81
82    return 0
83}
84