git@cosmocr.at/gitweb.git @ HEAD · summary refs log
HEAD / internal / render / rebase_test.go /
internal/render/rebase_test.go · 1.4 KiB · raw
view: SourceMarkdown ☑ force
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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", `<a href="DEPLOY.md">d</a>`, `href="` + link + `DEPLOY.md"`, ""},
		{"relative subdir link", `<a href="docs/x.md">d</a>`, `href="` + link + `docs/x.md"`, ""},
		{"parent link", `<a href="../y.md">y</a>`, `href="` + link + `../y.md"`, ""},
		{"relative image uses raw", `<img src="img.png">`, `src="` + raw + `img.png"`, ""},
		{"absolute link untouched", `<a href="https://x.io/a">a</a>`, `href="https://x.io/a"`, link},
		{"root-relative untouched", `<a href="/abs">a</a>`, `href="/abs"`, link + "abs"},
		{"anchor untouched", `<a href="#sec">s</a>`, `href="#sec"`, link},
		{"mailto untouched", `<a href="mailto:x@y.io">m</a>`, `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)
			}
		})
	}
}