@@ -14,12 +14,11 @@ pub enum MyEnum {
1414
1515#[ test]
1616fn test_enum_class_attr ( ) {
17- let gil = Python :: acquire_gil ( ) ;
18- let py = gil. python ( ) ;
19- let my_enum = py. get_type :: < MyEnum > ( ) ;
20- py_assert ! ( py, my_enum, "getattr(my_enum, 'Variant', None) is not None" ) ;
21- py_assert ! ( py, my_enum, "getattr(my_enum, 'foobar', None) is None" ) ;
22- py_run ! ( py, my_enum, "my_enum.Variant = None" ) ;
17+ Python :: with_gil ( |py| {
18+ let my_enum = py. get_type :: < MyEnum > ( ) ;
19+ let var = Py :: new ( py, MyEnum :: Variant ) . unwrap ( ) ;
20+ py_assert ! ( py, my_enum var, "my_enum.Variant == var" ) ;
21+ } )
2322}
2423
2524#[ pyfunction]
@@ -28,7 +27,6 @@ fn return_enum() -> MyEnum {
2827}
2928
3029#[ test]
31- #[ ignore] // need to implement __eq__
3230fn test_return_enum ( ) {
3331 let gil = Python :: acquire_gil ( ) ;
3432 let py = gil. python ( ) ;
@@ -44,14 +42,24 @@ fn enum_arg(e: MyEnum) {
4442}
4543
4644#[ test]
47- #[ ignore] // need to implement __eq__
4845fn test_enum_arg ( ) {
49- let gil = Python :: acquire_gil ( ) ;
50- let py = gil. python ( ) ;
51- let f = wrap_pyfunction ! ( enum_arg) ( py) . unwrap ( ) ;
52- let mynum = py. get_type :: < MyEnum > ( ) ;
46+ Python :: with_gil ( |py| {
47+ let f = wrap_pyfunction ! ( enum_arg) ( py) . unwrap ( ) ;
48+ let mynum = py. get_type :: < MyEnum > ( ) ;
49+
50+ py_run ! ( py, f mynum, "f(mynum.OtherVariant)" )
51+ } )
52+ }
5353
54- py_run ! ( py, f mynum, "f(mynum.Variant)" )
54+ #[ test]
55+ fn test_enum_eq ( ) {
56+ Python :: with_gil ( |py| {
57+ let var1 = Py :: new ( py, MyEnum :: Variant ) . unwrap ( ) ;
58+ let var2 = Py :: new ( py, MyEnum :: Variant ) . unwrap ( ) ;
59+ let other_var = Py :: new ( py, MyEnum :: OtherVariant ) . unwrap ( ) ;
60+ py_assert ! ( py, var1 var2, "var1 == var2" ) ;
61+ py_assert ! ( py, var1 other_var, "var1 != other_var" ) ;
62+ } )
5563}
5664
5765#[ test]
@@ -85,6 +93,47 @@ fn test_custom_discriminant() {
8593 } )
8694}
8795
96+ #[ test]
97+ fn test_enum_to_int ( ) {
98+ Python :: with_gil ( |py| {
99+ let one = Py :: new ( py, CustomDiscriminant :: One ) . unwrap ( ) ;
100+ py_assert ! ( py, one, "int(one) == 1" ) ;
101+ let v = Py :: new ( py, MyEnum :: Variant ) . unwrap ( ) ;
102+ let v_value = MyEnum :: Variant as isize ;
103+ py_run ! ( py, v v_value, "int(v) == v_value" ) ;
104+ } )
105+ }
106+
107+ #[ test]
108+ fn test_enum_compare_int ( ) {
109+ Python :: with_gil ( |py| {
110+ let one = Py :: new ( py, CustomDiscriminant :: One ) . unwrap ( ) ;
111+ py_run ! (
112+ py,
113+ one,
114+ r#"
115+ assert one == 1
116+ assert 1 == one
117+ assert one != 2
118+ "#
119+ )
120+ } )
121+ }
122+
123+ #[ pyclass]
124+ #[ repr( u8 ) ]
125+ enum SmallEnum {
126+ V = 1 ,
127+ }
128+
129+ #[ test]
130+ fn test_enum_compare_int_no_throw_when_overflow ( ) {
131+ Python :: with_gil ( |py| {
132+ let v = Py :: new ( py, SmallEnum :: V ) . unwrap ( ) ;
133+ py_assert ! ( py, v, "v != 1<<30" )
134+ } )
135+ }
136+
88137#[ pyclass]
89138#[ repr( usize ) ]
90139#[ allow( clippy:: enum_clike_unportable_variant) ]
0 commit comments