Skip to content

Commit 65626c3

Browse files
committed
add test for using null like 'None' for default arguments. See 35cd5fa
1 parent 8effe30 commit 65626c3

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

src/testing/Python.Test.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
</PropertyGroup>
109109
<ItemGroup>
110110
<Compile Include="arraytest.cs" />
111+
<Compile Include="callbacktest.cs" />
111112
<Compile Include="classtest.cs" />
112113
<Compile Include="constructortests.cs" />
113114
<Compile Include="conversiontest.cs" />
@@ -127,6 +128,7 @@
127128
<Compile Include="subclasstest.cs" />
128129
</ItemGroup>
129130
<ItemGroup>
131+
<Reference Include="Microsoft.CSharp" />
130132
<Reference Include="System" />
131133
<Reference Include="System.Core">
132134
<RequiredTargetFramework>3.5</RequiredTargetFramework>

src/testing/callbacktest.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Python.Test
7+
{
8+
//========================================================================
9+
// Tests callbacks into python code.
10+
//========================================================================
11+
12+
public class CallbackTest
13+
{
14+
public string Call_simpleDefaultArg_WithNull(string moduleName)
15+
{
16+
using (Runtime.Py.GIL())
17+
{
18+
dynamic module = Runtime.Py.Import(moduleName);
19+
return module.simpleDefaultArg(null);
20+
}
21+
}
22+
public string Call_simpleDefaultArg_WithEmptyArgs(string moduleName)
23+
{
24+
using (Runtime.Py.GIL())
25+
{
26+
dynamic module = Runtime.Py.Import(moduleName);
27+
return module.simpleDefaultArg();
28+
}
29+
}
30+
}
31+
}

src/tests/test_suite/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
__all__ = ['test_suite']
44

55
from .test_import import test_suite as import_tests
6+
from .test_callback import test_suite as callback_tests
67

78
def test_suite():
89
suite = unittest.TestSuite()
910
suite.addTests((import_tests(),))
11+
suite.addTests((callback_tests(),))
1012
return suite

src/tests/test_suite/test_callback.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import unittest, sys
2+
import clr
3+
4+
this_module = sys.modules[__name__]
5+
clr.AddReference("Python.Test")
6+
import Python.Test as Test
7+
from Python.Test import CallbackTest
8+
test_instance = CallbackTest()
9+
10+
def simpleDefaultArg(arg = 'test'):
11+
return arg
12+
13+
class CallbackTests(unittest.TestCase):
14+
"""Test that callbacks from C# into python work."""
15+
16+
def testDefaultForNull(self):
17+
"""Test that C# can use null for an optional python argument"""
18+
retVal = test_instance.Call_simpleDefaultArg_WithNull(__name__)
19+
pythonRetVal = simpleDefaultArg(None)
20+
self.assertEquals(retVal, pythonRetVal)
21+
22+
def testDefaultForNone(self):
23+
"""Test that C# can use no argument for an optional python argument"""
24+
retVal = test_instance.Call_simpleDefaultArg_WithEmptyArgs(__name__)
25+
pythonRetVal = simpleDefaultArg()
26+
self.assertEquals(retVal, pythonRetVal)
27+
28+
def test_suite():
29+
return unittest.makeSuite(CallbackTests)
30+

0 commit comments

Comments
 (0)