@@ -42,7 +42,7 @@ public enum HorizontalDirection
4242 public void CSharpEnumsBehaveAsEnumsInPython ( )
4343 {
4444 using var _ = Py . GIL ( ) ;
45- var module = PyModule . FromString ( "CSharpEnumsBehaveAsEnumsInPython" , $@ "
45+ using var module = PyModule . FromString ( "CSharpEnumsBehaveAsEnumsInPython" , $@ "
4646from clr import AddReference
4747AddReference(""Python.EmbeddingTest"")
4848
@@ -103,7 +103,7 @@ def operation2():
103103 public void ArithmeticOperatorsWorkWithoutExplicitCast ( string @operator , VerticalDirection operand1 , double operand2 , double expectedResult , double invertedOperationExpectedResult )
104104 {
105105 using var _ = Py . GIL ( ) ;
106- var module = GetTestOperatorsModule ( @operator , operand1 , operand2 ) ;
106+ using var module = GetTestOperatorsModule ( @operator , operand1 , operand2 ) ;
107107
108108 Assert . AreEqual ( expectedResult , module . InvokeMethod ( "operation1" ) . As < double > ( ) ) ;
109109
@@ -178,7 +178,7 @@ public void ArithmeticOperatorsWorkWithoutExplicitCast(string @operator, Vertica
178178 public void IntComparisonOperatorsWorkWithoutExplicitCast ( string @operator , VerticalDirection operand1 , int operand2 , bool expectedResult )
179179 {
180180 using var _ = Py . GIL ( ) ;
181- var module = GetTestOperatorsModule ( @operator , operand1 , operand2 ) ;
181+ using var module = GetTestOperatorsModule ( @operator , operand1 , operand2 ) ;
182182
183183 Assert . AreEqual ( expectedResult , module . InvokeMethod ( "operation1" ) . As < bool > ( ) ) ;
184184
@@ -289,7 +289,7 @@ public void IntComparisonOperatorsWorkWithoutExplicitCast(string @operator, Vert
289289 public void FloatComparisonOperatorsWorkWithoutExplicitCast ( string @operator , VerticalDirection operand1 , double operand2 , bool expectedResult )
290290 {
291291 using var _ = Py . GIL ( ) ;
292- var module = GetTestOperatorsModule ( @operator , operand1 , operand2 ) ;
292+ using var module = GetTestOperatorsModule ( @operator , operand1 , operand2 ) ;
293293
294294 Assert . AreEqual ( expectedResult , module . InvokeMethod ( "operation1" ) . As < bool > ( ) ) ;
295295
@@ -324,7 +324,7 @@ public static IEnumerable<TestCaseData> SameEnumTypeComparisonOperatorsTestCases
324324 public void SameEnumTypeComparisonOperatorsWorkWithoutExplicitCast ( string @operator , VerticalDirection operand1 , VerticalDirection operand2 , bool expectedResult )
325325 {
326326 using var _ = Py . GIL ( ) ;
327- var module = PyModule . FromString ( "SameEnumTypeComparisonOperatorsWorkWithoutExplicitCast" , $@ "
327+ using var module = PyModule . FromString ( "SameEnumTypeComparisonOperatorsWorkWithoutExplicitCast" , $@ "
328328from clr import AddReference
329329AddReference(""Python.EmbeddingTest"")
330330
@@ -358,7 +358,7 @@ def operation():
358358 public void EnumComparisonOperatorsWorkWithString ( string @operator , VerticalDirection operand1 , string operand2 , bool expectedResult )
359359 {
360360 using var _ = Py . GIL ( ) ;
361- var module = PyModule . FromString ( "EnumComparisonOperatorsWorkWithString" , $@ "
361+ using var module = PyModule . FromString ( "EnumComparisonOperatorsWorkWithString" , $@ "
362362from clr import AddReference
363363AddReference(""Python.EmbeddingTest"")
364364
@@ -403,7 +403,7 @@ public static IEnumerable<TestCaseData> OtherEnumsComparisonOperatorsTestCases
403403 public void OtherEnumsComparisonOperatorsWorkWithoutExplicitCast ( string @operator , VerticalDirection operand1 , HorizontalDirection operand2 , bool expectedResult , bool invertedOperationExpectedResult )
404404 {
405405 using var _ = Py . GIL ( ) ;
406- var module = PyModule . FromString ( "OtherEnumsComparisonOperatorsWorkWithoutExplicitCast" , $@ "
406+ using var module = PyModule . FromString ( "OtherEnumsComparisonOperatorsWorkWithoutExplicitCast" , $@ "
407407from clr import AddReference
408408AddReference(""Python.EmbeddingTest"")
409409
@@ -444,7 +444,7 @@ public void CSharpEnumsAreSingletonsInPthonAndIdentityComparisonWorks(VerticalDi
444444 var enumValue2Str = $ "{ nameof ( EnumTests ) } .{ nameof ( VerticalDirection ) } .{ enumValue2 } ";
445445
446446 using var _ = Py . GIL ( ) ;
447- var module = PyModule . FromString ( "CSharpEnumsAreSingletonsInPthonAndIdentityComparisonWorks" , $@ "
447+ using var module = PyModule . FromString ( "CSharpEnumsAreSingletonsInPthonAndIdentityComparisonWorks" , $@ "
448448from clr import AddReference
449449AddReference(""Python.EmbeddingTest"")
450450
@@ -505,7 +505,7 @@ public void IdentityComparisonBetweenDifferentEnumTypesIsNeverTrue(
505505 var enumValue2Str = $ "{ nameof ( EnumTests ) } .{ nameof ( HorizontalDirection ) } .{ enumValue2 } ";
506506
507507 using var _ = Py . GIL ( ) ;
508- var module = PyModule . FromString ( "IdentityComparisonBetweenDifferentEnumTypesIsNeverTrue" , $@ "
508+ using var module = PyModule . FromString ( "IdentityComparisonBetweenDifferentEnumTypesIsNeverTrue" , $@ "
509509from clr import AddReference
510510AddReference(""Python.EmbeddingTest"")
511511
@@ -544,5 +544,89 @@ 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+ [ Test ]
588+ public void SortingComparisonWithNullThrows ( [ Values ( "<" , "<=" , ">" , ">=" ) ] string @operator )
589+ {
590+ using var _ = Py . GIL ( ) ;
591+ using var module = GetCSharpObjectsComparisonTestModule ( @operator ) ;
592+
593+ using var pyNull = ( ( TestClass ) null ) . ToPython ( ) ;
594+
595+ var exception = Assert . Throws < PythonException > ( ( ) => module . InvokeMethod ( "compare_with_csharp_object1" , pyNull ) ) ;
596+ Assert . IsTrue ( exception . Message . Contains ( "Cannot compare" ) ) ;
597+ Assert . IsTrue ( exception . Message . Contains ( "with null" ) ) ;
598+ }
599+
600+ private static IEnumerable < TestCaseData > ComparisonWithNonEnumObjectsTestCases
601+ {
602+ get
603+ {
604+ foreach ( var op in new [ ] { "==" , "!=" } )
605+ {
606+ yield return new TestCaseData ( op , new [ ] { "No method matched to compare" } ) ;
607+ }
608+
609+ foreach ( var op in new [ ] { "<" , "<=" , ">" , ">=" } )
610+ {
611+ yield return new TestCaseData ( op , new [ ] { "Cannot compare" , "with null" } ) ;
612+ }
613+ }
614+ }
615+
616+ [ Test ]
617+ public void ComparisonOperatorsWithNonEnumObjectsThrows ( [ Values ( "==" , "!=" , "<" , "<=" , ">" , ">=" ) ] string @operator )
618+ {
619+ using var _ = Py . GIL ( ) ;
620+ using var module = GetCSharpObjectsComparisonTestModule ( @operator ) ;
621+
622+ using var pyCSharpObject = new TestClass ( ) . ToPython ( ) ;
623+
624+ var exception = Assert . Throws < PythonException > ( ( ) => module . InvokeMethod ( "compare_with_csharp_object1" , pyCSharpObject ) ) ;
625+ Assert . IsTrue ( exception . Message . Contains ( "No method matched" ) , $ "Expected exception message to contain 'No method matched' but got: { exception . Message } ") ;
626+ }
627+
628+ public class TestClass
629+ {
630+ }
547631 }
548632}
0 commit comments