-
-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: tag parser tests #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| /// 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 { |
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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\" %}" |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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],
}
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:
{{ my_var }})