@@ -544,5 +544,85 @@ def are_same7():
544544 Assert . IsFalse ( module . InvokeMethod ( "are_same6" ) . As < bool > ( ) ) ;
545545 Assert . IsFalse ( module . InvokeMethod ( "are_same7" ) . As < bool > ( ) ) ;
546546 }
547+
548+ private PyModule GetCSharpObjectsComparisonTestModule ( string @operator )
549+ {
550+ return PyModule . FromString ( "GetCSharpObjectsComparisonTestModule" , $@ "
551+ from clr import AddReference
552+ AddReference(""Python.EmbeddingTest"")
553+
554+ from Python.EmbeddingTest import *
555+
556+ enum_value = { nameof ( EnumTests ) } .{ nameof ( VerticalDirection ) } .{ VerticalDirection . Up }
557+
558+ def compare_with_none1():
559+ return enum_value { @operator } None
560+
561+ def compare_with_none2():
562+ return None { @operator } enum_value
563+
564+ def compare_with_csharp_object1(csharp_object):
565+ return enum_value { @operator } csharp_object
566+
567+ def compare_with_csharp_object2(csharp_object):
568+ return csharp_object { @operator } enum_value
569+ " ) ;
570+ }
571+
572+ [ TestCase ( "==" , false ) ]
573+ [ TestCase ( "!=" , true ) ]
574+ public void EqualityComparisonWithNull ( string @operator , bool expectedResult )
575+ {
576+ using var _ = Py . GIL ( ) ;
577+ using var module = GetCSharpObjectsComparisonTestModule ( @operator ) ;
578+
579+ Assert . AreEqual ( expectedResult , module . InvokeMethod ( "compare_with_none1" ) . As < bool > ( ) ) ;
580+ Assert . AreEqual ( expectedResult , module . InvokeMethod ( "compare_with_none2" ) . As < bool > ( ) ) ;
581+
582+ using var pyNull = ( ( TestClass ) null ) . ToPython ( ) ;
583+ Assert . AreEqual ( expectedResult , module . InvokeMethod ( "compare_with_csharp_object1" , pyNull ) . As < bool > ( ) ) ;
584+ Assert . AreEqual ( expectedResult , module . InvokeMethod ( "compare_with_csharp_object2" , pyNull ) . As < bool > ( ) ) ;
585+ }
586+
587+ [ TestCase ( "==" , false ) ]
588+ [ TestCase ( "!=" , true ) ]
589+ public void ComparisonOperatorsWithNonEnumObjectsThrows ( string @operator , bool expectedResult )
590+ {
591+ using var _ = Py . GIL ( ) ;
592+ using var module = GetCSharpObjectsComparisonTestModule ( @operator ) ;
593+
594+ using var pyCSharpObject = new TestClass ( ) . ToPython ( ) ;
595+ Assert . AreEqual ( expectedResult , module . InvokeMethod ( "compare_with_csharp_object1" , pyCSharpObject ) . As < bool > ( ) ) ;
596+ Assert . AreEqual ( expectedResult , module . InvokeMethod ( "compare_with_csharp_object2" , pyCSharpObject ) . As < bool > ( ) ) ;
597+ }
598+
599+ [ Test ]
600+ public void ThrowsOnObjectComparisonOperators ( [ Values ( "<" , "<=" , ">" , ">=" ) ] string @operator )
601+ {
602+ using var _ = Py . GIL ( ) ;
603+ using var module = GetCSharpObjectsComparisonTestModule ( @operator ) ;
604+
605+ using var pyCSharpObject = new TestClass ( ) . ToPython ( ) ;
606+ Assert . Throws < PythonException > ( ( ) => module . InvokeMethod ( "compare_with_csharp_object1" , pyCSharpObject ) ) ;
607+ Assert . Throws < PythonException > ( ( ) => module . InvokeMethod ( "compare_with_csharp_object2" , pyCSharpObject ) ) ;
608+ }
609+
610+ [ Test ]
611+ public void ThrowsOnNullComparisonOperators ( [ Values ( "<" , "<=" , ">" , ">=" ) ] string @operator )
612+ {
613+ using var _ = Py . GIL ( ) ;
614+ using var module = GetCSharpObjectsComparisonTestModule ( @operator ) ;
615+
616+ Assert . Throws < PythonException > ( ( ) => module . InvokeMethod ( "compare_with_none1" ) . As < bool > ( ) ) ;
617+ Assert . Throws < PythonException > ( ( ) => module . InvokeMethod ( "compare_with_none2" ) . As < bool > ( ) ) ;
618+
619+ using var pyNull = ( ( TestClass ) null ) . ToPython ( ) ;
620+ Assert . Throws < PythonException > ( ( ) => module . InvokeMethod ( "compare_with_csharp_object1" , pyNull ) ) ;
621+ Assert . Throws < PythonException > ( ( ) => module . InvokeMethod ( "compare_with_csharp_object2" , pyNull ) ) ;
622+ }
623+
624+ public class TestClass
625+ {
626+ }
547627 }
548628}
0 commit comments