slug_test.go
package main
import (
"strings"
"testing"
)
func TestSlugify(t *testing.T) {
cases := map[string]string{
"What the verifier catches": "what-the-verifier-catches",
"Use before definition": "use-before-definition",
"C++ / FFI (part 2)": "c-ffi-part-2",
" spaces everywhere ": "spaces-everywhere",
"???": "",
}
for in, want := range cases {
if got := slugify(in); got != want {
t.Errorf("slugify(%q) = %q, want %q", in, got, want)
}
}
}
func TestHeadingAnchorsInRenderedPage(t *testing.T) {
srv := newTestServer(t)
rec := get(t, srv, "/demo/README.md")
body := rec.Body.String()
if !strings.Contains(body, `<h1 id="hello">`) {
t.Errorf("heading missing slug id; body:\n%s", body)
}
if !strings.Contains(body, `<a href="#hello" class="anchor">`) {
t.Errorf("heading missing chainlink anchor; body:\n%s", body)
}
}
func TestDuplicateHeadingsGetDistinctIDs(t *testing.T) {
srv := newTestServer(t)
// dup.md is written by newTestServer with two identical headings.
rec := get(t, srv, "/demo/dup.md")
body := rec.Body.String()
if !strings.Contains(body, `id="twice"`) || !strings.Contains(body, `id="twice-2"`) {
t.Errorf("duplicate headings not disambiguated; body:\n%s", body)
}
}
func TestStylesheetServed(t *testing.T) {
rec := get(t, newTestServer(t), "/-/style.css")
if rec.Code != 200 || !strings.HasPrefix(rec.Header().Get("Content-Type"), "text/css") {
t.Errorf("status = %d, content-type = %q", rec.Code, rec.Header().Get("Content-Type"))
}
}
slug_test.go · 1.5 KiB · b0ad0cdd90