Skip to content

Commit

Permalink
Adding test case as example
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Jaegervall <[email protected]>
  • Loading branch information
erikbosch committed Dec 1, 2023
1 parent fc73044 commit 079ab4d
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 4 deletions.
51 changes: 51 additions & 0 deletions tests/generators/example_generator.py
Original file line number Diff line number Diff line change
@@ -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:])
27 changes: 27 additions & 0 deletions tests/generators/test.vspec
Original file line number Diff line number Diff line change
@@ -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!
30 changes: 30 additions & 0 deletions tests/generators/test_generator.py
Original file line number Diff line number Diff line change
@@ -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")
3 changes: 3 additions & 0 deletions vspec/vspec2vss_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions vspec/vspec2x.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='<vspec_file>',
help='The vehicle specification file to convert.')
parser.add_argument('output_file', metavar='<output_file>',
help='The file to write output to.')
if self.vspec2vss_config.output_file_required:
parser.add_argument('output_file', metavar='<output_file>',
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',
Expand Down
4 changes: 2 additions & 2 deletions vspec/vss2x.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 079ab4d

Please sign in to comment.