Gitfed
bastien-mrq/gitfed / cmd / gitfed-web / highlight_test.go
package main

import (
	"strings"
	"testing"
)

func TestHighlightCodeKnownLanguage(t *testing.T) {
	html, ok, err := highlightCode("main.go", "package main\n\nfunc main() {}\n")
	if err != nil {
		t.Fatalf("highlightCode: %v", err)
	}
	if !ok {
		t.Fatal("expected a lexer match for main.go")
	}
	s := string(html)
	if !strings.HasPrefix(s, "<pre><code>") || !strings.HasSuffix(s, "</code></pre>") {
		t.Fatalf("expected content wrapped in a single <pre><code>...</code></pre>, got: %s", s)
	}
	if !strings.Contains(s, "package") {
		t.Fatalf("expected the source text to survive highlighting, got: %s", s)
	}
	if !strings.Contains(s, "style=") {
		t.Fatalf("expected inline per-token styles (CSP relies on style-src 'unsafe-inline', not classes), got: %s", s)
	}
}

func TestHighlightCodeUnknownLanguage(t *testing.T) {
	html, ok, err := highlightCode("data.bin", "whatever")
	if err != nil {
		t.Fatalf("highlightCode: %v", err)
	}
	if ok {
		t.Fatalf("expected no lexer match for data.bin, got %q", html)
	}
	if html != "" {
		t.Fatalf("expected empty output when ok=false, got %q", html)
	}
}