|
| 1 | +using NUnit.Framework; |
| 2 | +using Python.Runtime; |
| 3 | + |
| 4 | +namespace Python.EmbeddingTest |
| 5 | +{ |
| 6 | + public class TestMethodBinder |
| 7 | + { |
| 8 | + [OneTimeSetUp] |
| 9 | + public void SetUp() |
| 10 | + { |
| 11 | + PythonEngine.Initialize(); |
| 12 | + } |
| 13 | + |
| 14 | + [OneTimeTearDown] |
| 15 | + public void Dispose() |
| 16 | + { |
| 17 | + PythonEngine.Shutdown(); |
| 18 | + } |
| 19 | + |
| 20 | + [Test] |
| 21 | + public void ImplicitConversionToString() |
| 22 | + { |
| 23 | + // create instance of python model |
| 24 | + dynamic pyMethodCall = PythonEngine.ModuleFromString("module", @" |
| 25 | +from clr import AddReference |
| 26 | +AddReference(""System"") |
| 27 | +AddReference(""Python.EmbeddingTest"") |
| 28 | +
|
| 29 | +from Python.EmbeddingTest import * |
| 30 | +
|
| 31 | +class PythonModel(TestMethodBinder.CSharpModel): |
| 32 | + def MethodCall(self): |
| 33 | + return self.OnlyString(TestMethodBinder.TestImplicitConversion()) |
| 34 | +").GetAttr("PythonModel").Invoke(); |
| 35 | + |
| 36 | + using (Py.GIL()) |
| 37 | + { |
| 38 | + var data = (string)pyMethodCall.MethodCall(); |
| 39 | + // we assert implicit conversion took place |
| 40 | + Assert.AreEqual("OnlyString impl: implicit to string", data); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + [Test] |
| 45 | + public void ImplicitConversionToClass() |
| 46 | + { |
| 47 | + // create instance of python model |
| 48 | + dynamic pyMethodCall = PythonEngine.ModuleFromString("module", @" |
| 49 | +from clr import AddReference |
| 50 | +AddReference(""System"") |
| 51 | +AddReference(""Python.EmbeddingTest"") |
| 52 | +
|
| 53 | +from Python.EmbeddingTest import * |
| 54 | +
|
| 55 | +class PythonModel(TestMethodBinder.CSharpModel): |
| 56 | + def MethodCall(self): |
| 57 | + return self.OnlyClass('input string') |
| 58 | +").GetAttr("PythonModel").Invoke(); |
| 59 | + |
| 60 | + using (Py.GIL()) |
| 61 | + { |
| 62 | + var data = (string)pyMethodCall.MethodCall(); |
| 63 | + // we assert implicit conversion took place |
| 64 | + Assert.AreEqual("OnlyClass impl", data); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + [Test] |
| 69 | + public void WillAvoidUsingImplicitConversionIfPossible_String() |
| 70 | + { |
| 71 | + // create instance of python model |
| 72 | + dynamic pyMethodCall = PythonEngine.ModuleFromString("module", @" |
| 73 | +from clr import AddReference |
| 74 | +AddReference(""System"") |
| 75 | +AddReference(""Python.EmbeddingTest"") |
| 76 | +
|
| 77 | +from Python.EmbeddingTest import * |
| 78 | +
|
| 79 | +class PythonModel(TestMethodBinder.CSharpModel): |
| 80 | + def MethodCall(self): |
| 81 | + return self.InvokeModel('input string') |
| 82 | +").GetAttr("PythonModel").Invoke(); |
| 83 | + |
| 84 | + using (Py.GIL()) |
| 85 | + { |
| 86 | + var data = (string)pyMethodCall.MethodCall(); |
| 87 | + // we assert no implicit conversion took place |
| 88 | + Assert.AreEqual("string impl: input string", data); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + [Test] |
| 93 | + public void WillAvoidUsingImplicitConversionIfPossible_Class() |
| 94 | + { |
| 95 | + // create instance of python model |
| 96 | + dynamic pyMethodCall = PythonEngine.ModuleFromString("module", @" |
| 97 | +from clr import AddReference |
| 98 | +AddReference(""System"") |
| 99 | +AddReference(""Python.EmbeddingTest"") |
| 100 | +
|
| 101 | +from Python.EmbeddingTest import * |
| 102 | +
|
| 103 | +class PythonModel(TestMethodBinder.CSharpModel): |
| 104 | + def MethodCall(self): |
| 105 | + return self.InvokeModel(TestMethodBinder.TestImplicitConversion()) |
| 106 | +").GetAttr("PythonModel").Invoke(); |
| 107 | + |
| 108 | + using (Py.GIL()) |
| 109 | + { |
| 110 | + var data = (string)pyMethodCall.MethodCall(); |
| 111 | + // we assert no implicit conversion took place |
| 112 | + Assert.AreEqual("TestImplicitConversion impl", data); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public class CSharpModel |
| 117 | + { |
| 118 | + public virtual string OnlyClass(TestImplicitConversion data) |
| 119 | + { |
| 120 | + return "OnlyClass impl"; |
| 121 | + } |
| 122 | + |
| 123 | + public virtual string OnlyString(string data) |
| 124 | + { |
| 125 | + return "OnlyString impl: " + data; |
| 126 | + } |
| 127 | + |
| 128 | + public virtual string InvokeModel(string data) |
| 129 | + { |
| 130 | + return "string impl: " + data; |
| 131 | + } |
| 132 | + |
| 133 | + public virtual string InvokeModel(TestImplicitConversion data) |
| 134 | + { |
| 135 | + return "TestImplicitConversion impl"; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public class TestImplicitConversion |
| 140 | + { |
| 141 | + public static implicit operator string(TestImplicitConversion symbol) |
| 142 | + { |
| 143 | + return "implicit to string"; |
| 144 | + } |
| 145 | + public static implicit operator TestImplicitConversion(string symbol) |
| 146 | + { |
| 147 | + return new TestImplicitConversion(); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments