//! Tokenization tests for the built-in `syntaxes` grammars.
#![cfg(feature = "syntaxes")]
use std::str::FromStr;
use rustik_highlight::{Grammar, LineState, LineTokenizer, syntaxes};
#[test]
fn built_in_syntaxes_compile() {
for source in [
syntaxes::C,
syntaxes::CSS,
syntaxes::ERB,
syntaxes::FISH,
syntaxes::GO,
syntaxes::HTML,
syntaxes::JAVASCRIPT,
syntaxes::LUA,
syntaxes::MAKE,
syntaxes::MARKDOWN,
syntaxes::RADIANCE,
syntaxes::RIL,
syntaxes::PYTHON,
syntaxes::RUBY,
syntaxes::RUST,
syntaxes::SHELL,
syntaxes::SQL,
syntaxes::TOML,
syntaxes::TYPESCRIPT,
syntaxes::TSX,
syntaxes::XML,
syntaxes::YAML,
] {
Grammar::from_str(source).unwrap();
}
}
#[test]
fn scripting_syntaxes_match_shebangs() {
let ruby = Grammar::from_str(syntaxes::RUBY).unwrap();
let python = Grammar::from_str(syntaxes::PYTHON).unwrap();
let shell = Grammar::from_str(syntaxes::SHELL).unwrap();
let fish = Grammar::from_str(syntaxes::FISH).unwrap();
assert!(ruby.matches_head("#!/usr/bin/env ruby3.3"));
assert!(python.matches_head("#!/usr/bin/env python3.11"));
assert!(shell.matches_head("#!/bin/sh"));
assert!(fish.matches_head("#!/usr/bin/env fish"));
}
#[test]
fn erb_embeds_html_and_ruby_scopes() {
let erb: rustik_highlight::RawGrammar = syntaxes::ERB.parse().unwrap();
let html: rustik_highlight::RawGrammar = syntaxes::HTML.parse().unwrap();
let ruby: rustik_highlight::RawGrammar = syntaxes::RUBY.parse().unwrap();
let grammar = Grammar::compile_with(&erb, &[html, ruby]).unwrap();
let mut state = LineState::default();
let mut tokenizer = LineTokenizer::new(&grammar);
let spans = tokenizer.tokenize_line(&mut state, "
<% if ok %>
");
let scopes = spans
.iter()
.filter_map(|span| grammar.scopes.get(span.scope.index()))
.map(String::as_str)
.collect::>();
assert!(scopes.contains(&"entity.name.tag.html"));
assert!(scopes.contains(&"punctuation.section.embedded.begin.erb"));
assert!(scopes.contains(&"source.ruby"));
assert!(scopes.contains(&"keyword.control.ruby"));
}
#[test]
fn erb_embeds_multiline_javascript_scopes() {
let erb: rustik_highlight::RawGrammar = syntaxes::ERB.parse().unwrap();
let html: rustik_highlight::RawGrammar = syntaxes::HTML.parse().unwrap();
let ruby: rustik_highlight::RawGrammar = syntaxes::RUBY.parse().unwrap();
let css: rustik_highlight::RawGrammar = syntaxes::CSS.parse().unwrap();
let javascript: rustik_highlight::RawGrammar = syntaxes::JAVASCRIPT.parse().unwrap();
let grammar = Grammar::compile_with(&erb, &[html, ruby, css, javascript]).unwrap();
let mut state = LineState::default();
let mut tokenizer = LineTokenizer::new(&grammar);
let _ = tokenizer.tokenize_line(&mut state, "");
assert!(scopes.contains(&"source.js"));
assert!(scopes.contains(&"keyword.control.conditional.js"));
assert!(scopes.contains(&"keyword.control.flow.js"));
}
#[test]
fn javascript_highlights_function_call_arguments() {
let grammar = Grammar::from_str(syntaxes::JAVASCRIPT).unwrap();
let line = "document.addEventListener('DOMContentLoaded', function () {";
let mut state = LineState::default();
let mut tokenizer = LineTokenizer::new(&grammar);
let spans = tokenizer.tokenize_line(&mut state, line);
let scopes = spans
.iter()
.filter_map(|span| grammar.scopes.get(span.scope.index()))
.map(String::as_str)
.collect::>();
assert!(scopes.contains(&"string.quoted.single.js"));
assert!(scopes.contains(&"storage.type.function.js"));
}
#[test]
fn typescript_highlights_comments_inside_interface_body() {
let grammar = Grammar::from_str(syntaxes::TYPESCRIPT).unwrap();
let mut state = LineState::default();
let mut tokenizer = LineTokenizer::new(&grammar);
let _ = tokenizer.tokenize_line(&mut state, "export interface OperationSpan {");
let spans = tokenizer.tokenize_line(&mut state, " /** Semantic role for this group. */");
let scopes = spans
.iter()
.filter_map(|span| grammar.scopes.get(span.scope.index()))
.map(String::as_str)
.collect::>();
assert!(
scopes.contains(&"comment.block.documentation.ts"),
"{scopes:?}"
);
}
#[test]
fn tsx_highlights_typescript_and_jsx() {
let grammar = Grammar::from_str(syntaxes::TSX).unwrap();
let scopes = tokenize_scopes(
&grammar,
"const view: JSX.Element = ;",
);
assert!(scopes.contains(&"storage.type.tsx"), "{scopes:?}");
assert!(scopes.contains(&"entity.name.tag.tsx"), "{scopes:?}");
assert!(
scopes.contains(&"entity.other.attribute-name.tsx"),
"{scopes:?}"
);
}
#[test]
fn xml_highlights_tags_attributes_and_strings() {
let grammar = Grammar::from_str(syntaxes::XML).unwrap();
let scopes = tokenize_scopes(&grammar, " ");
assert!(
scopes.contains(&"entity.name.tag.localname.xml"),
"{scopes:?}"
);
assert!(
scopes.contains(&"entity.other.attribute-name.localname.xml"),
"{scopes:?}"
);
assert!(scopes.contains(&"string.quoted.double.xml"), "{scopes:?}");
}
#[test]
fn rust_highlights_function_signature_parameters() {
let grammar = Grammar::from_str(syntaxes::RUST).unwrap();
let mut state = LineState::default();
let mut tokenizer = LineTokenizer::new(&grammar);
let _ = tokenizer.tokenize_line(&mut state, "fn reconcile_with(");
let spans = tokenizer.tokenize_line(&mut state, " mut cursor: String,");
let scopes = spans
.iter()
.filter_map(|span| grammar.scopes.get(span.scope.index()))
.map(String::as_str)
.collect::>();
assert!(scopes.contains(&"storage.modifier.mut.rust"));
assert!(scopes.contains(&"variable.parameter.rust"));
assert!(scopes.contains(&"entity.name.type.rust"));
}
#[test]
fn fish_highlights_keywords_variables_and_strings() {
let grammar = Grammar::from_str(syntaxes::FISH).unwrap();
let scopes = tokenize_scopes(&grammar, "if test -n \"$argv\"");
assert!(scopes.contains(&"keyword.control.fish"));
assert!(scopes.contains(&"source.option.fish"));
assert!(scopes.contains(&"string.quoted.double.fish"));
assert!(scopes.contains(&"variable.language.fish"));
}
#[test]
fn lua_highlights_keywords_booleans_and_strings() {
let grammar = Grammar::from_str(syntaxes::LUA).unwrap();
let scopes = tokenize_scopes(&grammar, "local ok = true and \"done\"");
assert!(scopes.contains(&"keyword.local.lua"));
assert!(scopes.contains(&"constant.language.lua"));
assert!(scopes.contains(&"keyword.operator.logical.lua"));
assert!(scopes.contains(&"string.quoted.double.lua"));
}
#[test]
fn css_root_comments_are_highlighted() {
let grammar = Grammar::from_str(syntaxes::CSS).unwrap();
let mut state = LineState::default();
let line = "/**/";
let spans = grammar.tokenize_line(&mut state, line);
let scopes = spans
.iter()
.filter_map(|span| grammar.scopes.get(span.scope.index()))
.map(String::as_str)
.collect::>();
assert!(scopes.contains(&"comment.block.css"));
}
#[test]
fn css_property_values_highlight_urls_and_colors_inside_rule() {
let grammar = Grammar::from_str(syntaxes::CSS).unwrap();
let scopes = css_rule_scopes(
&grammar,
"background: url(icon.svg); color: rgb(1 2 3); border-color: #aabbcc;",
);
assert!(scopes.contains(&"meta.property-list.css"));
assert!(scopes.contains(&"meta.property-value.css"));
assert!(scopes.contains(&"meta.function.url.css"));
assert!(scopes.contains(&"meta.function.color.css"));
assert!(scopes.contains(&"constant.other.color.rgb-value.hex.css"));
}
#[test]
fn go_make_calls_highlight_arguments_and_type_brackets() {
let grammar = Grammar::from_str(syntaxes::GO).unwrap();
let line = "make([]LayerModule, 0, len(layer.Modules))";
let mut state = LineState::default();
let mut tokenizer = LineTokenizer::new(&grammar);
let spans = tokenizer.tokenize_line(&mut state, line);
let scopes = spans
.iter()
.filter_map(|span| grammar.scopes.get(span.scope.index()))
.map(String::as_str)
.collect::>();
assert!(
scopes.contains(&"constant.numeric.decimal.go"),
"{scopes:?}"
);
assert!(
scopes.contains(&"entity.name.function.support.builtin.go"),
"{scopes:?}"
);
assert!(
scopes.contains(&"punctuation.definition.begin.bracket.square.go"),
"{scopes:?}"
);
assert!(
scopes.contains(&"punctuation.definition.end.bracket.square.go"),
"{scopes:?}"
);
}
#[test]
fn toml_highlights_keys_sections_strings_and_booleans() {
let grammar = Grammar::from_str(syntaxes::TOML).unwrap();
let section_scopes = tokenize_scopes(&grammar, "[package]");
let value_scopes = tokenize_scopes(&grammar, "name = \"rustik\"");
let boolean_scopes = tokenize_scopes(&grammar, "publish = false");
assert!(section_scopes.contains(&"entity.name.section.toml"));
assert!(value_scopes.contains(&"variable.other.key.toml"));
assert!(value_scopes.contains(&"punctuation.separator.key-value.toml"));
assert!(value_scopes.contains(&"string.quoted.double.toml"));
assert!(boolean_scopes.contains(&"constant.language.boolean.toml"));
}
#[test]
fn yaml_highlights_keys_scalars_and_comments() {
let grammar = Grammar::from_str(syntaxes::YAML).unwrap();
let key_scopes = tokenize_scopes(&grammar, "name: rustik");
let scalar_scopes = tokenize_scopes(&grammar, "name: \"rustik\"");
let comment_scopes = tokenize_scopes(&grammar, "# generated");
assert!(key_scopes.contains(&"entity.name.tag.yaml"));
assert!(key_scopes.contains(&"punctuation.separator.key-value.mapping.yaml"));
assert!(scalar_scopes.contains(&"string.quoted.double.yaml"));
assert!(comment_scopes.contains(&"comment.line.number-sign.yaml"));
}
#[test]
fn markdown_highlights_reference_links() {
let grammar = Grammar::from_str(syntaxes::MARKDOWN).unwrap();
let scopes = tokenize_scopes(&grammar, "[Earlier this week][post]");
assert!(
scopes.contains(&"meta.link.reference.markdown"),
"{scopes:?}"
);
assert!(
scopes.contains(&"string.other.link.title.markdown"),
"{scopes:?}"
);
assert!(
scopes.contains(&"constant.other.reference.link.markdown"),
"{scopes:?}"
);
}
#[test]
fn radiance_highlights_language_constructs() {
let grammar = Grammar::from_str(syntaxes::RADIANCE).unwrap();
let scopes = tokenize_scopes(
&grammar,
"@test export fn decode(ch: u8) -> bool { return ch >= '0'; }",
);
let module_scopes = tokenize_scopes(&grammar, "mod scanner;");
let import_scopes = tokenize_scopes(&grammar, "use scanner;");
assert!(
scopes.contains(&"support.function.builtin.radiance"),
"{scopes:?}"
);
assert!(
scopes.contains(&"entity.name.function.radiance"),
"{scopes:?}"
);
assert!(scopes.contains(&"storage.type.radiance"), "{scopes:?}");
assert!(
scopes.contains(&"string.quoted.single.radiance"),
"{scopes:?}"
);
assert!(scopes.contains(&"keyword.operator.radiance"), "{scopes:?}");
assert!(
module_scopes.contains(&"entity.name.namespace.radiance"),
"{module_scopes:?}"
);
assert!(
import_scopes.contains(&"entity.name.namespace.radiance"),
"{import_scopes:?}"
);
}
#[test]
fn radiance_highlights_lexical_regions() {
let grammar = Grammar::from_str(syntaxes::RADIANCE).unwrap();
let scopes = tokenize_scopes(
&grammar,
"fn store 'long 'short (dst: &'short mut ?&'long u32) where 'long: 'short {}",
);
let where_scopes = tokenize_scopes(&grammar, "where");
let character_scopes = tokenize_scopes(&grammar, "let ch = 'region';");
assert!(
scopes.contains(&"entity.name.function.radiance"),
"{scopes:?}"
);
assert!(
scopes.contains(&"entity.name.type.region.radiance"),
"{scopes:?}"
);
assert!(
where_scopes.contains(&"keyword.control.radiance"),
"{where_scopes:?}"
);
assert!(
!scopes.contains(&"string.quoted.single.radiance"),
"{scopes:?}"
);
assert!(
character_scopes.contains(&"string.quoted.single.radiance"),
"{character_scopes:?}"
);
assert!(
!character_scopes.contains(&"entity.name.type.region.radiance"),
"{character_scopes:?}"
);
}
#[test]
fn radiance_highlights_scoped_region_bindings() {
let grammar = Grammar::from_str(syntaxes::RADIANCE).unwrap();
let session_scopes = tokenize_scopes(&grammar, "use arena as objects in {");
let region_scopes = tokenize_scopes(
&grammar,
"let resolved: 'phase = &*res where 'arena: 'phase in {",
);
assert!(
session_scopes.contains(&"meta.binding.session.radiance"),
"{session_scopes:?}"
);
assert!(
session_scopes.contains(&"variable.other.radiance"),
"{session_scopes:?}"
);
assert!(
!session_scopes.contains(&"entity.name.namespace.radiance"),
"{session_scopes:?}"
);
assert!(
region_scopes.contains(&"meta.binding.region.radiance"),
"{region_scopes:?}"
);
assert!(
region_scopes.contains(&"variable.other.radiance"),
"{region_scopes:?}"
);
assert!(
region_scopes.contains(&"entity.name.type.region.radiance"),
"{region_scopes:?}"
);
}
#[test]
fn ril_highlights_language_constructs() {
let grammar = Grammar::from_str(syntaxes::RIL).unwrap();
let scopes = tokenize_scopes(&grammar, "br.ult w32 %7 3 @guard#pass $WORDS;");
let quoted_scopes = tokenize_scopes(&grammar, "fn w32 $\"quoted.name\"() {");
assert!(scopes.contains(&"keyword.control.branch.ril"), "{scopes:?}");
assert!(
scopes.contains(&"keyword.operator.comparison.ril"),
"{scopes:?}"
);
assert!(scopes.contains(&"storage.type.ril"), "{scopes:?}");
assert!(
scopes.contains(&"variable.other.register.ril"),
"{scopes:?}"
);
assert!(
scopes.contains(&"constant.numeric.integer.decimal.ril"),
"{scopes:?}"
);
assert!(scopes.contains(&"entity.name.label.ril"), "{scopes:?}");
assert!(scopes.contains(&"variable.other.symbol.ril"), "{scopes:?}");
assert!(
quoted_scopes.contains(&"variable.other.symbol.quoted.ril"),
"{quoted_scopes:?}"
);
}
/// Tokenizes `line` and returns its scope names.
fn tokenize_scopes<'a>(grammar: &'a Grammar, line: &str) -> Vec<&'a str> {
let mut state = LineState::default();
let mut tokenizer = LineTokenizer::new(grammar);
let spans = tokenizer.tokenize_line(&mut state, line);
spans
.iter()
.filter_map(|span| grammar.scopes.get(span.scope.index()))
.map(String::as_str)
.collect()
}
/// Tokenizes `line` inside a CSS rule body and returns its scope names.
fn css_rule_scopes<'a>(grammar: &'a Grammar, line: &str) -> Vec<&'a str> {
let mut state = LineState::default();
let mut tokenizer = LineTokenizer::new(grammar);
let _ = tokenizer.tokenize_line(&mut state, ".x {");
let spans = tokenizer.tokenize_line(&mut state, line);
spans
.iter()
.filter_map(|span| grammar.scopes.get(span.scope.index()))
.map(String::as_str)
.collect()
}