1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
// Copyright (c) 2023 Nicolas Paul All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"strconv"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestParseFrontMatter(t *testing.T) {
tests := []struct {
input []byte
want FrontMatter
rest string
}{
{
input: []byte(`---
title: "Test"
description: "Test"
publication_time: 2020-01-01T00:00:00Z
last_update_time: 2020-01-01T00:00:00Z
keywords: ["test"]
author: "Test"
hide: false
---
# Test`),
want: FrontMatter{
Title: "Test",
Description: "Test",
PublicationTime: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
LastUpdateTime: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
Keywords: []string{"test"},
Author: "Test",
Hide: false,
},
rest: "# Test",
},
{
input: []byte(`---
title: "Test"
description: "Test"
publication_time: 2020-01-01T00:00:00Z
keywords: ["test", "test2", "test3"]
---
# Test`),
want: FrontMatter{
Title: "Test",
Description: "Test",
PublicationTime: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
Keywords: []string{"test", "test2", "test3"},
},
rest: "# Test",
},
}
for _, tt := range tests {
fm, rest, err := ParseFrontMatter(tt.input)
if err != nil {
t.Fatalf("ParseFrontMatter(%v) got error %v", tt.input, err)
}
if !cmp.Equal(fm, tt.want) {
t.Fatalf("ParseFrontMatter(%v) got %v want %v", tt.input, fm, tt.want)
}
if string(rest) != tt.rest {
t.Fatalf("ParseFrontMatter(%v) got %v want %v", tt.input, rest, tt.rest)
}
}
}
func FuzzParseFrontMatter(f *testing.F) {
// Do not fuzz *_time fields as time.Time is not supported by f.Fuzz().
f.Fuzz(func(t *testing.T, title string, description string, keyword1 string, keyword2 string, keyword3 string, author string, hide bool, text string) {
input := []byte(`---
title: "` + title + `"
description: "` + description + `"
publication_time: 2020-01-01T00:00:00Z
last_update_time: 2020-01-01T00:00:00Z
keywords: ["` + keyword1 + `", "` + keyword2 + `", "` + keyword2 + `"]
author: "` + author + `"
hide: ` + strconv.FormatBool(hide) + `
---
` + text)
fm, rest, err := ParseFrontMatter(input)
if err != nil {
t.Fatalf("ParseFrontMatter(%v) got error %v", input, err)
}
if string(rest) != text {
t.Fatalf("ParseFrontMatter(%v) got %v want %v", input, rest, text)
}
result := FrontMatter{
Title: title,
Description: description,
Keywords: []string{keyword1, keyword2, keyword3},
PublicationTime: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
LastUpdateTime: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
Author: author,
Hide: hide,
}
if !cmp.Equal(fm, result) {
t.Fatalf("ParseFrontMatter(%v) got %v want %v", input, fm, result)
}
})
}
|