Ejemplo n.º 1
0
 def test_given_different_children_then_not_equal(self) -> None:
     from cmaj.testing.ast import tree
     node = tree(('node', [('child1', Token(0, 0, 'value')),
                           ('child2', Token(1, 0, 'value'))]))
     other = tree(('node', [('child1', Token(0, 0, 'value')),
                            ('child2', Token(1, 0, 'other'))]))
     self.assertNotEqual(other, node)
Ejemplo n.º 2
0
 def test_given_equal_nodes_then_equal(self) -> None:
     from cmaj.testing.ast import tree
     tree_def = ('node', [('child1', Token(0, 0, 'this')),
                          ('child2', Token(0, 5, 'is')),
                          ('child3', Token(2, 4, 'awesome'))])
     node = tree(tree_def)
     other = tree(tree_def)
     self.assertEqual(other, node)
Ejemplo n.º 3
0
    def test_children_are_copy(self) -> None:
        node = Node('node')
        child = Node('child', token=Token(0, 0, 'value'))
        node.add_child(child)

        children = node.children
        children.append(Node('no_child_of_node'))
        self.assertEqual([child], node.children)
Ejemplo n.º 4
0
def token(key: str, line: int) -> Tuple[str, Token]:
    return key, Token(line, 0, key.lower())
Ejemplo n.º 5
0
 def test_when_adding_node_then_node_is_child(self) -> None:
     node = Node('node')
     child = Node('child', token=Token(0, 0, 'value'))
     node.add_child(child)
     self.assertIn(child, node.children)
Ejemplo n.º 6
0
 def test_given_node_with_token_when_adding_child_then_error(self) -> None:
     node = Node('node', token=Token(0, 0, 'value'))
     child = Node('child', token=Token(1, 0, 'value'))
     self.assertRaises(AssertionError, node.add_child, child)
Ejemplo n.º 7
0
 def test_given_multiple_lines_then_length_of_last_line(self) -> None:
     from cmaj.testing.ast import tree
     node = tree(('node', [('child1', Token(3, 0, 'wrong length')),
                           ('child2', Token(5, 2, 'correct'))]))
     self.assertEqual(7, len(node))
Ejemplo n.º 8
0
 def test_given_children_then_length_of_column_span(self) -> None:
     from cmaj.testing.ast import tree
     node = tree(('node', [('child1', Token(3, 2, 'value')),
                           ('child2', Token(3, 9, 'other'))]))
     self.assertEqual(12, len(node))
Ejemplo n.º 9
0
 def test_given_token_then_length_of_token(self) -> None:
     node = Node('node', Token(3, 2, 'value'))
     self.assertEqual(5, len(node))
Ejemplo n.º 10
0
 def test_given_different_tokens_then_not_equal(self) -> None:
     node = Node('node', token=Token(0, 0, 'value'))
     self.assertNotEqual(Node('node', token=Token(0, 0, 'other')), node)
     self.assertNotEqual(Node('node', token=Token(0, 1, 'value')), node)
     self.assertNotEqual(Node('node', token=Token(1, 0, 'value')), node)
Ejemplo n.º 11
0
 def match(self, line_index: int, column_index: int,
           sequence: str) -> Optional[Node]:
     from cmaj.ast.node import Token
     if (result := self._regex(sequence)) is not None:
         return Node(self._key,
                     token=Token(line_index, column_index, result))
Ejemplo n.º 12
0
def tokens(keys: str) -> List[Node]:
    return [
        Node(key, token=Token(0, column, 'x'))
        for column, key in enumerate(keys)
    ]