/
assert_ext_test.go
110 lines (87 loc) · 2.4 KB
/
assert_ext_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
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
package assert_test
import (
"go/parser"
"go/token"
"io/ioutil"
"runtime"
"strings"
"testing"
"gotest.tools/v3/assert"
"gotest.tools/v3/internal/source"
)
func TestEqual_WithGoldenUpdate(t *testing.T) {
t.Run("assert failed with -update=false", func(t *testing.T) {
ft := &fakeTestingT{}
actual := `not this value`
assert.Equal(ft, actual, expectedOne)
assert.Assert(t, ft.failNowed)
})
t.Run("var is updated when -update=true", func(t *testing.T) {
patchUpdate(t)
t.Cleanup(func() {
resetVariable(t, "expectedOne", "")
})
actual := `this is the
actual value
that we are testing
`
assert.Equal(t, actual, expectedOne)
raw, err := ioutil.ReadFile(fileName(t))
assert.NilError(t, err)
expected := "var expectedOne = `this is the\nactual value\nthat we are testing\n`"
assert.Assert(t, strings.Contains(string(raw), expected), "actual=%v", string(raw))
})
t.Run("const is updated when -update=true", func(t *testing.T) {
patchUpdate(t)
t.Cleanup(func() {
resetVariable(t, "expectedTwo", "")
})
actual := `this is the new
expected value
`
assert.Equal(t, actual, expectedTwo)
raw, err := ioutil.ReadFile(fileName(t))
assert.NilError(t, err)
expected := "const expectedTwo = `this is the new\nexpected value\n`"
assert.Assert(t, strings.Contains(string(raw), expected), "actual=%v", string(raw))
})
}
var expectedOne = ``
const expectedTwo = ``
func patchUpdate(t *testing.T) {
source.Update = true
t.Cleanup(func() {
source.Update = false
})
}
func fileName(t *testing.T) string {
t.Helper()
_, filename, _, ok := runtime.Caller(1)
assert.Assert(t, ok, "failed to get call stack")
return filename
}
func resetVariable(t *testing.T, varName string, value string) {
t.Helper()
_, filename, _, ok := runtime.Caller(1)
assert.Assert(t, ok, "failed to get call stack")
fileset := token.NewFileSet()
astFile, err := parser.ParseFile(fileset, filename, nil, parser.AllErrors|parser.ParseComments)
assert.NilError(t, err)
err = source.UpdateVariable(filename, fileset, astFile, varName, value)
assert.NilError(t, err, "failed to reset file")
}
type fakeTestingT struct {
failNowed bool
failed bool
msgs []string
}
func (f *fakeTestingT) FailNow() {
f.failNowed = true
}
func (f *fakeTestingT) Fail() {
f.failed = true
}
func (f *fakeTestingT) Log(args ...interface{}) {
f.msgs = append(f.msgs, args[0].(string))
}
func (f *fakeTestingT) Helper() {}