def test_divisions_errors(): """Check divisions exceptions.""" with pytest.raises(NotImplementedError) as excinfo: Number(6, unit=Unit('cm', exponent=Number(2))) \ / Number(6, unit=Unit('dm', exponent=Number(3))) assert str(excinfo.value) == 'Cannot yet handle a ' \ 'division of Number(\'6 cm^2\') ' \ 'by Number(\'6 dm^3\').'
def test_printing(): """Check printing.""" assert Unit('cm', exponent=2).uiprinted == 'cm^2' assert Unit('cm', exponent=2).printed == r'\si{cm^{2}}' assert str(Unit('cm')) == 'cm' assert str(Unit('cm', exponent=2)) == 'cm^2' required.package['eurosym'] = False Unit(r'\officialeuro').printed assert required.package['eurosym']
def test_Number_instanciation(): """Check Number instanciations.""" assert Number(Number(4)).unit is None assert Number(Number(4, unit='cm')).unit == Unit('cm') assert Number(Number(4), unit='cm').unit == Unit('cm') assert Number(Number(4, unit='dm'), unit='cm').unit == Unit('cm') assert Number(4, unit='cm').unit == Unit('cm') assert Number(Number(4, unit='dm'), unit=None).unit is None assert Number(Number(4, unit='dm'), unit='undefined').unit == Unit('dm')
def test_physical_quantity(): """Check physical_quantity()""" assert physical_quantity('mm') == 'length' assert physical_quantity('hL') == 'capacity' assert physical_quantity('pg') == 'mass' assert physical_quantity(Unit('pg')) == 'mass' assert physical_quantity(Unit('mm', exponent=2)) == 'area' assert physical_quantity(Unit('mm', exponent=3)) == 'volume' assert physical_quantity('undefined') is None assert physical_quantity(r'\officialeuro') == 'currency' assert physical_quantity(r'\textdegree') == 'angle'
def test_multiplications_errors(): """Check subtractions exceptions.""" with pytest.raises(TypeError) as excinfo: 8 * Sign('-') assert str(excinfo.value) == 'Cannot multiply a Sign by a <class \'int\'>.' with pytest.raises(NotImplementedError) as excinfo: Number(6, unit=Unit('cm', exponent=Number(2))) \ * Number(6, unit=Unit('dm', exponent=Number(3))) assert str(excinfo.value) == 'Cannot yet handle a ' \ 'multiplication of Number(\'6 cm^2\') ' \ 'by Number(\'6 dm^3\').'
def test_printing(): """Check printing is correct.""" assert Number('8.6').printed == '8.6' assert Number('8.60').printed == '8.60' locale.setlocale(locale.LC_ALL, LOCALE_FR) assert locale.str(Decimal('8.6')) == '8,6' assert Number('8.6').printed == '8,6' assert Number('8.6').uiprinted == '8.6' assert Number('8.6').printed == '8,6' assert Number('8.6', unit='cm').printed == r'\SI{8,6}{cm}' locale.setlocale(locale.LC_ALL, LOCALE_US) assert Number('8.6').imprint(start_expr=False) == '+8.6' required.package['siunitx'] = False n = Number('9', unit='cm') assert n.printed == r'\SI{9}{cm}' assert required.package['siunitx'] assert n.uiprinted == '9 cm' assert str(n) == '9 cm' n = Number('9') assert str(n) == '9' assert Number(60, unit=Unit('cm', exponent=Number(2))).uiprinted \ == '60 cm^2' assert Number('3.6', unit='mL').quantize(Number('0.01')).printed \ == r'\SI{3.60}{mL}' required.package['eurosym'] = False assert Number('0.70', unit=r'\officialeuro').quantize(Number('0.01'))\ .printed == r'\SI{0.70}{\officialeuro}' assert required.package['eurosym'] n = Number('38', unit=r'\textdegree') assert n.printed == r'\ang{38}'
def test_AngleDecoration(): assert repr(AngleDecoration()) == 'AngleDecoration(variety=single; '\ 'hatchmark=None; label=default; color=None; thickness=thick; '\ 'radius=0.25 cm; eccentricity=2.6)' ad = AngleDecoration(radius=Number(1, unit='cm')) assert ad.arrow_tips is None assert repr(ad) == \ 'AngleDecoration(variety=single; '\ 'hatchmark=None; label=default; color=None; thickness=thick; '\ 'radius=1 cm; eccentricity=1.4)' ad.radius = Number(2, unit='cm') assert repr(ad) == \ 'AngleDecoration(variety=single; '\ 'hatchmark=None; label=default; color=None; thickness=thick; '\ 'radius=2 cm; eccentricity=1.2)' assert AngleDecoration().tikz_attributes() \ == '[draw, thick, angle radius = 0.25 cm]' assert AngleDecoration(color='green', thickness='thin').tikz_attributes() \ == '[draw, thin, angle radius = 0.25 cm, green]' assert AngleDecoration(radius=Number('0.5', unit=Unit('cm'))) \ .tikz_attributes() == '[draw, thick, angle radius = 0.5 cm]' with pytest.raises(ValueError) as excinfo: AngleDecoration(radius=Number(2, unit='cm'), gap=None) assert str(excinfo.value) == 'Cannot calculate the eccentricity if gap '\ 'is None.' with pytest.raises(TypeError) as excinfo: AngleDecoration(radius='2 cm') assert str(excinfo.value) == 'Expected a number as radius. Got ' \ '<class \'str\'> instead.' with pytest.raises(TypeError) as excinfo: AngleDecoration(gap='2 cm') assert str(excinfo.value) == 'The gap value must be None or a number. '\ 'Found \'2 cm\' instead (type: <class \'str\'>).' with pytest.raises(TypeError) as excinfo: AngleDecoration().tikz_attributes(radius_coeff='a') assert str(excinfo.value) == 'radius_coeff must be a number, '\ 'found <class \'str\'> instead.' with pytest.raises(TypeError) as excinfo: AngleDecoration(hatchmark='unknown') assert str(excinfo.value) == 'AngleDecoration\'s hatchmark can be None, '\ '\'singledash\', \'doubledash\' or \'tripledash\'. ' \ 'Found \'unknown\' instead (type: <class \'str\'>).' with pytest.raises(TypeError) as excinfo: AngleDecoration(eccentricity='a') assert str(excinfo.value) == 'The eccentricity of an AngleDecoration '\ 'must be None or a Number. Found <class \'str\'> instead.' with pytest.raises(RuntimeError) as excinfo: AngleDecoration().generate_tikz('A', 'B') assert str(excinfo.value) == 'Three Points\' names must be provided to '\ 'generate the AngleDecoration. Found 2 arguments instead.' with pytest.raises(TypeError) as excinfo: AngleDecoration(do_draw='a') assert str(excinfo.value) == 'do_draw must be a boolean; '\ 'got <class \'str\'> instead.'
def test_multiplications(): """Check __mul__, __rmul__, __imul__.""" assert isinstance(Number(4) * Number(4), Number) assert isinstance(4 * Number(4), Number) assert isinstance(Number(4) * 4, Number) n = Number(4) n *= Number(4) n *= 4 n = Number(6) p = Number(7) assert n * p == Number(42) assert (n * p).printed == '42' assert isinstance(n, Number) n = Number(6, unit='cm') p = Number(7) assert n * p == Number(42, unit='cm') n = Number(6) p = Number(7, unit='cm') assert n * p == Number(42, unit='cm') n = Number(6, unit='cm') p = Number(7, unit='cm') assert n * p == Number(42, unit=Unit('cm', exponent=Number(2))) n = Number(6, unit='cm') p = Number(7, unit=Unit('cm', exponent=Number(2))) assert n * p == Number(42, unit=Unit('cm', exponent=Number(3))) n = Number(6, unit=Unit('cm', exponent=Number(2))) p = Number(7, unit='cm') assert n * p == Number(42, unit=Unit('cm', exponent=Number(3))) n = Number(6, unit='W') p = Number(7, unit='h') assert n * p == Number(42, unit=Unit('W.h')) n = Number(6, unit='cm') p = Number(7, unit=Unit('h', exponent=Number(2))) assert n * p == Number(42, unit=Unit('cm.h', exponent=Number(2))) assert (n * p).printed == r'\SI{42}{cm.h^{2}}' n = Number(6, unit=Unit('cm', exponent=Number(2))) p = Number(7, unit='h') assert n * p == Number(42, unit=Unit('h.cm', exponent=Number(2)))
def test_sides_labeling(): """Check Rectangle's sides' labeling.""" r = Rectangle(name='TIDY', width=2, length=3) with pytest.raises(ValueError) as excinfo: r.lbl_area assert str(excinfo.value) == 'No width has been set as a Number.' r.setup_labels([Number(4, unit='cm'), '15 cm']) with pytest.raises(ValueError) as excinfo: r.lbl_length assert str(excinfo.value) == 'No length has been set as a Number.' with pytest.raises(ValueError) as excinfo: r.lbl_area assert str(excinfo.value) == 'No length has been set as a Number.' r.setup_labels([Number(4, unit='cm'), Number(15, unit='cm')]) assert r.lbl_width.uiprinted == '4 cm' assert r.lbl_length.uiprinted == '15 cm' assert r.lbl_area == Number(60, unit=Unit('cm', exponent=Number(2))) assert r.lbl_area.uiprinted == '60 cm^2' assert [s.label_mask for s in r.sides] == [' ', None, None, ' '] for s in r.sides: s.unlock_label() r.sides[3].label = Number(5, unit='cm') with pytest.raises(ValueError) as excinfo: r.lbl_area assert str(excinfo.value) == 'Two different labels have been set '\ 'for the width: Number(\'4 cm\') and Number(\'5 cm\').' r.sides[3].label = Number(4, unit='cm') r.sides[2].label = Number(12, unit='cm') with pytest.raises(ValueError) as excinfo: r.lbl_area assert str(excinfo.value) == 'Two different labels have been set '\ 'for the length: Number(\'15 cm\') and Number(\'12 cm\').' r.sides[0].label = 'undefined' r.sides[1].label = 'undefined' assert r.lbl_area.uiprinted == '48 cm^2' r.sides[0].label = Number(3, unit='cm') r.sides[1].label = Number(17, unit='cm') r.sides[2].label = 'undefined' r.sides[3].label = 'undefined' assert r.lbl_area.uiprinted == '51 cm^2'
def test_divisions(): """Check __truediv__, __floordiv__, __mod__, __divmod__ + their __r*__.""" assert isinstance(Number(4) / Number(4), Number) assert isinstance(4 / Number(4), Number) assert isinstance(Number(4) / 4, Number) assert isinstance(Number(4) // Number(4), Number) assert isinstance(4 // Number(4), Number) assert isinstance(Number(4) // 4, Number) assert isinstance(Number(4) % Number(4), Number) assert isinstance(4 % Number(4), Number) assert isinstance(Number(4) % 4, Number) assert divmod(Number(42), Number(15)) == (Number('2'), Number('12')) assert isinstance(divmod(Number(42), Number(15))[0], Number) assert isinstance(divmod(Number(42), Number(15))[1], Number) assert divmod(42, Number(15)) == (Number('2'), Number('12')) assert isinstance(divmod(42, Number(15))[0], Number) assert isinstance(divmod(42, Number(15))[1], Number) assert divmod(Number(42), 15) == (Number('2'), Number('12')) assert isinstance(divmod(Number(42), 15)[0], Number) assert isinstance(divmod(Number(42), 15)[1], Number) n = Number(4) assert isinstance(n, Number) n /= Number(4) assert isinstance(n, Number) n = Number(256) n //= Number(4) assert isinstance(n, Number) n = Number(256) n %= Number(3) assert isinstance(n, Number) n = Number(4) n /= 4 assert isinstance(n, Number) n = Number(256) n //= 4 assert isinstance(n, Number) n = Number(256) n %= 3 assert isinstance(n, Number) assert Number(4) / Sign('-') == -4 assert Sign('-') / Number(4) == Number('-0.25') assert Number(4) // Sign('-') == -4 assert Sign('-') // Number(4) == Number('0') n = Number(7, unit='cm') p = Number(4) assert n / p == Number('1.75', unit='cm') n = Number(7) p = Number(4, unit='cm') assert n / p == Number('1.75', unit=Unit('cm', exponent=Number(-1))) n = Number(7, unit='cm') p = Number(4, unit='cm') assert n / p == Number('1.75') n = Number(7, unit='km') p = Number(4, unit='h') assert n / p == Number('1.75', unit='km/h') n = Number(7, unit=Unit('cm', exponent=Number(2))) p = Number(4, unit='cm') assert n / p == Number('1.75', unit='cm') n = Number(7, unit='cm') p = Number(4, unit=Unit('cm', exponent=Number(2))) assert n / p == Number('1.75', unit=Unit('cm', exponent=Number(-1))) n = 7 p = Number(4, unit=Unit('cm', exponent=Number(2))) assert n / p == Number('1.75', unit=Unit('cm', exponent=Number(-2))) n = Number(7, unit='cm') p = 4 assert n / p == Number('1.75', unit='cm') n = 7 p = Number(4, unit='cm') assert n / p == Number('1.75', unit=Unit('cm', exponent=Number(-1)))
def test_convert_mL_to_dm3(): """Check conversions between volume units.""" i = Number(6, unit='mL') j = i.converted_to(Unit('dm', exponent=3)) assert j == Number(0.006, unit=Unit('dm', exponent=3))
def test_Unit(): """Check the Unit class.""" assert Unit('cm').printed == r'\si{cm}' assert Unit('cm', exponent=Number(2)).printed == r'\si{cm^{2}}' u = Unit('cm') assert Unit(u).printed == r'\si{cm}' assert Unit(u, exponent=2).printed == r'\si{cm^{2}}' assert Unit(u, exponent=Number(3)).printed == r'\si{cm^{3}}' u = Unit('cm', exponent=2) assert Unit(u).printed == r'\si{cm^{2}}' v = Unit('cm', exponent=2) assert u == v w = Unit(v, exponent=3) assert v != w w = Unit(v, exponent=2) assert v == w assert u != 6
def test_Unit_errors(): """Check the Unit class exceptions.""" with pytest.raises(TypeError) as excinfo: Unit(6) assert str(excinfo.value) == 'content must be a str or a Unit. ' \ 'Got <class \'int\'> instead.'
def test_repr(): """Check __repr__.""" assert repr(Unit('cm')) == 'Unit(\'cm\')' assert repr(Unit('cm', exponent=2)) == 'Unit(\'cm\'^Number(\'2\'))'
def test_convert_area_to_greater_unit(): """Check conversions between area units.""" i = Number(6, unit=Unit('dm', exponent=2)) j = i.converted_to(Unit('m', exponent=2)) assert j == Number(0.06, unit=Unit('m', exponent=2))
def test_convert_volume_to_geater_unit(): """Check conversions between volume units.""" i = Number(6, unit=Unit('dm', exponent=3)) j = i.converted_to(Unit('m', exponent=3)) assert j == Number(0.006, unit=Unit('m', exponent=3))
def test_convert_dL_to_cm3(): """Check conversions between volume units.""" i = Number(6, unit='dL') j = i.converted_to(Unit('cm', exponent=3)) assert j == Number(600, unit=Unit('cm', exponent=3))
def test_convert_dm3_to_mL(): """Check conversions between volume units.""" i = Number(6, unit=Unit('dm', exponent=3)) j = i.converted_to('mL') assert j == Number(6000, unit='mL')