git@cosmocr.at/gitweb.git @ main · summary refs log
main / internal / render / module_test.go /
internal/render/module_test.go · 1.5 KiB · raw
view: Source ☐ 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package render

import "testing"

func TestRegistrySelect(t *testing.T) {
	reg := NewRegistry(New())
	md := []byte("# hi\n")

	cases := []struct {
		name   string
		file   string
		id     string
		force  bool
		wantID string
	}{
		{"empty falls back to default", "main.go", "", false, "source"},
		{"unknown id falls back to default", "main.go", "nope", false, "source"},
		{"eligible id selected", "README.md", "markdown", false, "markdown"},
		{"ineligible id without force -> default", "main.go", "markdown", false, "source"},
		{"ineligible id with force -> selected", "main.go", "markdown", true, "markdown"},
		{"default id always allowed", "README.md", "source", false, "source"},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := reg.Select(c.file, md, c.id, c.force)
			if got.ID != c.wantID {
				t.Fatalf("Select(%q, %q, force=%v) = %q, want %q",
					c.file, c.id, c.force, got.ID, c.wantID)
			}
		})
	}
}

func TestRegistryEligible(t *testing.T) {
	reg := NewRegistry(New())

	// Markdown file: both source and markdown offered.
	got := ids(reg.Eligible("README.md", nil))
	if len(got) != 2 || got[0] != "source" || got[1] != "markdown" {
		t.Fatalf("eligible(README.md) = %v, want [source markdown]", got)
	}

	// Non-markdown file: only the default.
	got = ids(reg.Eligible("main.go", nil))
	if len(got) != 1 || got[0] != "source" {
		t.Fatalf("eligible(main.go) = %v, want [source]", got)
	}
}

func ids(mods []Module) []string {
	out := make([]string, len(mods))
	for i, m := range mods {
		out[i] = m.ID
	}
	return out
}