@@ -38,6 +38,16 @@ public enum HorizontalDirection
3838 Right = 2 ,
3939 }
4040
41+ [ Flags ]
42+ public enum FileAccessType
43+ {
44+ None = 0 ,
45+ Read = 1 ,
46+ Write = 2 ,
47+ ReadWrite = Read | Write ,
48+ Delete = 4 ,
49+ }
50+
4151 [ Test ]
4252 public void CSharpEnumsBehaveAsEnumsInPython ( )
4353 {
@@ -337,6 +347,59 @@ def operation():
337347 Assert . AreEqual ( expectedResult , module . InvokeMethod ( "operation" ) . As < bool > ( ) ) ;
338348 }
339349
350+ [ TestCase ( nameof ( VerticalDirection ) + ".DOWN" , "DOWN" ) ]
351+ [ TestCase ( nameof ( VerticalDirection ) + ".FLAT" , "FLAT" ) ]
352+ [ TestCase ( nameof ( VerticalDirection ) + ".UP" , "UP" ) ]
353+ [ TestCase ( nameof ( VerticalDirection ) + ".Down" , "DOWN" ) ]
354+ [ TestCase ( nameof ( FileAccessType ) + ".NONE" , "NONE" ) ]
355+ [ TestCase ( nameof ( FileAccessType ) + ".READ" , "READ" ) ]
356+ [ TestCase ( nameof ( FileAccessType ) + ".READ_WRITE" , "READ_WRITE" ) ]
357+ [ TestCase ( nameof ( FileAccessType ) + ".ReadWrite" , "READ_WRITE" ) ]
358+ [ TestCase ( nameof ( FileAccessType ) + ".READ | " + nameof ( EnumTests ) + "." + nameof ( FileAccessType ) + ".DELETE" , "READ, DELETE" ) ]
359+ public void StrReturnsSnakeCasedMemberName ( string valueExpression , string expectedStr )
360+ {
361+ using var _ = Py . GIL ( ) ;
362+ using var module = PyModule . FromString ( "StrReturnsSnakeCasedMemberName" , $@ "
363+ from clr import AddReference
364+ AddReference(""Python.EmbeddingTest"")
365+
366+ from Python.EmbeddingTest import *
367+
368+ enum_value = { nameof ( EnumTests ) } .{ valueExpression }
369+
370+ def get_str():
371+ return str(enum_value)
372+
373+ def get_formatted():
374+ return f'{{enum_value}}'
375+ " ) ;
376+
377+ Assert . AreEqual ( expectedStr , module . InvokeMethod ( "get_str" ) . As < string > ( ) ) ;
378+ Assert . AreEqual ( expectedStr , module . InvokeMethod ( "get_formatted" ) . As < string > ( ) ) ;
379+ }
380+
381+ [ Test ]
382+ public void StrReturnsNumericRepresentationForUndefinedValues ( )
383+ {
384+ using var _ = Py . GIL ( ) ;
385+ using var module = PyModule . FromString ( "StrReturnsNumericRepresentationForUndefinedValues" , $@ "
386+ from clr import AddReference
387+ AddReference(""Python.EmbeddingTest"")
388+
389+ from System import Enum
390+ from Python.EmbeddingTest import *
391+
392+ def get_str(int_value):
393+ return str(Enum.ToObject({ nameof ( EnumTests ) } .{ nameof ( VerticalDirection ) } , int_value))
394+ " ) ;
395+
396+ using var pyOne = 1 . ToPython ( ) ;
397+ Assert . AreEqual ( "1" , module . InvokeMethod ( "get_str" , pyOne ) . As < string > ( ) ) ;
398+
399+ using var pyMinusOne = ( - 1 ) . ToPython ( ) ;
400+ Assert . AreEqual ( "-1" , module . InvokeMethod ( "get_str" , pyMinusOne ) . As < string > ( ) ) ;
401+ }
402+
340403 [ TestCase ( "==" , VerticalDirection . Down , "Down" , true ) ]
341404 [ TestCase ( "==" , VerticalDirection . Down , "Flat" , false ) ]
342405 [ TestCase ( "==" , VerticalDirection . Down , "Up" , false ) ]
@@ -355,6 +418,13 @@ def operation():
355418 [ TestCase ( "!=" , VerticalDirection . Up , "Down" , true ) ]
356419 [ TestCase ( "!=" , VerticalDirection . Up , "Flat" , true ) ]
357420 [ TestCase ( "!=" , VerticalDirection . Up , "Up" , false ) ]
421+ // The Python-facing snake-cased names are accepted too, consistently with str()
422+ [ TestCase ( "==" , VerticalDirection . Down , "DOWN" , true ) ]
423+ [ TestCase ( "==" , VerticalDirection . Flat , "FLAT" , true ) ]
424+ [ TestCase ( "==" , VerticalDirection . Up , "UP" , true ) ]
425+ [ TestCase ( "==" , VerticalDirection . Down , "UP" , false ) ]
426+ [ TestCase ( "!=" , VerticalDirection . Down , "DOWN" , false ) ]
427+ [ TestCase ( "!=" , VerticalDirection . Down , "UP" , true ) ]
358428 public void EnumComparisonOperatorsWorkWithString ( string @operator , VerticalDirection operand1 , string operand2 , bool expectedResult )
359429 {
360430 using var _ = Py . GIL ( ) ;
@@ -375,6 +445,26 @@ def operation2():
375445 Assert . AreEqual ( expectedResult , module . InvokeMethod ( "operation2" ) . As < bool > ( ) ) ;
376446 }
377447
448+ [ TestCase ( "ReadWrite" , true ) ]
449+ [ TestCase ( "READ_WRITE" , true ) ]
450+ [ TestCase ( "READWRITE" , false ) ]
451+ [ TestCase ( "read_write" , false ) ]
452+ public void MultiWordEnumMembersCompareWithBothCSharpAndSnakeCasedNames ( string operand , bool expectedResult )
453+ {
454+ using var _ = Py . GIL ( ) ;
455+ using var module = PyModule . FromString ( "MultiWordEnumMembersCompareWithBothCSharpAndSnakeCasedNames" , $@ "
456+ from clr import AddReference
457+ AddReference(""Python.EmbeddingTest"")
458+
459+ from Python.EmbeddingTest import *
460+
461+ def operation():
462+ return { nameof ( EnumTests ) } .{ nameof ( FileAccessType ) } .READ_WRITE == ""{ operand } ""
463+ " ) ;
464+
465+ Assert . AreEqual ( expectedResult , module . InvokeMethod ( "operation" ) . As < bool > ( ) ) ;
466+ }
467+
378468 public static IEnumerable < TestCaseData > OtherEnumsComparisonOperatorsTestCases
379469 {
380470 get
0 commit comments