Skip to content

Commit

Permalink
Tweak test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jg-rp committed Mar 5, 2025
1 parent b5a71ff commit 2ee5fc3
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 66 deletions.
2 changes: 1 addition & 1 deletion liquid/builtin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def register(env: Environment) -> None: # noqa: PLR0915
env.add_tag(output.Output)
env.add_tag(illegal.Illegal)
env.add_tag(if_tag.IfTag)
env.add_tag(comment_tag.CommentTextTag)
env.add_tag(comment_tag.CommentTag)
env.add_tag(unless_tag.UnlessTag)
env.add_tag(case_tag.CaseTag)
env.add_tag(for_tag.ForTag)
Expand Down
4 changes: 2 additions & 2 deletions liquid/builtin/loaders/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


class _CachingLoaderProtocol(Protocol):
def load(
def load( # pragma: no cover
self,
env: Environment,
name: str,
Expand All @@ -37,7 +37,7 @@ def load(
**kwargs: object,
) -> BoundTemplate: ...

async def load_async(
async def load_async( # pragma: no cover
self,
env: Environment,
name: str,
Expand Down
20 changes: 1 addition & 19 deletions liquid/builtin/tags/comment_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from liquid import ast
from liquid.exceptions import LiquidSyntaxError
from liquid.parse import eat_block
from liquid.tag import Tag
from liquid.token import TOKEN_EOF
from liquid.token import TOKEN_TAG
Expand Down Expand Up @@ -44,24 +43,6 @@ def render_to_output(self, _: RenderContext, __: TextIO) -> int:


class CommentTag(Tag):
"""The built-in _comment_ tag.
This implementation does not include comment text in the resulting
AST node.
"""

name = TAG_COMMENT
end = TAG_ENDCOMMENT
node_class = CommentNode

def parse(self, stream: TokenStream) -> CommentNode:
"""Parse tokens from _stream_ into an AST node."""
node = self.node_class(stream.eat(TOKEN_TAG))
eat_block(stream, end=END_COMMENTBLOCK)
return node


class CommentTextTag(CommentTag):
"""An implementation of the built-in _comment_ tag that retains comment text.
Some Liquid markup might be stripped out by the lexer, so comment text is not
Expand All @@ -70,6 +51,7 @@ class CommentTextTag(CommentTag):

name = TAG_COMMENT
end = TAG_ENDCOMMENT
node_class = CommentNode

def parse(self, stream: TokenStream) -> CommentNode:
"""Parse tokens from _stream_ into an AST node."""
Expand Down
5 changes: 0 additions & 5 deletions liquid/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import Type
from typing import Union

from .token import TOKEN_STRING
from .token import Token


Expand Down Expand Up @@ -42,10 +41,6 @@ def detailed_message(self) -> str:

pad = " " * len(str(lineno))
length = len(self.token.value)

if self.token.kind == TOKEN_STRING:
length += 2

pointer = (" " * col) + ("^" * max(length, 1))

return (
Expand Down
12 changes: 12 additions & 0 deletions liquid/golden/comment_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@
expect="",
future=True,
),
Case(
description="nested comment blocks, with nested tags",
template=(
r"{% comment %}"
r" {% comment %}"
r" {% comment %}{% if true %}hello{%endif%}{% endcomment %}"
r" {% endcomment %}"
r"{% endcomment %}"
),
expect="",
future=True,
),
Case(
description="unclosed nested comment blocks",
template=(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ dependencies = [
]

[tool.hatch.envs.default.scripts]
cov = "pytest --cov-report=term-missing --cov-fail-under=97 --cov-config=pyproject.toml --cov=liquid --cov=tests {args}"
cov = "pytest --cov-report=term-missing --cov-fail-under=95 --cov-config=pyproject.toml --cov=liquid --cov=tests {args}"
benchmark = "python -O scripts/performance.py"
cov-html = "pytest --cov-report=html --cov-config=pyproject.toml --cov=liquid --cov=tests {args}"
lint = "ruff check ."
Expand Down
8 changes: 1 addition & 7 deletions tests/filters/test_index.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import operator
from dataclasses import dataclass
from dataclasses import field
from inspect import isclass
from typing import Any

import pytest

from liquid import Environment
from liquid.exceptions import Error
from liquid.extra.filters.array import index


Expand Down Expand Up @@ -44,8 +42,4 @@ class Case:

@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
def test_index_filter(case: Case) -> None:
if isclass(case.expect) and issubclass(case.expect, Error):
with pytest.raises(case.expect):
index(case.val, *case.args, **case.kwargs)
else:
assert index(case.val, *case.args, **case.kwargs) == case.expect
assert index(case.val, *case.args, **case.kwargs) == case.expect
8 changes: 1 addition & 7 deletions tests/filters/test_reject.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import operator
from dataclasses import dataclass
from dataclasses import field
from inspect import isclass
from typing import Any

import pytest

from liquid import Environment
from liquid.builtin.filters.array import reject
from liquid.exceptions import Error


@dataclass
Expand Down Expand Up @@ -37,8 +35,4 @@ class Case:

@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
def test_reject_filter(case: Case) -> None:
if isclass(case.expect) and issubclass(case.expect, Error):
with pytest.raises(case.expect):
reject(case.val, *case.args, **case.kwargs)
else:
assert reject(case.val, *case.args, **case.kwargs) == case.expect
assert reject(case.val, *case.args, **case.kwargs) == case.expect
14 changes: 2 additions & 12 deletions tests/filters/test_script_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
from dataclasses import dataclass
from dataclasses import field
from functools import partial
from inspect import isclass
from typing import Any

import pytest

from liquid import Environment
from liquid import Markup
from liquid.exceptions import Error
from liquid.extra.filters.html import script_tag


Expand Down Expand Up @@ -59,11 +57,7 @@ class Case:
@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
def test_script_tag_filter(case: Case) -> None:
script_tag_ = partial(script_tag, environment=ENV)
if isclass(case.expect) and issubclass(case.expect, Error):
with pytest.raises(case.expect):
script_tag_(case.val, *case.args, **case.kwargs)
else:
assert script_tag_(case.val, *case.args, **case.kwargs) == case.expect
assert script_tag_(case.val, *case.args, **case.kwargs) == case.expect


AUTO_ESCAPE_ENV = Environment(autoescape=True)
Expand Down Expand Up @@ -104,8 +98,4 @@ def test_script_tag_filter(case: Case) -> None:
)
def test_script_tag_filter_auto_escape(case: Case) -> None:
script_tag_ = partial(script_tag, environment=AUTO_ESCAPE_ENV)
if isclass(case.expect) and issubclass(case.expect, Error):
with pytest.raises(case.expect):
script_tag_(case.val, *case.args, **case.kwargs)
else:
assert script_tag_(case.val, *case.args, **case.kwargs) == case.expect
assert script_tag_(case.val, *case.args, **case.kwargs) == case.expect
14 changes: 2 additions & 12 deletions tests/filters/test_stylesheet_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
from dataclasses import dataclass
from dataclasses import field
from functools import partial
from inspect import isclass
from typing import Any

import pytest

from liquid import Environment
from liquid import Markup
from liquid.exceptions import Error
from liquid.extra.filters.html import stylesheet_tag


Expand Down Expand Up @@ -70,11 +68,7 @@ class Case:
@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
def test_stylesheet_tag_filter(case: Case) -> None:
stylesheet_tag_ = partial(stylesheet_tag, environment=ENV)
if isclass(case.expect) and issubclass(case.expect, Error):
with pytest.raises(case.expect):
stylesheet_tag_(case.val, *case.args, **case.kwargs)
else:
assert stylesheet_tag_(case.val, *case.args, **case.kwargs) == case.expect
assert stylesheet_tag_(case.val, *case.args, **case.kwargs) == case.expect


AUTO_ESCAPE_ENV = Environment(autoescape=True)
Expand Down Expand Up @@ -118,8 +112,4 @@ def test_stylesheet_tag_filter(case: Case) -> None:
)
def test_stylesheet_tag_filter_auto_escape(case: Case) -> None:
stylesheet_tag_ = partial(stylesheet_tag, environment=AUTO_ESCAPE_ENV)
if isclass(case.expect) and issubclass(case.expect, Error):
with pytest.raises(case.expect):
stylesheet_tag_(case.val, *case.args, **case.kwargs)
else:
assert stylesheet_tag_(case.val, *case.args, **case.kwargs) == case.expect
assert stylesheet_tag_(case.val, *case.args, **case.kwargs) == case.expect
18 changes: 18 additions & 0 deletions tests/test_static_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,24 @@ def test_analyze_macro_and_call(env: Environment) -> None:
)


def test_span_line_and_col(env: Environment) -> None:
source = "\n".join(
[
r"{% for x in (1..y) %}",
r" {{ x }}",
r"{% break %}",
r"{% else %}",
r" {{ z }}",
r"{% continue %}",
r"{% endfor %}",
]
)

analysis = env.from_string(source).analyze()
assert analysis.globals["y"][0].span.line_col(source) == (1, 16)
assert analysis.variables["x"][0].span.line_col(source) == (2, 5)


# TODO:
# def test_analyze_translate(env: Environment) -> None:
# source = (
Expand Down

0 comments on commit 2ee5fc3

Please sign in to comment.