|
| 1 | +package gor |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +func TestEncodePathInfo(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + input string |
| 11 | + expected string |
| 12 | + }{ |
| 13 | + {"hello world", "hello-world"}, |
| 14 | + {"go:lang", "go-lang"}, |
| 15 | + {"simple", "simple"}, |
| 16 | + {"a b:c", "a-b-c"}, |
| 17 | + } |
| 18 | + for _, tt := range tests { |
| 19 | + got := EncodePathInfo(tt.input) |
| 20 | + if got != tt.expected { |
| 21 | + t.Errorf("EncodePathInfo(%q) = %q, want %q", tt.input, got, tt.expected) |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestDecodePathInfo(t *testing.T) { |
| 27 | + tests := []struct { |
| 28 | + input string |
| 29 | + expected string |
| 30 | + }{ |
| 31 | + {"hello%20world", "hello world"}, |
| 32 | + {"go-lang", "go-lang"}, |
| 33 | + {"simple", "simple"}, |
| 34 | + } |
| 35 | + for _, tt := range tests { |
| 36 | + got := DecodePathInfo(tt.input) |
| 37 | + if got != tt.expected { |
| 38 | + t.Errorf("DecodePathInfo(%q) = %q, want %q", tt.input, got, tt.expected) |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestEncodeDecodePathInfo(t *testing.T) { |
| 44 | + original := "hello world" |
| 45 | + encoded := EncodePathInfo(original) |
| 46 | + // EncodePathInfo replaces spaces with dashes, not url-encode, so round-trip differs |
| 47 | + if encoded == original { |
| 48 | + t.Errorf("EncodePathInfo should transform %q", original) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestAsStrings(t *testing.T) { |
| 53 | + tests := []struct { |
| 54 | + name string |
| 55 | + input interface{} |
| 56 | + expected []string |
| 57 | + }{ |
| 58 | + {"nil", nil, []string{}}, |
| 59 | + {"string", "hello", []string{"hello"}}, |
| 60 | + {"string slice", []string{"a", "b"}, []string{"a", "b"}}, |
| 61 | + {"interface slice", []interface{}{"x", "y"}, []string{"x", "y"}}, |
| 62 | + } |
| 63 | + for _, tt := range tests { |
| 64 | + got := AsStrings(tt.input) |
| 65 | + if len(got) != len(tt.expected) { |
| 66 | + t.Errorf("[%s] AsStrings(%v) len=%d, want %d", tt.name, tt.input, len(got), len(tt.expected)) |
| 67 | + continue |
| 68 | + } |
| 69 | + for i := range got { |
| 70 | + if got[i] != tt.expected[i] { |
| 71 | + t.Errorf("[%s] AsStrings(%v)[%d] = %q, want %q", tt.name, tt.input, i, got[i], tt.expected[i]) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestCreatePostURL(t *testing.T) { |
| 78 | + db := make(map[string]interface{}) |
| 79 | + date := time.Date(2023, 5, 15, 0, 0, 0, 0, time.Local) |
| 80 | + post := map[string]interface{}{ |
| 81 | + "permalink": "/:year/:month/:title/", |
| 82 | + "_date": date, |
| 83 | + "title": "Hello World", |
| 84 | + "id": "posts/2023-05-15-hello-world.md", |
| 85 | + "categories": []string{"go"}, |
| 86 | + } |
| 87 | + CreatePostURL(db, "/", post) |
| 88 | + url, ok := post["url"].(string) |
| 89 | + if !ok { |
| 90 | + t.Fatal("post[url] should be a string") |
| 91 | + } |
| 92 | + if url != "/2023/05/Hello-World/" { |
| 93 | + t.Errorf("unexpected url: %q", url) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestCreatePostURL_StaticPermalink(t *testing.T) { |
| 98 | + db := make(map[string]interface{}) |
| 99 | + date := time.Date(2023, 1, 1, 0, 0, 0, 0, time.Local) |
| 100 | + post := map[string]interface{}{ |
| 101 | + "permalink": "/about/", |
| 102 | + "_date": date, |
| 103 | + "title": "About", |
| 104 | + "id": "pages/about.md", |
| 105 | + "categories": []string{}, |
| 106 | + } |
| 107 | + CreatePostURL(db, "/", post) |
| 108 | + url, ok := post["url"].(string) |
| 109 | + if !ok { |
| 110 | + t.Fatal("post[url] should be a string") |
| 111 | + } |
| 112 | + if url != "/about/" { |
| 113 | + t.Errorf("unexpected url: %q", url) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestMarkdownToHtml(t *testing.T) { |
| 118 | + tests := []struct { |
| 119 | + name string |
| 120 | + input string |
| 121 | + contains string |
| 122 | + }{ |
| 123 | + {"bold", "**hello**", "<strong>hello</strong>"}, |
| 124 | + {"header", "# Title", "<h1>Title</h1>"}, |
| 125 | + {"paragraph", "simple text", "simple text"}, |
| 126 | + } |
| 127 | + for _, tt := range tests { |
| 128 | + got := MarkdownToHtml(tt.input) |
| 129 | + if len(got) == 0 { |
| 130 | + t.Errorf("[%s] MarkdownToHtml(%q) returned empty string", tt.name, tt.input) |
| 131 | + continue |
| 132 | + } |
| 133 | + found := false |
| 134 | + for i := 0; i <= len(got)-len(tt.contains); i++ { |
| 135 | + if got[i:i+len(tt.contains)] == tt.contains { |
| 136 | + found = true |
| 137 | + break |
| 138 | + } |
| 139 | + } |
| 140 | + if !found { |
| 141 | + t.Errorf("[%s] MarkdownToHtml(%q) = %q, want it to contain %q", tt.name, tt.input, got, tt.contains) |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func TestSortPosts(t *testing.T) { |
| 147 | + date1 := time.Date(2023, 1, 1, 0, 0, 0, 0, time.Local) |
| 148 | + date2 := time.Date(2023, 6, 1, 0, 0, 0, 0, time.Local) |
| 149 | + dict := map[string]Mapper{ |
| 150 | + "posts/older.md": {"id": "posts/older.md", "_date": date1}, |
| 151 | + "posts/newer.md": {"id": "posts/newer.md", "_date": date2}, |
| 152 | + } |
| 153 | + ids := []string{"posts/older.md", "posts/newer.md"} |
| 154 | + sorted := SortPosts(dict, ids) |
| 155 | + if sorted[0] != "posts/newer.md" { |
| 156 | + t.Errorf("SortPosts: expected newer post first, got %v", sorted) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func TestMapperGetString(t *testing.T) { |
| 161 | + m := Mapper{"key": "value", "num": 42} |
| 162 | + if m.GetString("key") != "value" { |
| 163 | + t.Errorf("GetString(key) should return 'value'") |
| 164 | + } |
| 165 | + if m.GetString("missing") != "" { |
| 166 | + t.Errorf("GetString(missing) should return ''") |
| 167 | + } |
| 168 | + if m.GetString("num") != "42" { |
| 169 | + t.Errorf("GetString(num) should return '42'") |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +func TestMapperGetInt(t *testing.T) { |
| 174 | + m := Mapper{"a": int64(5), "b": 10, "c": "x"} |
| 175 | + if m.GetInt("a") != 5 { |
| 176 | + t.Errorf("GetInt(a) should return 5") |
| 177 | + } |
| 178 | + if m.GetInt("b") != 10 { |
| 179 | + t.Errorf("GetInt(b) should return 10") |
| 180 | + } |
| 181 | + if m.GetInt("missing") != 0 { |
| 182 | + t.Errorf("GetInt(missing) should return 0") |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +func TestPostPath(t *testing.T) { |
| 187 | + tests := []struct { |
| 188 | + title string |
| 189 | + contains string |
| 190 | + }{ |
| 191 | + {"my post", "my-post"}, |
| 192 | + {"hello-world", "hello-world"}, |
| 193 | + } |
| 194 | + for _, tt := range tests { |
| 195 | + got := postPath(tt.title) |
| 196 | + found := false |
| 197 | + for i := 0; i <= len(got)-len(tt.contains); i++ { |
| 198 | + if got[i:i+len(tt.contains)] == tt.contains { |
| 199 | + found = true |
| 200 | + break |
| 201 | + } |
| 202 | + } |
| 203 | + if !found { |
| 204 | + t.Errorf("postPath(%q) = %q, want it to contain %q", tt.title, got, tt.contains) |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments