Skip to content

Commit

Permalink
Fix usage of default
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Jaegervall <[email protected]>
  • Loading branch information
erikbosch committed Oct 23, 2024
1 parent c5ef757 commit dd1430d
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/vss_tools/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ def check_type_default_consistency(self) -> Self:
is consistent with the given datatype
"""
if self.default is not None:
if Datatypes.get_type(self.datatype) is None:
# Default for custom datatypes only allowed for the special case that the datatype is an array
# and the default value is []
# See rules at https://github.com/COVESA/vehicle_signal_specification/blob/master/docs-gen/content/rule_set/data_entry/data_types_struct.md#default-values # noqa: E501
assert (
self.datatype.endswith("[]") and not self.default
), f"Default values not allowed for user defined type '{self.datatype}'"

if is_array(self.datatype):
assert isinstance(
self.default, list
Expand Down
39 changes: 39 additions & 0 deletions tests/vspec/test_structs/test_data_type_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,42 @@ def test_data_types_for_multiple_apigear_templates(tmp_path):
expected_output = HERE / "out.apigear"
dcmp = filecmp.dircmp(out, expected_output)
assert not (dcmp.diff_files or dcmp.left_only or dcmp.right_only)


@pytest.mark.parametrize("file", ["test_struct_default.vspec", "test_struct_default_array.vspec"])
def test_error_default_for_struct(tmp_path, file):
"""
Test that we get an error if trying to use default for struct
"""
vspec = HERE / file
types_file = HERE / "VehicleDataTypes.vspec"
out = tmp_path / "out.json"
cmd = (
f"vspec export json -u {TEST_UNITS} -q {TEST_QUANT} --pretty --vspec {vspec} "
f"--output {out} --types {types_file}"
)
env = os.environ.copy()
env["COLUMNS"] = "200"
process = subprocess.run(cmd.split(), capture_output=True, text=True, env=env)
assert process.returncode != 0

# Only checking first of type name to be able to handle both test cases
error_msg = "Default values not allowed for user defined type 'VehicleDataTypes.TestBranch1.ParentStruct"
assert error_msg in process.stdout


def test_error_default_for_struct_array_empty(tmp_path):
"""
Test that we get an error if trying to use default for struct
"""
vspec = HERE / "test_struct_default_array_empty.vspec"
types_file = HERE / "VehicleDataTypes.vspec"
out = tmp_path / "out.json"
cmd = (
f"vspec export json -u {TEST_UNITS} -q {TEST_QUANT} --pretty --vspec {vspec} "
+ f"--output {out} --types {types_file}"
)
env = os.environ.copy()
env["COLUMNS"] = "200"
process = subprocess.run(cmd.split(), capture_output=True, text=True, env=env)
assert process.returncode == 0
12 changes: 12 additions & 0 deletions tests/vspec/test_structs/test_struct_default.vspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
A:
type: branch
description: Branch A.


A.ParentStructSensor:
datatype: VehicleDataTypes.TestBranch1.ParentStruct
type: sensor
default: "No we do not allow default"
description: A rich sensor with user-defined data type.
comment: "See https://covesa.github.io/vehicle_signal_specification/rule_set/data_entry/data_types_struct/index.html#default-values"
13 changes: 13 additions & 0 deletions tests/vspec/test_structs/test_struct_default_array.vspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
A:
type: branch
description: Branch A.

# Default allowed for array type if default is empty

A.ParentStructSensor:
datatype: VehicleDataTypes.TestBranch1.ParentStruct[]
type: sensor
default: "Only empty array allowed here"
description: A rich sensor with user-defined data type.
comment: "See https://covesa.github.io/vehicle_signal_specification/rule_set/data_entry/data_types_struct/index.html#default-values"
13 changes: 13 additions & 0 deletions tests/vspec/test_structs/test_struct_default_array_empty.vspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
A:
type: branch
description: Branch A.

# Default allowed for array type if default is empty

A.ParentStructSensor:
datatype: VehicleDataTypes.TestBranch1.ParentStruct[]
type: sensor
default: []
description: A rich sensor with user-defined data type.
comment: "See https://covesa.github.io/vehicle_signal_specification/rule_set/data_entry/data_types_struct/index.html#default-values"

0 comments on commit dd1430d

Please sign in to comment.