示例#1
0
    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
        )
示例#2
0
    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)
示例#3
0
 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)
示例#4
0
 def test_invalid_operand(self):
     with self.assertRaises(SyntaxError):
         calculate('2 * pi')
示例#5
0
    def test_non_string_input(self):
        self.assertEqual(calculate(1), 1)
        self.assertEqual(calculate(1.9999), 1.9999)

        with self.assertRaises(SyntaxError):
            calculate(object)
示例#6
0
 def test_abrites_example(self):
     self.assertEqual(calculate('((1 + 3) / 3.14) * 4 - 5.1'), -0.004458598726114538)
示例#7
0
 def test_parentheses(self):
     self.assertEqual(calculate('(1)'), 1)
     self.assertEqual(calculate('(1 + 1)'), 2)
示例#8
0
 def test_unary_not_supported(self):
     with self.assertRaises(SyntaxError):
         calculate('-1')
     with self.assertRaises(SyntaxError):
         calculate('(-1 + 3)')
示例#9
0
 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)