Skip to content

Added ToPythonAs() extension method for explicit conversion using an arbitrary type #2419

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Add test description
  • Loading branch information
joaompneves committed Jul 18, 2024
commit cf8ff1f1d9e8514660b3bcc8ca79543fa49ee971
11 changes: 4 additions & 7 deletions src/embed_tests/Codecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,22 +339,19 @@ public void ExceptionDecoded()
Assert.AreEqual(TestExceptionMessage, error.Message);
}

[Test]
[Test(Description = "Tests encoding of an object that explicitly implements an interface.")]
public void ExplicitObjectInterfaceEncoded()
{
var obj = new ExplicitInterfaceObject();

// explicitly pass an interface (but not a generic type argument) to simulate a scenario where the type is not know at build time
// var encoder = new InterfaceEncoder(typeof(IObjectInterface));
// PyObjectConversions.RegisterEncoder(encoder);

using var scope = Py.CreateScope();
scope.Exec(@"
def call(obj):
return dir(obj)
");
var callFunc = scope.Get("call");
var members = callFunc.Invoke(obj.ToPythonAs(typeof(IObjectInterface))).As<string[]>();
// explicitly pass an interface (but not a generic type argument) to simulate a scenario where the type is not know at build time
var callArg = obj.ToPythonAs(typeof(IObjectInterface));
var members = callFunc.Invoke(callArg).As<string[]>();
CollectionAssert.Contains(members, nameof(IObjectInterface.MemberFromInterface));
CollectionAssert.DoesNotContain(members, nameof(ExplicitInterfaceObject.MemberFromObject));
}
Expand Down