From 079ab4d60d443d7c0d16f3326a5e7dc78a6e53ce Mon Sep 17 00:00:00 2001 From: Erik Jaegervall Date: Fri, 1 Dec 2023 12:47:03 +0100 Subject: [PATCH] Adding test case as example Signed-off-by: Erik Jaegervall --- tests/generators/example_generator.py | 51 +++++++++++++++++++++++++++ tests/generators/test.vspec | 27 ++++++++++++++ tests/generators/test_generator.py | 30 ++++++++++++++++ vspec/vspec2vss_config.py | 3 ++ vspec/vspec2x.py | 5 +-- vspec/vss2x.py | 4 +-- 6 files changed, 116 insertions(+), 4 deletions(-) create mode 100755 tests/generators/example_generator.py create mode 100644 tests/generators/test.vspec create mode 100644 tests/generators/test_generator.py diff --git a/tests/generators/example_generator.py b/tests/generators/example_generator.py new file mode 100755 index 00000000..5e082d7f --- /dev/null +++ b/tests/generators/example_generator.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +# Copyright (c) 2023 Contributors to COVESA +# +# This program and the accompanying materials are made available under the +# terms of the Mozilla Public License 2.0 which is available at +# https://www.mozilla.org/en-US/MPL/2.0/ +# +# SPDX-License-Identifier: MPL-2.0 + +import sys +import logging +from vspec.model.vsstree import VSSNode +import argparse +from vspec.vss2x import Vss2X +from vspec.vspec2vss_config import Vspec2VssConfig +from vspec.vspec2x import Vspec2X + + +class ExampleGenerator(Vss2X): + """ + This is an example on how easy you can write your own generator + """ + + def __init__(self, keyword: str): + self.keyword = str.lower(keyword) + self.count = 0 + + def handle_node(self, node: VSSNode): + if self.keyword in str.lower(node.comment): + self.count += 1 + for child in node.children: + self.handle_node(child) + + def generate(self, config: argparse.Namespace, signal_root: VSSNode, vspec2vss_config: Vspec2VssConfig, + data_type_root: VSSNode): + self.handle_node(signal_root) + + logging.info("Generating Example output...") + logging.info("I found %d comments with %s", self.count, self.keyword) + + +if __name__ == "__main__": + # JSON supports default + vspec2vss_config = Vspec2VssConfig() + # We do not need an output file + vspec2vss_config.output_file_required = False + # The generator shall know nothing about vspec processing or vspec2vss arguments! + # (Even if it may have some expectations on how the model look like) + vss2json = ExampleGenerator("VSS contributor") + vspec2x = Vspec2X(vss2json, vspec2vss_config) + vspec2x.main(sys.argv[1:]) diff --git a/tests/generators/test.vspec b/tests/generators/test.vspec new file mode 100644 index 00000000..9161fd21 --- /dev/null +++ b/tests/generators/test.vspec @@ -0,0 +1,27 @@ +# +A: + type: branch + description: Branch A. + +############ Testing Single Line Comments ############## + +A.Erik: + datatype: float + type: sensor + unit: km + description: A sensor. + comment: A VSS contributor! + +A.Sebastian: + datatype: float + type: sensor + unit: km + description: A sensor. + comment: A VSS contributor! + +A.ChuckNorris: + datatype: float + type: sensor + unit: km + description: A sensor. + comment: An actor! diff --git a/tests/generators/test_generator.py b/tests/generators/test_generator.py new file mode 100644 index 00000000..017cff54 --- /dev/null +++ b/tests/generators/test_generator.py @@ -0,0 +1,30 @@ +# Copyright (c) 2023 Contributors to COVESA +# +# This program and the accompanying materials are made available under the +# terms of the Mozilla Public License 2.0 which is available at +# https://www.mozilla.org/en-US/MPL/2.0/ +# +# SPDX-License-Identifier: MPL-2.0 + +import pytest +import os + + +@pytest.fixture +def change_test_dir(request, monkeypatch): + # To make sure we run from test directory + monkeypatch.chdir(request.fspath.dirname) + + +def test_generator(change_test_dir): + + test_str = "./example_generator.py -u ../vspec/test_units.yaml test.vspec > out.txt 2>&1" + result = os.system(test_str) + assert os.WIFEXITED(result) + assert os.WEXITSTATUS(result) == 0 + + test_str = 'grep \"I found 2 comments with vss contributor\" out.txt > /dev/null' + result = os.system(test_str) + assert os.WIFEXITED(result) + assert os.WEXITSTATUS(result) == 0 + os.system("rm -f out.txt") diff --git a/vspec/vspec2vss_config.py b/vspec/vspec2vss_config.py index 01b66b0b..b564af4a 100644 --- a/vspec/vspec2vss_config.py +++ b/vspec/vspec2vss_config.py @@ -39,6 +39,9 @@ def __init__(self): # if so shall there anyway be an option to NOT expand self.no_expand_option_supported = True + # Is an output file required as part of command line arguments + self.output_file_required = True + # As of now we have only one type of nodes in the type tree # and that is structs, so if we support type tree we assume structs are supported diff --git a/vspec/vspec2x.py b/vspec/vspec2x.py index 2b9da046..900ea1a8 100755 --- a/vspec/vspec2x.py +++ b/vspec/vspec2x.py @@ -63,8 +63,9 @@ def main(self, arguments): help='Unit file to be used for generation. Argument -u may be used multiple times.') parser.add_argument('vspec_file', metavar='', help='The vehicle specification file to convert.') - parser.add_argument('output_file', metavar='', - help='The file to write output to.') + if self.vspec2vss_config.output_file_required: + parser.add_argument('output_file', metavar='', + help='The file to write output to.') if self.vspec2vss_config.type_tree_supported: type_group = parser.add_argument_group('VSS Data Type Tree arguments', diff --git a/vspec/vss2x.py b/vspec/vss2x.py index df90fb7f..59651411 100644 --- a/vspec/vss2x.py +++ b/vspec/vss2x.py @@ -25,11 +25,11 @@ class Vss2X(ABC): creating the VSS model to control the generation. """ - def add_arguments(parser: argparse.ArgumentParser): + def add_arguments(self, parser: argparse.ArgumentParser): pass @abstractmethod - def generate(config: argparse.Namespace, signal_root: VSSNode, vspec2vss_config: Vspec2VssConfig, + def generate(self, config: argparse.Namespace, signal_root: VSSNode, vspec2vss_config: Vspec2VssConfig, data_type_root: Optional[VSSNode] = None): """ Previously called export, but changing to a more generic name.