Skip to content

Commit 98715ca

Browse files
committed
parameterize flatdata-generator tests
Signed-off-by: mohapatr3 <[email protected]>
1 parent 9dce607 commit 98715ca

File tree

9 files changed

+117
-60
lines changed

9 files changed

+117
-60
lines changed

.github/workflows/generator.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ jobs:
2626
- name: Run tests
2727
run: |
2828
cd flatdata-generator
29-
uv run --with pytest pytest
29+
uv run --with pytest pytest -v
3030
pip install .
3131
flatdata-generator --help

flatdata-generator/tests/generators/test_cpp_generator.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ def generate_and_compare(test_case):
2121

2222
generate_and_assert_in(test, CppGenerator, *expectations)
2323

24-
def test_against_expectations():
24+
def get_test_cases():
25+
test_cases = []
2526
for x in schemas_and_expectations(generator='cpp', extension='h'):
26-
generate_and_compare(x)
27+
test_cases.append(x)
28+
return test_cases
29+
30+
@pytest.mark.parametrize("case", get_test_cases())
31+
def test_against_expectations(case):
32+
generate_and_compare(case)

flatdata-generator/tests/generators/test_dot_generator.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ def generate_and_compare(test_case):
3333

3434
generate_and_assert_in(test, DotGenerator, *expectations)
3535

36-
def test_against_expectations():
36+
def get_test_cases():
37+
test_cases = []
3738
for x in schemas_and_expectations(generator='dot', extension='dot'):
38-
generate_and_compare(x)
39+
test_cases.append(x)
40+
return test_cases
41+
42+
@pytest.mark.parametrize("case", get_test_cases())
43+
def test_against_expectations(case):
44+
generate_and_compare(case)

flatdata-generator/tests/generators/test_flatdata_generator.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ def generate_and_compare(test_case):
1818
contents = FlatdataGenerator().render(tree)
1919
assert expectation == contents
2020

21-
def test_against_expectations():
22-
for i in schemas_and_expectations(generator='flatdata', extension='flatdata'):
23-
generate_and_compare((i[1], i[1]))
21+
def get_test_cases():
22+
test_cases = []
23+
for x in schemas_and_expectations(generator='flatdata', extension='flatdata'):
24+
test_cases.append(x)
25+
return test_cases
2426

25-
def test_normalization_is_fixed_point():
26-
for i in schemas_and_expectations(generator='flatdata', extension='flatdata'):
27-
generate_and_compare((i[1], i[1]))
27+
@pytest.mark.parametrize("case", get_test_cases())
28+
def test_against_expectations(case):
29+
generate_and_compare(case)
30+
31+
@pytest.mark.parametrize("case", get_test_cases())
32+
def test_normalization_is_fixed_point(case):
33+
generate_and_compare((case[1], case[1]))

flatdata-generator/tests/generators/test_go_generator.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ def generate_and_compare(test_case):
2525
def skip(test_case):
2626
raise pytest.skip("Test %s is skipped" % test_case[0])
2727

28-
29-
def test_against_expectations():
28+
def get_test_cases():
29+
test_cases = []
3030
for x in schemas_and_expectations(generator='go', extension='go'):
31-
# Go does not yet support namespaces, enums, ranges, or constants, skip those tests
32-
if "enums" not in x[0] and "constants" not in x[0] and "namespaces" not in x[0] and "ranges" not in x[0]:
33-
generate_and_compare(x)
34-
else:
35-
skip(x)
31+
test_cases.append(x)
32+
return test_cases
33+
34+
@pytest.mark.parametrize("test_case", get_test_cases())
35+
def test_against_expectations(test_case):
36+
# Go does not yet support namespaces, enums, ranges, or constants, skip those tests
37+
if "enums" not in test_case[0] and "constants" not in test_case[0] and "namespaces" not in test_case[0] and "ranges" not in test_case[0]:
38+
generate_and_compare(test_case)
39+
else:
40+
skip(test_case)

flatdata-generator/tests/generators/test_python_generator.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,16 @@ def generate_and_compare(test_case):
2424
def skip(test_case):
2525
raise pytest.skip("Test %s is skipped" % test_case[0])
2626

27-
def test_against_expectations():
27+
def get_test_cases():
28+
test_cases = []
2829
for x in schemas_and_expectations(generator='py', extension='py'):
29-
# Python does not yet support enums or constants, skip those tests
30-
if "enums" not in x[0] and "constants" not in x[0]:
31-
generate_and_compare(x)
32-
else:
33-
skip(x)
30+
test_cases.append(x)
31+
return test_cases
32+
33+
@pytest.mark.parametrize("test_case", get_test_cases())
34+
def test_against_expectations(test_case):
35+
# Python does not yet support enums or constants, skip those tests
36+
if "enums" not in test_case[0] and "constants" not in test_case[0]:
37+
generate_and_compare(test_case)
38+
else:
39+
skip(test_case)

flatdata-generator/tests/generators/test_rust_generator.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ def generate_and_compare(test_case):
3737

3838
generate_and_assert_in(test, RustGenerator, *expectations)
3939

40-
def test_against_expectations():
40+
def get_test_cases():
41+
test_cases = []
4142
for x in schemas_and_expectations(generator='rust', extension='rs'):
42-
generate_and_compare(x)
43+
test_cases.append(x)
44+
return test_cases
45+
46+
@pytest.mark.parametrize("test_case", get_test_cases())
47+
def test_against_expectations(test_case):
48+
generate_and_compare(test_case)

flatdata-generator/tests/tree/test_node_tree_builder.py

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,8 @@ def test_two_structures_are_parsed_correctly():
162162
check_struct(tree.find(".foo.Bar"), 2, 1)
163163
check_struct(tree.find(".foo.Baz"), 17, 3)
164164

165-
166-
def test_implicit_field_widths_are_set_correctly():
167-
def __test(typename, width):
168-
tree = _build_node_tree("""namespace n{
169-
struct s {
170-
f : %s;
171-
}
172-
}
173-
""" % typename)
174-
_compute_structure_sizes(tree)
175-
check_field(tree.find('.n.s.f'), typename, width, 0)
176-
165+
def get_test_cases_implicit_field_widths_are_set_correctly():
166+
test_cases = []
177167
for typename, width in [
178168
('bool', 1),
179169
('u8', 8),
@@ -185,10 +175,37 @@ def __test(typename, width):
185175
('u64', 64),
186176
('i64', 64)
187177
]:
188-
__test(typename, width)
178+
test_cases.append((typename, width))
179+
return test_cases
189180

181+
@pytest.mark.parametrize("test_case", get_test_cases_implicit_field_widths_are_set_correctly())
182+
def test_implicit_field_widths_are_set_correctly(test_case):
183+
def __test(typename, width):
184+
tree = _build_node_tree("""namespace n{
185+
struct s {
186+
f : %s;
187+
}
188+
}
189+
""" % typename)
190+
_compute_structure_sizes(tree)
191+
check_field(tree.find('.n.s.f'), typename, width, 0)
192+
193+
__test(test_case[0], test_case[1])
190194

191-
def test_archives_resources_references_are_populated():
195+
196+
def get_test_cases_archives_resources_references_are_populated():
197+
test_cases = []
198+
for values in [
199+
("T", {".foo.A.resourceA.@T"}),
200+
("vector< bar.T >", {".foo.A.resourceA.@bar@T"}),
201+
("multivector< 33, T, foo.T, bar.baz.Boo >",
202+
{".foo.A.resourceA.@T", ".foo.A.resourceA.@foo@T", ".foo.A.resourceA.@bar@baz@Boo"})
203+
]:
204+
test_cases.append(values)
205+
return test_cases
206+
207+
@pytest.mark.parametrize("test_case", get_test_cases_archives_resources_references_are_populated())
208+
def test_archives_resources_references_are_populated(test_case):
192209
def __test(schema, references):
193210
tree = _build_node_tree("""namespace foo{
194211
archive A {
@@ -201,13 +218,7 @@ def __test(schema, references):
201218
for r in references:
202219
assert isinstance(tree.find(r), refs.TypeReference)
203220

204-
for values in [
205-
("T", {".foo.A.resourceA.@T"}),
206-
("vector< bar.T >", {".foo.A.resourceA.@bar@T"}),
207-
("multivector< 33, T, foo.T, bar.baz.Boo >",
208-
{".foo.A.resourceA.@T", ".foo.A.resourceA.@foo@T", ".foo.A.resourceA.@bar@baz@Boo"})
209-
]:
210-
__test(values[0], values[1])
221+
__test(test_case[0], test_case[1])
211222

212223

213224
def test_archives_archive_resources_references_are_populated():

flatdata-generator/tests/tree/test_syntax_tree_builder.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@
2020
FieldReference, ArchiveReference, BuiltinStructureReference, ConstantValueReference, \
2121
EnumerationReference, InvalidValueReference
2222

23-
def test_validating_archive_with_no_structure_defined_raises_missing_symbol_error():
23+
def get_test_cases():
24+
test_cases = []
25+
for t in ["T", "vector< T >", "multivector< 33, V>"]:
26+
test_cases.append(t)
27+
return test_cases
28+
29+
@pytest.mark.parametrize("test_case", get_test_cases())
30+
def test_validating_archive_with_no_structure_defined_raises_missing_symbol_error(test_case):
2431
def __test(resource_type):
2532
with pytest.raises(MissingSymbol):
2633
build_ast(
2734
"""namespace foo{ archive A { resourceA : %s; } }""" % resource_type)
2835

29-
for t in ["T", "vector< T >", "multivector< 33, V>"]:
30-
__test(t)
36+
__test(test_case)
3137

3238
def test_const_ref_with_mismatched_type():
3339
with pytest.raises(InvalidConstReference):
@@ -433,8 +439,21 @@ def test_tree_with_all_features_schema_results_in_the_same_normalized_tree():
433439
'.ns._builtin.multivector.IndexType14.value': Field,
434440
} == generated_tree.symbols(include_types=True)
435441

442+
def get_test_cases_resource_types_are_populated_from_structure_references():
443+
test_cases = []
444+
for values in [
445+
("T", res.Instance, {"referenced_structures": [".n.A.r.@@n@T"]}),
446+
("vector< T >", res.Vector, {"referenced_structures": [".n.A.r.@@n@T"]}),
447+
("multivector< 33, T>", res.Multivector, {
448+
"referenced_structures": ['.n.A.r.@@n@_builtin@multivector@IndexType33',
449+
'.n.A.r.@@n@T']}),
450+
("raw_data", res.RawData, {"referenced_structures": []})
451+
]:
452+
test_cases.append(values)
453+
return test_cases
436454

437-
def test_resource_types_are_populated_from_structure_references():
455+
@pytest.mark.parametrize("test_case", get_test_cases_resource_types_are_populated_from_structure_references())
456+
def test_resource_types_are_populated_from_structure_references(test_case):
438457
def __test(schema, resource_type, properties):
439458
tree = build_ast("""namespace n{
440459
struct T {
@@ -455,15 +474,7 @@ def __test(schema, resource_type, properties):
455474
assert hasattr(r, k)
456475
assert [tree.find(v) for v in values] == getattr(r, k)
457476

458-
for values in [
459-
("T", res.Instance, {"referenced_structures": [".n.A.r.@@n@T"]}),
460-
("vector< T >", res.Vector, {"referenced_structures": [".n.A.r.@@n@T"]}),
461-
("multivector< 33, T>", res.Multivector, {
462-
"referenced_structures": ['.n.A.r.@@n@_builtin@multivector@IndexType33',
463-
'.n.A.r.@@n@T']}),
464-
("raw_data", res.RawData, {"referenced_structures": []})
465-
]:
466-
__test(values[0], values[1], values[2])
477+
__test(test_case[0], test_case[1], test_case[2])
467478

468479

469480
def test_constants_are_referred_from_every_archive():

0 commit comments

Comments
 (0)