Skip to content

Commit 09e5fd3

Browse files
pekkaklarckyanne
authored andcommitted
Inline IF fixes
- Support multiple variables in assigment - Correct branch types - Cleanup and add more tests
1 parent 014f465 commit 09e5fd3

File tree

4 files changed

+160
-115
lines changed

4 files changed

+160
-115
lines changed
Lines changed: 68 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,74 @@
11
*** Settings ***
22
Suite Setup Run Tests ${EMPTY} running/if/inline_if_else.robot
3-
Resource atest_resource.robot
3+
Test Template Check IF/ELSE Status
4+
Resource if.resource
45

56
*** Test Cases ***
6-
Inline if passing
7-
Check Test Case ${TESTNAME}
8-
9-
Inline if failing
10-
Check Test Case ${TESTNAME}
11-
12-
Inline if not executed
13-
Check Test Case ${TESTNAME}
14-
15-
Inline if not executed failing
16-
Check Test Case ${TESTNAME}
17-
18-
Inline if else - if executed
19-
Check Test Case ${TESTNAME}
20-
21-
Inline if else - else executed
22-
Check Test Case ${TESTNAME}
23-
24-
Inline if else - if executed - failing
25-
Check Test Case ${TESTNAME}
26-
27-
Inline if else - else executed - failing
28-
Check Test Case ${TESTNAME}
29-
30-
Inline if inside for loop
31-
Check Test Case ${TESTNAME}
32-
33-
Inline if inside block if
34-
Check Test Case ${TESTNAME}
35-
36-
Inline if inside nested loop
37-
Check Test Case ${TESTNAME}
38-
39-
Inline if passing in keyword
40-
Check Test Case ${TESTNAME}
41-
42-
Inline if passing in else keyword
43-
Check Test Case ${TESTNAME}
44-
45-
Inline if failing in keyword
46-
Check Test Case ${TESTNAME}
47-
48-
Inline if failing in else keyword
49-
Check Test Case ${TESTNAME}
7+
IF passing
8+
PASS else=False
9+
10+
IF failing
11+
FAIL else=False
12+
13+
Not executed
14+
NOT RUN else=False
15+
16+
Not executed after failure
17+
NOT RUN NOT RUN NOT RUN index=1 run=False
18+
19+
ELSE IF not executed
20+
NOT RUN NOT RUN PASS index=0
21+
FAIL NOT RUN NOT RUN index=1 else=False
22+
23+
ELSE IF executed
24+
NOT RUN PASS NOT RUN index=0
25+
NOT RUN NOT RUN FAIL NOT RUN NOT RUN index=1
26+
27+
ELSE not executed
28+
PASS NOT RUN index=0
29+
FAIL NOT RUN index=1
30+
31+
ELSE executed
32+
NOT RUN PASS index=0
33+
NOT RUN FAIL index=1
34+
35+
Assign
36+
PASS NOT RUN NOT RUN index=0
37+
NOT RUN PASS NOT RUN index=1
38+
NOT RUN NOT RUN PASS index=2
39+
40+
Multi assign
41+
PASS NOT RUN
42+
43+
List assign
44+
PASS NOT RUN index=0
45+
NOT RUN PASS index=2
46+
47+
Dict assign
48+
NOT RUN PASS
49+
50+
Inside FOR
51+
[Template] NONE
52+
${tc} = Check Test Case ${TEST NAME}
53+
Check IF/ELSE Status NOT RUN PASS root=${tc.body[0].body[0].body[0]}
54+
Check IF/ELSE Status NOT RUN PASS root=${tc.body[0].body[1].body[0]}
55+
Check IF/ELSE Status FAIL NOT RUN root=${tc.body[0].body[2].body[0]}
56+
57+
Inside normal IF
58+
[Template] NONE
59+
${tc} = Check Test Case ${TEST NAME}
60+
Check IF/ELSE Status NOT RUN PASS root=${tc.body[0].body[0].body[1]}
61+
Check IF/ELSE Status NOT RUN NOT RUN root=${tc.body[0].body[1].body[0]} run=False
62+
63+
In keyword
64+
[Template] NONE
65+
${tc} = Check Test Case ${TEST NAME}
66+
Check IF/ELSE Status PASS root=${tc.body[0].body[0]}
67+
Check IF/ELSE Status NOT RUN PASS NOT RUN root=${tc.body[0].body[1]}
68+
Check IF/ELSE Status NOT RUN NOT RUN NOT RUN FAIL
69+
... NOT RUN NOT RUN NOT RUN root=${tc.body[0].body[2]}
5070

5171
Invalid END after inline header
52-
Check Test Case ${TESTNAME}
72+
# FIXME: Move to separate suite with other invalid syntax tests
73+
[Template] NONE
74+
Check Test Case ${TEST NAME}
Lines changed: 84 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,110 @@
11
*** Test Cases ***
2-
Inline if passing
2+
IF passing
33
IF True Log reached this
44

5-
Inline if failing
6-
[Documentation] FAIL failing inside if
7-
IF '1' == '1' Fail failing inside if
8-
9-
Inline if not executed
10-
IF False Fail should not go here
11-
12-
Inline if not executed failing
13-
[Documentation] FAIL after not passing
14-
IF 'a' == 'b' Pass Execution should not go here
15-
Fail after not passing
16-
17-
Inline if else - if executed
18-
IF 1 > 0 Log does go through here ELSE Fail should not go here
19-
20-
Inline if else - else executed
21-
IF 0 > 1 Fail should not go here ELSE Log does go through here
22-
23-
Inline if else - if executed - failing
5+
IF failing
6+
[Documentation] FAIL Inside IF
7+
IF '1' == '1' Fail Inside IF
8+
9+
Not executed
10+
[Documentation] FAIL After IF
11+
IF False Not run
12+
Fail After IF
13+
14+
Not executed after failure
15+
[Documentation] FAIL Before IF
16+
Fail Before IF
17+
IF True Not run ELSE IF True Not run ELSE Not run
18+
19+
ELSE IF not executed
20+
[Documentation] FAIL Expected failure
21+
IF False Not run ELSE IF False Not run ELSE Executed
22+
IF 1 > 0 Failure ELSE IF True Not run ELSE IF True Not run
23+
24+
ELSE IF executed
25+
[Documentation] FAIL Expected failure
26+
IF False Not run ELSE IF True Executed ELSE Not run
27+
IF False Not run
28+
... ELSE IF False Not run
29+
... ELSE IF True Failure
30+
... ELSE IF False Not run
31+
... ELSE Not run
32+
33+
ELSE not executed
2434
[Documentation] FAIL expected
25-
IF 1 > 0 Fail expected ELSE Log unexpected
35+
IF 1 > 0 Executed ELSE Not run
36+
IF 1 > 0 Fail expected ELSE Not run
2637

27-
Inline if else - else executed - failing
38+
ELSE executed
2839
[Documentation] FAIL expected
29-
IF 0 > 1 Log unexpected ELSE Fail expected
30-
31-
Inline if inside for loop
40+
IF 0 > 1 Not run ELSE Log does go through here
41+
IF 0 > 1 Not run ELSE Fail expected
42+
43+
Assign
44+
${x} = IF 1 Convert to integer 1 ELSE IF 2 Convert to integer 2 ELSE Convert to integer 3
45+
${y} = IF 0 Convert to integer 1 ELSE IF 2 Convert to integer 2 ELSE Convert to integer 3
46+
${z} = IF 0 Convert to integer 1 ELSE IF 0 Convert to integer 2 ELSE Convert to integer 3
47+
Should Be Equal ${x} ${1}
48+
Should Be Equal ${y} ${2}
49+
Should Be Equal ${z} ${3}
50+
51+
Multi assign
52+
${x} ${y} ${z} = IF True Create list a b c ELSE Not run
53+
Should Be Equal ${x} a
54+
Should Be Equal ${y} b
55+
Should Be Equal ${z} c
56+
57+
List assign
58+
@{x} = IF True Create list a b c ELSE Not run
59+
Should Be True ${x} == ['a', 'b', 'c']
60+
${x} @{y} ${z} = IF False Not run ELSE Create list a b c
61+
Should Be Equal ${x} a
62+
Should Be True ${y} == ['b']
63+
Should Be Equal ${z} c
64+
65+
Dict assign
66+
&{x} = IF False Not run ELSE Create dictionary a=1 b=2
67+
Should Be True ${x} == {'a': '1', 'b': '2'}
68+
69+
Inside FOR
3270
[Documentation] FAIL The end
3371
FOR ${i} IN 1 2 3
3472
IF ${i} == 3 Fail The end ELSE Log ${i}
3573
END
3674

37-
Inline if inside block if
75+
Inside normal IF
3876
IF ${True}
3977
Log Hi
4078
IF 3==4 Fail Should not be executed ELSE Log Hello
4179
Log Goodbye
42-
END
43-
44-
Inline if inside nested loop
45-
[Documentation] FAIL The end
46-
IF ${False}
47-
Fail Should not go here
4880
ELSE
49-
FOR ${i} IN 1 2 3
50-
IF ${i} == 3 Fail The end ELSE Log ${i}
51-
END
81+
IF True Not run ELSE Not run
5282
END
5383

54-
Inline if passing in keyword
55-
Passing if keyword
56-
57-
Inline if passing in else keyword
58-
Passing else keyword
59-
60-
Inline if failing in keyword
61-
[Documentation] FAIL expected
62-
Failing if keyword
63-
64-
Inline if failing in else keyword
65-
[Documentation] FAIL expected
66-
Failing else keyword
84+
In keyword
85+
[Documentation] FAIL Expected failure
86+
Keyword with inline IFs
6787

6888
Invalid END after inline header
6989
[Documentation] FAIL 'End' is a reserved keyword. It must be an upper case 'END' and follow an opening 'FOR' or 'IF' when used as a marker.
7090
IF True Log reached this
7191
Log this is a normal keyword call
7292
END
7393

74-
7594
*** Keywords ***
76-
Passing if keyword
77-
IF ${1} Log expected ELSE IF 12 < 14 Fail should not go here ELSE Fail not here
78-
79-
Passing else keyword
80-
IF ${False} Fail not here ELSE Log expected
81-
82-
Failing if keyword
83-
IF ${1} Fail expected ELSE IF 12 < 14 Log should not go here ELSE Log not here
84-
85-
Failing else keyword
86-
IF ${False} Log should not here ELSE Fail expected
95+
Keyword with inline IFs
96+
${x} = IF True Convert to integer 42
97+
IF ${x} == 0 Not run ELSE IF $x == 42 Executed ELSE Not run
98+
IF False Not run
99+
... ELSE IF False Not run
100+
... ELSE IF False Not run
101+
... ELSE IF True Failure
102+
... ELSE IF False Not run
103+
... ELSE IF False Not run
104+
... ELSE Not run
105+
106+
Executed
107+
No operation
108+
109+
Failure
110+
Fail Expected failure

src/robot/parsing/lexer/statementlexers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,12 @@ def lex(self):
170170
class InlineIfHeaderLexer(StatementLexer):
171171

172172
def handles(self, statement):
173-
if statement[0].value == 'IF':
174-
return True
175-
if len(statement) > 1 and is_assign(statement[0].value, allow_assign_mark=True) and \
176-
statement[1].value == 'IF':
177-
return True
178-
return False
173+
for token in statement:
174+
if token.value == 'IF':
175+
return True
176+
if is_assign(token.value, allow_assign_mark=True):
177+
continue
178+
return False
179179

180180
def lex(self):
181181
self.statement[0].type = Token.INLINE_IF

src/robot/running/builder/transformers.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,9 @@ def __init__(self, parent):
314314
def build(self, node):
315315
model = self.parent.body.create_if(lineno=node.lineno,
316316
error=format_error(self._get_errors(node)))
317-
is_inline_if = node.type == 'INLINE IF'
318-
assign = node.assign if is_inline_if else None
317+
assign = node.assign
319318
while node:
320-
type = node.type if not is_inline_if else 'IF'
319+
type = node.type if node.type != 'INLINE IF' else 'IF'
321320
self.model = model.body.create_branch(type, node.condition,
322321
lineno=node.lineno)
323322
for step in node.body:

0 commit comments

Comments
 (0)