Skip to content
quarry Artifact [7e09b549dd]
Login

Artifact [7e09b549dd]

Artifact 7e09b549dd597b32af1f932f25ce2957a3a4fa5b0fa7d24b0d8d789355d6e6dc:


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)
	}
}

func TestCSSHasBothThemes(t *testing.T) {
	css := CSS()
	if !strings.Contains(css, ".chroma") {
		t.Fatal("no chroma rules")
	}
	if !strings.Contains(css, "prefers-color-scheme: dark") {
		t.Fatal("no dark theme block")
	}
}

func TestDarkThemeCoversEveryLightClass(t *testing.T) {
	// Ticket 030: every token class colored in light mode must also be
	// colored inside the dark block, or identifiers/punctuation render
	// near-black on the dark background.
	css := CSS()
	i := strings.Index(css, "prefers-color-scheme: dark")
	if i < 0 {
		t.Fatal("no dark block")
	}
	light, dark := css[:i], css[i:]
	re := regexp.MustCompile(`\.chroma \.([a-zA-Z0-9]+) `)
	darkClasses := map[string]bool{}
	for _, m := range re.FindAllStringSubmatch(dark, -1) {
		darkClasses[m[1]] = true
	}
	for _, m := range re.FindAllStringSubmatch(light, -1) {
		if !darkClasses[m[1]] {
			t.Errorf("class .%s colored in light mode but not in dark", m[1])
		}
	}
	// The classes the test round found invisible must now be covered.
	for _, c := range []string{"nx", "p", "na", "nb", "bp"} {
		if !darkClasses[c] {
			t.Errorf("known-invisible class .%s still missing from dark block", c)
		}
	}
}