@@ -1003,6 +1003,86 @@ def call(instance):
1003
1003
}
1004
1004
1005
1005
#endregion
1006
+
1007
+ public enum TestEnum
1008
+ {
1009
+ FirstEnumValue ,
1010
+ SecondEnumValue ,
1011
+ ThirdEnumValue
1012
+ }
1013
+
1014
+ [ Test ]
1015
+ public void EnumPythonOperationsCanBePerformedOnManagedEnum ( )
1016
+ {
1017
+ using ( Py . GIL ( ) )
1018
+ {
1019
+ var module = PyModule . FromString ( "EnumPythonOperationsCanBePerformedOnManagedEnum" , $@ "
1020
+ from clr import AddReference
1021
+ AddReference(""Python.EmbeddingTest"")
1022
+
1023
+ from Python.EmbeddingTest import *
1024
+
1025
+ def get_enum_values():
1026
+ return [x for x in ClassManagerTests.TestEnum]
1027
+
1028
+ def count_enum_values():
1029
+ return len(ClassManagerTests.TestEnum)
1030
+
1031
+ def is_enum_value_defined(value):
1032
+ return value in ClassManagerTests.TestEnum
1033
+ " ) ;
1034
+
1035
+ using var pyEnumValues = module . InvokeMethod ( "get_enum_values" ) ;
1036
+ var enumValues = pyEnumValues . As < List < TestEnum > > ( ) ;
1037
+
1038
+ var expectedEnumValues = Enum . GetValues < TestEnum > ( ) ;
1039
+ CollectionAssert . AreEquivalent ( expectedEnumValues , enumValues ) ;
1040
+
1041
+ using var pyEnumCount = module . InvokeMethod ( "count_enum_values" ) ;
1042
+ var enumCount = pyEnumCount . As < int > ( ) ;
1043
+ Assert . AreEqual ( expectedEnumValues . Length , enumCount ) ;
1044
+
1045
+ var validEnumValues = expectedEnumValues
1046
+ . SelectMany ( x => new object [ ] { x , ( int ) x , Enum . GetName ( x . GetType ( ) , x ) } )
1047
+ . Select ( x => ( x , true ) ) ;
1048
+ var invalidEnumValues = new object [ ] { 5 , "INVALID_ENUM_VALUE" } . Select ( x => ( x , false ) ) ;
1049
+
1050
+ foreach ( var ( enumValue , isValid ) in validEnumValues . Concat ( invalidEnumValues ) )
1051
+ {
1052
+ using var pyEnumValue = enumValue . ToPython ( ) ;
1053
+ using var pyIsDefined = module . InvokeMethod ( "is_enum_value_defined" , pyEnumValue ) ;
1054
+ var isDefined = pyIsDefined . As < bool > ( ) ;
1055
+ Assert . AreEqual ( isValid , isDefined , $ "Failed for { enumValue } ({ enumValue . GetType ( ) } )") ;
1056
+ }
1057
+ }
1058
+ }
1059
+
1060
+ [ Test ]
1061
+ public void EnumInterableOperationsNotSupportedForManagedNonEnumTypes ( )
1062
+ {
1063
+ using ( Py . GIL ( ) )
1064
+ {
1065
+ var module = PyModule . FromString ( "EnumInterableOperationsNotSupportedForManagedNonEnumTypes" , $@ "
1066
+ from clr import AddReference
1067
+ AddReference(""Python.EmbeddingTest"")
1068
+
1069
+ from Python.EmbeddingTest import *
1070
+
1071
+ def get_enum_values():
1072
+ return [x for x in ClassManagerTests]
1073
+
1074
+ def count_enum_values():
1075
+ return len(ClassManagerTests)
1076
+
1077
+ def is_enum_value_defined():
1078
+ return 1 in ClassManagerTests
1079
+ " ) ;
1080
+
1081
+ Assert . Throws < PythonException > ( ( ) => module . InvokeMethod ( "get_enum_values" ) ) ;
1082
+ Assert . Throws < PythonException > ( ( ) => module . InvokeMethod ( "count_enum_values" ) ) ;
1083
+ Assert . Throws < PythonException > ( ( ) => module . InvokeMethod ( "is_enum_value_defined" ) ) ;
1084
+ }
1085
+ }
1006
1086
}
1007
1087
1008
1088
public class NestedTestParent
0 commit comments