package highlight
import (
"regexp"
"strings"
"testing"
)
func TestCodeByFilename(t *testing.T) {
out, ok := Code([]byte("package main\n"), "", "src/main.go")
if !ok {
t.Fatal("go file not highlighted")
}
if !strings.Contains(out, "<span class=") || !strings.Contains(out, "package") {
t.Fatalf("no classed spans:\n%s", out)
}
if strings.Contains(out, "style=") {
t.Fatalf("inline styles leak into output (classes only):\n%s", out)
}
}
func TestCodeByLanguage(t *testing.T) {
if _, ok := Code([]byte("SELECT 1;\n"), "sql", ""); !ok {
t.Fatal("language hint not used")
}
}
func TestCodeNoLexer(t *testing.T) {
// Pure binary-ish content with no filename or language: callers get
// ok=false and fall back to escaped <pre>.
if out, ok := Code([]byte{0x00, 0x01, 0x02}, "", "data.zzzunknown"); ok {
t.Fatalf("unexpected lexer match:\n%s", out)
}
}
// Every colour in the highlight CSS is a palette variable. The palette
// redefines each per theme, so both themes and all three theme states
// come from one set of rules, and a token class chroma leaves
// uncoloured inherits --fg on either ground (ticket 030's invisible
// identifiers cannot recur).
func TestCSSColoursAreAllPaletteTokens(t *testing.T) {
css := CSS()
if !strings.Contains(css, ".chroma") {
t.Fatal("no chroma rules")
}
if hex := regexp.MustCompile(`#[0-9a-fA-F]{3,8}\b`).FindAllString(css, -1); len(hex) > 0 {
t.Errorf("FAIL: raw colours in the highlight CSS, outside the palette: %v", hex)
}
if !regexp.MustCompile(`\.chroma \{[^}]*color: var\(--fg\)`).MatchString(css) {
t.Error("FAIL: the code block does not default to --fg; an uncoloured token could vanish on one ground")
}
for _, v := range []string{"--syn-kw", "--syn-str", "--syn-com", "--del-row", "--add-row"} {
if !strings.Contains(css, "var("+v+")") {
t.Errorf("FAIL: %s is never used", v)
}
}
if strings.Contains(css, "prefers-color-scheme") {
t.Error("FAIL: the highlight CSS themes itself; theming belongs to the palette")
}
}