Skip to content

Commit

Permalink
Enhance WHILE and IF validation failure messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
pekkaklarck committed May 5, 2023
1 parent 8cce3fc commit 12652d5
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 31 deletions.
2 changes: 1 addition & 1 deletion atest/robot/running/while/while_limit.robot
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ Limit used multiple times

Invalid values after limit
${tc} = Check Test Case ${TESTNAME}
Should Be Equal ${tc.body[0].condition} $variable < 2, limit=-1x, invalid, values
Should Be Equal ${tc.body[0].condition} $variable < 2, limit=2, invalid
9 changes: 4 additions & 5 deletions atest/testdata/running/if/invalid_if.robot
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ Recommend $var syntax if invalid condition contains ${var}
Fail Shouldn't be run
END


IF without END
[Documentation] FAIL IF must have closing END.
IF ${True}
Expand Down Expand Up @@ -98,7 +97,7 @@ ELSE IF without condition
END

ELSE IF with multiple conditions
[Documentation] FAIL ELSE IF cannot have more than one condition.
[Documentation] FAIL ELSE IF cannot have more than one condition, got '\${False}', 'ooops' and '\${True}'.
IF 'maa' == 'maa'
Fail Should not be run
ELSE IF ${False} ooops ${True}
Expand Down Expand Up @@ -216,16 +215,16 @@ Multiple errors
... - ELSE IF not allowed after ELSE.
... - Only one ELSE allowed.
... - IF must have closing END.
... - ELSE IF cannot have more than one condition.
... - ELSE IF cannot have more than one condition, got 'too' and 'many'.
... - ELSE IF branch cannot be empty.
... - ELSE does not accept arguments, got 'oops'.
... - ELSE does not accept arguments, got 'oops', 'i', 'did', 'it' and 'again'.
... - ELSE branch cannot be empty.
... - ELSE IF must have a condition.
... - ELSE IF branch cannot be empty.
... - ELSE branch cannot be empty.
IF
ELSE IF too many
ELSE oops
ELSE oops i did it again
ELSE IF
ELSE

Expand Down
16 changes: 8 additions & 8 deletions atest/testdata/running/while/invalid_while.robot
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
*** Test Cases ***
Multiple conditions
[Documentation] FAIL WHILE cannot have more than one condition, got 'Too', 'many', 'conditions' and '!'.
[Documentation] FAIL WHILE loop cannot have more than one condition, got 'Too', 'many', 'conditions' and '!'.
WHILE Too many conditions !
Fail Not executed!
END

Invalid condition
[Documentation] FAIL Invalid WHILE condition: \
[Documentation] FAIL Invalid WHILE loop condition: \
... Evaluating expression 'bad' failed: NameError: name 'bad' is not defined nor importable as module
WHILE bad
Fail Not executed!
END

Non-existing ${variable} in condition
[Documentation] FAIL Invalid WHILE condition: \
[Documentation] FAIL Invalid WHILE loop condition: \
... Evaluating expression '\${bad} > 0' failed: Variable '\${bad}' not found.
WHILE ${bad} > 0
Fail Not executed!
END

Non-existing $variable in condition
[Documentation] FAIL Invalid WHILE condition: \
[Documentation] FAIL Invalid WHILE loop condition: \
... Evaluating expression '$bad > 0' failed: Variable '$bad' not found.
WHILE $bad > 0
Fail Not executed!
END

Recommend $var syntax if invalid condition contains ${var}
[Documentation] FAIL Invalid WHILE condition: \
[Documentation] FAIL Invalid WHILE loop condition: \
... Evaluating expression 'x == 'x'' failed: NameError: name 'x' is not defined nor importable as module
...
... Variables in the original expression '\${x} == 'x'' were resolved before the expression was evaluated. \
Expand All @@ -38,7 +38,7 @@ Recommend $var syntax if invalid condition contains ${var}
END

Invalid condition on second round
[Documentation] FAIL Invalid WHILE condition: \
[Documentation] FAIL Invalid WHILE loop condition: \
... Evaluating expression 'bad' failed: NameError: name 'bad' is not defined nor importable as module
...
... Variables in the original expression '\${condition}' were resolved before the expression was evaluated. \
Expand Down Expand Up @@ -76,7 +76,7 @@ Invalid condition causes normal error
WHILE bad
Fail Should not be run
END
EXCEPT Invalid WHILE condition: Evaluating expression 'bad' failed: NameError: name 'bad' is not defined nor importable as module
EXCEPT Invalid WHILE loop condition: Evaluating expression 'bad' failed: NameError: name 'bad' is not defined nor importable as module
No Operation
END

Expand All @@ -85,6 +85,6 @@ Non-existing variable in condition causes normal error
WHILE ${bad}
Fail Should not be run
END
EXCEPT Invalid WHILE condition: Evaluating expression '\${bad}' failed: Variable '\${bad}' not found.
EXCEPT Invalid WHILE loop condition: Evaluating expression '\${bad}' failed: Variable '\${bad}' not found.
No Operation
END
6 changes: 3 additions & 3 deletions atest/testdata/running/while/on_limit.robot
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Invalid on_limit
END

On limit without limit defined
[Documentation] FAIL WHILE on_limit option cannot be used without limit.
[Documentation] FAIL WHILE loop 'on_limit' option cannot be used without 'limit'.
WHILE True on_limit=PaSS
No Operation
END
Expand All @@ -85,7 +85,7 @@ On limit message without limit
END

Wrong WHILE argument
[Documentation] FAIL WHILE cannot have more than one condition, got '$variable < 2', 'limit=5' and 'limit_exceed_messag=Custom error message'.
[Documentation] FAIL WHILE loop cannot have more than one condition, got '$variable < 2', 'limit=5' and 'limit_exceed_messag=Custom error message'.
WHILE $variable < 2 limit=5 limit_exceed_messag=Custom error message
Log ${variable}
END
Expand Down Expand Up @@ -135,7 +135,7 @@ On limit message with invalid variable
END

Wrong WHILE arguments
[Documentation] FAIL WHILE cannot have more than one condition, got '$variable < 2', 'limite=5' and 'limit_exceed_messag=Custom error message'.
[Documentation] FAIL WHILE loop cannot have more than one condition, got '$variable < 2', 'limite=5' and 'limit_exceed_messag=Custom error message'.
WHILE $variable < 2 limite=5 limit_exceed_messag=Custom error message
Log ${variable}
END
8 changes: 4 additions & 4 deletions atest/testdata/running/while/while_limit.robot
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ Invalid limit invalid value
END

Invalid limit mistyped prefix
[Documentation] FAIL WHILE cannot have more than one condition, got '$variable < 2' and 'limitation=-1x'.
WHILE $variable < 2 limitation=-1x
[Documentation] FAIL WHILE loop cannot have more than one condition, got '$variable < 2' and 'limitation=2'.
WHILE $variable < 2 limitation=2
Log ${variable}
END

Expand All @@ -112,8 +112,8 @@ Limit used multiple times
END

Invalid values after limit
[Documentation] FAIL WHILE cannot have more than one condition, got '$variable < 2', 'limit=-1x', 'invalid' and 'values'.
WHILE $variable < 2 limit=-1x invalid values
[Documentation] FAIL WHILE loop cannot have more than one condition, got '$variable < 2', 'limit=2' and 'invalid'.
WHILE $variable < 2 limit=2 invalid
Log ${variable}
END

Expand Down
21 changes: 13 additions & 8 deletions src/robot/parsing/model/statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,11 +980,12 @@ def assign(self) -> 'tuple[str, ...]':
return self.get_values(Token.ASSIGN)

def validate(self, ctx: 'ValidationContext'):
conditions = len(self.get_tokens(Token.ARGUMENT))
if conditions == 0:
conditions = self.get_tokens(Token.ARGUMENT)
if not conditions:
self.errors += (f'{self.type} must have a condition.',)
if conditions > 1:
self.errors += (f'{self.type} cannot have more than one condition.',)
if len(conditions) > 1:
self.errors += (f'{self.type} cannot have more than one condition, '
f'got {seq2str(c.value for c in conditions)}.',)


@Statement.register
Expand Down Expand Up @@ -1181,13 +1182,17 @@ def on_limit_message(self) -> 'str|None':
return self.get_option('on_limit_message')

def validate(self, ctx: 'ValidationContext'):
values = self.get_values(Token.ARGUMENT)
if len(values) > 1:
self.errors += (f'WHILE cannot have more than one condition, got {seq2str(values)}.',)
conditions = self.get_tokens(Token.ARGUMENT)
if len(conditions) > 1:
self._add_error(f'cannot have more than one condition, got '
f'{seq2str(c.value for c in conditions)}')
if self.on_limit and not self.limit:
self.errors += ('WHILE on_limit option cannot be used without limit.',)
self._add_error("'on_limit' option cannot be used without 'limit'")
self._validate_options()

def _add_error(self, error: str):
self.errors += (f'WHILE loop {error}.',)


@Statement.register
class ReturnStatement(Statement):
Expand Down
2 changes: 1 addition & 1 deletion src/robot/running/bodyrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def _should_run(self, condition, variables):
resolve_variables=True)
except Exception:
msg = get_error_message()
raise DataError(f'Invalid WHILE condition: {msg}')
raise DataError(f'Invalid WHILE loop condition: {msg}')


class IfRunner:
Expand Down
3 changes: 2 additions & 1 deletion utest/parsing/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,8 @@ def test_invalid(self):
Token(Token.ARGUMENT, 'many', 3, 20),
Token(Token.ARGUMENT, 'values', 3, 28),
Token(Token.ARGUMENT, '!', 3, 38)],
errors=('WHILE cannot have more than one condition, got \'too\', \'many\', \'values\' and \'!\'.',)
errors=("WHILE loop cannot have more than one condition, "
"got 'too', 'many', 'values' and '!'.",)
),
end=End([
Token(Token.END, 'END', 5, 4)
Expand Down

0 comments on commit 12652d5

Please sign in to comment.