def testBracketize(self): ob = t.OpenBracket cb = t.CloseBracket op = t.OpenParen cp = t.CloseParen tokens = [ t.NewLine, t.I("def"), t.I("foo"), op, cp, t.Colon, t.NewLine, t.Indent, op, t.I("bar"), cp, t.NewLine, t.I("def"), t.I("baz"), op, cp, t.Colon, t.NewLine, t.Indent, t.I("quux"), t.NewLine, t.Dedent, t.Dedent, t.I("hooj"), t.NewLine, ] expected = [ op, t.I("def"), t.I("foo"), op, cp, op, t.I("bar"), cp, op, t.I("def"), t.I("baz"), op, cp, t.I("quux"), cp, cp, t.I("hooj"), ] result = parse.bracketize(tokens) result = [x for x in result if x is not t.NewLine] self.assertEquals(result, expected)
def testCrappyBracketize(self): p = self.p.parse c = """ class Subol (unittest.TestCase): def testMath (self): (self.assertEquals (+ 3 (/ 6 2)) 6) (self.assertEquals (- 3 2) 1) (self.assertEquals (* 5 5) 25) def testInfix (self): (self.assertEquals {3 + {6 / 2}} 6) {a = 1} (= b 2) (self.assertEquals a 1) (self.assertEquals b 2) """ ob = t.OpenBracket cb = t.CloseBracket op = t.OpenParen cp = t.CloseParen oc = t.OpenCurly cc = t.CloseCurly expected = [ op, t.I("class"), t.I("Subol"), op, t.I("unittest.TestCase"), cp, op, t.I("def"), t.I("testMath"), op, t.I("self"), cp, op, t.I("self.assertEquals"), op, t.I("+"), 3, op, t.I("/"), 6, 2, cp, cp, 6, cp, op, t.I("self.assertEquals"), op, t.I("-"), 3, 2, cp, 1, cp, op, t.I("self.assertEquals"), op, t.I("*"), 5, 5, cp, 25, cp, cp, # close testMath op, t.I("def"), t.I("testInfix"), op, t.I("self"), cp, op, t.I("self.assertEquals"), oc, 3, t.I("+"), oc, 6, t.I("/"), 2, cc, cc, 6, cp, oc, t.I("a"), t.I("="), 1, cc, op, t.I("="), t.I("b"), 2, cp, op, t.I("self.assertEquals"), t.I("a"), 1, cp, op, t.I("self.assertEquals"), t.I("b"), 2, cp, cp, # close testInfix cp, # close class ] v = p(c) v2 = parse.bracketize(v) result = [x for x in v2 if x is not t.NewLine] ## for a, b in zip(v2, expected, ): ## print "---" ## print a, b ## print "===" self.assertEquals(result, expected)