Skip to content

Commit bb14b87

Browse files
committed
test(syntax): cover token mapping and injection guard paths in highlighter
1 parent cb3a23c commit bb14b87

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

‎src/syntax/mod.rs‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,15 +382,29 @@ mod tests {
382382
fn capture_names_map_to_tokens_and_theme_colors() {
383383
let theme = Theme::default();
384384
let cases = [
385+
("annotation", TokenType::Keyword, theme.syntax_keyword),
385386
("comment", TokenType::Comment, theme.syntax_comment),
386387
("boolean", TokenType::Constant, theme.syntax_constant),
388+
("character.escape", TokenType::String, theme.syntax_string),
389+
("conditional", TokenType::Keyword, theme.syntax_keyword),
390+
(
391+
"delimiter",
392+
TokenType::Punctuation,
393+
theme.syntax_punctuation,
394+
),
395+
("escape", TokenType::Operator, theme.syntax_operator),
396+
("field", TokenType::Property, theme.syntax_property),
397+
("float", TokenType::Number, theme.syntax_number),
387398
(
388399
"function.method",
389400
TokenType::Function,
390401
theme.syntax_function,
391402
),
403+
("identifier", TokenType::Variable, theme.syntax_variable),
392404
("keyword", TokenType::Keyword, theme.syntax_keyword),
393405
("label", TokenType::Label, theme.syntax_label),
406+
("method", TokenType::Function, theme.syntax_function),
407+
("namespace", TokenType::Type, theme.syntax_type),
394408
("number", TokenType::Number, theme.syntax_number),
395409
("operator", TokenType::Operator, theme.syntax_operator),
396410
("parameter", TokenType::Parameter, theme.syntax_parameter),
@@ -400,8 +414,12 @@ mod tests {
400414
TokenType::Punctuation,
401415
theme.syntax_punctuation,
402416
),
417+
("regexp", TokenType::String, theme.syntax_string),
418+
("special", TokenType::Operator, theme.syntax_operator),
403419
("string.special", TokenType::String, theme.syntax_string),
404420
("struct", TokenType::Type, theme.syntax_type),
421+
("tag", TokenType::Type, theme.syntax_type),
422+
("type.definition", TokenType::Type, theme.syntax_type),
405423
("variable", TokenType::Variable, theme.syntax_variable),
406424
];
407425

@@ -505,6 +523,27 @@ mod tests {
505523
);
506524
}
507525

526+
#[test]
527+
fn resolve_injection_language_rejects_blank_capture_text() {
528+
let html = get_language_by_name("html").unwrap();
529+
let query = Query::new(
530+
&html.language,
531+
"(script_element (raw_text) @injection.language)",
532+
)
533+
.unwrap();
534+
let source = "<script> </script>";
535+
let tree = parse_tree(&html, source);
536+
let mut cursor = QueryCursor::new();
537+
let mut matches = cursor.matches(&query, tree.root_node(), source.as_bytes());
538+
539+
assert_eq!(
540+
matches
541+
.next()
542+
.and_then(|matched| resolve_injection_language(&query, matched, source)),
543+
None
544+
);
545+
}
546+
508547
#[test]
509548
fn gather_injections_ignores_queries_without_content_or_known_language() {
510549
let html = get_language_by_name("html").unwrap();
@@ -528,6 +567,11 @@ mod tests {
528567
)
529568
.unwrap();
530569
assert!(gather_injections(&unknown_language_query, &tree, source, 0).is_empty());
570+
571+
let empty_script = "<script></script>";
572+
let empty_tree = parse_tree(&html, empty_script);
573+
let html_query = Query::new(&html.language, html.injection_query.unwrap()).unwrap();
574+
assert!(gather_injections(&html_query, &empty_tree, empty_script, 0).is_empty());
531575
}
532576

533577
#[test]
@@ -556,6 +600,21 @@ mod tests {
556600
.all(|span| span.start >= 7 && span.end > 7));
557601
}
558602

603+
#[test]
604+
fn highlight_slice_runs_nested_injection_queries_when_supported() {
605+
let html = get_language_by_name("html").unwrap();
606+
let source = "<script>const nested = 1;</script>";
607+
let base_offset = 11;
608+
let expected_start = base_offset + source.find("const nested = 1;").unwrap();
609+
let expected_end = expected_start + "const nested = 1;".len();
610+
let spans = highlight_slice(&html, source, base_offset, 0);
611+
612+
assert!(!spans.is_empty());
613+
assert!(spans
614+
.iter()
615+
.any(|span| span.start >= expected_start && span.end <= expected_end));
616+
}
617+
559618
#[test]
560619
fn highlight_slice_returns_empty_for_invalid_queries() {
561620
let rust = get_language_by_name("rust").unwrap();

0 commit comments

Comments
 (0)