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_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_objects_mutation_fragment_arguments(patch, magic): """ Ensures that mutation fragment objects with arguments are compiled correctly. """ patch.object(Objects, 'arguments') tree = magic() result = Objects().mutation_fragment(tree) Objects.arguments.assert_called_with(tree) assert result['args'] == Objects.arguments()
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_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_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_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_typed_argument(patch, tree): patch.object(Objects, 'values') result = Objects().typed_argument(tree) Objects.values.assert_called_with(Tree('anon', [tree.child(1)])) expected = { '$OBJECT': 'arg', 'name': tree.child().value, 'arg': Objects.values() } assert result == expected
def test_objects_typed_argument(patch, tree): patch.object(Objects, "values") result = Objects().typed_argument(tree) Objects.values.assert_called_with(Tree("anon", [tree.child(1)])) expected = { "$OBJECT": "arg", "name": tree.child().value, "arg": Objects.values(), } assert result == expected
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_argument(patch, tree): patch.object(Objects, 'expression') result = Objects().argument(tree) assert tree.child.call_count == 2 Objects.expression.assert_called_with(tree.child()) expected = { '$OBJECT': 'arg', 'name': tree.child().value, 'arg': Objects.expression() } assert result == expected
def test_objects_argument(patch, tree): patch.object(Objects, "expression") result = Objects().argument(tree) assert tree.child.call_count == 2 Objects.expression.assert_called_with(tree.child()) expected = { "$OBJECT": "arg", "name": tree.child().value, "arg": Objects.expression(), } assert result == 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()]]
def test_objects_mutation(patch, tree): """ Ensures that mutations objects are compiled correctly. """ patch.object(Objects, "entity") patch.object(Objects, "mutation_fragment") expected = { "method": "mutation", "name": [Objects.entity(tree.entity)], "args": [Objects.mutation_fragment(tree.mutation_fragment)], } assert Objects().mutation(tree) == expected
def test_objects_mutation(patch, tree): """ Ensures that mutations objects are compiled correctly. """ patch.object(Objects, 'entity') patch.object(Objects, 'mutation_fragment') expected = { 'method': 'mutation', 'name': [Objects.entity(tree.entity)], 'args': [Objects.mutation_fragment(tree.mutation_fragment)] } assert Objects().mutation(tree) == 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()]]
def test_objects_values(patch, magic, value_type): patch.object(Objects, value_type) item = magic(data=value_type) tree = magic(child=lambda x: item) result = Objects().values(tree) getattr(Objects, value_type).assert_called_with(item) assert result == getattr(Objects, value_type)()
def test_objects_mutation_fragment(token): """ Ensures that mutations fragments are compiled correctly. """ tree = Tree("mutation", [token]) expected = {"$OBJECT": "mutation", "mutation": token.value, "args": []} assert Objects().mutation_fragment(tree) == expected
def test_objects_map_type(tree, patch): tree.data = "types" types = Objects.types patch.object(Objects, "types") patch.object(Objects, "base_type") c = tree.first_child() c.data = "map_type" r = types(tree) Objects.base_type.assert_called_with(c.child(0)) Objects.types.assert_called_with(c.child(1)) assert r == { "$OBJECT": "type", "type": "Map", "values": [Objects.base_type(), Objects.types(c.child(1))], }
def test_objects_map_type(tree, patch): tree.data = 'types' types = Objects.types patch.object(Objects, 'types') patch.object(Objects, 'base_type') c = tree.first_child() c.data = 'map_type' r = types(tree) Objects.base_type.assert_called_with(c.child(0)) Objects.types.assert_called_with(c.child(1)) assert r == { '$OBJECT': 'type', 'type': 'Map', 'values': [Objects.base_type(), Objects.types(c.child(1))] }
def test_objects_mutation_fragment_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, "args": []} assert Objects().mutation_fragment(tree) == expected
def test_objects_values_void(patch, magic): """ Ensures Objects.values returns None when given a void value. """ item = magic(data='void') tree = magic(child=lambda x: item) assert Objects().values(tree) is None
def test_objects_string(patch, tree): # manual function patching objects = modules['storyscript.compiler.json.Objects'] patch.object(objects, 'unicode_escape') result = Objects().string(tree) objects.unicode_escape.assert_called_with(tree, tree.child(0).value) assert result == {'$OBJECT': 'string', 'string': objects.unicode_escape()}
def test_objects_string(patch, tree): # manual function patching objects = modules["storyscript.compiler.json.Objects"] patch.object(objects, "unicode_escape") result = Objects().string(tree) objects.unicode_escape.assert_called_with(tree, tree.child(0).value) assert result == {"$OBJECT": "string", "string": objects.unicode_escape()}
def test_objects_mutation_fragment_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, 'args': []} assert Objects().mutation_fragment(tree) == expected
def test_objects_mutation_fragment(token): """ Ensures that mutations fragments are compiled correctly. """ tree = Tree('mutation', [token]) expected = {'$OBJECT': 'mutation', 'mutation': token.value, 'args': []} assert Objects().mutation_fragment(tree) == expected
def test_objects_path_fragments(magic): fragment = magic() fragment.child().type = "NAME" tree = Tree("path", [magic(), fragment]) assert Objects().path(tree)["paths"][1] == { "$OBJECT": "dot", "dot": fragment.child().value, }
def test_objects_regular_expression(): """ Ensures regular expressions are compiled correctly """ tok = Token("regexp", "/regexp/") tree = Tree("regular_expression", [tok]) result = Objects().regular_expression(tree) assert result == {"$OBJECT": "regexp", "regexp": "regexp"}
def test_objects_path_fragments(magic): fragment = magic() fragment.child().type = 'NAME' tree = Tree('path', [magic(), fragment]) assert Objects().path(tree)['paths'][1] == { '$OBJECT': 'dot', 'dot': fragment.child().value }
def test_objects_regular_expression(): """ Ensures regular expressions are compiled correctly """ tok = Token('regexp', '/regexp/') tree = Tree('regular_expression', [tok]) result = Objects().regular_expression(tree) assert result == {'$OBJECT': 'regexp', 'regexp': 'regexp'}