|
2 | 2 | import io
|
3 | 3 | import sys
|
4 | 4 | from think.interpreter import ThinkInterpreter
|
| 5 | +from think.errors import ThinkRuntimeError |
5 | 6 |
|
6 | 7 | @pytest.fixture
|
7 | 8 | def capture_output():
|
@@ -145,6 +146,137 @@ def test_list_operations(self, interpreter, parser, capture_output):
|
145 | 146 | interpreter.execute(ast)
|
146 | 147 | assert interpreter.state['items'][0] == 0
|
147 | 148 | assert interpreter.state['items'][2] == 2
|
| 149 | + |
| 150 | + def test_list_indexing(self, interpreter, parser, capture_output): |
| 151 | + """Test comprehensive list indexing operations.""" |
| 152 | + code = '''objective "Test list indexing" |
| 153 | + task "ListIndexing": |
| 154 | + step "Setup": |
| 155 | + numbers = [10, 20, 30, 40, 50] |
| 156 | + |
| 157 | + first = numbers[0] |
| 158 | + last = numbers[4] |
| 159 | + |
| 160 | + last_item = numbers[-1] |
| 161 | + second_to_last = numbers[-2] |
| 162 | + |
| 163 | + idx = 2 |
| 164 | + middle = numbers[idx] |
| 165 | + |
| 166 | + expr_idx = numbers[1 + 1] |
| 167 | + |
| 168 | + matrix = [[1, 2, 3], [4, 5, 6]] |
| 169 | + nested_val = matrix[1][2] |
| 170 | + |
| 171 | + calc_idx = 10 / 2 - 1 |
| 172 | + computed = numbers[calc_idx] |
| 173 | + |
| 174 | + run "ListIndexing"''' |
| 175 | + |
| 176 | + ast = parser.parse(code) |
| 177 | + interpreter.execute(ast) |
| 178 | + |
| 179 | + # Verify basic positive indexing |
| 180 | + assert interpreter.state['first'] == 10, "First element incorrect" |
| 181 | + assert interpreter.state['last'] == 50, "Last element incorrect" |
| 182 | + |
| 183 | + # Verify negative indexing |
| 184 | + assert interpreter.state['last_item'] == 50, "Negative indexing failed" |
| 185 | + assert interpreter.state['second_to_last'] == 40, "Negative indexing failed" |
| 186 | + |
| 187 | + # Verify variable as index |
| 188 | + assert interpreter.state['middle'] == 30, "Variable index failed" |
| 189 | + |
| 190 | + # Verify expression as index |
| 191 | + assert interpreter.state['expr_idx'] == 30, "Expression index failed" |
| 192 | + |
| 193 | + # Verify nested indexing |
| 194 | + assert interpreter.state['nested_val'] == 6, "Nested indexing failed" |
| 195 | + |
| 196 | + # Verify computed index |
| 197 | + assert interpreter.state['computed'] == 50, "Computed index failed" |
| 198 | + |
| 199 | + def test_list_indexing_errors(self, interpreter, parser, capture_output): |
| 200 | + """Test error cases for list indexing.""" |
| 201 | + # Test index out of bounds (positive) |
| 202 | + code = '''objective "Test list index errors" |
| 203 | + task "ListErrors": |
| 204 | + step "OutOfBounds": |
| 205 | + numbers = [1, 2, 3] |
| 206 | + invalid = numbers[5] |
| 207 | + run "ListErrors"''' |
| 208 | + |
| 209 | + with pytest.raises(ThinkRuntimeError) as exc_info: |
| 210 | + ast = parser.parse(code) |
| 211 | + interpreter.execute(ast) |
| 212 | + assert "Invalid index/key" in str(exc_info.value) |
| 213 | + |
| 214 | + # Test index out of bounds (negative) |
| 215 | + code = '''objective "Test negative index errors" |
| 216 | + task "ListErrors": |
| 217 | + step "NegativeOutOfBounds": |
| 218 | + numbers = [1, 2, 3] |
| 219 | + invalid = numbers[-4] |
| 220 | + run "ListErrors"''' |
| 221 | + |
| 222 | + with pytest.raises(ThinkRuntimeError) as exc_info: |
| 223 | + ast = parser.parse(code) |
| 224 | + interpreter.execute(ast) |
| 225 | + assert "Invalid index/key" in str(exc_info.value) |
| 226 | + |
| 227 | + # Test non-integer index |
| 228 | + code = '''objective "Test non-integer index" |
| 229 | + task "ListErrors": |
| 230 | + step "NonInteger": |
| 231 | + numbers = [1, 2, 3] |
| 232 | + invalid = numbers[1.5] |
| 233 | + run "ListErrors"''' |
| 234 | + |
| 235 | + with pytest.raises(ThinkRuntimeError) as exc_info: |
| 236 | + ast = parser.parse(code) |
| 237 | + interpreter.execute(ast) |
| 238 | + assert "Invalid index/key" in str(exc_info.value) |
| 239 | + |
| 240 | + # Test invalid type for index |
| 241 | + code = '''objective "Test invalid index type" |
| 242 | + task "ListErrors": |
| 243 | + step "InvalidType": |
| 244 | + numbers = [1, 2, 3] |
| 245 | + invalid = numbers["one"] |
| 246 | + run "ListErrors"''' |
| 247 | + |
| 248 | + with pytest.raises(ThinkRuntimeError) as exc_info: |
| 249 | + ast = parser.parse(code) |
| 250 | + interpreter.execute(ast) |
| 251 | + assert "Invalid index/key" in str(exc_info.value) |
| 252 | + |
| 253 | + def test_nested_list_indexing_errors(self, interpreter, parser, capture_output): |
| 254 | + """Test error cases for nested list indexing.""" |
| 255 | + # Test accessing index of non-list |
| 256 | + code = '''objective "Test invalid nested indexing" |
| 257 | + task "NestedErrors": |
| 258 | + step "NonList": |
| 259 | + numbers = [1, [2, 3], 4] |
| 260 | + invalid = numbers[0][1] |
| 261 | + run "NestedErrors"''' |
| 262 | + |
| 263 | + with pytest.raises(ThinkRuntimeError) as exc_info: |
| 264 | + ast = parser.parse(code) |
| 265 | + interpreter.execute(ast) |
| 266 | + assert "Cannot index into type" in str(exc_info.value) |
| 267 | + |
| 268 | + # Test out of bounds on nested list |
| 269 | + code = '''objective "Test nested out of bounds" |
| 270 | + task "NestedErrors": |
| 271 | + step "OutOfBounds": |
| 272 | + matrix = [[1, 2], [3, 4]] |
| 273 | + invalid = matrix[1][5] |
| 274 | + run "NestedErrors"''' |
| 275 | + |
| 276 | + with pytest.raises(ThinkRuntimeError) as exc_info: |
| 277 | + ast = parser.parse(code) |
| 278 | + interpreter.execute(ast) |
| 279 | + assert "Invalid index" in str(exc_info.value) |
148 | 280 |
|
149 | 281 |
|
150 | 282 | class TestFunctions:
|
|
0 commit comments