def test_explicit_reference_decoration_fails_when_unknown_resource_is_referenced():
    with assert_raises(MissingSymbol):
        SyntaxTreeBuilder.build("""namespace foo{
            struct A {
                refB : u64 : 64;
            }
            archive R {
                @explicit_reference( A.refB, resourceB )
                resourceA : vector< A >;
            }
            }
            """)
def test_implicit_references_fail_on_unknown_resource():
    with assert_raises(MissingSymbol):
        SyntaxTreeBuilder.build("""namespace foo{
            struct A {
                refB : u64 : 64;
            }
            @bound_implicitly( B: resourceA, resourceC )
            archive R {
                resourceA : vector< A >;
                resourceB : vector< A >;
            }
            }
            """)
Example #3
0
 def __init__(self, schema):
     """
     Instantiates generator engine for a given schema.
     :raises FlatdataSyntaxError
     """
     self.schema = schema
     self.tree = SyntaxTreeBuilder.build(schema)
def test_multi_vector_references_builtin_type():
    tree = SyntaxTreeBuilder.build("""namespace n{
        struct T { t : u64 : 17; }
        archive A {
            r : multivector< 33, T >;
        }
        }
        """)
    assert_equal({
        ".n", ".n.T", ".n.T.t", ".n.A", ".n.A.r", ".n.A.r.@@n@T",
        ".n.A.r.@@_builtin@multivector@IndexType33",
        "._builtin", "._builtin.multivector",
        "._builtin.multivector.IndexType33", "._builtin.multivector.IndexType33.value"
    }, tree.symbols())
Example #5
0
def test_all_flatdata_features_look_as_expected_in_fully_built_tree():
    tree = SyntaxTreeBuilder.build(TREE_WITH_ALL_FEATURES)

    assert_equal.__self__.maxDiff = None
    assert_equal(
        {
            '._builtin': Namespace,
            '._builtin.multivector': Namespace,
            '._builtin.multivector.IndexType14': Structure,
            '._builtin.multivector.IndexType14.value': Field,
            '.ns': Namespace,
            '.ns.A0': Archive,
            '.ns.A0.@@ns@C': ConstantReference,
            '.ns.A0.b': BoundResource,
            '.ns.A0.b.@@ns@A0@v0': ResourceReference,
            '.ns.A0.b.@@ns@A0@v1': ResourceReference,
            '.ns.A0.v0': Vector,
            '.ns.A0.v0.@@ns@S1': StructureReference,
            '.ns.A0.v1': Multivector,
            '.ns.A0.v1.@@_builtin@multivector@IndexType14':
            BuiltinStructureReference,
            '.ns.A0.v1.@@ns@S1': StructureReference,
            '.ns.A1': Archive,
            '.ns.A1.@@ns@C': ConstantReference,
            '.ns.A1.a': res.Archive,
            '.ns.A1.a.@@ns@A0': ArchiveReference,
            '.ns.A1.i': Instance,
            '.ns.A1.i.@@ns@S0': StructureReference,
            '.ns.A1.mv': Multivector,
            '.ns.A1.mv.@@_builtin@multivector@IndexType14':
            BuiltinStructureReference,
            '.ns.A1.mv.@@ns@S0': StructureReference,
            '.ns.A1.mv.er_S0_f0_v0': ExplicitReference,
            '.ns.A1.mv.er_S0_f0_v0.@@ns@A1@v0': ResourceReference,
            '.ns.A1.mv.er_S0_f0_v0.@@ns@S0': StructureReference,
            '.ns.A1.mv.er_S0_f0_v0.@@ns@S0@f0': FieldReference,
            '.ns.A1.mv.er_S0_f1_A1_v0': ExplicitReference,
            '.ns.A1.mv.er_S0_f1_A1_v0.@@ns@A1@v0': ResourceReference,
            '.ns.A1.mv.er_S0_f1_A1_v0.@@ns@S0': StructureReference,
            '.ns.A1.mv.er_S0_f1_A1_v0.@@ns@S0@f1': FieldReference,
            '.ns.A1.rd': RawData,
            '.ns.A1.v0': Vector,
            '.ns.A1.v0.@@ns@S1': StructureReference,
            '.ns.C': Constant,
            '.ns.S0': Structure,
            '.ns.S0.f0': Field,
            '.ns.S0.f1': Field,
            '.ns.S1': Structure,
            '.ns.S1.f0': Field
        }, tree.symbols(include_types=True))
def test_duplicate_multivector_builtin_types_are_not_produced():
    tree = SyntaxTreeBuilder.build("""namespace n{
        struct T { t : u64 : 17; }
        archive A {
            r : multivector< 33, T >;
            r2 : multivector< 33, T >;
        }
        }
        """)
    assert_equal({
        ".n", ".n.T", ".n.T.t", ".n.A",
        ".n.A.r", ".n.A.r.@@n@T", ".n.A.r.@@_builtin@multivector@IndexType33",
        ".n.A.r2", ".n.A.r2.@@n@T", ".n.A.r2.@@_builtin@multivector@IndexType33",
        "._builtin", "._builtin.multivector",
        "._builtin.multivector.IndexType33", "._builtin.multivector.IndexType33.value"
    }, tree.symbols())
Example #7
0
def test_archive_schemas_include_constants():
    root = SyntaxTreeBuilder.build("""namespace foo{
const u8 C = 42;
struct T { f : u8 : 7; }
archive A {
    resourceA : T;
}
}
        """)
    resolve_references(root)
    expected = """namespace foo { struct T { f : u8 : 7; } }
namespace foo { const u8 C = 42; }
namespace foo { archive A {
    resourceA : T;
} }"""
    actual = SyntaxTree.schema(root.find(".foo.A"))
    assert_equal(actual, expected)
def test_constants_are_referred_from_every_archive():
    tree = SyntaxTreeBuilder.build("""
namespace m {
    const u8 C = 17;
    }
namespace n.m {
    const u8 C = 13;
    }
namespace n{
    const u8 C = 42;

    struct T { f : u8 : 7; }
    archive A {
        resourceA : T;
    }
}""")
    tree.find(".n.A.@@n@C")
    tree.find(".n.A.@@m@C")
    tree.find(".n.A.@@n@m@C")
    def __test(schema, resource_type, properties):
        tree = SyntaxTreeBuilder.build("""namespace n{
            struct T {
                f0 : u8 : 1;
            }
            archive A {
                r : %s;
            }
            }
            """ % schema)

        a = tree.find(".n.A")
        assert_is_instance(a, Archive)
        r = a.find("A.r")
        assert_is_instance(r, resource_type)

        for k, values in properties.items():
            assert_true(hasattr(r, k))
            assert_equal([tree.find(v) for v in values], getattr(r, k))
def test_tree_with_all_features_schema_results_in_the_same_tree():
    expected_tree = SyntaxTreeBuilder.build(TREE_WITH_ALL_FEATURES)
    schema = expected_tree.schema(expected_tree.find('.ns.A1'))
    actual_tree = SyntaxTreeBuilder.build(schema)
    assert_equal(expected_tree.symbols(include_types=True), actual_tree.symbols(include_types=True))
 def __test(resource_type):
     with assert_raises(MissingSymbol):
         SyntaxTreeBuilder.build(
             """namespace foo{ archive A { resourceA : %s; } }""" % resource_type)
Example #12
0
def generate_and_compare(definition, generator, expectation):
    tree = SyntaxTreeBuilder.build(definition=definition)
    contents = generator().render(tree)
    assert_equal.__self__.maxDiff = None
    assert_equal(expectation, contents)