Skip to content

Commit

Permalink
Give error if unit is used for branch or struct
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Jaegervall <[email protected]>
  • Loading branch information
erikbosch committed Oct 26, 2023
1 parent 590d86e commit 309ff41
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
26 changes: 26 additions & 0 deletions tests/vspec/test_structs/VehicleDataTypesStructWithUnit.vspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
VehicleDataTypes:
type: branch
description: Top-level branch for vehicle data types.

VehicleDataTypes.TestBranch1:
description: "A branch"
type: branch

VehicleDataTypes.TestBranch1.NestedStruct:
type: struct
description: "ERROR: A struct with unit defined."
unit: km

VehicleDataTypes.TestBranch1.NestedStruct.x:
type: property
description: "x property"
datatype: double

VehicleDataTypes.TestBranch1.ParentStruct:
type: struct
description: "A struct that is going to contain properties that are structs themselves"

VehicleDataTypes.TestBranch1.ParentStruct.x_property:
type: property
description: "A property of struct-type. The struct name is specified relative to the branch"
datatype: NestedStruct
21 changes: 21 additions & 0 deletions tests/vspec/test_structs/test_data_type_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,24 @@ def test_warning_when_data_type_is_provided_for_struct_nodes(change_test_dir):
os.system("rm -f VehicleDataTypes.json out.txt")
assert os.WIFEXITED(result)
assert os.WEXITSTATUS(result) == 0


def test_error_when_unit_is_provided_for_struct_nodes(change_test_dir):
"""
Test that error message is provided when unit is specified for struct nodes.
"""
test_str = " ".join(["../../../vspec2json.py", "-u", "../test_units.yaml", "--format", "json",
"--json-pretty", "-vt",
"VehicleDataTypesStructWithUnit.vspec", "-ot", "VehicleDataTypes.json", "test.vspec",
"out.json", "1>", "out.txt", "2>&1"])
result = os.system(test_str)
assert os.WIFEXITED(result)
assert os.WEXITSTATUS(result) != 0

error_msg = 'cannot have unit'
test_str = f'grep \"{error_msg}\" out.txt > /dev/null'
result = os.system(test_str)
os.system("cat out.txt")
os.system("rm -f VehicleDataTypes.json out.txt")
assert os.WIFEXITED(result)
assert os.WEXITSTATUS(result) == 0
6 changes: 5 additions & 1 deletion tests/vspec/test_units/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,12 @@ def test_unit_error_no_unit_file(change_test_dir):
def test_unit_error_unit_file_incomplete(change_test_dir):
run_unit_error("signals_with_special_units.vspec", "-u units_hogshead.yaml", "Unknown unit")

# FIle not found
# File not found


def test_unit_error_missing_file(change_test_dir):
run_unit_error("signals_with_special_units.vspec", "-u file_that_does_not_exist.yaml", "FileNotFoundError")


def test_unit_on_branch(change_test_dir):
run_unit_error("unit_on_branch.vspec", "-u units_all.yaml", "cannot have unit")
10 changes: 10 additions & 0 deletions tests/vspec/test_units/unit_on_branch.vspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
A:
type: branch
description: Specifying unit on branch is not allowed!
unit: hogshead

A.UInt8:
datatype: uint8
type: sensor
unit: hogshead
description: This description is mandatory!
4 changes: 4 additions & 0 deletions vspec/model/vsstree.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ def __init__(self, name, source_dict: dict, available_types: Set[str], parent=No
logging.error(f"Orphan property detected. {self.name} is not defined under a struct")
sys.exit(-1)

if ((self.is_branch() or self.is_struct()) and "unit" in self.source_dict.keys()):
logging.error("%s cannot have unit, only signals and struct properties may define unit", self.name)
sys.exit(-1)

if (self.is_signal() or self.is_property()) and "datatype" not in self.source_dict.keys():
raise IncompleteElementException(
(f"Incomplete element {self.name} from {self.source_dict['$file_name$']}: "
Expand Down

0 comments on commit 309ff41

Please sign in to comment.