예제 #1
0
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}
예제 #2
0
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}
예제 #3
0
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()
예제 #4
0
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
예제 #5
0
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
예제 #6
0
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}
예제 #7
0
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}
예제 #8
0
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
예제 #9
0
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
예제 #10
0
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()
예제 #11
0
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
예제 #12
0
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
예제 #13
0
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()]]
예제 #14
0
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
예제 #15
0
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
예제 #16
0
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()]]
예제 #17
0
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)()
예제 #18
0
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
예제 #19
0
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))],
    }
예제 #20
0
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))]
    }
예제 #21
0
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
예제 #22
0
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
예제 #23
0
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()}
예제 #24
0
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()}
예제 #25
0
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
예제 #26
0
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
예제 #27
0
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,
    }
예제 #28
0
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"}
예제 #29
0
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
    }
예제 #30
0
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'}