test_yaml_parser.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #
  2. # Copyright (c) 2022 Project CHIP Authors
  3. # All rights reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # TODO Once yamltest is a proper self contained module we can move this file
  18. # to a more appropriate spot. For now, having this file to do some quick checks
  19. # is arguably better then no checks at all.
  20. import io
  21. import tempfile
  22. import unittest
  23. from matter_yamltests.definitions import ParseSource, SpecDefinitions
  24. from matter_yamltests.parser import TestParser, TestParserConfig
  25. simple_test_description = '''<?xml version="1.0"?>
  26. <configurator>
  27. <struct name="TestStruct">
  28. <cluster code="0x1234"/>
  29. <item name="a" type="boolean"/>
  30. </struct>
  31. <cluster>
  32. <name>Test</name>
  33. <code>0x1234</code>
  34. </cluster>
  35. </configurator>
  36. '''
  37. simple_test_yaml = '''
  38. name: Test Cluster Tests
  39. config:
  40. nodeId: 0x12344321
  41. cluster: "Test"
  42. endpoint: 1
  43. tests:
  44. - label: "Send Test Command"
  45. command: "test"
  46. - label: "Send Test Not Handled Command"
  47. command: "testNotHandled"
  48. response:
  49. error: INVALID_COMMAND
  50. - label: "Send Test Specific Command"
  51. command: "testSpecific"
  52. response:
  53. values:
  54. - name: "returnValue"
  55. value: 7
  56. '''
  57. class TestYamlParser(unittest.TestCase):
  58. def setUp(self):
  59. self._definitions = SpecDefinitions(
  60. [ParseSource(source=io.StringIO(simple_test_description), name='simple_test_description')])
  61. self._temp_file = tempfile.NamedTemporaryFile(suffix='.yaml')
  62. with open(self._temp_file.name, 'w') as f:
  63. f.writelines(simple_test_yaml)
  64. def test_able_to_iterate_over_all_parsed_tests(self):
  65. # self._yaml_parser.tests implements `__next__`, which does value substitution. We are
  66. # simply ensure there is no exceptions raise.
  67. parser_config = TestParserConfig(None, self._definitions)
  68. yaml_parser = TestParser(self._temp_file.name, parser_config)
  69. count = 0
  70. for idx, test_step in enumerate(yaml_parser.tests):
  71. count += 1
  72. pass
  73. self.assertEqual(count, 3)
  74. def test_config(self):
  75. parser_config = TestParserConfig(None, self._definitions)
  76. yaml_parser = TestParser(self._temp_file.name, parser_config)
  77. for idx, test_step in enumerate(yaml_parser.tests):
  78. self.assertEqual(test_step.node_id, 0x12344321)
  79. self.assertEqual(test_step.cluster, 'Test')
  80. self.assertEqual(test_step.endpoint, 1)
  81. def test_config_override(self):
  82. config_override = {'nodeId': 12345,
  83. 'cluster': 'TestOverride', 'endpoint': 4}
  84. parser_config = TestParserConfig(
  85. None, self._definitions, config_override)
  86. yaml_parser = TestParser(self._temp_file.name, parser_config)
  87. for idx, test_step in enumerate(yaml_parser.tests):
  88. self.assertEqual(test_step.node_id, 12345)
  89. self.assertEqual(test_step.cluster, 'TestOverride')
  90. self.assertEqual(test_step.endpoint, 4)
  91. def test_config_override_unknown_field(self):
  92. config_override = {'unknown_field': 1}
  93. parser_config = TestParserConfig(
  94. None, self._definitions, config_override)
  95. self.assertRaises(KeyError, TestParser,
  96. self._temp_file.name, parser_config)
  97. def main():
  98. unittest.main()
  99. if __name__ == '__main__':
  100. main()