File: assertions_test.go

package info (click to toggle)
golang-github-sebdah-goldie 2.5.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 200 kB
  • sloc: makefile: 2
file content (149 lines) | stat: -rw-r--r-- 3,177 bytes parent folder | download | duplicates (2)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package goldie

import (
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestCompare(t *testing.T) {
	tests := []struct {
		name         string
		actualData   []byte
		expectedData []byte
		update       bool
		err          error
	}{
		{
			name:         "example",
			actualData:   []byte("abc"),
			expectedData: []byte("abc"),
			update:       true,
			err:          nil,
		},
		{
			name:         "example",
			actualData:   []byte("abc"),
			expectedData: []byte("abc"),
			update:       false,
			err:          &errFixtureNotFound{},
		},
		{
			name:         "example",
			actualData:   []byte("bc"),
			expectedData: []byte("abc"),
			update:       true,
			err:          &errFixtureMismatch{},
		},
		{
			name:         "nil",
			actualData:   nil,
			expectedData: nil,
			update:       true,
			err:          nil,
		},
	}

	g := New(t)

	for _, test := range tests {
		if test.update {
			err := g.Update(t, test.name, test.expectedData)
			assert.Nil(t, err)
		}

		err := g.compare(t, test.name, test.actualData)
		assert.IsType(t, test.err, err)

		g.GoldenFileName(t, test.name)
		err = os.RemoveAll(filepath.Dir(g.GoldenFileName(t, test.name)))
		assert.Nil(t, err)
	}
}

func TestCompareTemplate(t *testing.T) {
	data := struct {
		Name string
	}{
		Name: "example",
	}

	tests := []struct {
		name         string
		actualData   []byte
		expectedData []byte
		data         interface{}
		update       bool
		err          error
	}{
		{
			name:         "example",
			actualData:   []byte("abc example"),
			expectedData: []byte("abc {{ .Name }}"),
			data:         data,
			update:       true,
			err:          nil,
		},
		{
			name:         "example",
			actualData:   []byte("abc example"),
			expectedData: []byte("abc {{ .Name }}"),
			data:         nil,
			update:       false,
			err:          &errFixtureNotFound{},
		},
		{
			name:         "example",
			actualData:   []byte("bc example"),
			expectedData: []byte("abc {{ .Name }}"),
			data:         data,
			update:       true,
			err:          &errFixtureMismatch{},
		},
		{
			name:         "example",
			actualData:   []byte("bc example"),
			expectedData: []byte("abc {{ .Name }}"),
			data:         nil,
			update:       true,
			err:          &errMissingKey{},
		}}

	g := New(t)

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			if test.update {
				err := g.Update(t, test.name, test.expectedData)
				assert.Nil(t, err)
			}

			err := g.compareTemplate(t, test.name, test.data, test.actualData)
			assert.IsType(t, test.err, err)

			err = os.RemoveAll(g.fixtureDir)
			assert.Nil(t, err)
		})
	}
}

func TestNormalizeLF(t *testing.T) {
	tests := map[string]struct {
		input     []byte
		expectedD []byte
	}{
		"windows style": {[]byte("Hello\r\nWorld"), []byte("Hello\nWorld")},
		"mac style":     {[]byte("Hello\rWorld"), []byte("Hello\nWorld")},
		"unix style":    {[]byte("Hello\nWorld"), []byte("Hello\nWorld")},
		"empty slice":   {[]byte(""), []byte{}},
		"nil input":     {nil, nil},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			assert.Equal(t, test.expectedD, normalizeLF(test.input))
		})
	}
}