Skip to content

Commit 77b8968

Browse files
committed
feat(if) add own statement for InlineIfHeader
1 parent 3b11555 commit 77b8968

File tree

15 files changed

+61
-45
lines changed

15 files changed

+61
-45
lines changed

atest/robot/running/if/inline_if_else.robot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ Assignment inside inline if
3333
Inline if inside for loop
3434
Check Test Case ${TESTNAME}
3535

36+
Inline if inside block if
37+
Check Test Case ${TESTNAME}
38+
3639
Inline if inside nested loop
3740
Check Test Case ${TESTNAME}
3841

atest/testdata/cli/dryrun/if.robot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Dryrun fail inside of ELSE
4242
This is validated
4343

4444
Dryrun fail invalid IF in non executed branch
45-
[Documentation] FAIL IF has more than one condition.
45+
[Documentation] FAIL IF has no condition.
4646
IF 1 > 2
4747
Keyword with invalid if
4848
END
@@ -75,7 +75,7 @@ Dryrun fail empty if in non executed branch
7575

7676
*** Keywords ***
7777
Keyword with invalid if
78-
IF 1 == 1 2 == 2
78+
IF
7979
Log invalid
8080
END
8181

atest/testdata/running/if/inline_if_else.robot

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,21 @@ Inline if else - else executed - failing
3030

3131
Assignment inside inline if
3232
IF True ${num}= Convert to number 12
33-
Variable Should Exist $num
33+
Should Be Equal ${num} ${12}
3434

3535
Inline if inside for loop
3636
[Documentation] FAIL The end
3737
FOR ${i} IN 1 2 3
3838
IF ${i} == 3 Fail The end ELSE Log ${i}
3939
END
4040

41+
Inline if inside block if
42+
IF ${True}
43+
Log Hi
44+
IF 3==4 Fail Should not be executed ELSE Log Hello
45+
Log Goodbye
46+
END
47+
4148
Inline if inside nested loop
4249
[Documentation] FAIL The end
4350
IF ${False}

src/robot/api/parsing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ class were exposed directly via the :mod:`robot.api` package, but other
225225
- :class:`~robot.parsing.model.statements.TemplateArguments`
226226
- :class:`~robot.parsing.model.statements.ForHeader`
227227
- :class:`~robot.parsing.model.statements.IfHeader`
228+
- :class:`~robot.parsing.model.statements.InlineIfHeader`
228229
- :class:`~robot.parsing.model.statements.ElseIfHeader`
229230
- :class:`~robot.parsing.model.statements.ElseHeader`
230231
- :class:`~robot.parsing.model.statements.End`
@@ -519,6 +520,7 @@ def visit_File(self, node):
519520
TemplateArguments,
520521
ForHeader,
521522
IfHeader,
523+
InlineIfHeader,
522524
ElseIfHeader,
523525
ElseHeader,
524526
End,

src/robot/model/body.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class BodyItem(ModelObject):
2727
FOR_ITERATION = 'FOR ITERATION'
2828
IF_ELSE_ROOT = 'IF/ELSE ROOT'
2929
IF = 'IF'
30+
INLINE_IF = 'INLINE IF'
3031
ELSE_IF = 'ELSE IF'
3132
ELSE = 'ELSE'
3233
MESSAGE = 'MESSAGE'

src/robot/model/control.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def id(self):
106106
return '%s-k%d' % (self.parent.parent.id, index)
107107

108108
def __str__(self):
109-
if self.type == self.IF:
109+
if self.type in (self.IF, self.INLINE_IF):
110110
return 'IF %s' % self.condition
111111
if self.type == self.ELSE_IF:
112112
return 'ELSE IF %s' % self.condition

src/robot/output/logger.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ class LoggerProxy(AbstractLoggerProxy):
249249
_start_keyword_methods = {
250250
'IF/ELSE ROOT': 'start_if',
251251
'IF': 'start_if_branch',
252+
'INLINE IF': 'start_if_branch',
252253
'ELSE IF': 'start_if_branch',
253254
'ELSE': 'start_if_branch',
254255
'FOR': 'start_for',
@@ -257,6 +258,7 @@ class LoggerProxy(AbstractLoggerProxy):
257258
_end_keyword_methods = {
258259
'IF/ELSE ROOT': 'end_if',
259260
'IF': 'end_if_branch',
261+
'INLINE IF': 'end_if_branch',
260262
'ELSE IF': 'end_if_branch',
261263
'ELSE': 'end_if_branch',
262264
'FOR': 'end_for',

src/robot/parsing/lexer/blocklexers.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
ErrorSectionHeaderLexer,
2424
TestOrKeywordSettingLexer,
2525
KeywordCallLexer,
26-
ForHeaderLexer,
26+
ForHeaderLexer, InlineIfHeaderLexer,
2727
IfHeaderLexer, ElseIfHeaderLexer, ElseHeaderLexer,
2828
EndLexer)
2929

@@ -228,7 +228,7 @@ def handles(self, statement):
228228
return IfHeaderLexer(self.ctx).handles(statement)
229229

230230
def lexer_classes(self):
231-
return (IfHeaderLexer, ElseIfHeaderLexer, ElseHeaderLexer,
231+
return (InlineIfLexer, IfHeaderLexer, ElseIfHeaderLexer, ElseHeaderLexer,
232232
ForLexer, EndLexer, KeywordCallLexer)
233233

234234

@@ -241,16 +241,15 @@ def accepts_more(self, statement):
241241
return False
242242

243243
def lexer_classes(self):
244-
return (IfHeaderLexer, ElseIfHeaderLexer, ElseHeaderLexer,
244+
return (InlineIfHeaderLexer, ElseIfHeaderLexer, ElseHeaderLexer,
245245
KeywordCallLexer)
246246

247247
def input(self, statement):
248248
for part in self.split_statements(statement):
249249
super().input(part)
250250
return self
251251

252-
@staticmethod
253-
def split_statements(statement):
252+
def split_statements(self, statement):
254253
current_statement = []
255254
expect_arg = False
256255
for token in statement:
@@ -272,6 +271,3 @@ def split_statements(statement):
272271
else:
273272
current_statement.append(token)
274273
yield current_statement
275-
276-
277-

src/robot/parsing/lexer/lexer.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def _get_tokens(self, statements):
119119
else:
120120
ignored_types = {None}
121121
name_types = {Token.TESTCASE_NAME, Token.KEYWORD_NAME}
122-
if_type = Token.IF
122+
if_type = Token.INLINE_IF
123123
for statement in statements:
124124
eos_adder = None
125125
result = []
@@ -162,12 +162,9 @@ def _add_eos_to_name_statement(self, statement):
162162
statement.append(EOS.from_token(statement[-1]))
163163

164164
def _add_eos_to_if_statement(self, statement):
165-
if_else_markers = {Token.IF: (False, True),
165+
if_else_markers = {Token.INLINE_IF: (False, True),
166166
Token.ELSE_IF: (True, True),
167167
Token.ELSE: (True, False)}
168-
normal_if_statement_types = {Token.IF, Token.ARGUMENT, # TODO: Continuation?
169-
Token.SEPARATOR, Token.EOL}
170-
inline_if = False
171168
added = 0
172169
add_after_arg = False
173170
for index, token in enumerate(statement[:]):
@@ -184,15 +181,10 @@ def _add_eos_to_if_statement(self, statement):
184181
statement.insert(index + added + 1, EOS.from_token(token))
185182
added += 1
186183
add_after_arg = False
187-
if token_type not in normal_if_statement_types:
188-
inline_if = True
189184
last = statement[-1]
190-
if not added:
191-
statement.append(EOS.from_token(last))
192-
if inline_if:
193-
statement.extend([EOS.from_token(last),
194-
END.from_token(last, virtual=True),
195-
EOS.from_token(last)])
185+
statement.extend([EOS.from_token(last),
186+
END.from_token(last, virtual=True),
187+
EOS.from_token(last)])
196188

197189
def _split_trailing_commented_and_empty_lines(self, statement):
198190
lines = self._split_to_lines(statement)

src/robot/parsing/lexer/statementlexers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ def lex(self):
167167
token.type = Token.ARGUMENT
168168

169169

170+
class InlineIfHeaderLexer(StatementLexer):
171+
172+
def handles(self, statement):
173+
return statement[0].value == 'IF'
174+
175+
def lex(self):
176+
self.statement[0].type = Token.INLINE_IF
177+
for token in self.statement[1:]:
178+
token.type = Token.ARGUMENT
179+
180+
170181
class ElseIfHeaderLexer(StatementLexer):
171182

172183
def handles(self, statement):

0 commit comments

Comments
 (0)