package markup
import (
"regexp"
"strings"
"testing"
)
// A diagram is framed for the page: the frame carries the drawn width
// the stylesheet shrinks and floors against, and the diagram links to
// its own frame, which is what opens it full size.
func TestADiagramIsFramedWithItsWidthAndOpensItself(t *testing.T) {
out, err := Render([]byte("before\n\n```pikchr\nbox \"a\"; arrow; box \"b\"\n```\n\nafter\n"), nil)
if err != nil {
t.Fatal(err)
}
html := string(out)
fig := regexp.MustCompile(`<figure class="pikchr-fig" id="(pk-[0-9a-f]{10})" style="--pk-w:([0-9]+)px">`).FindStringSubmatch(html)
if fig == nil {
t.Fatalf("FAIL: the diagram has no frame carrying its width: %.300s", html)
}
if !strings.Contains(html, `<a class="pikchr-open" href="#`+fig[1]+`"`) {
t.Errorf("FAIL: the diagram does not link to its own frame %s", fig[1])
}
if strings.Contains(html, "<p><figure") {
t.Error("FAIL: the frame sits inside a paragraph")
}
if !t.Failed() {
t.Logf("ok: framed as %s at %spx", fig[1], fig[2])
}
}
// Two different diagrams on one page never share a frame id.
func TestTwoDiagramsHaveTwoIds(t *testing.T) {
a := Figure([]byte(`<svg class="pikchr" viewBox="0 0 100 50"><path d="M0,0"/></svg>`), nil)
b := Figure([]byte(`<svg class="pikchr" viewBox="0 0 200 50"><path d="M0,0"/></svg>`), nil)
id := regexp.MustCompile(`id="(pk-[0-9a-f]+)"`)
if id.FindStringSubmatch(string(a))[1] == id.FindStringSubmatch(string(b))[1] {
t.Error("FAIL: two diagrams share one frame id")
} else {
t.Log("ok: each diagram has its own id")
}
}