//! 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 =