def test_a_lot_of_operations(self): self.assertAlmostEqual( calculate('3*(5+(39+(18*3/13)/7)*6-3)/0.999'), 719.4007194007193 ) self.assertAlmostEqual( calculate('(3 * (5 + ((39 + (((18 * 3) / 13) / 7)) * 6) - 3)) / 0.999'), 719.4007194007193 )
def test_integer(self): self.assertEqual(calculate('1'), 1) self.assertEqual(calculate('1 * 1'), 1) self.assertEqual(calculate('1 / 1'), 1) self.assertEqual(calculate('2 / 2'), 1) self.assertEqual(calculate('1 + 1'), 2) self.assertEqual(calculate('1 * 2'), 2) self.assertEqual(calculate('4 / 2'), 2)
def test_operator_precedence(self): self.assertEqual(calculate('2 + 2 - 2 + 2'), 4) self.assertEqual(calculate('2 + 2 * 2 - 2'), 4) self.assertEqual(calculate('2 + 2 / 2 - 2'), 1) self.assertEqual(calculate('2 + 2 * 2 / 2 - 2'), 2) self.assertAlmostEqual(calculate('1 * 2 / 3'), 0.66666666) self.assertAlmostEqual(calculate('1 / 2 * 3'), 1.5)
def test_invalid_operand(self): with self.assertRaises(SyntaxError): calculate('2 * pi')
def test_non_string_input(self): self.assertEqual(calculate(1), 1) self.assertEqual(calculate(1.9999), 1.9999) with self.assertRaises(SyntaxError): calculate(object)
def test_abrites_example(self): self.assertEqual(calculate('((1 + 3) / 3.14) * 4 - 5.1'), -0.004458598726114538)
def test_parentheses(self): self.assertEqual(calculate('(1)'), 1) self.assertEqual(calculate('(1 + 1)'), 2)
def test_unary_not_supported(self): with self.assertRaises(SyntaxError): calculate('-1') with self.assertRaises(SyntaxError): calculate('(-1 + 3)')
def test_float(self): self.assertAlmostEqual(calculate('2.2 + 2.2'), 4.4) self.assertAlmostEqual(calculate('2.2 - 2.2'), 0) self.assertAlmostEqual(calculate('2.2 * 2.2'), 4.84) self.assertAlmostEqual(calculate('2.2 / 2.2'), 1)