def test_trivial(self): """ This tests the rendering of trivial trees, like this: head """ q = Qtree("head") self.assertEqual(q.render(), r"[.{head} ]")
def test_oneBranch(self): """ head | tail """ q = Qtree("head", [Qtree("tail")]) self.assertEqual(q.renderTree(), r"[.{head} [.{tail} ] ]")
def test_twoBranches(self): r""" head | \ tail tail2 """ q = Qtree("head", [Qtree("tail"), Qtree("tail2")]) self.assertEqual(q.renderTree(), r"[.{head} [.{tail} ] [.{tail2} ] ]")
def test_newlines(self): r""" head | tailpartA tailpartB """ q = Qtree("head", [Qtree("tailpartA\ntailpartB")]) self.assertEqual(q.render(), r"[.{head} [.{tailpartA\\tailpartB} ] ]")
def test_twoBranchesDeep(self): r""" head | tail | tail2 """ q = Qtree("head", [ Qtree("tail", [ Qtree("tail2")])]) self.assertEqual(q.renderTree(), r"[.{head} [.{tail} [.{tail2} ] ] ]")
def test_or(self): r""" Rendering an Or renders the Or expression in the root and branches on each of the subexpressions. Or(p, q) / \ p q """ p = Or(PropVar('p'), PropVar('q')) expectedOutput = Qtree("Or(p, q)", [Qtree("p"), Qtree("q")]) self.assertEqual(p.getSubTree().render(), expectedOutput.render())
def test_and(self): r""" Rendering an And renders the And expression in the root and both of the conjuncts newline-separated in a single subtree. And(p, q) | p q """ p = And(PropVar('p'), PropVar('q')) expectedOutput = Qtree("And(p, q)", [Qtree("p\nq")]) self.assertEqual(p.getSubTree().render(), expectedOutput.render())
def test_complexOr(self): r""" Or(p, Or(z, q)) / \ p Or(z, q) / \ z q """ p = Or(PropVar('p'), (Or(PropVar('z'), PropVar('q')))) expectedOutput = Qtree("Or(p, Or(z, q))", [Qtree("p"), Qtree("Or(z, q)", [Qtree("z"), Qtree("q")])]) self.assertEqual(p.getSubTree().render(), expectedOutput.render())
def test_complexAnd(self): r""" And(And(p, q), r) | And(p, q) r | p q """ p = And(And(PropVar('p'), PropVar('q')), PropVar('r')) expectedOutput = Qtree("And(And(p, q), r)", [Qtree("And(p, q)\nr", [Qtree("p\nq")])]) actual = p.getSubTree().render() expected = expectedOutput.render() self.assertEqual(actual, expected)
def test_propvar(self): """ Rendering a PropVar trivially renders just that PropVar in a tree on its own. """ p = PropVar('p') self.assertEqual(p.getSubTree().render(), Qtree('p').render())
def test_complexAndRightHand(self): r""" And(r, And(p, q)) | r And(p, q) | p q """ p = And(PropVar('r'), And(PropVar('p'), PropVar('q'))) expectedOutput = Qtree("And(r, And(p, q))", [Qtree("r\nAnd(p, q)", [Qtree("p\nq")])]) actual = p.getSubTree().render() expected = expectedOutput.render() self.assertEqual(actual, expected)
def qtree(self, elideRoot=False): """ For And expressions, render a subtree with each of the conjuncts separated by a newline as the head, and then proceed to recursively expand each conjunct in turn, one at a time. """ if self.lhs.isComplex and self.rhs.isComplex: self.lhs.stack.append(self.rhs) subQtreeBranches = [] if self.lhs.isComplex: subQtreeBranches.append(self.lhs.getSubTree(elideRoot=True)) elif self.rhs.isComplex: subQtreeBranches.append(self.rhs.getSubTree(elideRoot=True)) innerQtree = Qtree(self._newlineHead(), subQtreeBranches) if elideRoot: return innerQtree else: return Qtree(self.render(), [innerQtree])
def test_complexDoubleAnd(self): r""" And(And(a, b), And(c, d)) | And(a, b) And(c, d) | a b | c d """ p = And(And(PropVar('a'), PropVar('b')), And(PropVar('c'), PropVar('d'))) expectedOutput = Qtree("And(And(a, b), And(c, d))", [Qtree("And(a, b)\nAnd(c, d)", [Qtree("a\nb", [Qtree("c\nd")])])]) actual = p.getSubTree().render() expected = expectedOutput.render() self.assertEqual(actual, expected)
def test_complexOr(self): r""" Or(p, Or(z, q)) / \ p Or(z, q) / \ z q """ p = Or(PropVar('p'), (Or(PropVar('z'), PropVar('q')))) expectedOutput = Qtree( "Or(p, Or(z, q))", [Qtree("p"), Qtree("Or(z, q)", [Qtree("z"), Qtree("q")])]) self.assertEqual(p.getSubTree().render(), expectedOutput.render())
def test_complexDoubleAnd(self): r""" And(And(a, b), And(c, d)) | And(a, b) And(c, d) | a b | c d """ p = And(And(PropVar('a'), PropVar('b')), And(PropVar('c'), PropVar('d'))) expectedOutput = Qtree( "And(And(a, b), And(c, d))", [Qtree("And(a, b)\nAnd(c, d)", [Qtree("a\nb", [Qtree("c\nd")])])]) actual = p.getSubTree().render() expected = expectedOutput.render() self.assertEqual(actual, expected)
def test_trivial(self): """ head """ q = Qtree("head") self.assertEqual(q.renderTree(), r"[.{head} ]")
def qtree(self, elideRoot=False): """ Given the expression that is self, return a Qtree which can be rendered into the LaTeX we want to output. """ return Qtree(self.render())
def qtree(self, elideRoot=False): if elideRoot: text = "" else: text = self.render() return Qtree(text, [self.lhs.getSubTree(), self.rhs.getSubTree()])