Skip to content

Commit

Permalink
[matter_yamltests] Update the parser to fix some parsing issues with …
Browse files Browse the repository at this point in the history
…the current yaml tests (#24494)
  • Loading branch information
vivien-apple authored and pull[bot] committed Feb 1, 2024
1 parent d6ba100 commit 9062401
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
31 changes: 29 additions & 2 deletions scripts/py_matter_yamltests/matter_yamltests/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, message):


class BaseConstraint(ABC):
'''Constrain Interface'''
'''Constraint Interface'''

def __init__(self, types: list, is_null_allowed: bool = False):
'''An empty type list provided that indicates any type is accepted'''
Expand Down Expand Up @@ -58,7 +58,8 @@ def __init__(self, has_value):
self._has_value = has_value

def check_response(self, value) -> bool:
raise ConstraintValidationError('HasValue constraint currently not implemented')
raise ConstraintValidationError(
'HasValue constraint currently not implemented')


class _ConstraintType(BaseConstraint):
Expand Down Expand Up @@ -355,3 +356,29 @@ def get_constraints(constraints: dict) -> list[BaseConstraint]:
raise ConstraintParseError(f'Unknown constraint type:{constraint}')

return _constraints


def is_typed_constraint(constraint: str):
constraints = {
'hasValue': False,
'type': False,
'minLength': False,
'maxLength': False,
'isHexString': False,
'startsWith': True,
'endsWith': True,
'isUpperCase': False,
'isLowerCase': False,
'minValue': True,
'maxValue': True,
'contains': True,
'excludes': True,
'hasMasksSet': False,
'hasMasksClear': False,
'notValue': True,
}

is_typed = constraints.get(constraint)
if is_typed is None:
raise ConstraintParseError(f'Unknown constraint type:{constraint}')
return is_typed
27 changes: 20 additions & 7 deletions scripts/py_matter_yamltests/matter_yamltests/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import yaml

from . import fixes
from .constraints import get_constraints
from .constraints import get_constraints, is_typed_constraint

_TESTS_SECTION = [
'name',
Expand Down Expand Up @@ -163,16 +163,15 @@ def _check_valid_keys(section, valid_keys_dict):
if section:
for key in section:
if key not in valid_keys_dict:
print(f'Unknown key: {key}')
raise KeyError
raise KeyError(f'Unknown key: {key}')


def _value_or_none(data, key):
return data[key] if key in data else None


def _value_or_config(data, key, config):
return data[key] if key in data else config[key]
return data[key] if key in data else config.get(key)


class _TestStepWithPlaceholders:
Expand Down Expand Up @@ -303,7 +302,18 @@ def _update_with_definition(self, container: dict, mapping_type):

for value in list(container['values']):
for key, item_value in list(value.items()):
mapping = mapping_type if self.is_attribute else mapping_type[value['name']]
if self.is_attribute:
mapping = mapping_type
else:
target_key = value['name']
if mapping_type.get(target_key) is None:
for candidate_key in mapping_type:
if candidate_key.lower() == target_key.lower():
raise KeyError(
f'"{self.label}": Unknown key: "{target_key}". Did you mean "{candidate_key}" ?')
raise KeyError(
f'"{self.label}": Unknown key: "{target_key}". Candidates are: "{[ key for key in mapping_type]}".')
mapping = mapping_type[target_key]

if key == 'value':
value[key] = self._update_value_with_definition(
Expand All @@ -312,8 +322,11 @@ def _update_with_definition(self, container: dict, mapping_type):
self._parsing_config_variable_storage[item_value] = None
elif key == 'constraints':
for constraint, constraint_value in item_value.items():
value[key][constraint] = self._update_value_with_definition(
constraint_value, mapping_type)
# Only apply update_value_with_definition to constraints that have a value that depends on
# the the value type for the target field.
if is_typed_constraint(constraint):
value[key][constraint] = self._update_value_with_definition(
constraint_value, mapping_type)
else:
# This key, value pair does not rely on cluster specifications.
pass
Expand Down

0 comments on commit 9062401

Please sign in to comment.