mirror of
https://github.com/joshuadavidthomas/django-language-server.git
synced 2025-09-15 14:56:09 +00:00
add testing
This commit is contained in:
parent
fab360bb60
commit
070a66f161
2 changed files with 151 additions and 4 deletions
|
@ -741,4 +741,115 @@ mod tests {
|
|||
insta::assert_yaml_snapshot!(ast);
|
||||
}
|
||||
}
|
||||
|
||||
mod span_tracking {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_span_tracking() {
|
||||
let mut tokens = TokenStream::default();
|
||||
// First line: "Hello\n"
|
||||
tokens.add_token(Token::new(TokenType::Text("Hello".to_string()), 0, Some(0)));
|
||||
tokens.add_token(Token::new(TokenType::Newline, 0, Some(5)));
|
||||
// Second line: "{{ name }}\n"
|
||||
tokens.add_token(Token::new(
|
||||
TokenType::DjangoVariable("name".to_string()),
|
||||
1,
|
||||
Some(6),
|
||||
));
|
||||
tokens.add_token(Token::new(TokenType::Newline, 1, Some(16)));
|
||||
// Third line: "{% if condition %}\n"
|
||||
tokens.add_token(Token::new(
|
||||
TokenType::DjangoBlock("if condition".to_string()),
|
||||
2,
|
||||
Some(17),
|
||||
));
|
||||
tokens.add_token(Token::new(TokenType::Newline, 2, Some(34)));
|
||||
// Fourth line: " Content\n"
|
||||
tokens.add_token(Token::new(TokenType::Whitespace(2), 3, Some(35)));
|
||||
tokens.add_token(Token::new(
|
||||
TokenType::Text("Content".to_string()),
|
||||
3,
|
||||
Some(37),
|
||||
));
|
||||
tokens.add_token(Token::new(TokenType::Newline, 3, Some(44)));
|
||||
// Fifth line: "{% endif %}"
|
||||
tokens.add_token(Token::new(
|
||||
TokenType::DjangoBlock("endif".to_string()),
|
||||
4,
|
||||
Some(45),
|
||||
));
|
||||
tokens.finalize(4);
|
||||
|
||||
let mut parser = Parser::new(tokens);
|
||||
let ast = parser.parse().unwrap();
|
||||
|
||||
// Verify line offsets
|
||||
let offsets = ast.line_offsets();
|
||||
assert_eq!(offsets.position_to_line_col(0), (0, 0)); // Start of first line
|
||||
assert_eq!(offsets.position_to_line_col(6), (1, 0)); // Start of second line
|
||||
assert_eq!(offsets.position_to_line_col(17), (2, 0)); // Start of third line
|
||||
assert_eq!(offsets.position_to_line_col(35), (3, 0)); // Start of fourth line
|
||||
assert_eq!(offsets.position_to_line_col(45), (4, 0)); // Start of fifth line
|
||||
|
||||
// Verify node spans
|
||||
let nodes = ast.nodes();
|
||||
|
||||
// First node: Text "Hello"
|
||||
if let Node::Text { content, span } = &nodes[0] {
|
||||
assert_eq!(content, "Hello");
|
||||
assert_eq!(*span.start(), 0);
|
||||
assert_eq!(*span.length(), 5);
|
||||
} else {
|
||||
panic!("Expected Text node");
|
||||
}
|
||||
|
||||
// Second node: Variable "name"
|
||||
if let Node::Variable {
|
||||
bits,
|
||||
filters,
|
||||
span,
|
||||
} = &nodes[1]
|
||||
{
|
||||
assert_eq!(bits[0], "name");
|
||||
assert!(filters.is_empty());
|
||||
assert_eq!(*span.start(), 6);
|
||||
assert_eq!(*span.length(), 4);
|
||||
} else {
|
||||
panic!("Expected Variable node");
|
||||
}
|
||||
|
||||
// Third node: Block "if condition"
|
||||
if let Node::Block {
|
||||
name,
|
||||
bits,
|
||||
children,
|
||||
span,
|
||||
tag_span,
|
||||
..
|
||||
} = &nodes[2]
|
||||
{
|
||||
assert_eq!(name, "if");
|
||||
assert_eq!(bits[1], "condition");
|
||||
assert_eq!(*span.start(), 17);
|
||||
assert_eq!(*tag_span.start(), 17);
|
||||
assert_eq!(*tag_span.length(), 11);
|
||||
|
||||
// Check content node
|
||||
if let Some(child_nodes) = children {
|
||||
if let Node::Text { content, span } = &child_nodes[0] {
|
||||
assert_eq!(content.trim(), "Content");
|
||||
assert_eq!(*span.start(), 37);
|
||||
assert_eq!(*span.length(), 7);
|
||||
} else {
|
||||
panic!("Expected Text node as child");
|
||||
}
|
||||
} else {
|
||||
panic!("Expected children in if block");
|
||||
}
|
||||
} else {
|
||||
panic!("Expected Block node");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue