12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import pytest
- import copy
- import finoex
- @pytest.mark.parametrize(('params', 'kwargs', 'expected_value', 'expected_unit'), [
- [[1.0, u'dm'], {}, 1.0, u'dm'],
- [[-4.0], {'unit': u'cm'}, -4.0, u'cm'],
- [[], {'value': 2.3, 'unit': u'cm'}, 2.3, u'cm'],
- ])
- def test_init(params, kwargs, expected_value, expected_unit):
- f = finoex.Distance(*params, **kwargs)
- assert type(f.value) == type(expected_value)
- assert type(f.unit) == type(expected_unit)
- assert f.value == expected_value
- assert f.unit == expected_unit
- @pytest.mark.parametrize(('params', 'kwargs'), [
- [[], {}],
- [[None], {}],
- [[None, u'kg'], {}],
- [[1, 'cm'], {}],
- [[], {'unit': u'cm'}],
- [[1], {'unit': 'cm'}],
- [[1, u'cm'], {}],
- [[1], {'unit': u'cm'}],
- [[(-1, 3), u'cm'], {}],
- [[], {'value': (3, -1), 'unit': u'cm'}],
- [[1.2], {'value': 2.3, 'unit': u'cm'}],
- ])
- def test_init_fail(params, kwargs):
- with pytest.raises(Exception):
- finoex.Distance(*params, **kwargs)
- @pytest.mark.parametrize(('factor_a', 'factor_b', 'product'), [
- [finoex.Distance(5.0, u'cm'), -0.5, finoex.Distance(-2.5, u'cm')],
- [finoex.Distance(1.0, u'kg'), 10, finoex.Distance(10.0, u'kg')],
- ])
- def test_scalar_figure_mul(factor_a, factor_b, product):
- factor_a_copy = copy.deepcopy(factor_a)
- factor_b_copy = copy.deepcopy(factor_b)
- assert (factor_a * factor_b) == product
- assert factor_a_copy == factor_a
- assert factor_b_copy == factor_b
- @pytest.mark.parametrize(('factor_a', 'factor_b'), [
- [finoex.Distance(5.0, u'cm'), finoex.Distance(2.0, u'cm')],
- [finoex.Distance(5.0, u'cm'), '23'],
- ])
- def test_figure_mul_fail(factor_a, factor_b):
- with pytest.raises(Exception):
- (factor_a * factor_b)
- @pytest.mark.parametrize(('dist', 'expected_metres'), [
- [finoex.Distance(12.3, 'm'), 12.3],
- [finoex.Distance(12.3, 'km'), 12300],
- ])
- def test_metres(dist, expected_metres):
- metres = dist.metres
- assert 'm' == metres.unit
- assert expected_metres == metres.value
|