def test_unit_variable(calc): input_should_match( calc, { 'a inch': ('variable', 'a', 'inch'), 'cos(a meter)': ('function_expr', ('variable', 'a', 'meter'), 'cos'), })
def test_unit_quantity(calc): input_should_match( calc, { '2 inch': _i(2, 'inch'), '3e7 km': _f(3e7, 'km'), '0 kblam': _i(0, 'kblam'), })
def test_binop_power_to_quantity_with_unit(red): '''1 m ^ 2 foot''' input_should_match( red, { ('binop_power', ('quantity', _i(1), 'm'), ('quantity', _i(1), 'apple')): (None, _sunit()), })
def test_binop_power_without_unit(red): '''2 m ^ 2''' input_should_match( red, { ('binop_power', ('quantity', ('integer_number', 2), 'm'), ('quantity', ('integer_number', 2), None)): (4, ({('m', 2)}, set())), })
def test_assignment(calc): input_should_match( calc, { 'a = 1': ('assignment', _i(1), 'a'), 'a = 1 ^ 2': ('assignment', ('binop_power', _i(1), _i(2)), 'a'), '42 % a = 1': ('binop_mod', _i(42), ('assignment', _i(1), 'a')), 'cos(a = 1)': ('function_expr', ('assignment', _i(1), 'a'), 'cos'), })
def test_number_only_quantity(calc): input_should_match(calc, { '42': _i(42), '0o3': _i(3), '0': _i(0), '3e2': _f(3e2), '0.3': _f(0.3), })
def test_binop_mod(calc): input_should_match( calc, { '2 % 2': ('binop_mod', _i(2), _i(2)), '1 + 2 % 2': ('binop_plus', _i(1), ('binop_mod', _i(2), _i(2))), '1 / 2 % 2': ('binop_mod', ('binop_divide', _i(1), _i(2)), _i(2)), '1 % 2 ^ 2': ('binop_mod', _i(1), ('binop_power', _i(2), _i(2))), })
def test_basic_binop(red): input_should_match( red, { ('binop_plus', ('quantity', ('integer_number', 1), None), ('quantity', ('integer_number', 2), None)): (3, _sunit()), ('binop_minus', ('quantity', ('integer_number', 2), None), ('quantity', ('integer_number', 1), None)): (1, _sunit()), })
def test_binop_power(calc): input_should_match( calc, { '2 ^ 2': ('binop_power', _i(2), _i(2)), '1 + 2 ^ 2': ('binop_plus', _i(1), ('binop_power', _i(2), _i(2))), '1 / 2 ^ 2': ('binop_divide', _i(1), ('binop_power', _i(2), _i(2))), '1 ^ 2 ^ 2': ('binop_power', _i(1), ('binop_power', _i(2), _i(2))), })
def test_binop_plus(calc): input_should_match( calc, { '2 + 2': ('binop_plus', _i(2), _i(2)), '1 + 2 + 2': ('binop_plus', ('binop_plus', _i(1), _i(2)), _i(2)), '1 + 2 * 2': ('binop_plus', _i(1), ('binop_multiply', _i(2), _i(2))), '1 + 2 ^ 2': ('binop_plus', _i(1), ('binop_power', _i(2), _i(2))), })
def test_reduce_convert_expr(red): input_should_match( red, { ('convert_expr', _i(1), 'm'): (1, _sunit('m')), ('convert_expr', _i(1, 'km'), 'm'): (1000, _sunit('m')), ('convert_expr', ('binop_plus', _i(1, 'm'), _i(2)), 'meters'): (3, _sunit('m')), ('convert_expr', ('convert_expr', _i(1, 'm'), 'meters'), 'm'): (1, _sunit('m')), })
def test_group_expr(calc): input_should_match( calc, { '1 + ( 2 + 3 )': ('binop_plus', _i(1), ('binop_plus', _i(2), _i(3))), '1 / ( 2 + 3 )': ('binop_divide', _i(1), ('binop_plus', _i(2), _i(3))), '( 2 + 3 ) / 1': ('binop_divide', ('binop_plus', _i(2), _i(3)), _i(1)), })
def test_negative_expr(calc): input_should_match( calc, { '-1': ('negative_expr', _i(1)), '-sin(2)': ('negative_expr', ('function_expr', _i(2), 'sin')), '-(2 % 3)': ('negative_expr', ('binop_mod', _i(2), _i(3))), '2 + -3': ('binop_plus', _i(2), ('negative_expr', _i(3))), '2 / -3': ('binop_divide', _i(2), ('negative_expr', _i(3))), '2 ^ -3': ('binop_power', _i(2), ('negative_expr', _i(3))), })
def test_binop_multiply(calc): input_should_match( calc, { '2 * 2': ('binop_multiply', _i(2), _i(2)), '1 + 2 * 2': ('binop_plus', _i(1), ('binop_multiply', _i(2), _i(2))), '1 / 2 * 2': ('binop_multiply', ('binop_divide', _i(1), _i(2)), _i(2)), '1 * 2 ^ 2': ('binop_multiply', _i(1), ('binop_power', _i(2), _i(2))), })
def test_convert_expr(calc): input_should_match( calc, { '1 in meters': ('convert_expr', _i(1), 'meters'), '1 inch in meters': ('convert_expr', _i(1, 'inch'), 'meters'), '1 inch + 2 in meters': ('convert_expr', ('binop_plus', _i(1, 'inch'), _i(2)), 'meters'), '1 inch in meters in lightyears': ('convert_expr', ('convert_expr', _i(1, 'inch'), 'meters'), 'lightyears'), })
def test_function_expr(calc): input_should_match( calc, { 'sin(0.4)': ('function_expr', _f(0.4), 'sin'), 'blow(0.4)': ('function_expr', _f(0.4), 'blow'), 'wat(1+2)': ('function_expr', ( 'binop_plus', _i(1), _i(2), ), 'wat'), })
def test_binop_with_complex_unit(red): input_should_match( red, { # 1 m * 1 m = 1 m^2 ('binop_multiply', ('quantity', ('integer_number', 1), 'm'), ('quantity', ('integer_number', 1), 'm')): (1, ({('m', 2)}, set())), # 1 m * 1 second = 1 (m * second) ('binop_multiply', ('quantity', ('integer_number', 1), 'm'), ('quantity', ('integer_number', 1), 'second')): (1, ({('m', 1), ('s', 1)}, set())), # 1 m / 1 m = 1 ('binop_divide', ('quantity', ('integer_number', 1), 'm'), ('quantity', ('integer_number', 1), 'm')): (1, _sunit()), # 1 m / 1 second = 1 (m / second) ('binop_divide', ('quantity', ('integer_number', 1), 'm'), ('quantity', ('integer_number', 1), 'second')): (1, ({('m', 1)}, {('s', 1)})), })
def test_basic_quantity(red): input_should_match(red, { _i(1, 'm'): (1, _sunit('m')), })
def test_unitless_quantity(red): input_should_match(red, { _i(1): (1, _sunit()), _i(0): (0, _sunit()), _f(4e1): (4e1, _sunit()), })
def test_negative_expr(red): input_should_match( red, { ('negative_expr', ('quantity', ('integer_number', 1), None)): (-1, _sunit()) })