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