1/* 2 * Copyright (c) 2023-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 16enum Color { Red, Green, Blue } 17 18function main(): void { 19 let x: int = Color.Red.valueOf(); 20 assertEQ(x, 0) 21 22 let blue = Color.Blue; 23 let str = blue.getName(); 24 assertEQ("Blue", str) 25 str = blue.toString(); 26 assertEQ("2", str) 27 28 29 let values = Color.values(); 30 assertEQ(values.length, 3) 31 assertEQ(values[0], Color.Red) 32 assertEQ(values[1], Color.Green) 33 assertEQ(values[2], Color.Blue) 34 35 let red: Color = Color.Red; 36 37 try { 38 red = Color.getValueOf("Red"); 39 } catch (e) {} 40 41 assertEQ(red, Color.Red) 42 43 try { 44 let yellow: Color = Color.getValueOf("Yellow"); 45 assertTrue(false) 46 } catch (e: Exception) { 47 assertTrue((e as Object).toString().startsWith("No enum constant Color.Yellow")) 48 } catch (e) {} 49 50 let one: int = 1; 51 let green = one as Color; 52 assertEQ(green, Color.Green) 53 54 try { 55 let x = 5 as Color; 56 assertTrue( false) 57 } catch (e: Error) { 58 assertTrue( (e as Object).toString().startsWith("Error: No enum Color with value 5")) 59 } 60 61 assertEQ(2 as Color as int, 2) 62 assertEQ(Color.Blue as int as Color, Color.Blue) 63 assertEQ((Color.Red as int + 1) as Color, (Color.Blue as int - 1) as Color) 64} 65