Пример #1
0
    def testAdd(self):
        x = prefix("1 + 2")
        self.assertEqual(x, ("+", 1, 2))

        x = prefix("1 + 2 + 3")
        self.assertEqual(x, ("+", ("+", 1, 2), 3))

        x = prefix("1 + 2 + 3 + 4")
        self.assertEqual(x, ("+", ("+", ("+", 1, 2), 3), 4))
Пример #2
0
    def testParenNumber(self):
        x = prefix("( 1 )")
        self.assertEqual(x, 1)

        x = prefix("( 10 )")
        self.assertEqual(x, 10)

        x = prefix("( 999 )")
        self.assertEqual(x, 999)
Пример #3
0
    def testLetter(self):
        x = prefix("a")
        self.assertEqual(x, "a")

        x = prefix("z")
        self.assertEqual(x, "z")

        x = prefix("a + b + c + d")
        self.assertEqual(x, ("+", ("+", ("+", "a", "b"), "c"), "d"))
Пример #4
0
    def testParenAdd(self):
        x = prefix("( 3 + 2 )")
        self.assertEqual(x, ("+", 3, 2))

        x = prefix("5 + ( 3 + 2 )")
        self.assertEqual(x, ("+", 5, ("+", 3, 2)))

        x = prefix("( 3 + 2 ) + 5")
        self.assertEqual(x, ("+", ("+", 3, 2), 5))
Пример #5
0
    def testNumber(self):
        x = prefix("1")
        self.assertEqual(x, 1)

        x = prefix("10")
        self.assertEqual(x, 10)

        x = prefix("999")
        self.assertEqual(x, 999)
Пример #6
0
    def testParenMult(self):
        x = prefix("( 3 * 2 )")
        self.assertEqual(x, ("*", 3, 2))

        x = prefix("5 * ( 3 * 2 )")
        self.assertEqual(x, ("*", 5, ("*", 3, 2)))

        x = prefix("( 3 * 2 ) * 5")
        self.assertEqual(x, ("*", ("*", 3, 2), 5))
Пример #7
0
    def testEmpty(self):
        x = prefix("")
        self.assertEqual(x, None)

        x = prefix(" ")
        self.assertEqual(x, None)

        x = prefix("(  )")
        self.assertEqual(x, None)

        x = prefix("( ( ) )")
        self.assertEqual(x, None)
Пример #8
0
    def testPrecedence(self):
        x = prefix("2 * 3 + 5")
        self.assertEqual(x, ("+", ("*", 2, 3), 5))

        x = prefix("5 + 2 * 3")
        self.assertEqual(x, ("+", 5, ("*", 2, 3)))

        x = prefix("1 + 2 * 3 + 5")
        self.assertEqual(x, ("+", ("+", 1, ("*", 2, 3)), 5))

        x = prefix("1 * 2 + 3 * 5")
        self.assertEqual(x, ("+", ("*", 1, 2), ("*", 3, 5)))
Пример #9
0
    def testPrefixExample(self):
        x = prefix("3")
        self.assertEqual(x, 3)

        x = prefix("1 + 1")
        self.assertEqual(x, ("+", 1, 1))

        x = prefix("2 * 5 + 1")
        self.assertEqual(x, ("+", ("*", 2, 5), 1))

        x = prefix("2 * ( 5 + 1 )")
        self.assertEqual(x, ("*", 2, ("+", 5, 1)))

        x = prefix("3 * x + ( 9 + y ) / 4")
        self.assertEqual(x, ("+", ("*", 3, "x"), ("/", ("+", 9, "y"), 4)))
Пример #10
0
 def testParenError(self):
     with self.assertRaises(ParseError):
         prefix("( 1 ")
Пример #11
0
    def testEmbedParenNumber(self):
        x = prefix("( ( 1 ) )")
        self.assertEqual(x, 1)

        x = prefix("( ( ( 1 ) ) )")
        self.assertEqual(x, 1)
Пример #12
0
    def testParenLetter(self):
        x = prefix("( a )")
        self.assertEqual(x, "a")

        x = prefix("( z )")
        self.assertEqual(x, "z")
Пример #13
0
    def testParenMixed(self):
        x = prefix("5 * ( 3 + 2 )")
        self.assertEqual(x, ("*", 5, ("+", 3, 2)))

        x = prefix("( 3 + 2 ) * 5")
        self.assertEqual(x, ("*", ("+", 3, 2), 5))
Пример #14
0
    def testMult(self):
        x = prefix("3 * 2")
        self.assertEqual(x, ("*", 3, 2))

        x = prefix("3 * 2 * 4")
        self.assertEqual(x, ("*", ("*", 3, 2), 4))