Source code for priops.test.test_casting
"""Tests addition ``X() + Z()`` via ``Z -> Y``."""
import priops
from priops.test.classes import X, Y, Z
import nose.tools
[docs]def add_xy(x, y):
"""Adds an :class:`~priops.test.classes.X` instance and a
:class:`~priops.test.classes.Y` instance."""
return x.x + y.y
[docs]def cast_yz(z):
"""Casts a :class:`~priops.test.classes.Z` instance to a
:class:`~priops.test.classes.Y` instance."""
return Y(z.z)
p = priops.Priop()
edge_add_xy = priops.CallableEdge(
input_node=priops.ClassNode(items=[X, Y]),
output_node=priops.ClassNode(item=int),
callable=add_xy, name='Add X + Y')
p.append(edge_add_xy)
edge_cast_yz = priops.CallableEdge(
input_node=priops.ClassNode(item=Z),
output_node=priops.ClassNode(item=Y),
callable=cast_yz, name='Cast Z -> Y')
p.append(edge_cast_yz)
cpf = priops.ClassPathfinder(
priop=p, output_node=priops.ClassNode(item=None), max_components=2,
max_weight=2)
[docs]def test_succeeds():
"""Tests if ``X(1) + Z(3) == 4``."""
nose.tools.eq_(cpf(X(1), Z(3)), 4)
[docs]def test_direct():
"""Tests if ``X(1) + Y(2) == 3`` still."""
nose.tools.eq_(cpf(X(1), Y(2)), 3)
@nose.tools.raises(priops.NoPath)
[docs]def test_fails():
"""Tests that ``X(10)`` cannot be coerced with ``X(20)``."""
cpf(X(10), X(20))