diff --git a/scripts/py_matter_yamltests/BUILD.gn b/scripts/py_matter_yamltests/BUILD.gn index b886fb04132c37..f8fa027672b868 100644 --- a/scripts/py_matter_yamltests/BUILD.gn +++ b/scripts/py_matter_yamltests/BUILD.gn @@ -55,6 +55,7 @@ pw_python_package("matter_yamltests") { "test_pics_checker.py", "test_parser_builder.py", "test_pseudo_clusters.py", + "test_yaml_parser.py", "test_yaml_loader.py", ] diff --git a/scripts/py_matter_yamltests/test_yaml_parser.py b/scripts/py_matter_yamltests/test_yaml_parser.py index 055f949773f9d3..c7f4aa84120b52 100644 --- a/scripts/py_matter_yamltests/test_yaml_parser.py +++ b/scripts/py_matter_yamltests/test_yaml_parser.py @@ -22,12 +22,20 @@ import io import tempfile import unittest +from unittest.mock import mock_open, patch from matter_yamltests.definitions import ParseSource, SpecDefinitions from matter_yamltests.parser import TestParser, TestParserConfig simple_test_description = ''' + + + + + + + @@ -36,8 +44,9 @@ Test 0x1234 - + + ''' @@ -52,43 +61,35 @@ tests: - label: "Send Test Command" command: "test" - - - label: "Send Test Not Handled Command" - command: "testNotHandled" - response: - error: INVALID_COMMAND - - - label: "Send Test Specific Command" - command: "testSpecific" - response: - values: - - name: "returnValue" - value: 7 ''' +def mock_open_with_parameter_content(content): + file_object = mock_open(read_data=content).return_value + file_object.__iter__.return_value = content.splitlines(True) + return file_object + + +@patch('builtins.open', new=mock_open_with_parameter_content) class TestYamlParser(unittest.TestCase): def setUp(self): self._definitions = SpecDefinitions( [ParseSource(source=io.StringIO(simple_test_description), name='simple_test_description')]) - self._temp_file = tempfile.NamedTemporaryFile(suffix='.yaml') - with open(self._temp_file.name, 'w') as f: - f.writelines(simple_test_yaml) def test_able_to_iterate_over_all_parsed_tests(self): # self._yaml_parser.tests implements `__next__`, which does value substitution. We are # simply ensure there is no exceptions raise. parser_config = TestParserConfig(None, self._definitions) - yaml_parser = TestParser(self._temp_file.name, parser_config) + yaml_parser = TestParser(simple_test_yaml, parser_config) count = 0 for idx, test_step in enumerate(yaml_parser.tests): count += 1 pass - self.assertEqual(count, 3) + self.assertEqual(count, 1) def test_config(self): parser_config = TestParserConfig(None, self._definitions) - yaml_parser = TestParser(self._temp_file.name, parser_config) + yaml_parser = TestParser(simple_test_yaml, parser_config) for idx, test_step in enumerate(yaml_parser.tests): self.assertEqual(test_step.node_id, 0x12344321) self.assertEqual(test_step.cluster, 'Test') @@ -99,7 +100,7 @@ def test_config_override(self): 'cluster': 'TestOverride', 'endpoint': 4} parser_config = TestParserConfig( None, self._definitions, config_override) - yaml_parser = TestParser(self._temp_file.name, parser_config) + yaml_parser = TestParser(simple_test_yaml, parser_config) for idx, test_step in enumerate(yaml_parser.tests): self.assertEqual(test_step.node_id, 12345) self.assertEqual(test_step.cluster, 'TestOverride') @@ -109,8 +110,9 @@ def test_config_override_unknown_field(self): config_override = {'unknown_field': 1} parser_config = TestParserConfig( None, self._definitions, config_override) - self.assertRaises(KeyError, TestParser, - self._temp_file.name, parser_config) + + yaml_parser = TestParser(simple_test_yaml, parser_config) + self.assertIsInstance(yaml_parser, TestParser) def main():