Example #1
0
def assert_parse_tree(test, nodes, tups, tree=None, history=None):
    """
  Check the output tree structure against that of expect_tree: a nested tuple
  tree.
  """

    if tree is None:
        tree = nodes

    if history is None:
        history = []

    for node, tup in overzip(nodes, tups):
        if isinstance(node, lexer.Token):
            continue
        message = ("For node {} at\n {} within \n{}. "
                   "If this is infact correct, copy-paste this:\n\n{}".format(
                       node, parser.tree_string([node]),
                       parser.tree_string(tree, history),
                       parser.test_string(tree, ' ' * 6, ' ' * 2)))
        test.assertIsNotNone(node, msg="Missing node " + message)
        test.assertIsNotNone(tup, msg="Extra node " + message)
        expect_type, expect_children = tup
        test.assertEqual(node.node_type,
                         expect_type,
                         msg="Expected type={} ".format(expect_type) + message)
        assert_parse_tree(test, node.children, expect_children, tree,
                          history + [node])
Example #2
0
def assert_tree_type(test, nodes, tups, tree=None):
  """
  Check the output tree structure against that of expect_tree: a nested tuple
  tree.
  """

  if tree is None:
    tree = nodes

  for node, tup in overzip(nodes, tups):
    if isinstance(node, lexer.Token):
      continue
    message = ("For node {} at\n {} within \n{}. "
               "If this is infact correct, copy-paste this:\n\n{}"
               .format(node, parser.tree_string([node]),
                       parser.tree_string(tree),
                       parser.test_string(tree)))
    test.assertIsNotNone(node, msg="Missing node" + message)
    test.assertIsNotNone(tup, msg="Extra node" + message)
    expect_type, expect_children = tup
    test.assertEqual(node.node_type, expect_type,
                     msg="Expected type={} ".format(expect_type) + message)
    assert_tree_type(test, node.children, expect_children, tree)