-
Notifications
You must be signed in to change notification settings - Fork 3
/
todo_render_test.go
57 lines (50 loc) · 1.13 KB
/
todo_render_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
package model_test
// exercise
import (
"bytes"
"flag"
"os"
"path/filepath"
"strings"
"testing"
apiv1 "github.com/gotestbootcamp/go-todo-app/api/v1"
"github.com/gotestbootcamp/go-todo-app/model"
)
var update = flag.Bool("update", false, "update .golden.json files")
func TestRender(t *testing.T) {
tests := []struct {
name string
toRender model.Todo
}{
{
name: "non assigned",
toRender: model.Todo{
Title: "todo2",
Assignee: "",
Description: "second todo",
Status: apiv1.Pending,
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
rendered, err := tc.toRender.HTMLRow()
if err != nil {
t.Fatal("HTMLRow should not fail", err)
}
goldenFile := filepath.Join("testdata", strings.ReplaceAll(tc.name, " ", "-")+".golden")
if *update {
if err := os.WriteFile(goldenFile, rendered, os.ModePerm); err != nil {
panic("write failed")
}
}
expected, err := os.ReadFile(goldenFile)
if err != nil {
t.Errorf("failed to open golden file %s: %v", goldenFile, err)
}
if !bytes.Equal(expected, rendered) {
t.Fail()
}
})
}
}