Skip to content

Commit 8357409

Browse files
committed
Test shorthand indexes with case tags
1 parent 3fda3d0 commit 8357409

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

liquid/builtin/tags/case_tag.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Tag and node definition for the built-in "case" tag."""
2+
23
from __future__ import annotations
34

45
import sys
@@ -193,14 +194,16 @@ def parse(self, stream: TokenStream) -> ast.Node:
193194

194195
def _parse_case_expression(self, expr: str, linenum: int) -> Expression:
195196
stream = ExpressionTokenStream(
196-
tokenize_common_expression(expr, linenum=linenum)
197+
tokenize_common_expression(expr, linenum=linenum),
198+
shorthand_indexes=self.env.shorthand_indexes,
197199
)
198200
return parse_common_expression(stream)
199201

200202
def _parse_when_expression(self, expr: str, linenum: int) -> List[Expression]:
201203
expressions = []
202204
stream = ExpressionTokenStream(
203-
tokenize_common_expression(expr, linenum=linenum)
205+
tokenize_common_expression(expr, linenum=linenum),
206+
shorthand_indexes=self.env.shorthand_indexes,
204207
)
205208

206209
while True:

liquid/expressions/common.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ def _parse_range_literal(stream: "TokenStream") -> RangeLiteral:
254254
TOKEN_IDENTINDEX,
255255
TOKEN_DOT,
256256
TOKEN_LBRACKET,
257+
TOKEN_INTEGER,
258+
TOKEN_FLOAT,
257259
)
258260
)
259261

@@ -267,7 +269,7 @@ def _parse_common_identifier(stream: "TokenStream") -> Identifier:
267269
path: IdentifierPath = []
268270

269271
while True:
270-
_, _type, value = stream.current
272+
pos, _type, value = stream.current
271273
if _type == TOKEN_IDENTIFIER:
272274
path.append(IdentifierPathElement(value))
273275
elif _type == TOKEN_IDENTINDEX:
@@ -277,6 +279,19 @@ def _parse_common_identifier(stream: "TokenStream") -> Identifier:
277279
path.append(_parse_common_identifier(stream))
278280
stream.next_token()
279281
stream.expect(TOKEN_RBRACKET)
282+
elif _type == TOKEN_FLOAT:
283+
if stream.shorthand_indexes:
284+
path.extend(
285+
IdentifierPathElement(to_int(i))
286+
for i in value.rstrip(".").split(".")
287+
)
288+
else:
289+
raise LiquidSyntaxError(
290+
f"expected an identifier, found {value!r}",
291+
linenum=pos,
292+
)
293+
elif _type == TOKEN_INTEGER and stream.shorthand_indexes:
294+
path.append(IdentifierPathElement(to_int(value)))
280295
elif _type == TOKEN_DOT:
281296
pass
282297

tests/test_shorthand_indexes.py

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ def test_shorthand_index_in_conditional_expression() -> None:
5757
assert template.render(**data) == ""
5858

5959

60+
def test_shorthand_indexes_in_case_tag() -> None:
61+
data = {"foo": ["World", "Liquid"]}
62+
template = ENV.from_string(
63+
"{% case foo.0 %}{% when 'World' %}Hello, World!{% endcase %}"
64+
)
65+
assert template.render(**data) == "Hello, World!"
66+
67+
6068
def test_shorthand_indexes_in_ternary_expressions() -> None:
6169
env = MockEnv()
6270
add_inline_expression_tags(env)

0 commit comments

Comments
 (0)