2018-12-15 23:19:42 -05:00
""" Item constraint creation tests.
2008-12-08 20:07:15 +00:00
2020-09-15 20:23:50 -04:00
The test check functionality of ` Item . constraint ` method , not
constraints themselves .
2018-12-15 23:19:42 -05:00
"""
import pytest
2008-12-08 20:07:15 +00:00
2020-08-18 21:16:39 -04:00
from gaphas . constraint import EqualsConstraint , LessThanConstraint , LineConstraint
2018-12-15 23:19:42 -05:00
from gaphas . item import Item
2008-12-08 20:07:15 +00:00
from gaphas . solver import Variable
2018-11-25 16:07:58 -05:00
2019-12-28 20:16:43 -05:00
class ItemPosition :
2018-12-15 23:19:42 -05:00
def __init__ ( self ) :
self . item = Item ( )
self . pos1 = Variable ( 1 ) , Variable ( 2 )
self . pos2 = Variable ( 3 ) , Variable ( 4 )
@pytest.fixture ( )
def item_pos ( ) :
return ItemPosition ( )
def test_line_constraint ( item_pos ) :
2020-09-15 20:23:50 -04:00
""" Test line creation constraint. """
2018-12-15 23:19:42 -05:00
line = ( Variable ( 3 ) , Variable ( 4 ) ) , ( Variable ( 5 ) , Variable ( 6 ) )
item_pos . item . constraint ( line = ( item_pos . pos1 , line ) )
assert 1 == len ( item_pos . item . _constraints )
c = item_pos . item . _constraints [ 0 ]
assert isinstance ( c , LineConstraint )
assert ( 1 , 2 ) == c . _point
assert ( ( 3 , 4 ) , ( 5 , 6 ) ) == c . _line
def test_horizontal_constraint ( item_pos ) :
2020-09-15 20:23:50 -04:00
""" Test horizontal constraint creation. """
2018-12-15 23:19:42 -05:00
item_pos . item . constraint ( horizontal = ( item_pos . pos1 , item_pos . pos2 ) )
assert 1 == len ( item_pos . item . _constraints )
c = item_pos . item . _constraints [ 0 ]
assert isinstance ( c , EqualsConstraint )
# Expect constraint on y-axis
assert 2 == c . a
assert 4 == c . b
def test_vertical_constraint ( item_pos ) :
2020-09-15 20:23:50 -04:00
""" Test vertical constraint creation. """
2018-12-15 23:19:42 -05:00
item_pos . item . constraint ( vertical = ( item_pos . pos1 , item_pos . pos2 ) )
assert 1 == len ( item_pos . item . _constraints )
c = item_pos . item . _constraints [ 0 ]
assert isinstance ( c , EqualsConstraint )
# Expect constraint on x-axis
assert 1 == c . a
assert 3 == c . b
def test_left_of_constraint ( item_pos ) :
2020-09-15 20:23:50 -04:00
""" Test " less than " constraint (horizontal) creation. """
2018-12-15 23:19:42 -05:00
item_pos . item . constraint ( left_of = ( item_pos . pos1 , item_pos . pos2 ) )
assert 1 == len ( item_pos . item . _constraints )
c = item_pos . item . _constraints [ 0 ]
assert isinstance ( c , LessThanConstraint )
assert 1 == c . smaller
assert 3 == c . bigger
def test_above_constraint ( item_pos ) :
2020-09-15 20:23:50 -04:00
""" Test " less than " constraint (vertical) creation. """
2018-12-15 23:19:42 -05:00
item_pos . item . constraint ( above = ( item_pos . pos1 , item_pos . pos2 ) )
assert 1 == len ( item_pos . item . _constraints )
2018-11-25 16:07:58 -05:00
2018-12-15 23:19:42 -05:00
c = item_pos . item . _constraints [ 0 ]
assert isinstance ( c , LessThanConstraint )
assert 2 == c . smaller
assert 4 == c . bigger