|
| 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 | +} |
0 commit comments