def test_tree_end_column(): """ Ensures Tree.end_column can find the end column of a tree. """ token = Token('WORD', 'word') token.end_column = 1 tree = Tree('outer', [Tree('path', [token])]) assert tree.end_column() == '1'
def test_tree_end_column(): """ Ensures Tree.end_column can find the end column of a tree. """ token = Token("WORD", "word") token.end_column = 1 tree = Tree("outer", [Tree("path", [token])]) assert tree.end_column() == "1"
def test_tree_find_operator(magic): """ Ensures find_operator can find the operator. """ tree = Tree('any', []) tree.multiplication = magic() result = tree.find_operator() assert result == tree.multiplication.exponential.factor.child()
def test_tree_walk_token(): """ Ensures that encountered tokens are skipped """ inner_tree = Tree('inner', []) tree = Tree('rule', [Token('test', 'test'), inner_tree]) result = Tree.walk(tree, 'inner') assert result == inner_tree
def test_objects_objects_key_path(patch, tree): """ Ensures that objects like {x: 0} are compiled """ patch.many(Objects, ['path', 'expression']) subtree = Tree('key_value', [Tree('path', ['path'])]) tree.children = [subtree] result = Objects.objects(tree) assert result['items'][0][0] == Objects.path()
def test_compiler_parse_tree(compiler, patch): """ Ensures that the parse_tree method can parse a complete tree """ patch.object(Compiler, 'subtree') tree = Tree('start', [Tree('command', ['token'])]) compiler.parse_tree(tree) compiler.subtree.assert_called_with(Tree('command', ['token']), parent=None)
def test_objects_string_templating(patch): patch.object(Objects, 'path') patch.object(re, 'findall', return_value=['color']) tree = Tree('string', [Token('DOUBLE_QUOTED', '"{{color}}"')]) result = Objects.string(tree) re.findall.assert_called_with(r'{{([^}]*)}}', '{{color}}') Objects.path.assert_called_with(Tree('path', [Token('WORD', 'color')])) assert result['string'] == '{}' assert result['values'] == [Objects.path()]
def test_objects_names_string(patch, magic, tree): """ Ensures that paths like x['y'] are compiled correctly """ patch.object(Objects, 'string') tree.children = [magic(), Tree('fragment', [Tree('string', 'token')])] result = Objects.names(tree) Objects.string.assert_called_with(Tree('string', 'token')) assert result[1] == Objects.string()
def test_objects_names_string(patch, magic, tree): """ Ensures that paths like x['y'] are compiled correctly """ patch.object(Objects, "string") tree.children = [magic(), Tree("fragment", [Tree("string", "token")])] result = Objects().names(tree) Objects.string.assert_called_with(Tree("string", "token")) assert result[1] == Objects.string()
def test_objects_number_float(): """ Ensures that a float is compiled correctly. """ tree = Tree("number", [Token("FLOAT", "1.2")]) assert Objects.number(tree) == {"$OBJECT": "float", "float": 1.2} tree = Tree("number", [Token("FLOAT", "+1.2")]) assert Objects.number(tree) == {"$OBJECT": "float", "float": 1.2} tree = Tree("number", [Token("FLOAT", "-1.2")]) assert Objects.number(tree) == {"$OBJECT": "float", "float": -1.2}
def test_scope_selective_visitor_multiple(): tv = ScopeSelectiveTestVisitor() tv.visit_children(Tree("c", [ Tree("a", []), Tree("b", []), Tree("a", []), ]), scope=None) assert tv._a == 2 assert tv._b == 1
def test_compiler_parse_tree(compiler, patch): """ Ensures that the parse_tree method can parse a complete tree """ patch.object(JSONCompiler, "subtree") tree = Tree("start", [Tree("command", ["token"])]) compiler.parse_tree(tree) compiler.subtree.assert_called_with( Tree("command", ["token"]), parent=None )
def test_scope_selective_visitor_nested(): tv = ScopeSelectiveTestVisitor() tv.visit_children(Tree('c', [ Tree('a', [Tree('a', [])]), Tree('b', []), Tree('a', []), ]), scope=None) assert tv._a == 3 assert tv._b == 1
def test_objects_map(patch, tree): patch.many(Objects, ["string", "base_expression"]) subtree = Tree("key_value", [Tree("string", ["key"]), "value"]) tree.children = [subtree] result = Objects().map(tree) Objects.string.assert_called_with(subtree.string) Objects.base_expression.assert_called_with("value") items = [[Objects.string(), Objects.base_expression()]] expected = {"$OBJECT": "dict", "items": items} assert result == expected
def test_transformer_function_block_empty(patch, tree, magic): """ Ensures that indented arguments are added back to the their original node """ assert Transformer.function_block([]) == Tree('function_block', []) assert Transformer.function_block([0]) == Tree('function_block', [0]) m = magic() m.data = 'some_block' assert Transformer.function_block([m]) == Tree('function_block', [m]) assert Transformer.function_block([m, m]) == Tree('function_block', [m, m])
def test_faketree_check_existing_one_child(block, fake_tree): """ Checks checking for fake lines with an one child block """ block.children = [ Tree("path", [Token("NAME", "foo")]), Tree("assignment", [Tree("path", [Token("NAME", "foo")])]), ] fake_tree._check_existing_fake_lines(block) assert fake_tree.new_lines == {}
def test_objects_number(): """ Ensures that an int is compiled correctly. """ tree = Tree("number", [Token("INT", "1")]) assert Objects.number(tree) == {"$OBJECT": "int", "int": 1} tree = Tree("number", [Token("INT", "+1")]) assert Objects.number(tree) == {"$OBJECT": "int", "int": 1} tree = Tree("number", [Token("INT", "-1")]) assert Objects.number(tree) == {"$OBJECT": "int", "int": -1}
def test_objects_names_path(patch, magic, tree): """ Ensures that paths like x[y] are compiled correctly """ patch.object(Objects, 'path') subtree = Tree('path', ['token']) tree.children = [magic(), Tree('fragment', [subtree])] result = Objects.names(tree) Objects.path.assert_called_with(subtree) assert result[1] == Objects.path()
def test_objects_number(): """ Ensures that an int is compiled correctly. """ tree = Tree('number', [Token('INT', '1')]) assert Objects.number(tree) == 1 tree = Tree('number', [Token('INT', '+1')]) assert Objects.number(tree) == 1 tree = Tree('number', [Token('INT', '-1')]) assert Objects.number(tree) == -1
def test_objects_number_float(): """ Ensures that a float is compiled correctly. """ tree = Tree('number', [Token('FLOAT', '1.2')]) assert Objects.number(tree) == 1.2 tree = Tree('number', [Token('FLOAT', '+1.2')]) assert Objects.number(tree) == 1.2 tree = Tree('number', [Token('FLOAT', '-1.2')]) assert Objects.number(tree) == -1.2
def test_objects_objects(patch, tree): patch.many(Objects, ['string', 'expression']) subtree = Tree('key_value', [Tree('string', ['key']), 'value']) tree.children = [subtree] result = Objects.objects(tree) Objects.string.assert_called_with(subtree.string) Objects.expression.assert_called_with('value') expected = {'$OBJECT': 'dict', 'items': [[Objects.string(), Objects.expression()]]} assert result == expected
def test_objects_number(): """ Ensures that an int is compiled correctly. """ tree = Tree('number', [Token('INT', '1')]) assert Objects.number(tree) == {'$OBJECT': 'int', 'int': 1} tree = Tree('number', [Token('INT', '+1')]) assert Objects.number(tree) == {'$OBJECT': 'int', 'int': 1} tree = Tree('number', [Token('INT', '-1')]) assert Objects.number(tree) == {'$OBJECT': 'int', 'int': -1}
def test_faketree_check_existing_with_fake(block, fake_tree): """ Checks checking for fake lines with a fake path """ block.children = [ Tree('assignment', [Tree('path', [Token('NAME', 'foo')])]), Tree('assignment', [Tree('path', [Token('NAME', '__p-bar')])]), ] fake_tree._check_existing_fake_lines(block) assert fake_tree.new_lines == {'__p-bar': False}
def test_faketree_check_existing_one_child(block, fake_tree): """ Checks checking for fake lines with an one child block """ block.children = [ Tree('path', [Token('NAME', 'foo')]), Tree('assignment', [Tree('path', [Token('NAME', 'foo')])]), ] fake_tree._check_existing_fake_lines(block) assert fake_tree.new_lines == {}
def test_transformer_absolute_expression(patch, tree): """ Ensures absolute_expression are untouched when they don't contain just a path """ patch.object(Tree, 'follow_node_chain') tree.follow_node_chain.return_value = None result = Transformer.absolute_expression([tree]) assert result == Tree('absolute_expression', [tree]) result = Transformer.absolute_expression([tree, tree]) assert result == Tree('absolute_expression', [tree, tree])
def name_to_path(name): """ Builds the tree for a name or dotted name. """ names = name.split(".") tree = Tree("path", [Token("NAME", names[0])]) if len(names) > 1: for name in names[1:]: fragment = Tree("path_fragment", [Token("NAME", name)]) tree.children.append(fragment) return tree
def name_to_path(name): """ Builds the tree for a name or dotted name. """ names = name.split('.') tree = Tree('path', [Token('NAME', names[0])]) if len(names) > 1: for name in names[1:]: fragment = Tree('path_fragment', [Token('NAME', name)]) tree.children.append(fragment) return tree
def test_objects_mutation_from_service(token): """ Ensures that mutations objects from service trees are compiled correctly. """ tree = Tree('service_fragment', [Tree('command', [token])]) expected = { '$OBJECT': 'mutation', 'mutation': token.value, 'arguments': [] } assert Objects.mutation(tree) == expected
def test_faketree_assignment(patch, tree, fake_tree): patch.many(FakeTree, ["path", "get_line", "set_line"]) result = fake_tree.assignment(tree) FakeTree.get_line.assert_called_with(tree) line = FakeTree.get_line() FakeTree.set_line.assert_called_with(tree, line) FakeTree.path.assert_called_with(line=line) assert result.children[0] == FakeTree.path() tree = Tree("base_expression", [tree]) subtree = [Token("EQUALS", "=", line=line), tree] expected = Tree("assignment_fragment", subtree) assert result.children[1] == expected
def test_objects_objects_key_path(patch, tree): """ Ensures that objects like {x: 0} are compiled """ patch.many(Objects, ['base_expression', 'path']) subtree = Tree('key_value', [ Tree('path', ['string.name']), Tree('path', ['value.path']), ]) tree.children = [subtree] result = Objects().map(tree) assert result['items'] == [[Objects.path(), Objects.base_expression()]]