Files
summercms/internal/docsite/fences_test.go
Jakub Zych 9ba5530ca7 test(11.1-07): planted fixtures and unit tests for fence, execution and name-form holes
- eighteen violation plants pin nested src=, Go aliases, unrun examples, build membership and command forms
- unit tests cover fence collection, captions, goLang, go doc -c, commandWord and Sync
2026-10-01 08:45:34 +02:00

85 lines
1.8 KiB
Go

package docsite
import (
"strings"
"testing"
)
func TestCollectFences(t *testing.T) {
body := strings.Join([]string{
"```go",
"top",
"```",
"",
"> [!TIP]",
"> ```go",
"> callout line",
"> ```",
"",
"> ```yaml",
"> quote line",
"> ```",
"",
"- item",
"",
" ```go",
" list line",
" ```",
"",
"```",
"noinfo",
"```",
"",
" ```",
" four spaces",
" ```",
"",
"```sh",
"unterminated",
}, "\n")
doc := (&site{}).parseRaw(newMarkdown(), []byte(body))
fences, err := collectFences(doc, []byte(body))
if err != nil {
t.Fatal(err)
}
if len(fences) != 6 {
t.Fatalf("fences = %d, want 6 (four-space backticks are not a fence)", len(fences))
}
want := []struct {
info, code string
line int
top bool
close int // -2 means top was not filled
}{
{"go", "top", 0, true, 2},
{"go", "callout line", 5, false, -2},
{"yaml", "quote line", 9, false, -2},
{"go", "list line", 15, false, -2},
{"", "noinfo", 19, true, -2},
{"sh", "unterminated", 27, true, -1},
}
for i, w := range want {
f := fences[i]
if f.info != w.info || f.line != w.line || f.topLevel != w.top {
t.Errorf("fence %d = info %q line %d top %v, want info %q line %d top %v",
i, f.info, f.line, f.topLevel, w.info, w.line, w.top)
}
if len(f.code) != 1 || f.code[0].text != w.code {
got := ""
if len(f.code) > 0 {
got = f.code[0].text
}
t.Errorf("fence %d code = %q, want %q", i, got, w.code)
}
if w.close != -2 && f.top.close != w.close {
t.Errorf("fence %d top.close = %d, want %d", i, f.top.close, w.close)
}
}
if _, ok := openFence(" ```go"); !ok {
t.Error("openFence rejected three leading spaces")
}
if _, ok := openFence(" ```go"); ok {
t.Error("openFence accepted four leading spaces")
}
}