Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonabreul committed Nov 1, 2024
1 parent 0acc2db commit e26db13
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion src/embed_tests/TestMethodBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ public void Method()
CalledMethodMessage = "Overload 1";
}

public void Method(CSharpClass csharpClassArgument, decimal decimalArgument = 1.2m, PyObject pyObjectKArgument = null)
public void Method(CSharpClass csharpClassArgument, decimal decimalArgument = 1.2m, PyObject pyObjectKwArgument = null)
{
CalledMethodMessage = "Overload 2";
}
Expand Down Expand Up @@ -998,6 +998,56 @@ public void PyObjectArgsHavePrecedenceOverOtherTypes()
Assert.IsFalse(Exceptions.ErrorOccurred());
}

[Test]
public void OtherTypesHavePrecedenceOverPyObjectArgsIfMoreArgsAreMatched()
{
using var _ = Py.GIL();

var instance = new CSharpClass2();
using var pyInstance = instance.ToPython();
using var pyArg = new CSharpClass().ToPython();

Assert.DoesNotThrow(() =>
{
using var kwargs = Py.kw("pyObjectKwArgument", new CSharpClass2());
pyInstance.InvokeMethod("Method", new[] { pyArg }, kwargs);
});

Assert.AreEqual("Overload 2", instance.CalledMethodMessage);
Assert.IsFalse(Exceptions.ErrorOccurred());
instance.Clear();

Assert.DoesNotThrow(() =>
{
using var kwargs = Py.kw("py_object_kw_argument", new CSharpClass2());
pyInstance.InvokeMethod("method", new[] { pyArg }, kwargs);
});

Assert.AreEqual("Overload 2", instance.CalledMethodMessage);
Assert.IsFalse(Exceptions.ErrorOccurred());
instance.Clear();

Assert.DoesNotThrow(() =>
{
using var kwargs = Py.kw("objectArgument", "somestring");
pyInstance.InvokeMethod("Method", new[] { pyArg }, kwargs);
});

Assert.AreEqual("Overload 3", instance.CalledMethodMessage);
Assert.IsFalse(Exceptions.ErrorOccurred());
instance.Clear();

Assert.DoesNotThrow(() =>
{
using var kwargs = Py.kw("object_argument", "somestring");
pyInstance.InvokeMethod("method", new[] { pyArg }, kwargs);
});

Assert.AreEqual("Overload 3", instance.CalledMethodMessage);
Assert.IsFalse(Exceptions.ErrorOccurred());
instance.Clear();
}

[Test]
public void BindsConstructorToSnakeCasedArgumentsVersion([Values] bool useCamelCase, [Values] bool passOptionalArgument)
{
Expand Down

0 comments on commit e26db13

Please sign in to comment.