Skip to content

Commit 79b061c

Browse files
committed
Support slices with variables containing lists like ${var}[1:]
By design only support new `${list}[index]` syntax, not old and to-be-deprecated `@{list}[index]`. Implements robotframework#2973. Documentation still missing.
1 parent d58a8c1 commit 79b061c

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed

atest/robot/variables/list_variable_items.robot

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Valid index
99
Valid index using variable
1010
Check Test Case ${TESTNAME}
1111

12+
Slicing
13+
Check Test Case ${TESTNAME}
14+
1215
Invalid index
1316
Check Test Case ${TESTNAME}
1417

@@ -25,6 +28,14 @@ Non-int index using variable
2528
Empty index
2629
Check Test Case ${TESTNAME}
2730

31+
Invalid slice
32+
Check Test Case ${TESTNAME}
33+
34+
Non-int slice index
35+
Check Test Case ${TESTNAME} 1
36+
Check Test Case ${TESTNAME} 2
37+
Check Test Case ${TESTNAME} 3
38+
2839
Non-existing variable
2940
Check Test Case ${TESTNAME}
3041

@@ -38,3 +49,7 @@ Old syntax with `@` still works like earlier
3849
[Documentation] `${list}[1]` and `@{list}[1]` work same way still.
3950
... In the future latter is deprecated and changed.
4051
Check Test Case ${TESTNAME}
52+
53+
Old syntax with `@` doesn't support new slicing syntax
54+
[Documentation] Slicing support should be added in RF 3.3 when `@{list}[index]` changes.
55+
Check Test Case ${TESTNAME}

atest/robot/variables/nested_item_access.robot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Nested dict access
1212
Nested mixed access
1313
Check Test Case ${TESTNAME}
1414

15+
Nested access with slicing
16+
Check Test Case ${TESTNAME}
17+
1518
Non-existing nested list item
1619
Check Test Case ${TESTNAME}
1720

atest/testdata/variables/list_variable_items.robot

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ Valid index using variable
1414
Should Be Equal ${LIST}[${ONE}] B
1515
Should Be Equal ${LIST}[${-1}] G
1616

17+
Slicing
18+
Should Be Equal ${LIST}[1:] ${LIST[1:]}
19+
Should Be Equal ${LIST}[:2] ${LIST[:2]}
20+
Should Be Equal ${LIST}[::-1] ${LIST[::-1]}
21+
Should Be Equal ${LIST}[1::] ${LIST[1::]}
22+
Should Be Equal ${LIST}[:2:] ${LIST[:2:]}
23+
Should Be Equal ${LIST}[1:2:] ${LIST[1:2:]}
24+
Should Be Equal ${LIST}[:3:2] ${LIST[:3:2]}
25+
Should Be Equal ${LIST}[1:-1:2] ${LIST[1:-1:2]}
26+
Should Be Equal ${LIST}[:] ${LIST}
27+
Should Be Equal ${LIST}[::] ${LIST}
28+
Should Be Empty ${LIST}[100:]
29+
1730
Invalid index
1831
[Documentation] FAIL List '\${LIST}' has no item in index 7.
1932
Log ${LIST}[7]
@@ -38,6 +51,22 @@ Empty index
3851
[Documentation] FAIL List '\${LIST}' used with invalid index ''.
3952
Log ${LIST}[]
4053

54+
Invalid slice
55+
[Documentation] FAIL List '\${LIST}' used with invalid index '1:2:3:4'.
56+
Log ${LIST}[1:2:3:4]
57+
58+
Non-int slice index 1
59+
[Documentation] FAIL List '\${LIST}' used with invalid index 'ooops:'.
60+
Log ${LIST}[ooops:]
61+
62+
Non-int slice index 2
63+
[Documentation] FAIL List '\${LIST}' used with invalid index '1:ooops'.
64+
Log ${LIST}[1:ooops]
65+
66+
Non-int slice index 3
67+
[Documentation] FAIL List '\${LIST}' used with invalid index '1:2:ooops'.
68+
Log ${LIST}[1:2:ooops]
69+
4170
Non-existing variable
4271
[Documentation] FAIL Variable '\${nonex list}' not found.
4372
Log ${nonex list}[0]
@@ -53,7 +82,14 @@ Non-list variable
5382
Log ${INVALID}[0]
5483

5584
Old syntax with `@` still works like earlier
56-
[Documentation] FAIL List '\@{LIST}' has no item in index 10.
85+
[Documentation] `${list}[1]` and `@{list}[1]` work same way still.
86+
... In the future latter is deprecated and changed.
87+
... FAIL List '\@{LIST}' has no item in index 10.
5788
Should Be Equal @{LIST}[0] A
5889
Should Be Equal @{LIST}[${-1}] G
5990
Log @{LIST}[10]
91+
92+
Old syntax with `@` doesn't support new slicing syntax
93+
[Documentation] Slicing support should be added in RF 3.3 when `@{list}[index]` changes.
94+
... FAIL List '\@{LIST}' used with invalid index '1:'.
95+
Log @{LIST}[1:]

atest/testdata/variables/nested_item_access.robot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ Nested mixed access
2323
${MIXED}[x][0][0] ${1}
2424
${MIXED}[x][0][1][y][z][-1][][0] ${42}
2525

26+
Nested access with slicing
27+
${LIST}[1:][:-1] ${LIST[1:-1]}
28+
${LIST}[1:-1][-1][-2:1:-2][0][0] ${3}
29+
2630
Non-existing nested list item
2731
[Documentation] FAIL List '\${LIST}[1][2]' has no item in index 666.
2832
${LIST}[1][2][666] whatever

src/robot/variables/replacer.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def _get_reserved_variable(self, splitter):
159159
def _get_list_variable_item(self, name, variable, index):
160160
index = self.replace_string(index)
161161
try:
162-
index = int(index)
162+
index = self._parse_list_variable_index(index, name[0] == '$')
163163
except ValueError:
164164
raise VariableError("List '%s' used with invalid index '%s'."
165165
% (name, index))
@@ -169,6 +169,13 @@ def _get_list_variable_item(self, name, variable, index):
169169
raise VariableError("List '%s' has no item in index %d."
170170
% (name, index))
171171

172+
def _parse_list_variable_index(self, index, support_slice=True):
173+
if ':' not in index:
174+
return int(index)
175+
if index.count(':') > 2 or not support_slice:
176+
raise ValueError
177+
return slice(*[int(i) if i else None for i in index.split(':')])
178+
172179
def _get_dict_variable_item(self, name, variable, key):
173180
key = self.replace_scalar(key)
174181
try:

0 commit comments

Comments
 (0)