def test_constructor_should_raise_TypeError_on_wrong_number_of_args(Vector, data): with raises(TypeError): Vector(*data)
def test_should_be_able_to_add_vectors_in_place(Vector, names, data_l, data_r): v = Vector(*data_l) v += Vector(*data_r) for name, datum_l, datum_r in zip(names, data_l, data_r): assert getattr(v, name) == datum_l + datum_r
def test_should_be_able_to_multiply_vector_by_scalar_on_left(Vector, names, data, s): v = s * Vector(*data) for name, datum in zip(names, data): assert getattr(v, name) == datum * s
def test_should_be_able_to_set_components_by_number(Vector, names, data, data_new): v = Vector(*data) for dim, (datum, new_datum) in enumerate(zip(data, data_new)): assert v[dim] != new_datum v[dim] = new_datum assert v[dim] == new_datum
def test_should_be_able_to_access_components_by_number(Vector, names, data, data_new): v = Vector(*data) for dim, datum in enumerate(data): assert v[dim] == datum
def test_should_be_able_to_set_components_by_name(Vector, names, data, data_new): v = Vector(*data) for name, datum, new_datum in zip(names, data, data_new): assert getattr(v, name) != new_datum setattr(v, name, new_datum) assert getattr(v, name) == new_datum
def test_should_be_able_to_get_vector_components_by_name(Vector, names, data, data_new): v = Vector(*data) for name, datum in zip(names, data): assert getattr(v, name) == datum
from math import sqrt from pytest import mark, raises from vector_factory import Vector, Vector2D, Vector3D component_access_test_data = ( "Vector names data data_new".split(), ((Vector2D, 'xy', (1, 2), (3, 4)), (Vector3D, 'xyz', (-2, 7, 3), (4, 3, 8)), (Vector('abcd'), 'abcd', (8, 6, 5, 2), (1, 7, 8, 7)))) @mark.parametrize(*component_access_test_data) def test_should_be_able_to_get_vector_components_by_name(Vector, names, data, data_new): v = Vector(*data) for name, datum in zip(names, data): assert getattr(v, name) == datum @mark.parametrize(*component_access_test_data) def test_should_be_able_to_set_components_by_name(Vector, names, data, data_new): v = Vector(*data) for name, datum, new_datum in zip(names, data, data_new): assert getattr(v, name) != new_datum setattr(v, name, new_datum) assert getattr(v, name) == new_datum @mark.parametrize(*component_access_test_data) def test_should_be_able_to_access_components_by_number(Vector, names, data, data_new): v = Vector(*data) for dim, datum in enumerate(data):