Skip to content

Commit 9d46b23

Browse files
authored
Fix python evaluation of arithmetic statements with no spaces (#32911)
1 parent d65f300 commit 9d46b23

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

scripts/py_matter_yamltests/matter_yamltests/parser.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,8 @@ def _config_variable_substitution(self, value):
12231223
# But some other tests were relying on the fact that the expression was put 'as if' in
12241224
# the generated code and was resolved before being sent over the wire. For such
12251225
# expressions (e.g 'myVar + 1') we need to compute it before sending it over the wire.
1226-
tokens = value.split()
1226+
delimiter_regex = "(\ |\(|\)|\+|\-|\*|\/|\%)"
1227+
tokens = re.split(delimiter_regex, value)
12271228
if len(tokens) == 0:
12281229
return value
12291230

@@ -1240,7 +1241,7 @@ def _config_variable_substitution(self, value):
12401241
return tokens[0]
12411242

12421243
tokens = [str(token) for token in tokens]
1243-
value = ' '.join(tokens)
1244+
value = ''.join(tokens)
12441245
# TODO we should move away from eval. That will mean that we will need to do extra
12451246
# parsing, but it would be safer then just blindly running eval.
12461247
return value if not substitution_occured else eval(value)

scripts/py_matter_yamltests/test_yaml_parser.py

+89
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
<attribute side="server" code="0x0024" type="TestEnum" writable="true" optional="false">test_enum</attribute>
4949
5050
<command source="client" code="1" name="test"></command>
51+
<command source="client" code="2" name="IntTest">
52+
<arg name="arg" type="int8u"/>
53+
</command>
5154
</cluster>
5255
</configurator>
5356
'''
@@ -175,6 +178,81 @@
175178
value: TestEnum.UnknownEnumValue(0)
176179
'''
177180

181+
_BASIC_ARITHMETIC_ARG_RESULTS = [6, 6, 2, 2, 8, 8, 2, 2, 1]
182+
basic_arithmetic_yaml = '''
183+
name: Test Cluster Tests
184+
185+
config:
186+
nodeId: 0x12344321
187+
cluster: "Test"
188+
endpoint: 1
189+
myVariable: 4
190+
191+
tests:
192+
- label: "Add 2"
193+
command: "IntTest"
194+
arguments:
195+
values:
196+
- name: "arg"
197+
value: myVariable + 2
198+
199+
- label: "Add 2 with no space"
200+
command: "IntTest"
201+
arguments:
202+
values:
203+
- name: "arg"
204+
value: myVariable+2
205+
206+
- label: "Minus 2"
207+
command: "IntTest"
208+
arguments:
209+
values:
210+
- name: "arg"
211+
value: myVariable - 2
212+
213+
- label: "Minus 2 with no space"
214+
command: "IntTest"
215+
arguments:
216+
values:
217+
- name: "arg"
218+
value: myVariable-2
219+
220+
- label: "Multiply by 2"
221+
command: "IntTest"
222+
arguments:
223+
values:
224+
- name: "arg"
225+
value: myVariable * 2
226+
227+
- label: "Multiply by 2 with no space"
228+
command: "IntTest"
229+
arguments:
230+
values:
231+
- name: "arg"
232+
value: myVariable*2
233+
234+
- label: "Divide by 2"
235+
command: "IntTest"
236+
arguments:
237+
values:
238+
- name: "arg"
239+
value: myVariable / 2
240+
241+
- label: "Divide by 2 with no space"
242+
command: "IntTest"
243+
arguments:
244+
values:
245+
- name: "arg"
246+
value: myVariable/2
247+
248+
- label: "Arithmetic with parentheses"
249+
command: "IntTest"
250+
arguments:
251+
values:
252+
- name: "arg"
253+
value: (myVariable +3)/7
254+
'''
255+
178256

179257
def mock_open_with_parameter_content(content):
180258
file_object = mock_open(read_data=content).return_value
@@ -207,6 +285,17 @@ def test_config(self):
207285
self.assertEqual(test_step.cluster, 'Test')
208286
self.assertEqual(test_step.endpoint, 1)
209287

288+
def test_basic_arithmetic(self):
289+
parser_config = TestParserConfig(None, self._definitions)
290+
291+
yaml_parser = TestParser(basic_arithmetic_yaml, parser_config)
292+
for idx, test_step in enumerate(yaml_parser.tests):
293+
values = test_step.arguments['values']
294+
self.assertEqual(len(values), 1)
295+
value = values[0]
296+
self.assertEqual(value['name'], 'arg')
297+
self.assertEqual(value['value'], _BASIC_ARITHMETIC_ARG_RESULTS[idx])
298+
210299
def test_config_override(self):
211300
config_override = {'nodeId': 12345,
212301
'cluster': 'TestOverride', 'endpoint': 4}

0 commit comments

Comments
 (0)