2020-10-18 13:01:40 +02:00
import pytest
from gaphas . matrix import Matrix
2020-10-18 20:32:37 +02:00
from gaphas . position import MatrixProjection , Position
2020-10-18 16:01:26 +02:00
from gaphas . solver import Solver , Variable
@pytest.fixture
def solver ( ) :
return Solver ( )
2020-10-18 13:01:40 +02:00
2020-10-18 20:32:37 +02:00
@pytest.mark.parametrize ( " position " , [ ( 0 , 0 ) , ( 1 , 2 ) ] )
def test_position ( position ) :
pos = Position ( position )
assert position [ 0 ] == pos . x
assert position [ 1 ] == pos . y
2020-10-18 13:01:40 +02:00
def test_matrix_projection_exposes_variables ( ) :
proj = MatrixProjection ( Position ( ( 0 , 0 ) ) , Matrix ( ) )
assert isinstance ( proj . x , Variable )
assert isinstance ( proj . y , Variable )
@pytest.mark.parametrize (
" position,matrix,result " ,
[
2020-10-18 16:01:26 +02:00
[ ( 0 , 0 ) , Matrix ( 1 , 0 , 0 , 1 , 0 , 0 ) , ( 0 , 0 ) ] ,
[ ( 2 , 4 ) , Matrix ( 2 , 0 , 0 , 1 , 2 , 3 ) , ( 6 , 7 ) ] ,
[ ( 2 , 4 ) , Matrix ( 2 , 0 , 0 , 1 , 2 , 3 ) , ( 6 , 7 ) ] ,
2020-10-18 13:01:40 +02:00
] ,
)
2020-10-18 17:16:51 +02:00
def test_projection_updates_when_original_is_changed ( solver , position , matrix , result ) :
2020-10-18 16:01:26 +02:00
pos = Position ( ( 0 , 0 ) )
proj = MatrixProjection ( pos , matrix )
solver . add_constraint ( proj )
2020-10-18 17:16:51 +02:00
solver . solve ( )
2020-10-18 16:01:26 +02:00
pos . x , pos . y = position
solver . solve ( )
2020-10-18 13:01:40 +02:00
assert proj . x == result [ 0 ]
assert proj . y == result [ 1 ]
@pytest.mark.parametrize (
" position,matrix,result " ,
[
2020-10-18 16:01:26 +02:00
[ ( 0 , 0 ) , Matrix ( 1 , 0 , 0 , 1 , 0 , 0 ) , ( 0 , 0 ) ] ,
[ ( 2 , 0 ) , Matrix ( 1 , 0 , 0 , 1 , 0 , 0 ) , ( 2 , 0 ) ] ,
[ ( 2 , 0 ) , Matrix ( 1 , 0 , 0 , 1 , 4 , 3 ) , ( - 2 , - 3 ) ] ,
[ ( 1 , 2 ) , Matrix ( 1 , 0 , 0 , 1 , 4 , 3 ) , ( - 3 , - 1 ) ] ,
2020-10-18 13:01:40 +02:00
] ,
)
2020-10-18 17:16:51 +02:00
def test_original_updates_when_projection_is_changed ( solver , position , matrix , result ) :
2020-10-18 13:01:40 +02:00
pos = Position ( ( 0 , 0 ) )
2020-10-18 16:01:26 +02:00
proj = MatrixProjection ( pos , matrix )
solver . add_constraint ( proj )
2020-10-18 17:16:51 +02:00
solver . solve ( )
2020-10-18 16:01:26 +02:00
2020-10-18 13:01:40 +02:00
proj . x , proj . y = position
2020-10-18 16:01:26 +02:00
solver . solve ( )
print ( pos )
print ( proj . x , proj . y )
2020-10-18 13:01:40 +02:00
assert pos . x == result [ 0 ]
assert pos . y == result [ 1 ]
2020-10-18 17:16:51 +02:00
def test_projection_updates_when_matrix_is_changed ( solver ) :
pos = Position ( ( 0 , 0 ) )
matrix = Matrix ( )
proj = MatrixProjection ( pos , matrix )
solver . add_constraint ( proj )
solver . solve ( )
matrix . translate ( 2 , 3 )
solver . solve ( )
assert proj . x == 2
assert proj . y == 3