Skip to content

Commit efb0aba

Browse files
committed
add some tests
1 parent 6c7e4be commit efb0aba

3 files changed

Lines changed: 163 additions & 11 deletions

File tree

.github/workflows/validate.yml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Validate and compile
1+
name: Test
22

33
on:
44
push:
@@ -7,7 +7,7 @@ on:
77
branches: [main]
88

99
jobs:
10-
validate:
10+
test:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- uses: actions/checkout@v4
@@ -18,12 +18,6 @@ jobs:
1818
go-version: "1.21"
1919
cache-dependency-path: compiler/go.sum
2020

21-
- name: Build compiler
22-
run: make build
23-
24-
- name: Validate and compile template
25-
run: cd template && ../compiler/bin/compile
26-
27-
- name: Check template output is up to date
28-
run: |
29-
cd template && OUTPUT=$(../compiler/bin/compile -print-out) && git diff --exit-code "$OUTPUT" || (echo "::error::$OUTPUT is out of date. Run: make build && cd template && ../compiler/bin/compile" && exit 1)
21+
- name: Run tests
22+
run: go test ./...
23+
working-directory: compiler

compiler/compile/render_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package compile
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func TestGroupByMainCategory_sortByName(t *testing.T) {
9+
info := &Info{} // no PositionOrder
10+
categories := []Category{{ID: "tools", Name: "Tools"}}
11+
items := map[string]*Item{
12+
"b": {Name: "B", MainCategory: "tools"},
13+
"a": {Name: "A", MainCategory: "tools"},
14+
}
15+
byCat := GroupByMainCategory(info, categories, items)
16+
list := byCat["tools"]
17+
if len(list) != 2 {
18+
t.Fatalf("expected 2 items, got %d", len(list))
19+
}
20+
if list[0].Name != "A" || list[1].Name != "B" {
21+
t.Errorf("expected order A, B; got %s, %s", list[0].Name, list[1].Name)
22+
}
23+
}
24+
25+
func TestGroupByMainCategory_sortByPosition(t *testing.T) {
26+
info := &Info{PositionOrder: []string{"featured", "ordinary"}}
27+
categories := []Category{{ID: "tools", Name: "Tools"}}
28+
items := map[string]*Item{
29+
"a": {Name: "A", MainCategory: "tools", Position: "ordinary"},
30+
"b": {Name: "B", MainCategory: "tools", Position: "featured"},
31+
}
32+
byCat := GroupByMainCategory(info, categories, items)
33+
list := byCat["tools"]
34+
if len(list) != 2 {
35+
t.Fatalf("expected 2 items, got %d", len(list))
36+
}
37+
if list[0].Name != "B" || list[1].Name != "A" {
38+
t.Errorf("expected featured first (B, A); got %s, %s", list[0].Name, list[1].Name)
39+
}
40+
}
41+
42+
func TestRender_containsTitleAndContents(t *testing.T) {
43+
info := &Info{Name: "Awesome List", Description: "A list."}
44+
categories := []Category{{ID: "tools", Name: "Tools"}}
45+
items := map[string]*Item{
46+
"foo": {
47+
Name: "Foo", URL: "https://foo.com", MainCategory: "tools",
48+
},
49+
}
50+
items["foo"].onelinerValue = "A tool."
51+
byCat := GroupByMainCategory(info, categories, items)
52+
md := Render(info, categories, byCat)
53+
if !strings.Contains(md, "# Awesome List") {
54+
t.Error("output should contain title")
55+
}
56+
if !strings.Contains(md, "## Contents") {
57+
t.Error("output should contain Contents")
58+
}
59+
if !strings.Contains(md, "## Tools") {
60+
t.Error("output should contain Tools section")
61+
}
62+
if !strings.Contains(md, "[Foo](https://foo.com)") {
63+
t.Error("output should contain item link")
64+
}
65+
}
66+
67+
func TestEscapeMarkdownBracket(t *testing.T) {
68+
// escapeMarkdownBracket is unexported; test via Render with item name containing ]
69+
info := &Info{Name: "T", Description: "D"}
70+
categories := []Category{{ID: "x", Name: "X"}}
71+
items := map[string]*Item{
72+
"a": {Name: "Item [extra]", URL: "https://x.com", MainCategory: "x"},
73+
}
74+
items["a"].onelinerValue = "Desc."
75+
byCat := GroupByMainCategory(info, categories, items)
76+
md := Render(info, categories, byCat)
77+
if !strings.Contains(md, `\]`) {
78+
t.Error("expected bracket in name to be escaped")
79+
}
80+
}

compiler/compile/validate_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package compile
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestValidate_emptyInfo(t *testing.T) {
8+
info := &Info{}
9+
categories := []Category{{ID: "tools", Name: "Tools"}}
10+
items := map[string]*Item{}
11+
errs := Validate(info, categories, items)
12+
if len(errs) < 2 {
13+
t.Fatalf("expected at least 2 errors for empty info, got %d", len(errs))
14+
}
15+
}
16+
17+
func TestValidate_valid(t *testing.T) {
18+
info := &Info{Name: "Test", Description: "Desc"}
19+
categories := []Category{{ID: "tools", Name: "Tools"}}
20+
item := &Item{
21+
Name: "Foo", Slug: "foo", URL: "https://foo.com",
22+
MainCategory: "tools",
23+
}
24+
item.onelinerValue = "A tool."
25+
items := map[string]*Item{"foo": item}
26+
errs := Validate(info, categories, items)
27+
if len(errs) != 0 {
28+
t.Fatalf("expected no errors, got %v", errs)
29+
}
30+
}
31+
32+
func TestValidate_slugMismatch(t *testing.T) {
33+
info := &Info{Name: "Test", Description: "Desc"}
34+
categories := []Category{{ID: "tools", Name: "Tools"}}
35+
item := &Item{
36+
Name: "Foo", Slug: "wrong", URL: "https://foo.com",
37+
MainCategory: "tools",
38+
}
39+
item.onelinerValue = "A tool."
40+
items := map[string]*Item{"foo": item}
41+
errs := Validate(info, categories, items)
42+
if len(errs) == 0 {
43+
t.Fatal("expected error for slug mismatch")
44+
}
45+
}
46+
47+
func TestValidate_unknownCategory(t *testing.T) {
48+
info := &Info{Name: "Test", Description: "Desc"}
49+
categories := []Category{{ID: "tools", Name: "Tools"}}
50+
item := &Item{
51+
Name: "Foo", Slug: "foo", URL: "https://foo.com",
52+
MainCategory: "unknown",
53+
}
54+
item.onelinerValue = "A tool."
55+
items := map[string]*Item{"foo": item}
56+
errs := Validate(info, categories, items)
57+
if len(errs) == 0 {
58+
t.Fatal("expected error for unknown main_category")
59+
}
60+
}
61+
62+
func TestOnelinerSuffix(t *testing.T) {
63+
tests := []struct {
64+
in string
65+
want string
66+
}{
67+
{"hello", "hello."},
68+
{"hello.", "hello."},
69+
{" hi ", "hi."},
70+
{"", ""},
71+
}
72+
for _, tt := range tests {
73+
got := OnelinerSuffix(tt.in)
74+
if got != tt.want {
75+
t.Errorf("OnelinerSuffix(%q) = %q, want %q", tt.in, got, tt.want)
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)