Skip to content

Commit bf1755d

Browse files
committed
Add MethodBinder tests
1 parent af30873 commit bf1755d

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

src/embed_tests/TestMethodBinder.cs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using System;
2+
3+
using NUnit.Framework;
4+
5+
using Python.Runtime;
6+
7+
namespace Python.EmbeddingTest
8+
{
9+
public class TestMethodBinder
10+
{
11+
private static dynamic module;
12+
private static string testModule = @"
13+
from clr import AddReference
14+
AddReference(""System"")
15+
AddReference(""Python.EmbeddingTest"")
16+
from Python.EmbeddingTest import *
17+
class PythonModel(TestMethodBinder.CSharpModel):
18+
def TestA(self):
19+
return self.OnlyString(TestMethodBinder.TestImplicitConversion())
20+
def TestB(self):
21+
return self.OnlyClass('input string')
22+
def TestC(self):
23+
return self.InvokeModel('input string')
24+
def TestD(self):
25+
return self.InvokeModel(TestMethodBinder.TestImplicitConversion())
26+
def TestE(self, array):
27+
return array.Length == 2";
28+
29+
30+
[OneTimeSetUp]
31+
public void SetUp()
32+
{
33+
PythonEngine.Initialize();
34+
module = PythonEngine.ModuleFromString("module", testModule).GetAttr("PythonModel").Invoke();
35+
}
36+
37+
[OneTimeTearDown]
38+
public void Dispose()
39+
{
40+
PythonEngine.Shutdown();
41+
}
42+
43+
[Test]
44+
public void ImplicitConversionToString()
45+
{
46+
var data = (string)module.TestA();
47+
// we assert implicit conversion took place
48+
Assert.AreEqual("OnlyString impl: implicit to string", data);
49+
}
50+
51+
[Test]
52+
public void ImplicitConversionToClass()
53+
{
54+
var data = (string)module.TestB();
55+
// we assert implicit conversion took place
56+
Assert.AreEqual("OnlyClass impl", data);
57+
}
58+
59+
[Test]
60+
public void WillAvoidUsingImplicitConversionIfPossible_String()
61+
{
62+
var data = (string)module.TestC();
63+
// we assert no implicit conversion took place
64+
Assert.AreEqual("string impl: input string", data);
65+
}
66+
67+
[Test]
68+
public void WillAvoidUsingImplicitConversionIfPossible_Class()
69+
{
70+
var data = (string)module.TestD();
71+
// we assert no implicit conversion took place
72+
Assert.AreEqual("TestImplicitConversion impl", data);
73+
74+
}
75+
76+
[Test]
77+
public void ArrayLength()
78+
{
79+
var array = new[] { "pepe", "pinocho" };
80+
var data = (bool)module.TestE(array);
81+
82+
// Assert it is true
83+
Assert.AreEqual(true, data);
84+
}
85+
86+
public class CSharpModel
87+
{
88+
public virtual string OnlyClass(TestImplicitConversion data)
89+
{
90+
return "OnlyClass impl";
91+
}
92+
93+
public virtual string OnlyString(string data)
94+
{
95+
return "OnlyString impl: " + data;
96+
}
97+
98+
public virtual string InvokeModel(string data)
99+
{
100+
return "string impl: " + data;
101+
}
102+
103+
public virtual string InvokeModel(TestImplicitConversion data)
104+
{
105+
return "TestImplicitConversion impl";
106+
}
107+
}
108+
109+
public class TestImplicitConversion
110+
{
111+
public static implicit operator string(TestImplicitConversion symbol)
112+
{
113+
return "implicit to string";
114+
}
115+
public static implicit operator TestImplicitConversion(string symbol)
116+
{
117+
return new TestImplicitConversion();
118+
}
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)