@@ -927,76 +927,6 @@ def SetValue(self):
927927 }
928928 }
929929
930- private static TestCaseData [ ] DynamicPropertiesGetterTestCases ( ) => new [ ]
931- {
932- new TestCaseData ( true ) ,
933- new TestCaseData ( 10 ) ,
934- new TestCaseData ( 10.1 ) ,
935- new TestCaseData ( 10.2m ) ,
936- new TestCaseData ( "Some string" ) ,
937- new TestCaseData ( new DateTime ( 2023 , 6 , 22 ) ) ,
938- new TestCaseData ( new List < int > { 1 , 2 , 3 , 4 , 5 } ) ,
939- new TestCaseData ( new Dictionary < string , int > { { "first" , 1 } , { "second" , 2 } , { "third" , 3 } } ) ,
940- new TestCaseData ( new Fixture ( ) ) ,
941- } ;
942-
943- [ TestCaseSource ( nameof ( DynamicPropertiesGetterTestCases ) ) ]
944- public void TestGetPublicDynamicObjectPropertyWorks ( object property )
945- {
946- dynamic model = PyModule . FromString ( "module" , @"
947- from clr import AddReference
948- AddReference(""Python.EmbeddingTest"")
949- AddReference(""System"")
950-
951- from Python.EmbeddingTest import *
952-
953- class TestGetPublicDynamicObjectPropertyWorks:
954- def GetValue(self, fixture):
955- return fixture.SomeProperty
956- " ) . GetAttr ( "TestGetPublicDynamicObjectPropertyWorks" ) . Invoke ( ) ;
957-
958- dynamic fixture = new DynamicFixture ( ) ;
959- fixture . SomeProperty = property ;
960-
961- using ( Py . GIL ( ) )
962- {
963- Assert . AreEqual ( property , ( model . GetValue ( fixture ) as PyObject ) . AsManagedObject ( property . GetType ( ) ) ) ;
964- }
965- }
966-
967- [ Test ]
968- public void TestGetNonExistingPublicDynamicObjectPropertyThrows ( )
969- {
970- dynamic model = PyModule . FromString ( "module" , @"
971- from clr import AddReference
972- AddReference(""Python.EmbeddingTest"")
973- AddReference(""System"")
974-
975- from Python.EmbeddingTest import *
976-
977- class TestGetNonExistingPublicDynamicObjectPropertyThrows:
978- def GetValue(self, fixture):
979- try:
980- prop = fixture.AnotherProperty
981- except AttributeError as e:
982- return e
983-
984- return None
985- " ) . GetAttr ( "TestGetNonExistingPublicDynamicObjectPropertyThrows" ) . Invoke ( ) ;
986-
987- dynamic fixture = new DynamicFixture ( ) ;
988- fixture . SomeProperty = "Some property" ;
989-
990- using ( Py . GIL ( ) )
991- {
992- var result = model . GetValue ( fixture ) as PyObject ;
993- Assert . IsFalse ( result . IsNone ( ) ) ;
994- Assert . AreEqual ( result . PyType , Exceptions . AttributeError ) ;
995- Assert . AreEqual ( "'Python.EmbeddingTest.TestPropertyAccess+DynamicFixture' object has no attribute 'AnotherProperty'" ,
996- result . ToString ( ) ) ;
997- }
998- }
999-
1000930 public class DynamicFixture : DynamicObject
1001931 {
1002932 private Dictionary < string , object > _properties = new Dictionary < string , object > ( ) ;
@@ -1026,6 +956,10 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
1026956 return false ;
1027957 }
1028958 }
959+
960+ public Dictionary < string , object > Properties { get { return _properties ; } }
961+
962+ public string NonDynamicProperty { get ; set ; }
1029963 }
1030964
1031965 public class TestPerson : IComparable , IComparable < TestPerson >
@@ -1064,6 +998,76 @@ public bool Equals(TestPerson other)
1064998 }
1065999 }
10661000
1001+ private static TestCaseData [ ] DynamicPropertiesGetterTestCases ( ) => new [ ]
1002+ {
1003+ new TestCaseData ( true ) ,
1004+ new TestCaseData ( 10 ) ,
1005+ new TestCaseData ( 10.1 ) ,
1006+ new TestCaseData ( 10.2m ) ,
1007+ new TestCaseData ( "Some string" ) ,
1008+ new TestCaseData ( new DateTime ( 2023 , 6 , 22 ) ) ,
1009+ new TestCaseData ( new List < int > { 1 , 2 , 3 , 4 , 5 } ) ,
1010+ new TestCaseData ( new Dictionary < string , int > { { "first" , 1 } , { "second" , 2 } , { "third" , 3 } } ) ,
1011+ new TestCaseData ( new Fixture ( ) ) ,
1012+ } ;
1013+
1014+ [ TestCaseSource ( nameof ( DynamicPropertiesGetterTestCases ) ) ]
1015+ public void TestGetPublicDynamicObjectPropertyWorks ( object property )
1016+ {
1017+ dynamic model = PyModule . FromString ( "module" , @"
1018+ from clr import AddReference
1019+ AddReference(""Python.EmbeddingTest"")
1020+ AddReference(""System"")
1021+
1022+ from Python.EmbeddingTest import *
1023+
1024+ class TestGetPublicDynamicObjectPropertyWorks:
1025+ def GetValue(self, fixture):
1026+ return fixture.DynamicProperty
1027+ " ) . GetAttr ( "TestGetPublicDynamicObjectPropertyWorks" ) . Invoke ( ) ;
1028+
1029+ dynamic fixture = new DynamicFixture ( ) ;
1030+ fixture . DynamicProperty = property ;
1031+
1032+ using ( Py . GIL ( ) )
1033+ {
1034+ Assert . AreEqual ( property , ( model . GetValue ( fixture ) as PyObject ) . AsManagedObject ( property . GetType ( ) ) ) ;
1035+ }
1036+ }
1037+
1038+ [ Test ]
1039+ public void TestGetNonExistingPublicDynamicObjectPropertyThrows ( )
1040+ {
1041+ dynamic model = PyModule . FromString ( "module" , @"
1042+ from clr import AddReference
1043+ AddReference(""Python.EmbeddingTest"")
1044+ AddReference(""System"")
1045+
1046+ from Python.EmbeddingTest import *
1047+
1048+ class TestGetNonExistingPublicDynamicObjectPropertyThrows:
1049+ def GetValue(self, fixture):
1050+ try:
1051+ prop = fixture.AnotherProperty
1052+ except AttributeError as e:
1053+ return e
1054+
1055+ return None
1056+ " ) . GetAttr ( "TestGetNonExistingPublicDynamicObjectPropertyThrows" ) . Invoke ( ) ;
1057+
1058+ dynamic fixture = new DynamicFixture ( ) ;
1059+ fixture . DynamicProperty = "Dynamic property" ;
1060+
1061+ using ( Py . GIL ( ) )
1062+ {
1063+ var result = model . GetValue ( fixture ) as PyObject ;
1064+ Assert . IsFalse ( result . IsNone ( ) ) ;
1065+ Assert . AreEqual ( result . PyType , Exceptions . AttributeError ) ;
1066+ Assert . AreEqual ( "'Python.EmbeddingTest.TestPropertyAccess+DynamicFixture' object has no attribute 'AnotherProperty'" ,
1067+ result . ToString ( ) ) ;
1068+ }
1069+ }
1070+
10671071 private static TestCaseData [ ] DynamicPropertiesSetterTestCases ( ) => new [ ]
10681072 {
10691073 new TestCaseData ( "True" , null ) ,
@@ -1093,7 +1097,7 @@ from Python.EmbeddingTest import *
10931097
10941098class TestGetPublicDynamicObjectPropertyWorks:
10951099 def SetValue(self, fixture):
1096- fixture.SomeProperty = value
1100+ fixture.DynamicProperty = value
10971101
10981102 def GetPythonValue(self):
10991103 return value
@@ -1107,7 +1111,36 @@ def GetPythonValue(self):
11071111 var expectedAsPyObject = model . GetPythonValue ( ) as PyObject ;
11081112 var expected = expectedType != null ? expectedAsPyObject . AsManagedObject ( expectedType ) : expectedAsPyObject ;
11091113
1110- Assert . AreEqual ( expected , fixture . SomeProperty ) ;
1114+ Assert . AreEqual ( expected , fixture . DynamicProperty ) ;
1115+ }
1116+ }
1117+
1118+ [ Test ]
1119+ public void TestSetPublicNonDynamicObjectPropertyToActualPropertyWorks ( )
1120+ {
1121+ var expected = "Non Dynamic Property" ;
1122+ dynamic model = PyModule . FromString ( "module" , $@ "
1123+ from clr import AddReference
1124+ AddReference(""Python.EmbeddingTest"")
1125+ AddReference(""System"")
1126+
1127+ from datetime import datetime
1128+ import System
1129+ from Python.EmbeddingTest import *
1130+
1131+ class TestGetPublicDynamicObjectPropertyWorks:
1132+ def SetValue(self, fixture):
1133+ fixture.NonDynamicProperty = ""{ expected } ""
1134+ " ) . GetAttr ( "TestGetPublicDynamicObjectPropertyWorks" ) . Invoke ( ) ;
1135+
1136+ var fixture = new DynamicFixture ( ) ;
1137+
1138+ using ( Py . GIL ( ) )
1139+ {
1140+ model . SetValue ( fixture ) ;
1141+ Assert . AreEqual ( expected , fixture . NonDynamicProperty ) ;
1142+ Assert . AreEqual ( expected , ( ( dynamic ) fixture ) . NonDynamicProperty ) ;
1143+ Assert . IsFalse ( fixture . Properties . ContainsKey ( nameof ( fixture . NonDynamicProperty ) ) ) ;
11111144 }
11121145 }
11131146
0 commit comments