-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
84 lines (77 loc) · 2.09 KB
/
main_test.go
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
package main
import (
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"log"
"os"
"testing"
)
func TestParseFrontMatter(t *testing.T) {
tables := []struct {
FrontMatterBytes []byte
ExpectedResult map[string][]string
ExpectedError error
}{
{
[]byte("name: pasta salad\ntags: [summer, salad, pasta]"),
map[string][]string{"name": []string{"pasta salad"}, "tags": []string{"summer", "salad", "pasta"}},
nil,
},
{
[]byte("name: nachos\ningredients: [minced beef]"),
map[string][]string{"name": []string{"nachos"}, "ingredients": []string{"minced beef"}},
nil,
},
{
[]byte("name: \ningredients: [a mystery]"),
nil,
fmt.Errorf("Key 'name' has the value <nil>"),
},
}
for _, table := range tables {
actualResult, actualError := ParseFrontMatter(table.FrontMatterBytes)
assert.Equal(t, table.ExpectedResult, actualResult)
assert.Equal(t, table.ExpectedError, actualError)
}
}
func CreateTempTestFile(contents []byte) *os.File {
// Specifying an empty string for the first arg means that Tempfile will
// use the default directory for temporary files
tmpfile, err := ioutil.TempFile("", "testrecipe")
if err != nil {
log.Fatal(err)
}
if _, err := tmpfile.Write(contents); err != nil {
log.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
log.Fatal(err)
}
return tmpfile
}
func TestParseFile(t *testing.T) {
tables := []struct {
FileContentString []byte
ExpectedResult RecipeFile
ExpectedError error
}{
{
[]byte("---\nname: french fries\ntexture: [crispy]\n---\nThe recipe."),
RecipeFile{[]byte("name: french fries\ntexture: [crispy]"), []byte("The recipe.")},
nil,
},
{
[]byte("---\nname: lasagna\n---\n---\nThere was no Markdown."),
RecipeFile{},
fmt.Errorf("No Markdown has been defined in this file."),
},
}
for _, table := range tables {
f := CreateTempTestFile(table.FileContentString)
actualResult, actualError := ParseFile(f.Name())
assert.Equal(t, table.ExpectedResult, actualResult)
assert.Equal(t, table.ExpectedError, actualError)
os.Remove(f.Name()) // Remember to clean up!
}
}