| 208 | } |
| 209 | |
| 210 | func TestSerialize(t *testing.T) { |
| 211 | tests := []struct { |
| 212 | name string |
| 213 | frontmatter map[string]any |
| 214 | body string |
| 215 | wantPrefix string |
| 216 | wantSuffix string |
| 217 | wantContains []string |
| 218 | }{ |
| 219 | { |
| 220 | name: "with body", |
| 221 | frontmatter: map[string]any{"name": "test"}, |
| 222 | body: "# Body content", |
| 223 | wantPrefix: "---\n", |
| 224 | wantContains: []string{ |
| 225 | "name: test", |
| 226 | "# Body content", |
| 227 | }, |
| 228 | }, |
| 229 | { |
| 230 | name: "empty body", |
| 231 | frontmatter: map[string]any{"name": "test"}, |
| 232 | body: "", |
| 233 | wantSuffix: "---\n", |
| 234 | }, |
| 235 | { |
| 236 | name: "body without trailing newline gets one added", |
| 237 | frontmatter: map[string]any{"name": "test"}, |
| 238 | body: "# No trailing newline", |
| 239 | wantSuffix: "# No trailing newline\n", |
| 240 | }, |
| 241 | } |
| 242 | |
| 243 | for _, tt := range tests { |
| 244 | t.Run(tt.name, func(t *testing.T) { |
| 245 | got, err := Serialize(tt.frontmatter, tt.body) |
| 246 | require.NoError(t, err) |
| 247 | if tt.wantPrefix != "" { |
| 248 | assert.True(t, strings.HasPrefix(got, tt.wantPrefix)) |
| 249 | } |
| 250 | if tt.wantSuffix != "" { |
| 251 | assert.True(t, strings.HasSuffix(got, tt.wantSuffix)) |
| 252 | } |
| 253 | for _, s := range tt.wantContains { |
| 254 | assert.Contains(t, got, s) |
| 255 | } |
| 256 | }) |
| 257 | } |
| 258 | } |