forked from QuantConnect/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassManagerTests.cs
More file actions
1285 lines (1050 loc) · 53 KB
/
ClassManagerTests.cs
File metadata and controls
1285 lines (1050 loc) · 53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using Python.Runtime;
namespace Python.EmbeddingTest
{
public class ClassManagerTests
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}
[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}
[Test]
public void NestedClassDerivingFromParent()
{
var f = new NestedTestContainer().ToPython();
f.GetAttr(nameof(NestedTestContainer.Bar));
}
#region Snake case naming tests
public enum SnakeCaseEnum
{
EnumValue1,
EnumValue2,
EnumValue3
}
public class SnakeCaseNamesTesClass
{
// Purposely long names to test snake case conversion
public string PublicStringField = "public_string_field";
public const string PublicConstStringField = "public_const_string_field";
public readonly string PublicReadonlyStringField = "public_readonly_string_field";
public static string PublicStaticStringField = "public_static_string_field";
public static readonly string PublicStaticReadonlyStringField = "public_static_readonly_string_field";
public static string SettablePublicStaticStringField = "settable_public_static_string_field";
public string PublicStringProperty { get; set; } = "public_string_property";
public string PublicStringGetOnlyProperty { get; } = "public_string_get_only_property";
public static string PublicStaticStringProperty { get; set; } = "public_static_string_property";
public static string PublicStaticReadonlyStringGetterOnlyProperty { get; } = "public_static_readonly_string_getter_only_property";
public static string PublicStaticReadonlyStringPrivateSetterProperty { get; private set; } = "public_static_readonly_string_private_setter_property";
public static string PublicStaticReadonlyStringProtectedSetterProperty { get; protected set; } = "public_static_readonly_string_protected_setter_property";
public static string PublicStaticReadonlyStringInternalSetterProperty { get; internal set; } = "public_static_readonly_string_internal_setter_property";
public static string PublicStaticReadonlyStringProtectedInternalSetterProperty { get; protected internal set; } = "public_static_readonly_string_protected_internal_setter_property";
public static string PublicStaticReadonlyStringExpressionBodiedProperty => "public_static_readonly_string_expression_bodied_property";
protected string ProtectedStringGetOnlyProperty { get; } = "protected_string_get_only_property";
protected static string ProtectedStaticStringProperty { get; set; } = "protected_static_string_property";
protected static string ProtectedStaticReadonlyStringGetterOnlyProperty { get; } = "protected_static_readonly_string_getter_only_property";
protected static string ProtectedStaticReadonlyStringPrivateSetterProperty { get; private set; } = "protected_static_readonly_string_private_setter_property";
protected static string ProtectedStaticReadonlyStringExpressionBodiedProperty => "protected_static_readonly_string_expression_bodied_property";
public event EventHandler<string> PublicStringEvent;
public static event EventHandler<string> PublicStaticStringEvent;
public SnakeCaseEnum EnumValue = SnakeCaseEnum.EnumValue2;
public void InvokePublicStringEvent(string value)
{
PublicStringEvent?.Invoke(this, value);
}
public static void InvokePublicStaticStringEvent(string value)
{
PublicStaticStringEvent?.Invoke(null, value);
}
public int AddNumbersAndGetHalf(int a, int b)
{
return (a + b) / 2;
}
public static int AddNumbersAndGetHalf_Static(int a, int b)
{
return (a + b) / 2;
}
public string JoinToString(string thisIsAStringParameter,
char thisIsACharParameter,
int thisIsAnIntParameter,
float thisIsAFloatParameter,
double thisIsADoubleParameter,
decimal? thisIsADecimalParameter,
bool thisIsABoolParameter,
DateTime thisIsADateTimeParameter = default)
{
// Join all parameters into a single string separated by "-"
return string.Join("-", thisIsAStringParameter, thisIsACharParameter, thisIsAnIntParameter, thisIsAFloatParameter,
thisIsADoubleParameter, thisIsADecimalParameter ?? 123.456m, thisIsABoolParameter, string.Format("{0:MMddyyyy}", thisIsADateTimeParameter));
}
public static Action StaticReadonlyActionProperty { get; } = () => Throw();
public static Action<int> StaticReadonlyActionWithParamsProperty { get; } = (i) => Throw();
public static Func<int> StaticReadonlyFuncProperty { get; } = () =>
{
Throw();
return 42;
};
public static Func<int, int> StaticReadonlyFuncWithParamsProperty { get; } = (i) =>
{
Throw();
return i * 2;
};
public static Action StaticReadonlyExpressionBodiedActionProperty => () => Throw();
public static Action<int> StaticReadonlyExpressionBodiedActionWithParamsProperty => (i) => Throw();
public static Func<int> StaticReadonlyExpressionBodiedFuncProperty => () =>
{
Throw();
return 42;
};
public static Func<int, int> StaticReadonlyExpressionBodiedFuncWithParamsProperty => (i) =>
{
Throw();
return i * 2;
};
public static readonly Action StaticReadonlyActionField = () => Throw();
public static readonly Action<int> StaticReadonlyActionWithParamsField = (i) => Throw();
public static readonly Func<int> StaticReadonlyFuncField = () =>
{
Throw();
return 42;
};
public static readonly Func<int, int> StaticReadonlyFuncWithParamsField = (i) =>
{
Throw();
return i * 2;
};
public static readonly Action StaticReadonlyExpressionBodiedActionField = () => Throw();
public static readonly Action<int> StaticReadonlyExpressionBodiedActionWithParamsField = (i) => Throw();
public static readonly Func<int> StaticReadonlyExpressionBodiedFuncField = () =>
{
Throw();
return 42;
};
public static readonly Func<int, int> StaticReadonlyExpressionBodiedFuncWithParamsField = (i) =>
{
Throw();
return i * 2;
};
private static void Throw() => throw new Exception("Pepe");
public static string GenericMethodBindingStatic<T>(int arg1, SnakeCaseEnum enumValue)
{
return "GenericMethodBindingStatic";
}
public string GenericMethodBinding<T>(int arg1, SnakeCaseEnum enumValue = SnakeCaseEnum.EnumValue3)
{
return "GenericMethodBinding" + arg1;
}
}
[TestCase("generic_method_binding_static", "GenericMethodBindingStatic")]
[TestCase("generic_method_binding", "GenericMethodBinding1")]
[TestCase("generic_method_binding2", "GenericMethodBinding2")]
[TestCase("generic_method_binding3", "GenericMethodBinding3")]
public void GenericMethodBinding(string targetMethod, string expectedReturn)
{
using (Py.GIL())
{
var module = PyModule.FromString("module", $@"
from clr import AddReference
AddReference(""Python.EmbeddingTest"")
from Python.EmbeddingTest import *
def generic_method_binding_static(value):
return ClassManagerTests.SnakeCaseNamesTesClass.generic_method_binding_static[bool](1, enum_value=ClassManagerTests.SnakeCaseEnum.EnumValue1)
def generic_method_binding(value):
return value.generic_method_binding[bool](1, enum_value=ClassManagerTests.SnakeCaseEnum.EnumValue1)
def generic_method_binding2(value):
return value.generic_method_binding[bool](2, ClassManagerTests.SnakeCaseEnum.EnumValue1)
def generic_method_binding3(value):
return value.generic_method_binding[bool](3)
");
using var obj = new SnakeCaseNamesTesClass().ToPython();
var result = module.InvokeMethod(targetMethod, new[] { obj }).As<string>();
Assert.AreEqual(expectedReturn, result);
}
}
[TestCase("StaticReadonlyActionProperty", "static_readonly_action_property", new object[] { })]
[TestCase("StaticReadonlyActionWithParamsProperty", "static_readonly_action_with_params_property", new object[] { 42 })]
[TestCase("StaticReadonlyFuncProperty", "static_readonly_func_property", new object[] { })]
[TestCase("StaticReadonlyFuncWithParamsProperty", "static_readonly_func_with_params_property", new object[] { 42 })]
[TestCase("StaticReadonlyExpressionBodiedActionProperty", "static_readonly_expression_bodied_action_property", new object[] { })]
[TestCase("StaticReadonlyExpressionBodiedActionWithParamsProperty", "static_readonly_expression_bodied_action_with_params_property", new object[] { 42 })]
[TestCase("StaticReadonlyExpressionBodiedFuncProperty", "static_readonly_expression_bodied_func_property", new object[] { })]
[TestCase("StaticReadonlyExpressionBodiedFuncWithParamsProperty", "static_readonly_expression_bodied_func_with_params_property", new object[] { 42 })]
[TestCase("StaticReadonlyActionField", "static_readonly_action_field", new object[] { })]
[TestCase("StaticReadonlyActionWithParamsField", "static_readonly_action_with_params_field", new object[] { 42 })]
[TestCase("StaticReadonlyFuncField", "static_readonly_func_field", new object[] { })]
[TestCase("StaticReadonlyFuncWithParamsField", "static_readonly_func_with_params_field", new object[] { 42 })]
[TestCase("StaticReadonlyExpressionBodiedActionField", "static_readonly_expression_bodied_action_field", new object[] { })]
[TestCase("StaticReadonlyExpressionBodiedActionWithParamsField", "static_readonly_expression_bodied_action_with_params_field", new object[] { 42 })]
[TestCase("StaticReadonlyExpressionBodiedFuncField", "static_readonly_expression_bodied_func_field", new object[] { })]
[TestCase("StaticReadonlyExpressionBodiedFuncWithParamsField", "static_readonly_expression_bodied_func_with_params_field", new object[] { 42 })]
public void StaticReadonlyCallableFieldsAndPropertiesAreBothUpperAndLowerCased(string propertyName, string snakeCasedName, object[] args)
{
using var obj = new SnakeCaseNamesTesClass().ToPython();
var lowerCasedName = snakeCasedName.ToLowerInvariant();
var upperCasedName = snakeCasedName.ToUpperInvariant();
var memberInfo = typeof(SnakeCaseNamesTesClass).GetMember(propertyName).First();
var callableType = memberInfo switch
{
PropertyInfo propertyInfo => propertyInfo.PropertyType,
FieldInfo fieldInfo => fieldInfo.FieldType,
_ => throw new InvalidOperationException()
};
var property = obj.GetAttr(propertyName).AsManagedObject(callableType);
var lowerCasedProperty = obj.GetAttr(lowerCasedName).AsManagedObject(callableType);
var upperCasedProperty = obj.GetAttr(upperCasedName).AsManagedObject(callableType);
Assert.IsNotNull(property);
Assert.IsNotNull(property as MulticastDelegate);
Assert.AreSame(property, lowerCasedProperty);
Assert.AreSame(property, upperCasedProperty);
var call = () =>
{
try
{
(property as Delegate).DynamicInvoke(args);
}
catch (TargetInvocationException e)
{
throw e.InnerException;
}
};
var exception = Assert.Throws<Exception>(() => call());
Assert.AreEqual("Pepe", exception.Message);
}
[TestCase("PublicStaticReadonlyStringField", "public_static_readonly_string_field")]
[TestCase("PublicStaticReadonlyStringGetterOnlyProperty", "public_static_readonly_string_getter_only_property")]
public void NonCallableStaticReadonlyFieldsAndPropertiesAreOnlyUpperCased(string propertyName, string snakeCasedName)
{
using var obj = new SnakeCaseNamesTesClass().ToPython();
var lowerCasedName = snakeCasedName.ToLowerInvariant();
var upperCasedName = snakeCasedName.ToUpperInvariant();
Assert.IsTrue(obj.HasAttr(propertyName));
Assert.IsTrue(obj.HasAttr(upperCasedName));
Assert.IsFalse(obj.HasAttr(lowerCasedName));
}
[TestCase("AddNumbersAndGetHalf", "add_numbers_and_get_half")]
[TestCase("AddNumbersAndGetHalf_Static", "add_numbers_and_get_half_static")]
public void BindsSnakeCaseClassMethods(string originalMethodName, string snakeCaseMethodName)
{
using var obj = new SnakeCaseNamesTesClass().ToPython();
using var a = 10.ToPython();
using var b = 20.ToPython();
var originalMethodResult = obj.InvokeMethod(originalMethodName, a, b).As<int>();
var snakeCaseMethodResult = obj.InvokeMethod(snakeCaseMethodName, a, b).As<int>();
Assert.AreEqual(15, originalMethodResult);
Assert.AreEqual(originalMethodResult, snakeCaseMethodResult);
}
[TestCase("PublicStringField", "public_string_field")]
[TestCase("PublicStaticStringField", "public_static_string_field")]
[TestCase("PublicReadonlyStringField", "public_readonly_string_field")]
// Constants
[TestCase("PublicConstStringField", "PUBLIC_CONST_STRING_FIELD")]
[TestCase("PublicStaticReadonlyStringField", "PUBLIC_STATIC_READONLY_STRING_FIELD")]
public void BindsSnakeCaseClassFields(string originalFieldName, string snakeCaseFieldName)
{
using var obj = new SnakeCaseNamesTesClass().ToPython();
var expectedValue = originalFieldName switch
{
"PublicStringField" => "public_string_field",
"PublicConstStringField" => "public_const_string_field",
"PublicReadonlyStringField" => "public_readonly_string_field",
"PublicStaticStringField" => "public_static_string_field",
"PublicStaticReadonlyStringField" => "public_static_readonly_string_field",
_ => throw new ArgumentException("Invalid field name")
};
var originalFieldValue = obj.GetAttr(originalFieldName).As<string>();
var snakeCaseFieldValue = obj.GetAttr(snakeCaseFieldName).As<string>();
Assert.AreEqual(expectedValue, originalFieldValue);
Assert.AreEqual(expectedValue, snakeCaseFieldValue);
}
[Test]
public void CanSetFieldUsingSnakeCaseName()
{
var obj = new SnakeCaseNamesTesClass();
using var pyObj = obj.ToPython();
// Try with the original field name
var newValue1 = "new value 1";
using var pyNewValue1 = newValue1.ToPython();
pyObj.SetAttr("PublicStringField", pyNewValue1);
Assert.AreEqual(newValue1, obj.PublicStringField);
// Try with the snake case field name
var newValue2 = "new value 2";
using var pyNewValue2 = newValue2.ToPython();
pyObj.SetAttr("public_string_field", pyNewValue2);
Assert.AreEqual(newValue2, obj.PublicStringField);
}
[Test]
public void CanSetStaticFieldUsingSnakeCaseName()
{
using (Py.GIL())
{
var module = PyModule.FromString("module", $@"
from clr import AddReference
AddReference(""Python.EmbeddingTest"")
from Python.EmbeddingTest import *
def SetCamelCaseStaticProperty(value):
ClassManagerTests.SnakeCaseNamesTesClass.PublicStaticStringField = value
def SetSnakeCaseStaticProperty(value):
ClassManagerTests.SnakeCaseNamesTesClass.public_static_string_field = value
");
// Try with the original field name
var newValue1 = "new value 1";
using var pyNewValue1 = newValue1.ToPython();
module.InvokeMethod("SetCamelCaseStaticProperty", pyNewValue1);
Assert.AreEqual(newValue1, SnakeCaseNamesTesClass.PublicStaticStringField);
// Try with the snake case field name
var newValue2 = "new value 2";
using var pyNewValue2 = newValue2.ToPython();
module.InvokeMethod("SetSnakeCaseStaticProperty", pyNewValue2);
Assert.AreEqual(newValue2, SnakeCaseNamesTesClass.PublicStaticStringField);
}
}
[TestCase("PublicStringProperty", "public_string_property")]
[TestCase("PublicStringGetOnlyProperty", "public_string_get_only_property")]
[TestCase("PublicStaticStringProperty", "public_static_string_property")]
[TestCase("PublicStaticReadonlyStringPrivateSetterProperty", "public_static_readonly_string_private_setter_property")]
[TestCase("PublicStaticReadonlyStringProtectedSetterProperty", "public_static_readonly_string_protected_setter_property")]
[TestCase("PublicStaticReadonlyStringInternalSetterProperty", "public_static_readonly_string_internal_setter_property")]
[TestCase("PublicStaticReadonlyStringProtectedInternalSetterProperty", "public_static_readonly_string_protected_internal_setter_property")]
[TestCase("ProtectedStringGetOnlyProperty", "protected_string_get_only_property")]
[TestCase("ProtectedStaticStringProperty", "protected_static_string_property")]
[TestCase("ProtectedStaticReadonlyStringPrivateSetterProperty", "protected_static_readonly_string_private_setter_property")]
// Constants
[TestCase("PublicStaticReadonlyStringGetterOnlyProperty", "PUBLIC_STATIC_READONLY_STRING_GETTER_ONLY_PROPERTY")]
[TestCase("PublicStaticReadonlyStringExpressionBodiedProperty", "PUBLIC_STATIC_READONLY_STRING_EXPRESSION_BODIED_PROPERTY")]
[TestCase("ProtectedStaticReadonlyStringGetterOnlyProperty", "PROTECTED_STATIC_READONLY_STRING_GETTER_ONLY_PROPERTY")]
[TestCase("ProtectedStaticReadonlyStringExpressionBodiedProperty", "PROTECTED_STATIC_READONLY_STRING_EXPRESSION_BODIED_PROPERTY")]
public void BindsSnakeCaseClassProperties(string originalPropertyName, string snakeCasePropertyName)
{
using var obj = new SnakeCaseNamesTesClass().ToPython();
var expectedValue = originalPropertyName switch
{
"PublicStringProperty" => "public_string_property",
"PublicStringGetOnlyProperty" => "public_string_get_only_property",
"PublicStaticStringProperty" => "public_static_string_property",
"PublicStaticReadonlyStringPrivateSetterProperty" => "public_static_readonly_string_private_setter_property",
"PublicStaticReadonlyStringProtectedSetterProperty" => "public_static_readonly_string_protected_setter_property",
"PublicStaticReadonlyStringInternalSetterProperty" => "public_static_readonly_string_internal_setter_property",
"PublicStaticReadonlyStringProtectedInternalSetterProperty" => "public_static_readonly_string_protected_internal_setter_property",
"PublicStaticReadonlyStringGetterOnlyProperty" => "public_static_readonly_string_getter_only_property",
"PublicStaticReadonlyStringExpressionBodiedProperty" => "public_static_readonly_string_expression_bodied_property",
"ProtectedStringGetOnlyProperty" => "protected_string_get_only_property",
"ProtectedStaticStringProperty" => "protected_static_string_property",
"ProtectedStaticReadonlyStringGetterOnlyProperty" => "protected_static_readonly_string_getter_only_property",
"ProtectedStaticReadonlyStringPrivateSetterProperty" => "protected_static_readonly_string_private_setter_property",
"ProtectedStaticReadonlyStringExpressionBodiedProperty" => "protected_static_readonly_string_expression_bodied_property",
_ => throw new ArgumentException("Invalid property name")
};
var originalPropertyValue = obj.GetAttr(originalPropertyName).As<string>();
var snakeCasePropertyValue = obj.GetAttr(snakeCasePropertyName).As<string>();
Assert.AreEqual(expectedValue, originalPropertyValue);
Assert.AreEqual(expectedValue, snakeCasePropertyValue);
}
[Test]
public void CanSetPropertyUsingSnakeCaseName()
{
var obj = new SnakeCaseNamesTesClass();
using var pyObj = obj.ToPython();
// Try with the original property name
var newValue1 = "new value 1";
using var pyNewValue1 = newValue1.ToPython();
pyObj.SetAttr("PublicStringProperty", pyNewValue1);
Assert.AreEqual(newValue1, obj.PublicStringProperty);
// Try with the snake case property name
var newValue2 = "new value 2";
using var pyNewValue2 = newValue2.ToPython();
pyObj.SetAttr("public_string_property", pyNewValue2);
Assert.AreEqual(newValue2, obj.PublicStringProperty);
}
[Test]
public void CanSetStaticPropertyUsingSnakeCaseName()
{
using (Py.GIL())
{
var module = PyModule.FromString("module", $@"
from clr import AddReference
AddReference(""Python.EmbeddingTest"")
from Python.EmbeddingTest import *
def SetCamelCaseStaticProperty(value):
ClassManagerTests.SnakeCaseNamesTesClass.PublicStaticStringProperty = value
def SetSnakeCaseStaticProperty(value):
ClassManagerTests.SnakeCaseNamesTesClass.public_static_string_property = value
");
// Try with the original property name
var newValue1 = "new value 1";
using var pyNewValue1 = newValue1.ToPython();
module.InvokeMethod("SetCamelCaseStaticProperty", pyNewValue1);
Assert.AreEqual(newValue1, SnakeCaseNamesTesClass.PublicStaticStringProperty);
// Try with the snake case property name
var newValue2 = "new value 2";
using var pyNewValue2 = newValue2.ToPython();
module.InvokeMethod("SetSnakeCaseStaticProperty", pyNewValue2);
Assert.AreEqual(newValue2, SnakeCaseNamesTesClass.PublicStaticStringProperty);
}
}
[TestCase("PublicStringEvent")]
[TestCase("public_string_event")]
public void BindsSnakeCaseEvents(string eventName)
{
var obj = new SnakeCaseNamesTesClass();
using var pyObj = obj.ToPython();
var value = "";
var eventHandler = new EventHandler<string>((sender, arg) => { value = arg; });
// Try with the original event name
using (Py.GIL())
{
var module = PyModule.FromString("module", $@"
def AddEventHandler(obj, handler):
obj.{eventName} += handler
def RemoveEventHandler(obj, handler):
obj.{eventName} -= handler
");
using var pyEventHandler = eventHandler.ToPython();
module.InvokeMethod("AddEventHandler", pyObj, pyEventHandler);
obj.InvokePublicStringEvent("new value 1");
Assert.AreEqual("new value 1", value);
module.InvokeMethod("RemoveEventHandler", pyObj, pyEventHandler);
obj.InvokePublicStringEvent("new value 2");
Assert.AreEqual("new value 1", value); // Should not have changed
}
}
[TestCase("PublicStaticStringEvent")]
[TestCase("public_static_string_event")]
public void BindsSnakeCaseStaticEvents(string eventName)
{
var value = "";
var eventHandler = new EventHandler<string>((sender, arg) => { value = arg; });
// Try with the original event name
using (Py.GIL())
{
var module = PyModule.FromString("module", $@"
from clr import AddReference
AddReference(""Python.EmbeddingTest"")
from Python.EmbeddingTest import *
def AddEventHandler(handler):
ClassManagerTests.SnakeCaseNamesTesClass.{eventName} += handler
def RemoveEventHandler(handler):
ClassManagerTests.SnakeCaseNamesTesClass.{eventName} -= handler
");
using var pyEventHandler = eventHandler.ToPython();
module.InvokeMethod("AddEventHandler", pyEventHandler);
SnakeCaseNamesTesClass.InvokePublicStaticStringEvent("new value 1");
Assert.AreEqual("new value 1", value);
module.InvokeMethod("RemoveEventHandler", pyEventHandler);
SnakeCaseNamesTesClass.InvokePublicStaticStringEvent("new value 2");
Assert.AreEqual("new value 1", value); // Should not have changed
}
}
private static IEnumerable<TestCaseData> SnakeCasedNamedArgsTestCases
{
get
{
var stringParam = "string";
var charParam = 'c';
var intParam = 1;
var floatParam = 2.0f;
var doubleParam = 3.0;
var decimalParam = 4.0m;
var boolParam = true;
var dateTimeParam = new DateTime(2013, 01, 05);
// 1. All kwargs:
// 1.1. Original method name:
var args = Array.Empty<object>();
var namedArgs = new Dictionary<string, object>()
{
{ "thisIsAStringParameter", stringParam },
{ "thisIsACharParameter", charParam },
{ "thisIsAnIntParameter", intParam },
{ "thisIsAFloatParameter", floatParam },
{ "thisIsADoubleParameter", doubleParam },
{ "thisIsADecimalParameter", decimalParam },
{ "thisIsABoolParameter", boolParam },
{ "thisIsADateTimeParameter", dateTimeParam }
};
var expectedResult = "string-c-1-2-3-4.0-True-01052013";
yield return new TestCaseData("JoinToString", args, namedArgs, expectedResult);
// 1.2. Snake-cased method name:
namedArgs = new Dictionary<string, object>()
{
{ "this_is_a_string_parameter", stringParam },
{ "this_is_a_char_parameter", charParam },
{ "this_is_an_int_parameter", intParam },
{ "this_is_a_float_parameter", floatParam },
{ "this_is_a_double_parameter", doubleParam },
{ "this_is_a_decimal_parameter", decimalParam },
{ "this_is_a_bool_parameter", boolParam },
{ "this_is_a_date_time_parameter", dateTimeParam }
};
yield return new TestCaseData("join_to_string", args, namedArgs, expectedResult);
// 2. Some args and some kwargs:
// 2.1. Original method name:
args = new object[] { stringParam, charParam, intParam, floatParam };
namedArgs = new Dictionary<string, object>()
{
{ "thisIsADoubleParameter", doubleParam },
{ "thisIsADecimalParameter", decimalParam },
{ "thisIsABoolParameter", boolParam },
{ "thisIsADateTimeParameter", dateTimeParam }
};
yield return new TestCaseData("JoinToString", args, namedArgs, expectedResult);
// 2.2. Snake-cased method name:
namedArgs = new Dictionary<string, object>()
{
{ "this_is_a_double_parameter", doubleParam },
{ "this_is_a_decimal_parameter", decimalParam },
{ "this_is_a_bool_parameter", boolParam },
{ "this_is_a_date_time_parameter", dateTimeParam }
};
yield return new TestCaseData("join_to_string", args, namedArgs, expectedResult);
// 3. Nullable args:
namedArgs = new Dictionary<string, object>()
{
{ "thisIsADoubleParameter", doubleParam },
{ "thisIsADecimalParameter", null },
{ "thisIsABoolParameter", boolParam },
{ "thisIsADateTimeParameter", dateTimeParam }
};
expectedResult = "string-c-1-2-3-123.456-True-01052013";
yield return new TestCaseData("JoinToString", args, namedArgs, expectedResult);
// 4. Parameters with default values:
namedArgs = new Dictionary<string, object>()
{
{ "this_is_a_double_parameter", doubleParam },
{ "this_is_a_decimal_parameter", decimalParam },
{ "this_is_a_bool_parameter", boolParam },
// Purposefully omitting the DateTime parameter so the default value is used
};
expectedResult = "string-c-1-2-3-4.0-True-01010001";
yield return new TestCaseData("join_to_string", args, namedArgs, expectedResult);
}
}
[TestCaseSource(nameof(SnakeCasedNamedArgsTestCases))]
public void CanCallSnakeCasedMethodWithSnakeCasedNamedArguments(string methodName, object[] args, Dictionary<string, object> namedArgs,
string expectedResult)
{
using var obj = new SnakeCaseNamesTesClass().ToPython();
var pyArgs = args.Select(a => a.ToPython()).ToArray();
using var pyNamedArgs = new PyDict();
foreach (var (key, value) in namedArgs)
{
pyNamedArgs[key] = value.ToPython();
}
var result = obj.InvokeMethod(methodName, pyArgs, pyNamedArgs).As<string>();
Assert.AreEqual(expectedResult, result);
}
[Test]
public void BindsEnumValuesWithPEPStyleNaming([Values] bool useSnakeCased)
{
using (Py.GIL())
{
var module = PyModule.FromString("module", $@"
from clr import AddReference
AddReference(""Python.EmbeddingTest"")
from Python.EmbeddingTest import *
def SetEnumValue1(obj):
obj.EnumValue = ClassManagerTests.SnakeCaseEnum.EnumValue1
def SetEnumValue2(obj):
obj.EnumValue = ClassManagerTests.SnakeCaseEnum.EnumValue2
def SetEnumValue3(obj):
obj.EnumValue = ClassManagerTests.SnakeCaseEnum.EnumValue3
def SetEnumValue1SnakeCase(obj):
obj.enum_value = ClassManagerTests.SnakeCaseEnum.ENUM_VALUE_1
def SetEnumValue2SnakeCase(obj):
obj.enum_value = ClassManagerTests.SnakeCaseEnum.ENUM_VALUE_2
def SetEnumValue3SnakeCase(obj):
obj.enum_value = ClassManagerTests.SnakeCaseEnum.ENUM_VALUE_3
");
using var obj = new SnakeCaseNamesTesClass().ToPython();
if (useSnakeCased)
{
module.InvokeMethod("SetEnumValue1SnakeCase", obj);
Assert.AreEqual(SnakeCaseEnum.EnumValue1, obj.GetAttr("enum_value").As<SnakeCaseEnum>());
module.InvokeMethod("SetEnumValue2SnakeCase", obj);
Assert.AreEqual(SnakeCaseEnum.EnumValue2, obj.GetAttr("enum_value").As<SnakeCaseEnum>());
module.InvokeMethod("SetEnumValue3SnakeCase", obj);
Assert.AreEqual(SnakeCaseEnum.EnumValue3, obj.GetAttr("enum_value").As<SnakeCaseEnum>());
}
else
{
module.InvokeMethod("SetEnumValue1", obj);
Assert.AreEqual(SnakeCaseEnum.EnumValue1, obj.GetAttr("EnumValue").As<SnakeCaseEnum>());
module.InvokeMethod("SetEnumValue2", obj);
Assert.AreEqual(SnakeCaseEnum.EnumValue2, obj.GetAttr("EnumValue").As<SnakeCaseEnum>());
module.InvokeMethod("SetEnumValue3", obj);
Assert.AreEqual(SnakeCaseEnum.EnumValue3, obj.GetAttr("EnumValue").As<SnakeCaseEnum>());
}
}
}
private class AlreadyDefinedSnakeCaseMemberTestBaseClass
{
private int private_field = 123;
public int PrivateField = 333;
public virtual int SomeIntProperty { get; set; } = 123;
public int some_int_property { get; set; } = 321;
public virtual int AnotherIntProperty { get; set; } = 456;
public int another_int_property()
{
return 654;
}
public dynamic a(AlreadyDefinedSnakeCaseMemberTestBaseClass a)
{
throw new Exception("a(AlreadyDefinedSnakeCaseMemberTestBaseClass)");
}
public int a()
{
throw new Exception("a()");
}
public int get_value()
{
throw new Exception("get_value()");
}
public virtual int get_value(int x)
{
throw new Exception("get_value(int x)");
}
public virtual int get_value_2(int x)
{
throw new Exception("get_value_2(int x)");
}
public int get_value_3(int x)
{
throw new Exception("get_value_3(int x)");
}
public int GetValue(int x)
{
throw new Exception("GetValue(int x)");
}
public virtual int GetValue(int x, int y)
{
throw new Exception("GetValue(int x, int y)");
}
public virtual int GetValue2(int x)
{
throw new Exception("GetValue2(int x)");
}
public int GetValue3(int x)
{
throw new Exception("GetValue3(int x)");
}
}
private class AlreadyDefinedSnakeCaseMemberTestDerivedClass : AlreadyDefinedSnakeCaseMemberTestBaseClass
{
public int SomeIntProperty { get; set; } = 111;
public override int AnotherIntProperty { get; set; } = 222;
public int A()
{
throw new Exception("A()");
}
public PyObject A(PyObject a)
{
throw new Exception("A(PyObject)");
}
public override int get_value(int x)
{
throw new Exception("override get_value(int x)");
}
public override int GetValue(int x, int y)
{
throw new Exception("override GetValue(int x, int y)");
}
public override int GetValue2(int x)
{
throw new Exception("override GetValue2(int x)");
}
public new int GetValue3(int x)
{
throw new Exception("new GetValue3(int x)");
}
}
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestBaseClass), "get_value", new object[] { 2, 3 }, "GetValue(int x, int y)")]
// 1 int arg, binds to the original c# class get_value(int x)
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestBaseClass), "get_value", new object[] { 2 }, "get_value(int x)")]
// 2 int args, binds to the snake-cased overriden GetValue(int x, int y)
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "get_value", new object[] { 2, 3 }, "override GetValue(int x, int y)")]
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "get_value", new object[] { 2 }, "override get_value(int x)")]
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "get_value", new object[] { }, "get_value()")]
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "A", new object[] { }, "A()")]
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "a", new object[] { }, "a()")]
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "GetValue2", new object[] { 2 }, "override GetValue2(int x)")]
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "GetValue3", new object[] { 2 }, "new GetValue3(int x)")]
// original beats fake
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "get_value_2", new object[] { 2 }, "get_value_2(int x)")]
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "get_value_3", new object[] { 2 }, "get_value_3(int x)")]
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "a", new object[] { "AlreadyDefinedSnakeCaseMemberTestBaseClass" }, "a(AlreadyDefinedSnakeCaseMemberTestBaseClass)")]
// A(PyObject) is real
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "A", new object[] { "AlreadyDefinedSnakeCaseMemberTestBaseClass" }, "A(PyObject)")]
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "a", new object[] { "Type" }, "A(PyObject)")]
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "A", new object[] { "Type" }, "A(PyObject)")]
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "A", new object[] { "Type" }, "A(PyObject)")]
public void BindsSnakeCasedMethodAsOverload(Type type, string methodName, object[] args, string expectedMessage)
{
if (args.Length == 1)
{
if (args[0] is "AlreadyDefinedSnakeCaseMemberTestBaseClass")
{
args = new object[] { new AlreadyDefinedSnakeCaseMemberTestBaseClass() };
}
else if (args[0] is "Type")
{
args = new object[] { typeof(string) };
}
}
var obj = Activator.CreateInstance(type);
using var pyObj = obj.ToPython();
using var method = pyObj.GetAttr(methodName);
var pyArgs = args.Select(x => x.ToPython()).ToArray();
var exception = Assert.Throws<Exception>(() => method.Invoke(pyArgs));
Assert.AreEqual(expectedMessage, exception.Message);
foreach (var x in pyArgs)
{
x.Dispose();
}
}
[Test]
public void DoesntBindSnakeCasedMemberIfAlreadyOriginallyDefinedAsProperty()
{
var obj = new AlreadyDefinedSnakeCaseMemberTestBaseClass();
using var pyObj = obj.ToPython();
Assert.AreEqual(123, pyObj.GetAttr("SomeIntProperty").As<int>());
Assert.AreEqual(321, pyObj.GetAttr("some_int_property").As<int>());
}
[Test]
public void DoesntBindSnakeCasedMemberIfAlreadyOriginallyDefinedAsMethod()
{
var obj = new AlreadyDefinedSnakeCaseMemberTestBaseClass();
using var pyObj = obj.ToPython();
Assert.AreEqual(456, pyObj.GetAttr("AnotherIntProperty").As<int>());
using var method = pyObj.GetAttr("another_int_property");
Assert.IsTrue(method.IsCallable());
Assert.AreEqual(654, method.Invoke().As<int>());
}
[Test]
public void DoesntBindSnakeCasedMemberIfAlreadyOriginallyDefinedAsPropertyInBaseClass()
{
var obj = new AlreadyDefinedSnakeCaseMemberTestDerivedClass();
using var pyObj = obj.ToPython();
Assert.AreEqual(111, pyObj.GetAttr("SomeIntProperty").As<int>());
Assert.AreEqual(321, pyObj.GetAttr("some_int_property").As<int>());
}
[Test]
public void DoesntBindSnakeCasedMemberIfAlreadyOriginallyDefinedAsMethodInBaseClass()
{
var obj = new AlreadyDefinedSnakeCaseMemberTestDerivedClass();
using var pyObj = obj.ToPython();
Assert.AreEqual(222, pyObj.GetAttr("AnotherIntProperty").As<int>());
using var method = pyObj.GetAttr("another_int_property");
Assert.IsTrue(method.IsCallable());
Assert.AreEqual(654, method.Invoke().As<int>());
}
[Test]
public void BindsMemberWithSnakeCasedNameMatchingExistingPrivateMember()
{
using var obj = new AlreadyDefinedSnakeCaseMemberTestBaseClass().ToPython();
Assert.AreEqual(333, obj.GetAttr("private_field").As<int>());
}
private abstract class AlreadyDefinedSnakeCaseMemberTestBaseAbstractClass
{
public abstract int AbstractProperty { get; }
public virtual int SomeIntProperty { get; set; } = 123;
public int some_int_property { get; set; } = 321;
public virtual int AnotherIntProperty { get; set; } = 456;
public int another_int_property()
{
return 654;
}
}
private class AlreadyDefinedSnakeCaseMemberTestDerivedFromAbstractClass : AlreadyDefinedSnakeCaseMemberTestBaseAbstractClass
{
public override int AbstractProperty => 0;
public int SomeIntProperty { get; set; } = 333;
public int AnotherIntProperty { get; set; } = 444;
}
[Test]
public void DoesntBindSnakeCasedMemberIfAlreadyOriginallyDefinedAsPropertyInBaseAbstractClass()
{
var obj = new AlreadyDefinedSnakeCaseMemberTestDerivedFromAbstractClass();
using var pyObj = obj.ToPython();
Assert.AreEqual(333, pyObj.GetAttr("SomeIntProperty").As<int>());
Assert.AreEqual(321, pyObj.GetAttr("some_int_property").As<int>());
}
[Test]
public void DoesntBindSnakeCasedMemberIfAlreadyOriginallyDefinedAsMethodInBaseAbstractClass()
{
var obj = new AlreadyDefinedSnakeCaseMemberTestDerivedFromAbstractClass();
using var pyObj = obj.ToPython();
Assert.AreEqual(444, pyObj.GetAttr("AnotherIntProperty").As<int>());
using var method = pyObj.GetAttr("another_int_property");
Assert.IsTrue(method.IsCallable());
Assert.AreEqual(654, method.Invoke().As<int>());
}
public class Class1
{
}
private class TestClass1
{
public dynamic get(Class1 s)
{
return "dynamic get(Class1 s)";
}
}
private class TestClass2 : TestClass1
{
public PyObject Get(PyObject o)
{
return "PyObject Get(PyObject o)".ToPython();
}
public dynamic Get(Type t)
{
return "dynamic Get(Type t)";
}
}
[Test]
public void BindsCorrectOverloadForClassName()
{
using var obj = new TestClass2().ToPython();
var result = obj.GetAttr("get").Invoke(new Class1().ToPython()).As<string>();
Assert.AreEqual("dynamic get(Class1 s)", result);
result = obj.GetAttr("get").Invoke(new TestClass1().ToPython()).As<string>();
Assert.AreEqual("PyObject Get(PyObject o)", result);
using (Py.GIL())
{
// Passing type name directly instead of typeof(Class1) from C#
var module = PyModule.FromString("module", $@"
from clr import AddReference
AddReference(""Python.EmbeddingTest"")
from Python.EmbeddingTest import *
def call(instance):
return instance.get(ClassManagerTests.Class1)
");