From 142e6865ed56eeaa3372ba6ed62601e1a9e855e5 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Tue, 24 Jan 2023 20:37:13 +0100 Subject: [PATCH] [matter_yamltests] Add is_nullable method to SpecDefinitions (#24624) --- .../matter_yamltests/definitions.py | 5 +++ .../test_spec_definitions.py | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/scripts/py_matter_yamltests/matter_yamltests/definitions.py b/scripts/py_matter_yamltests/matter_yamltests/definitions.py index 750fd676aa1a50..9f1960a1ae815a 100644 --- a/scripts/py_matter_yamltests/matter_yamltests/definitions.py +++ b/scripts/py_matter_yamltests/matter_yamltests/definitions.py @@ -146,6 +146,11 @@ def is_fabric_scoped(self, target) -> bool: return bool(target.qualities & StructQuality.FABRIC_SCOPED) return False + def is_nullable(self, target) -> bool: + if hasattr(target, 'qualities'): + return bool(target.qualities & FieldQuality.NULLABLE) + return False + def __get_by_name(self, cluster_name: str, target_name: str, target_type: _ItemType): if not cluster_name or not target_name: return None diff --git a/scripts/py_matter_yamltests/test_spec_definitions.py b/scripts/py_matter_yamltests/test_spec_definitions.py index 9065a5c0a182cd..1f32670ced962d 100644 --- a/scripts/py_matter_yamltests/test_spec_definitions.py +++ b/scripts/py_matter_yamltests/test_spec_definitions.py @@ -54,6 +54,21 @@ ''' +source_response_with_nullable = ''' + + + Test + 0x1234 + + + + + + + + +''' + source_attribute = ''' @@ -184,6 +199,24 @@ def test_response_name(self): self.assertEqual(definitions.get_response_name( 0x1234, 0x0), 'TestCommandResponse') + def test_response_name_with_nullable(self): + definitions = SpecDefinitions( + [ParseSource(source=io.StringIO(source_response_with_nullable), name='source_response_with_nullable')]) + cluster_name = 'Test' + response_name = 'TestCommandResponse' + + self.assertEqual(definitions.get_cluster_name(0x1234), cluster_name) + self.assertEqual(definitions.get_response_name( + 0x1234, 0x0), response_name) + + response = definitions.get_response_by_name( + cluster_name, response_name) + for field in response.fields: + if field.name == 'arg1': + self.assertFalse(definitions.is_nullable(field)) + else: + self.assertTrue(definitions.is_nullable(field)) + def test_attribute_name(self): definitions = SpecDefinitions( [ParseSource(source=io.StringIO(source_attribute), name='source_attribute')])