add test for error recovery

This commit is contained in:
Josh Thomas 2025-01-04 19:40:40 -06:00
parent 6dfe47b87c
commit 92ecfcb83c
3 changed files with 83 additions and 3 deletions

View file

@ -736,9 +736,32 @@ mod tests {
let source = "<style>body { color: blue; ";
let tokens = Lexer::new(source).tokenize().unwrap();
let mut parser = Parser::new(tokens);
let result = parser.parse().unwrap();
assert_eq!(result.errors().len(), 1);
assert!(matches!(&result.errors()[0], AstError::UnclosedTag(tag) if tag == "style"));
let ast = parser.parse().unwrap();
insta::assert_yaml_snapshot!(ast);
assert_eq!(ast.errors().len(), 1);
assert!(matches!(&ast.errors()[0], AstError::UnclosedTag(tag) if tag == "style"));
}
#[test]
fn test_parse_error_recovery() {
let source = r#"<div class="container">
<h1>Header</h1>
{% if user.is_authenticated %}
<p>Welcome {{ user.name }}</p>
<div>
{# This div is unclosed #}
{% for item in items %}
<span>{{ item }}</span>
{% endfor %}
{% endif %}
<footer>Page Footer</footer>
</div>"#;
let tokens = Lexer::new(source).tokenize().unwrap();
let mut parser = Parser::new(tokens);
let ast = parser.parse().unwrap();
insta::assert_yaml_snapshot!(ast);
assert_eq!(ast.errors().len(), 1);
assert!(matches!(&ast.errors()[0], AstError::UnclosedTag(tag) if tag == "div"));
}
}

View file

@ -0,0 +1,50 @@
---
source: crates/djls-template-ast/src/parser.rs
expression: ast
---
nodes:
- Html:
Element:
tag_name: div
attributes:
class:
Value: container
children:
- Html:
Element:
tag_name: h1
attributes: {}
children:
- Text: Header
- Django:
Tag:
Block:
name: if
bits:
- if
- user.is_authenticated
children:
- Html:
Element:
tag_name: p
attributes: {}
children:
- Text: "Welcome "
- Django:
Variable:
bits:
- user
- name
filters: []
- Django:
Tag:
Closing:
name: endif
bits: []
- Html:
Element:
tag_name: footer
attributes: {}
children:
- Text: Page Footer
errors: []

View file

@ -0,0 +1,7 @@
---
source: crates/djls-template-ast/src/parser.rs
expression: ast
---
nodes: []
errors:
- UnclosedTag: style