-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathpatch_test.go
More file actions
122 lines (109 loc) · 2.53 KB
/
patch_test.go
File metadata and controls
122 lines (109 loc) · 2.53 KB
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
// Licensed under the MIT license, see LICENSE file for details.
package quicktest_test
import (
"errors"
"os"
"path/filepath"
"testing"
qt "github.com/frankban/quicktest"
)
func TestCPatchSetInt(t *testing.T) {
c := qt.New(t)
i := 99
testCleanup(t, func(c *qt.C) {
c.Patch(&i, 88)
c.Assert(i, qt.Equals, 88)
})
c.Assert(i, qt.Equals, 99)
}
func TestCPatchSetError(t *testing.T) {
c := qt.New(t)
oldErr := errors.New("foo")
newErr := errors.New("bar")
err := oldErr
testCleanup(t, func(c *qt.C) {
c.Patch(&err, newErr)
c.Assert(err, qt.Equals, newErr)
})
c.Assert(err, qt.Equals, oldErr)
}
func TestCPatchSetErrorToNil(t *testing.T) {
c := qt.New(t)
oldErr := errors.New("foo")
err := oldErr
testCleanup(t, func(c *qt.C) {
c.Patch(&err, nil)
c.Assert(err, qt.IsNil)
})
c.Assert(err, qt.Equals, oldErr)
}
func TestCPatchSetMapToNil(t *testing.T) {
c := qt.New(t)
oldMap := map[string]int{"foo": 1234}
m := oldMap
testCleanup(t, func(c *qt.C) {
c.Patch(&m, nil)
c.Assert(m, qt.IsNil)
})
c.Assert(m, qt.DeepEquals, oldMap)
}
func TestCPatchPanicsWhenNotAssignable(t *testing.T) {
c := qt.New(t)
i := 99
type otherInt int
c.Assert(func() {
c.Patch(&i, otherInt(88))
}, qt.PanicMatches, `reflect\.Set: value of type quicktest_test\.otherInt is not assignable to type int`)
}
func TestCUnsetenv(t *testing.T) {
c := qt.New(t)
const envName = "SOME_VAR"
os.Setenv(envName, "initial")
testCleanup(t, func(c *qt.C) {
c.Unsetenv(envName)
_, ok := os.LookupEnv(envName)
c.Assert(ok, qt.IsFalse)
})
c.Check(os.Getenv(envName), qt.Equals, "initial")
}
func TestCUnsetenvWithUnsetVariable(t *testing.T) {
c := qt.New(t)
const envName = "SOME_VAR"
os.Unsetenv(envName)
testCleanup(t, func(c *qt.C) {
c.Unsetenv(envName)
_, ok := os.LookupEnv(envName)
c.Assert(ok, qt.IsFalse)
})
_, ok := os.LookupEnv(envName)
c.Assert(ok, qt.IsFalse)
}
func TestCMkdir(t *testing.T) {
c := qt.New(t)
var dir string
testCleanup(t, func(c *qt.C) {
dir = c.Mkdir()
c.Assert(dir, qt.Not(qt.Equals), "")
info, err := os.Stat(dir)
c.Assert(err, qt.IsNil)
c.Assert(info.IsDir(), qt.IsTrue)
f, err := os.Create(filepath.Join(dir, "hello"))
c.Assert(err, qt.IsNil)
f.Close()
})
_, err := os.Stat(dir)
c.Assert(err, qt.Not(qt.IsNil))
}
func testCleanup(t *testing.T, f func(c *qt.C)) {
t.Run("subtest", func(t *testing.T) {
c := qt.New(t)
if _, ok := c.TB.(cleaner); !ok {
// Calling Done is required when testing on Go < 1.14.
defer c.Done()
}
f(c)
})
}
type cleaner interface {
Cleanup(func())
}