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