Example #1
0
def test_primitive(parse, scope):
    ast = parse('set<i32>')
    spec = type_spec_or_ref(ast).link(scope)

    prim_value = spec.to_primitive(set([1, 2, 3]))
    assert any(prim_value == list(xs) for xs in permutations([1, 2, 3]))
    assert spec.from_primitive(prim_value) == set([1, 2, 3])
Example #2
0
def test_link(parse, scope):
    ast = parse('map<string, Foo>')
    spec = type_spec_or_ref(ast)

    scope.add_type_spec(
        'Foo', TypedefTypeSpec('Foo', prim_spec.I32TypeSpec), 1
    )

    spec = spec.link(scope)
    assert spec.vspec == prim_spec.I32TypeSpec

    value = {u'foo': 1, u'bar': 2}
    assert (
        spec.to_wire(value) == vmap(
            ttype.BINARY, ttype.I32,
            (vbinary(b'foo'), vi32(1)),
            (vbinary(b'bar'), vi32(2)),
        )
    ) or (
        spec.to_wire(value) == vmap(
            ttype.BINARY, ttype.I32,
            (vbinary(b'bar'), vi32(2)),
            (vbinary(b'foo'), vi32(1)),
        )
    )
    assert value == spec.from_wire(spec.to_wire(value))
Example #3
0
def test_link(parse, scope):
    ast = parse('list<Foo>')
    spec = type_spec_or_ref(ast)

    scope.add_type_spec(
        'Foo', TypedefTypeSpec('Foo', prim_spec.TextTypeSpec), 1
    )

    spec = spec.link(scope)
    assert spec.vspec == prim_spec.TextTypeSpec

    value = [u'foo', u'bar']
    assert spec.to_wire(value) == vlist(
        ttype.BINARY, vbinary(b'foo'), vbinary(b'bar')
    )
    assert value == spec.from_wire(spec.to_wire(value))
Example #4
0
def test_primitive(parse, scope, loads):
    Foo = loads('struct Foo { 1: required i64 i }').Foo
    scope.add_type_spec('Foo', Foo.type_spec, 1)

    spec = type_spec_or_ref(parse('list<Foo>')).link(scope)

    value = [
        Foo(1234),
        Foo(1234567890),
        Foo(1234567890123456789),
    ]

    prim_value = [
        {'i': 1234},
        {'i': 1234567890},
        {'i': 1234567890123456789},
    ]

    assert spec.to_primitive(value) == prim_value
    assert spec.from_primitive(prim_value) == value
Example #5
0
def test_primitive(parse, scope, loads):
    Foo = loads('struct Foo { 1: required string bar }').Foo
    scope.add_type_spec('Foo', Foo.type_spec, 1)

    spec = type_spec_or_ref(parse('map<string, Foo>')).link(scope)

    value = {
        'a': Foo('1'),
        'b': Foo('2'),
        'c': Foo('3'),
    }

    prim_value = {
        'a': {'bar': '1'},
        'b': {'bar': '2'},
        'c': {'bar': '3'},
    }

    assert spec.to_primitive(value) == prim_value
    assert spec.from_primitive(prim_value) == value
Example #6
0
def test_mapper(parse):
    ast = parse('set<binary>')
    spec = type_spec_or_ref(ast)
    assert spec == SetTypeSpec(prim_spec.BinaryTypeSpec)
Example #7
0
def test_from_primitive_invalid(parse, scope):
    spec = type_spec_or_ref(parse('list<i32>')).link(scope)

    with pytest.raises(ValueError):
        spec.from_primitive(['a', 'b', 'c'])
Example #8
0
def test_mapper(parse):
    ast = parse('map<string, i32>')
    spec = type_spec_or_ref(ast)
    assert spec == MapTypeSpec(prim_spec.TextTypeSpec, prim_spec.I32TypeSpec)