package render import ( "html/template" "strings" "testing" ) func TestRebase(t *testing.T) { const link = "/u@d.com/x.git/blob/HEAD/" const raw = "/u@d.com/x.git/raw/HEAD/" cases := []struct { name string in string want string // substring expected in the output bad string // substring that must NOT appear (optional) }{ {"relative link", `d`, `href="` + link + `DEPLOY.md"`, ""}, {"relative subdir link", `d`, `href="` + link + `docs/x.md"`, ""}, {"parent link", `y`, `href="` + link + `../y.md"`, ""}, {"relative image uses raw", ``, `src="` + raw + `img.png"`, ""}, {"absolute link untouched", `a`, `href="https://x.io/a"`, link}, {"root-relative untouched", `a`, `href="/abs"`, link + "abs"}, {"anchor untouched", `s`, `href="#sec"`, link}, {"mailto untouched", `m`, `href="mailto:x@y.io"`, link}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { got := string(Rebase(template.HTML(c.in), link, raw)) if !strings.Contains(got, c.want) { t.Fatalf("Rebase(%q) = %q, want substring %q", c.in, got, c.want) } if c.bad != "" && strings.Contains(got, c.bad) { t.Fatalf("Rebase(%q) = %q, must not contain %q", c.in, got, c.bad) } }) } }