Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add tuple comparison operator tests (2/6)
  • Loading branch information
christabella committed Jan 12, 2021
commit a8c659158d1daff05063bbdff7710217ff4b697b
40 changes: 40 additions & 0 deletions src/embed_tests/TestOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,25 @@ public OperableObject(int num)
return (a.Num >= b);
}

public static bool operator >=(OperableObject a, PyObject b)
{
using (Py.GIL())
{
// Assuming b is a tuple, take the first element.
Comment thread
lostmsu marked this conversation as resolved.
int bNum = (int)b.ToArray()[0].AsManagedObject(typeof(int));
return a.Num >= bNum;
}
}
public static bool operator <=(OperableObject a, PyObject b)
{
using (Py.GIL())
{
// Assuming b is a tuple, take the first element.
int bNum = (int)b.ToArray()[0].AsManagedObject(typeof(int));
return a.Num <= bNum;
}
}

public static bool operator <(int a, OperableObject b)
{
return (a < b.Num);
Expand Down Expand Up @@ -391,6 +410,27 @@ public void ForwardOperatorOverloads()
");
}

[Test]
public void TupleComparisonOperatorOverloads()
{
string name = string.Format("{0}.{1}",
typeof(OperableObject).DeclaringType.Name,
typeof(OperableObject).Name);
string module = MethodBase.GetCurrentMethod().DeclaringType.Namespace;
PythonEngine.Exec($@"
from {module} import *
cls = {name}
a = cls(2)
b = (1, 2)

c = a >= b
assert c == (a.Num >= b[0])

c = a <= b
assert c == (a.Num <= b[0])
");
}


[Test]
public void ReverseOperatorOverloads()
Expand Down