|
| 1 | +# Copyright (C) 2024 Intel Corporation |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, |
| 10 | +# software distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions |
| 13 | +# and limitations under the License. |
| 14 | +# |
| 15 | +# SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +import unittest |
| 18 | +import py_trees |
| 19 | +from datetime import datetime |
| 20 | +from scenario_execution.scenario_execution_base import ScenarioExecution |
| 21 | +from scenario_execution.model.osc2_parser import OpenScenario2Parser |
| 22 | +from scenario_execution.model.model_to_py_tree import create_py_tree |
| 23 | +from .common import DebugLogger |
| 24 | +from antlr4.InputStream import InputStream |
| 25 | + |
| 26 | + |
| 27 | +class TestExternalMethodsRandom(unittest.TestCase): |
| 28 | + # pylint: disable=missing-function-docstring |
| 29 | + |
| 30 | + def setUp(self) -> None: |
| 31 | + self.logger = DebugLogger("") |
| 32 | + self.parser = OpenScenario2Parser(self.logger) |
| 33 | + self.tree = py_trees.composites.Sequence(name="", memory=True) |
| 34 | + self.scenario_execution = ScenarioExecution(debug=False, |
| 35 | + log_model=False, |
| 36 | + live_tree=False, |
| 37 | + scenario_file='test', |
| 38 | + output_dir='', logger=self.logger) |
| 39 | + self.tree = py_trees.composites.Sequence(name="", memory=True) |
| 40 | + |
| 41 | + def execute(self, scenario_content): |
| 42 | + parsed_tree = self.parser.parse_input_stream(InputStream(scenario_content)) |
| 43 | + model = self.parser.create_internal_model(parsed_tree, self.tree, "test.osc", False) |
| 44 | + self.tree = create_py_tree(model, self.tree, self.parser.logger, False) |
| 45 | + self.scenario_execution.tree = self.tree |
| 46 | + self.scenario_execution.run() |
| 47 | + |
| 48 | + def test_get_random_int(self): |
| 49 | + scenario_content = """ |
| 50 | +import osc.helpers |
| 51 | +
|
| 52 | +scenario test_success: |
| 53 | + do serial: |
| 54 | + wait elapsed(random.get_int(0, 5)) |
| 55 | + emit end |
| 56 | +""" |
| 57 | + parsed_tree = self.parser.parse_input_stream(InputStream(scenario_content)) |
| 58 | + model = self.parser.create_internal_model(parsed_tree, self.tree, "test.osc", False) |
| 59 | + self.tree = create_py_tree(model, self.tree, self.parser.logger, False) |
| 60 | + self.scenario_execution.tree = self.tree |
| 61 | + |
| 62 | + start_time = datetime.now() |
| 63 | + self.scenario_execution.run() |
| 64 | + end_time = datetime.now() |
| 65 | + self.assertTrue(self.scenario_execution.process_results()) |
| 66 | + |
| 67 | + delta = end_time - start_time |
| 68 | + self.assertLess(delta.total_seconds(), 5.) |
| 69 | + |
| 70 | + def test_get_random_string(self): |
| 71 | + scenario_content = """ |
| 72 | +import osc.helpers |
| 73 | +
|
| 74 | +scenario test_success: |
| 75 | + do serial: |
| 76 | + log(random.get_random_string(["test", "test-scenario", "scenario-test"])) |
| 77 | + emit end |
| 78 | +""" |
| 79 | + self.execute(scenario_content) |
| 80 | + self.assertTrue(self.scenario_execution.process_results()) |
| 81 | + valid_strings = ["test", "test-scenario", "scenario-test"] |
| 82 | + log_messages = self.logger.logs_info |
| 83 | + for log_message in log_messages: |
| 84 | + if log_message in valid_strings: |
| 85 | + self.assertIn(log_message, valid_strings) |
0 commit comments