Page MenuHomec4science

test_matrix.py
No OneTemporary

File Metadata

Created
Mon, May 13, 03:37

test_matrix.py

import unittest
from os import sep, path
from os.path import isdir, dirname
from datatypes.matrix import Matrix
from datatypes.transkriptionField import TranskriptionField
class TestMatrix(unittest.TestCase):
def setUp(self):
self.x = 219.4058
self.y = 106.4634
self.matrix_string = 'matrix(1 0 0 1 {} {})'.format(str(self.x), str(self.y))
self.test_data_dir = dirname(__file__) + sep + 'test_data'
if not isdir(self.test_data_dir):
self.test_data_dir = dirname(dirname(__file__)) + sep + 'test_data'
self.test_file = self.test_data_dir + sep + 'test_ai.svg'
self.rotation_angle = 20
self.rotation_matrix_string = 'matrix(0.94 0.342 -0.342 0.94 0 0)'
def test_Matrix(self):
matrix = Matrix(self.matrix_string)
self.assertEqual(matrix.getX(), self.x)
self.assertEqual(matrix.add2X(1), self.x + 1)
self.assertEqual(matrix.getY(), self.y)
def test_Matrix_rotation(self):
rotation_string = 'rotate({})'.format(self.rotation_angle)
rotation_stringC = 'rotate(-{})'.format(self.rotation_angle)
matrixA = Matrix(rotation_string)
matrixB = Matrix(self.rotation_matrix_string)
matrixC = Matrix(rotation_stringC)
self.assertEqual(matrixA.matrix[Matrix.A], matrixB.matrix[Matrix.A])
self.assertEqual(matrixA.matrix[Matrix.B], matrixB.matrix[Matrix.B])
self.assertEqual(matrixA.matrix[Matrix.C], matrixB.matrix[Matrix.C])
self.assertEqual(matrixA.matrix[Matrix.D], matrixB.matrix[Matrix.D])
self.assertEqual(matrixA.matrix[Matrix.E], matrixB.matrix[Matrix.E])
self.assertEqual(matrixA.matrix[Matrix.F], matrixB.matrix[Matrix.F])
self.assertEqual(matrixA.toString(), self.rotation_matrix_string)
self.assertEqual(matrixC.toCSSTransformString(), 'rotate(-{}deg)'.format(self.rotation_angle))
def test_get_rotation_direction(self):
rotation_string = 'rotate(-{})'.format(self.rotation_angle)
matrixA = Matrix(rotation_string)
matrixB = Matrix(self.rotation_matrix_string)
matrixC = Matrix(self.matrix_string)
self.assertEqual(matrixA.get_rotation_direction(), Matrix.UP)
self.assertEqual(matrixB.get_rotation_direction(), Matrix.DOWN)
self.assertEqual(matrixC.get_rotation_direction(), Matrix.STRAIGHT)
def test_isRotationMatrix(self):
rotation_string = 'rotate({})'.format(self.rotation_angle)
matrixA = Matrix(rotation_string)
self.assertEqual(matrixA.isRotationMatrix(), True)
matrixB = Matrix(self.matrix_string)
self.assertEqual(matrixB.isRotationMatrix(), False)
def test_toCSSTransformString(self):
rotation_string = 'rotate({})'.format(self.rotation_angle)
matrixA = Matrix(rotation_string)
self.assertEqual(matrixA.toCSSTransformString(), 'rotate({}deg)'.format(self.rotation_angle))
matrixB = Matrix(self.rotation_matrix_string)
self.assertEqual(matrixB.toCSSTransformString(), 'rotate({}deg)'.format(self.rotation_angle))
def test_Matrix_Exception(self):
with self.assertRaises(Exception):
Matrix('matrix({})'.format(' '.join([ '0.0' for i in range(5)])))
def test_Matrix_TranskriptionField(self):
tf = TranskriptionField(self.test_file)
matrix = Matrix(self.matrix_string, transkription_field=tf)
self.assertEqual(round(matrix.getX(), 3) , 28.706)
self.assertEqual(round(matrix.getY(), 3) , 31.563)
def test_get_transformed_positions(self):
# Test relies on the example from "https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform"
x = 10
y = 10
width = 30
height = 20
matrix = Matrix(transform_matrix_string='matrix(3 1 -1 3 30 40)')
new_x, new_y, new_width, new_height = matrix.get_transformed_positions(x=x, y=y, width=width, height=height)
self.assertEqual(new_x, 50)
self.assertEqual(new_y, 80)
self.assertEqual(new_width, 90)
self.assertEqual(new_height, 60)
def test_is_matrix_horizontal(self):
matrix = Matrix(transform_matrix_string='matrix(3 1 -1 3 30 40)')
self.assertEqual(matrix.is_matrix_horizontal(), False)
matrix = Matrix(transform_matrix_string='matrix(1 0 0 1 30 40)')
self.assertEqual(matrix.is_matrix_horizontal(), True)
def test_is_part_of_transkription_field(self):
tf = TranskriptionField(self.test_file)
matrix_string = 'matrix(1 0 0 1 244.1211 91.7134)'
self.assertEqual(Matrix.IS_PART_OF_TRANSKRIPTION_FIELD(matrix_string, tf), True)
matrix_string = 'matrix(1 0 0 1 244.1211 51.7134)'
self.assertEqual(Matrix.IS_PART_OF_TRANSKRIPTION_FIELD(matrix_string, tf), False)
matrix_string = 'matrix(1 0 0 1 44.1211 91.7134)'
self.assertEqual(Matrix.IS_PART_OF_TRANSKRIPTION_FIELD(matrix_string, tf), False)
matrix_string = 'matrix(1 0 0 1 244.1211 891.7134)'
self.assertEqual(Matrix.IS_PART_OF_TRANSKRIPTION_FIELD(matrix_string, tf), False)
matrix_string = 'matrix(1 0 0 1 844.1211 91.7134)'
self.assertEqual(Matrix.IS_PART_OF_TRANSKRIPTION_FIELD(matrix_string, tf), False)
def test_is_nearx_tf(self):
tf = TranskriptionField(self.test_file)
matrix_string = 'matrix(1 0 0 1 180.8755 315.9131)'
self.assertEqual(Matrix.IS_NEARX_TRANSKRIPTION_FIELD(matrix_string, tf), True)
matrix_string = 'matrix(1 0 0 1 100.8755 315.9131)'
self.assertEqual(Matrix.IS_NEARX_TRANSKRIPTION_FIELD(matrix_string, tf), False)
def test_do_conversion_factors_differ(self):
self.assertEqual(Matrix.DO_CONVERSION_FACTORS_DIFFER(None, None), False)
matrix_a = Matrix('matrix(1 0 0 1 180.8755 315.9131)')
self.assertEqual(Matrix.DO_CONVERSION_FACTORS_DIFFER(matrix_a, None), True)
matrix_b = Matrix('matrix(1 0 0 1 100.8755 315.9131)')
self.assertEqual(Matrix.DO_CONVERSION_FACTORS_DIFFER(matrix_a, matrix_b), False)
matrix_b = Matrix('matrix(0 0 0 1 100.8755 315.9131)')
self.assertEqual(Matrix.DO_CONVERSION_FACTORS_DIFFER(matrix_a, matrix_b), True)
matrix_b = Matrix('matrix(1 1 0 1 100.8755 315.9131)')
self.assertEqual(Matrix.DO_CONVERSION_FACTORS_DIFFER(matrix_a, matrix_b), True)
matrix_b = Matrix('matrix(1 0 1 1 100.8755 315.9131)')
self.assertEqual(Matrix.DO_CONVERSION_FACTORS_DIFFER(matrix_a, matrix_b), True)
matrix_b = Matrix('matrix(1 0 0 0 100.8755 315.9131)')
self.assertEqual(Matrix.DO_CONVERSION_FACTORS_DIFFER(matrix_a, matrix_b), True)
def test_clone_transformation_matrix(self):
matrix_a = Matrix(matrix_list=[ 1, 0, 0, 1, 180.8755, 315.9131 ])
matrix_b = matrix_a.clone_transformation_matrix()
self.assertEqual(Matrix.DO_CONVERSION_FACTORS_DIFFER(matrix_a, matrix_b), False)
self.assertEqual(matrix_b.matrix[Matrix.E], 0)
self.assertEqual(matrix_b.matrix[Matrix.F], 0)
def test_toString(self):
matrix_string = 'matrix(1.0 0.0 0.0 1.0 180.8755 315.9131)'
matrix = Matrix(matrix_string)
self.assertEqual(matrix.toString(), matrix_string)
def test_get_semanticAndDataDict(self):
matrix = Matrix('rotate(20)')
#self.assertEqual(matrix.get_data_dictionary()['body'].get('matrix'), matrix.matrix)
if __name__ == "__main__":
unittest.main()

Event Timeline