Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests to expect no binding by keyword to *args/**kwargs #1042

Merged
merged 2 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions Tests/interop/net/method/test_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def setUp(self):
self.o = VariousParameters()

def test_0_1_args(self):
import System
from Merlin.Testing import Flag

# public void M100() { Flag.Set(10); }
f = self.o.M100
Expand Down Expand Up @@ -95,6 +97,9 @@ def test_0_1_args(self):
self.assertRaisesMessage(TypeError, "M202() got an unexpected keyword argument 'arg'", lambda: f(**{'arg': 3}))# msg
self.assertRaisesMessage(TypeError, "M202() got an unexpected keyword argument 'other'", lambda: f(**{'other': 4}))

ar_int = System.Array[System.Int32](3)
f(ar_int); Flag.Check(3)

# public void M203([ParamDictionaryAttribute] IDictionary<object, object> arg) { Flag.Set(arg.Count); }
f = self.o.M203
f()
Expand All @@ -106,8 +111,17 @@ def test_0_1_args(self):
f(**{'a':2, 'b':3})
f(a=1, **{'b':2, 'c':5})
self.assertRaisesMessage(TypeError, "M203() got multiple values for keyword argument 'a'", lambda: f(a=1, **{'a':2, 'c':5}))
self.assertRaisesMessage(TypeError, "M203() takes no arguments (3 given)", lambda: f(*(1,2,3)))
self.assertRaisesMessage(TypeError, "M203() takes no arguments (3 given)", lambda: f(*(1,2,3))) # msg: no positional arguments
dict_obj = System.Collections.Generic.Dictionary[System.Object, System.Object]()
self.assertRaisesMessage(TypeError, "M203() takes no arguments (1 given)", lambda: f(dict_obj)) # msg: no positional arguments

# public void M204(params object[] arg)
f = self.o.M204

ar_obj = System.Array[System.Object](3)
f(ar_obj); Flag.Check(3)
f(ar_obj, ar_obj); Flag.Check(2)
f(ar_int); Flag.Check(1)

def test_optional(self):
import System
Expand Down Expand Up @@ -230,7 +244,7 @@ def test_two_args(self):
# TODO: mixed
f(x = 1) # check the value

#public void M351(int x, [ParamDictionary] IDictionary<object, object> arg) { Flag<object>.Set(arg); }
#public void M351(int x, [ParamDictionary] IDictionary<object, object> y) { Flag<object>.Set(y); }
f = self.o.M351
self.assertRaisesMessage(TypeError, "M351() takes exactly 1 argument (0 given)", lambda: f())
f(1); self.assertEqual(Flag[object].Value1, {})
Expand All @@ -240,7 +254,7 @@ def test_two_args(self):
f(x=1); self.assertEqual(Flag[object].Value1, {})
f(**{'x' : 1}); self.assertEqual(Flag[object].Value1, {})

#public void M352([ParamDictionary] IDictionary<object, object> arg, params int[] x) { Flag<object>.Set(arg); }
#public void M352([ParamDictionary] IDictionary<object, object> x, params object[] y) { Flag<object>.Set(x); }

f=self.o.M352
f(); self.assertEqual(Flag[object].Value1, {})
Expand Down Expand Up @@ -269,20 +283,22 @@ def test_default_values_2(self):

f(1, **{'y':2}); Flag.Check(3)

# public void M320([DefaultParameterValue(40)] int y, int x) { Flag.Set(x + y); }
# public void M320([DefaultParameterValue(40)] int x, int y) { Flag.Set(x + y); }
f = self.o.M320
self.assertRaisesMessage(TypeError, "M320() takes at least 1 argument (0 given)", f)
f(1); Flag.Check(41) # !!!
f(2, 3); Flag.Check(5)
self.assertRaisesMessage(TypeError, "M320() takes at most 2 arguments (3 given)", lambda : f(1, 2, 3))

f(x = 2); Flag.Check(42)
f(y = 2); Flag.Check(42)
f(y = 2, x = 3); Flag.Check(5)
f(x = 2, y = 3); Flag.Check(5)
f(*(1,)); Flag.Check(41)
f(*(1, 2)); Flag.Check(3)

self.assertRaisesMessage(TypeError, "Argument for M320() given by name ('y') and position (1)", lambda : f(5, y = 6)) # !!!
f(6, x = 7); Flag.Check(13)
self.assertRaisesMessage(TypeError, "Argument for M320() given by name ('x') and position (1)", lambda : f(5, x = 6)) # !!!
self.assertRaisesMessage(TypeError, "M320() got an unexpected keyword argument 'x'", lambda : f(x = 6)) # !!!
f(6, y = 7); Flag.Check(13)

# public void M330([DefaultParameterValue(50)] int x, [DefaultParameterValue(60)] int y) { Flag.Set(x + y); }
f = self.o.M330
Expand Down Expand Up @@ -362,6 +378,13 @@ def test_3_args(self):
#f(*(1, 2, 0, 1), **{'z':3, 'y':4}) # FIXME: M550() takes at least 2 arguments (6 given) => e.g. Argument for M550() given by name ('x') and position (1)
#f(*(1, 2, 0, 1), **{'z':3, 'x':4}) # FIXME: M550() takes at least 2 arguments (6 given) => e.g. Argument for M550() given by name ('x') and position (1)

# public void M560(int x, [ParamDictionary] IDictionary<string, int> y, params int[] z) { Flag.Set(x * 100 + y.Count * 10 + z.Length); }
f = self.o.M560
f(2, 3, 4, a=5, b=6, c=7); Flag.Check(2 * 100 + 3 * 10 + 2)
self.assertRaisesMessage(TypeError, "Argument for M560() given by name ('x') and position (1)", lambda: f(2, 3, 4, x=4, b=6, c=7))
f(2, 3, 4, y=5, b=6, c=7); Flag.Check(2 * 100 + 3 * 10 + 2)
f(2, 3, 4, z=5, b=6, c=7); Flag.Check(2 * 100 + 3 * 10 + 2)

def test_many_args(self):
from Merlin.Testing import Flag
#public void M650(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8, int arg9, int arg10) { }
Expand Down
9 changes: 3 additions & 6 deletions Tests/test_dlrkwarg.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,10 @@ def test_bad(self):
self.assertRaisesMessage(TypeError, "FuncWithIRoDictGenSOKwargs() takes no arguments (1 given)",
Variadics.FuncWithIRoDictGenSOKwargs, {})

# TODO: this should not work, 'kwargs' should be a key in 'kwargs' dict passed to the builtin function
def test_keyword_arg(self):
clrdict = System.Collections.Generic.Dictionary[System.String, System.Object]()
self.assertEqual(Variadics.FuncWithIRoDictGenSOKwargs(kwargs=clrdict), 0)

# TODO: This should work
#self.assertEqual(Variadics.FuncWithIRoDictGenSOKwargs(kwargs={}), 'kwargs')
#self.assertEqual(Variadics.FuncWithIRoDictGenSOKwargs(kwargs=clrdict), 'kwargs')
self.assertEqual(Variadics.FuncWithIRoDictGenSOKwargs(kwargs=clrdict), 1) # no binding by keyword
self.assertEqual(Variadics.FuncWithIRoDictGenSOKwargs(kwargs={}), 1)

def test_attribcol(self):
self.assertRaisesMessage(SystemError, "Unsupported param dictionary type: System.ComponentModel.AttributeCollection",
Expand Down
14 changes: 14 additions & 0 deletions Tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,20 @@ def foo(**kwargs):
self.assertTrue(SplatTest2.FuncWithIDictGenOOKwargs(**mymapping({mystr("bla"): "bla"})))
self.assertTrue(SplatTest2.FuncWithIDictGenSOKwargs(**mymapping({mystr("bla"): "bla"})))

def test_kwargs4(self):
"""verify args/kwargs are not bindable by name or position"""

def foo(*args, **kwargs):
return args, kwargs

self.assertEqual(foo(), ((), {}))
self.assertEqual(foo(args=()), ((), {'args': ()}))
self.assertEqual(foo(kwargs={}), ((), {'kwargs': {}}))
self.assertEqual(foo(args=(), kwargs={}), ((), {'args': (), 'kwargs': {}}))
self.assertEqual(foo(()), (((),), {}))
self.assertEqual(foo({}), (({},), {}))
self.assertEqual(foo((), {}), (((), {}), {}))

@skipUnlessIronPython()
def test_params_method_no_params(self):
"""call a params method w/ no params"""
Expand Down