diff --git a/scripts/py_matter_yamltests/matter_yamltests/errors.py b/scripts/py_matter_yamltests/matter_yamltests/errors.py index 97a71a24133106..b6205dad9efabb 100644 --- a/scripts/py_matter_yamltests/matter_yamltests/errors.py +++ b/scripts/py_matter_yamltests/matter_yamltests/errors.py @@ -176,3 +176,14 @@ def __init__(self, content): self.tag_key_with_error(content, 'wait') self.tag_key_with_error(content, 'response') + + +class TestStepResponseVariableError(TestStepError): + """Raise when a test step response use a variable but this variable does not exist in the config section. + """ + + def __init__(self, content): + message = 'The variable does not exist in the config section.' + super().__init__(message) + + self.tag_key_with_error(content, 'response') diff --git a/scripts/py_matter_yamltests/matter_yamltests/yaml_loader.py b/scripts/py_matter_yamltests/matter_yamltests/yaml_loader.py index f806127c96f7d1..63170fbcc66460 100644 --- a/scripts/py_matter_yamltests/matter_yamltests/yaml_loader.py +++ b/scripts/py_matter_yamltests/matter_yamltests/yaml_loader.py @@ -16,8 +16,8 @@ from typing import Tuple, Union from .errors import (TestStepError, TestStepGroupResponseError, TestStepInvalidTypeError, TestStepKeyError, - TestStepNodeIdAndGroupIdError, TestStepValueAndValuesError, TestStepVerificationStandaloneError, - TestStepWaitResponseError) + TestStepNodeIdAndGroupIdError, TestStepResponseVariableError, TestStepValueAndValuesError, + TestStepVerificationStandaloneError, TestStepWaitResponseError) from .fixes import add_yaml_support_for_scientific_notation_without_dot try: @@ -78,12 +78,13 @@ def __check_content(self, content): tests = content.get('tests', []) for step_index, step in enumerate(tests): try: - self.__check_test_step(step) + config = content.get('config', {}) + self.__check_test_step(config, step) except TestStepError as e: e.update_context(step, step_index) raise - def __check_test_step(self, content): + def __check_test_step(self, config: dict, content): schema = { 'label': str, 'identity': str, @@ -101,7 +102,7 @@ def __check_test_step(self, content): 'verification': str, 'PICS': str, 'arguments': dict, - 'response': (dict, list), + 'response': (dict, list, str), # Can be a variable 'minInterval': int, 'maxInterval': int, 'timedInteractionTimeoutMs': int, @@ -116,6 +117,7 @@ def __check_test_step(self, content): self.__rule_step_with_verification_should_be_disabled_or_interactive( content) self.__rule_wait_should_not_expect_a_response(content) + self.__rule_response_variable_should_exist_in_config(config, content) if 'arguments' in content: arguments = content.get('arguments') @@ -123,6 +125,13 @@ def __check_test_step(self, content): if 'response' in content: response = content.get('response') + + # If the response is a variable, update the response value with the content of the variable such + # such that the error message looks nice if needed. + if isinstance(response, str): + response = config.get(response) + content['response'] = response + if isinstance(response, list): [self.__check_test_step_response(x) for x in response] else: @@ -241,3 +250,9 @@ def __rule_response_value_and_values_are_mutually_exclusive(self, content): def __rule_wait_should_not_expect_a_response(self, content): if 'wait' in content and 'response' in content: raise TestStepWaitResponseError(content) + + def __rule_response_variable_should_exist_in_config(self, config, content): + if 'response' in content: + response = content.get('response') + if isinstance(response, str) and response not in config: + raise TestStepResponseVariableError(content) diff --git a/scripts/py_matter_yamltests/test_yaml_loader.py b/scripts/py_matter_yamltests/test_yaml_loader.py index 067dc831627907..146c30e0a13dc6 100644 --- a/scripts/py_matter_yamltests/test_yaml_loader.py +++ b/scripts/py_matter_yamltests/test_yaml_loader.py @@ -301,7 +301,7 @@ def test_key_tests_step_response_key(self): _, _, _, _, tests = load(content.format(value=value)) self.assertEqual(tests, [{'response': [{'value': True}]}]) - wrong_values = self._get_wrong_values([dict, list], spaces=6) + wrong_values = self._get_wrong_values([dict, list, str], spaces=6) for value in wrong_values: x = content.format(value=value) self.assertRaises(TestStepInvalidTypeError, load, x)