• 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 EnumTest from "enum_test";
17
18loadLibrary("ani_enum");
19
20function test_nextEnum_with_color() {
21    const color = EnumTest.Color.GREEN;
22    const nextColor = EnumTest.nextEnum(color);
23    assertEQ(nextColor, EnumTest.Color.BLUE)
24}
25
26function test_getValueOfEnum() {
27    let color = EnumTest.Color.GREEN;
28    let value = EnumTest.getValueOfEnum(color);
29    assertEQ(value, "Green");
30}
31
32function test_getValueOfEnumWeekday() {
33    let day = EnumTest.Weekday.WEDNESDAY;
34    let value = EnumTest.getValueOfEnumWeekday(day);
35    assertEQ(value, 3);
36}
37
38function test_nextEnumWeekday() {
39    let day = EnumTest.Weekday.WEDNESDAY;
40    let nextDay = EnumTest.nextEnumWeekday(day);
41    assertEQ(nextDay, EnumTest.Weekday.THURSDAY);
42}
43
44function test_nextEnum() {
45    const color = EnumTest.Color.BLUE;
46    const nextColor = EnumTest.nextEnum(color);
47    assertEQ(nextColor, EnumTest.Color.RED);
48}
49
50function test_nextEnum_with_i8() {
51    const first = EnumTest.NumTypeI8.ONE;
52    const next = EnumTest.nextEnumI8(first);
53    assertEQ(next, EnumTest.NumTypeI8.TWO)
54}
55
56function test_nextEnum_with_i16() {
57    const first = EnumTest.NumTypeI16.ONE;
58    const next = EnumTest.nextEnumI16(first);
59    assertEQ(next, EnumTest.NumTypeI16.TWO)
60}
61
62function test_nextEnum_with_i32() {
63    const first = EnumTest.NumTypeI32.ONE;
64    const next = EnumTest.nextEnumI32(first);
65    assertEQ(next, EnumTest.NumTypeI32.TWO)
66}
67
68function test_nextEnum_with_i64() {
69    const first = EnumTest.NumTypeI64.ONE;
70    const next = EnumTest.nextEnumI64(first);
71    assertEQ(next, EnumTest.NumTypeI64.TWO)
72}
73
74function test_nextEnum_with_string() {
75    const first = EnumTest.EnumString.ONE;
76    const next = EnumTest.nextEnumString(first);
77    assertEQ(next, EnumTest.EnumString.TWO)
78}
79
80function test_nextEnum_with_const_i8() {
81    const first = EnumTest.FLAG_I8_TWO;
82    assertEQ(first, 0 as byte)
83}
84
85function test_nextEnum_with_const_i16() {
86    const first = EnumTest.FLAG_I16_TWO;
87    assertEQ(first, 0 as short)
88}
89
90function test_nextEnum_with_const_i32() {
91    const first = EnumTest.FLAG_I32_TWO;
92    assertEQ(first, 0)
93}
94
95function test_nextEnum_with_const_i64() {
96    const first = EnumTest.FLAG_I64_TWO;
97    assertEQ(first, 0 as long)
98}
99
100function test_nextEnum_with_const_f32() {
101    const first = EnumTest.FLAG_F32_A;
102    assertEQ(first, 10.0 as float)
103}
104
105function test_nextEnum_with_const_f64() {
106    const first = EnumTest.FLAG_F64_A;
107    assertEQ(first, 1.0 as double)
108}
109
110function test_nextEnum_with_const_string() {
111    const first = EnumTest.FLAG_STRING_ONE;
112    assertEQ(first, "hello")
113}
114
115function test_fromValueToEnum() {
116    const first = EnumTest.fromValueToEnum("Red");
117    assertEQ(first, EnumTest.Color.RED);
118    const second = EnumTest.fromValueToEnum("Green");
119    assertEQ(second, EnumTest.Color.GREEN);
120    const third = EnumTest.fromValueToEnum("Blue");
121    assertEQ(third, EnumTest.Color.BLUE);
122    let errCode: number = 0;
123    try {
124        EnumTest.fromValueToEnum("Yellow");
125    } catch (e: BusinessError) {
126        errCode = e.code;
127    }
128    assertEQ(errCode, 1);
129}
130
131function test_fromValueToEnumWeekday() {
132    const first = EnumTest.fromValueToEnumWeekday(0);
133    assertEQ(first, EnumTest.Weekday.SUNDAY);
134    let errCode: number = 0;
135    try {
136        EnumTest.fromValueToEnumWeekday(8);
137    } catch (e: BusinessError) {
138        errCode = e.code;
139    }
140    assertEQ(errCode, 1);
141}
142
143function main() {
144    console.log("##############start#############");
145    const suite = new ArkTestsuite("EnumTest Tests");
146
147    suite.addTest("test nextEnum with color", test_nextEnum_with_color);
148
149    suite.addTest("test nextEnum with i8", test_nextEnum_with_i8);
150    suite.addTest("test nextEnum with i16", test_nextEnum_with_i16);
151    suite.addTest("test nextEnum with i32", test_nextEnum_with_i32);
152    suite.addTest("test nextEnum with i64", test_nextEnum_with_i64);
153    suite.addTest("test nextEnum with String", test_nextEnum_with_string);
154
155    suite.addTest("test nextEnum with const i8", test_nextEnum_with_const_i8);
156    suite.addTest("test nextEnum with const i16", test_nextEnum_with_const_i16);
157    suite.addTest("test nextEnum with const i32", test_nextEnum_with_const_i32);
158    suite.addTest("test nextEnum with const i64", test_nextEnum_with_const_i64);
159    suite.addTest("test nextEnum with const f32", test_nextEnum_with_const_f32);
160    suite.addTest("test nextEnum with const f64", test_nextEnum_with_const_f64);
161    suite.addTest(
162        "test nextEnum with const string", test_nextEnum_with_const_string);
163
164    suite.addTest("test getValueOfEnum", test_getValueOfEnum);
165    suite.addTest("test getValueOfEnumWeekday", test_getValueOfEnumWeekday);
166
167    suite.addTest("test nextEnum", test_nextEnum);
168    suite.addTest("test nextEnumWeekday", test_nextEnumWeekday);
169
170    suite.addTest("test fromValueToEnum", test_fromValueToEnum);
171    suite.addTest("test fromValueToEnumWeekday", test_fromValueToEnumWeekday);
172
173    exit(suite.run());
174}
175