Skip to content

Commit

Permalink
Common YAML test parser (#24066)
Browse files Browse the repository at this point in the history
* Initial commit of 3 files from Vivien's branch

These 3 files came from commit ID
e391dad

* Adding a test commit containing a TODO

* Restyle

* Small tweaks plus adding unit test

* Moving towards PEP8 naming convension
* Refactor results to be less focused string matching
* Added very basic unit test file

Test run using:
`python3 scripts/tests/test_yaml_parser.py`

* Refactor constraints

* [Yaml Parser] Add scripts/tests/yamltests/SpecDefinitions.py with some unit tests

* [Yaml Parser] Use SpecDefinitions API instead of ClusterDefinitions

* [Yaml Parser] Uses convert_yaml_octet_string_to_bytes for octet_string

* [Yaml Parser / Constraints] Move the check for None before the type checking

* [Yaml Parser] Add _convert_single_value_to_values and make it sooner such that some code can be made simpler

* Small fixes

* [Yaml Parser] Moves _convert_single_value_to_values up one block such that update_with_definition can be simplified

* Move `__update_placeholder` to TestStep

Also TestStep have internal pointer to config dictionary

* Add docstring, and initial constraint parsing

* Refactor for TestStep into two parts

As well as stype formatting to get it ready for review

* Converge to PEP8 python format standard

* Add and clean up doc strings

* Attempt at creating a python package

* Make Restyle Happy

* Clean up yaml parser unit test to be self contained

* Fix test name

* Fix test name

* Address PR comments

Co-authored-by: Andrei Litvin <andy314@gmail.com>
Co-authored-by: Vivien Nicolas <vnicolas@apple.com>
  • Loading branch information
3 people authored and pull[bot] committed Jul 14, 2023
1 parent 902fa65 commit 2006850
Show file tree
Hide file tree
Showing 9 changed files with 1,834 additions and 0 deletions.
95 changes: 95 additions & 0 deletions scripts/tests/test_yaml_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#
# Copyright (c) 2022 Project CHIP Authors
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# TODO Once yamltest is a proper self contained module we can move this file
# to a more appropriate spot. For now, having this file to do some quick checks
# is arguably better then no checks at all.

import io
import tempfile
import unittest

from yamltests.definitions import *
from yamltests.parser import TestParser

simple_test_description = '''<?xml version="1.0"?>
<configurator>
<struct name="TestStruct">
<cluster code="0x1234"/>
<item name="a" type="boolean"/>
</struct>
<cluster>
<name>Test</name>
<code>0x1234</code>
</cluster>
</configurator>
'''

simple_test_yaml = '''
name: Test Cluster Tests
config:
nodeId: 0x12344321
cluster: "Test"
endpoint: 1
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
'''


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)
pics_file = None
self._yaml_parser = TestParser(self._temp_file.name, pics_file, self._definitions)

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.
count = 0
for idx, test_step in enumerate(self._yaml_parser.tests):
count += 1
pass
self.assertEqual(count, 3)


def main():
unittest.main()


if __name__ == '__main__':
main()
39 changes: 39 additions & 0 deletions scripts/tests/yamltests/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (c) 2022 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")

import("//build_overrides/pigweed.gni")
import("$dir_pw_build/python.gni")

pw_python_package("yamltests") {
setup = [ "setup.py" ]

sources = [
"__init__.py",
"constraints.py",
"definitions.py",
"fixes.py",
"parser.py",
]

python_deps = [ "${chip_root}/scripts/idl" ]

tests = [ "test_spec_definitions.py" ]

# TODO: at a future time consider enabling all (* or missing) here to get
# pylint checking these files
static_analysis = []
}
Empty file.
Loading

0 comments on commit 2006850

Please sign in to comment.