Skip to content

Conversation

@JuroOravec
Copy link
Contributor

Follow up for #14. This just adds tests. Kept as a separate PR due to the sheer size of it.

The tests are huge because there's a lot of data types that can interact with each other:

  • data types: int, float, str, nested template, translation, list, dict, variable, python expression
  • Each data type can:
    • have filter(s)
    • be nested in dict, list or nested template
  • some data types require certain variables to be present in the context (e.g. plain {{ my_var }})

/// Helper function to create a Token struct
/// Takes content, start_index, line number, and column number
/// Calculates end_index automatically as start_index + content.len()
pub fn token(content: &str, start_index: usize, line: usize, col: usize) -> Token {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many of the tests check for the exact AST tree shape (correct values, indices, etc).

To make it less verbose, this file contains some helpers to define individual AST nodes.

fn test_compile_tag_attrs_no_attributes() {
assert_compile_tag_attrs(
"{% my_tag / %}",
"def compiled_func(context):\n args = []\n kwargs = []\n return args, kwargs",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the compile_tag(), we test that the input template tags returns a string of a Python function.

let (result, _context) = plain_parse_tag_v1(input).unwrap();
assert_eq!(
result,
Tag::Generic(GenericTag {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the parse_tag() function, we assert the exact AST tree.

assert kwargs == [("key", [1, 2, 3]), ("key2", "val2 two")]

def test_nested_quotes(self):
tag_content = "{% component 'my_comp' key=val key2='val2 \"two\"' text=\"organisation's\" %}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are repeated in Python, to ensure the Python API works too.


assert tag == expected_tag

tag_func = _simple_compile_tag(tag, tag_content)
Copy link
Contributor Author

@JuroOravec JuroOravec Jan 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Python tests check not only the AST shape, but also call compile_tag(), and ensure that calling the resulting function returns the expected Python data.

So e.g.

{% my_tag key=[1, 2, 3] %}

Should return following Python data:

args = []
kwargs = {
  "key": [1, 2, 3],
}

@JuroOravec JuroOravec merged commit 15e10d9 into main Jan 20, 2026
14 checks passed
@JuroOravec JuroOravec deleted the jo-tests-template-tag-parser branch January 20, 2026 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants