| 1 |
// Licensed under the MIT license, see LICENSE file for details. |
| 2 |
|
| 3 |
package quicktest_test |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"testing" |
| 10 |
|
| 11 |
qt "github.com/frankban/quicktest" |
| 12 |
) |
| 13 |
|
| 14 |
func TestCPatchSetInt(t *testing.T) { |
| 15 |
c := qt.New(t) |
| 16 |
i := 99 |
| 17 |
testCleanup(t, func(c *qt.C) { |
| 18 |
c.Patch(&i, 88) |
| 19 |
c.Assert(i, qt.Equals, 88) |
| 20 |
}) |
| 21 |
c.Assert(i, qt.Equals, 99) |
| 22 |
} |
| 23 |
|
| 24 |
func TestCPatchSetError(t *testing.T) { |
| 25 |
c := qt.New(t) |
| 26 |
oldErr := errors.New("foo") |
| 27 |
newErr := errors.New("bar") |
| 28 |
err := oldErr |
| 29 |
testCleanup(t, func(c *qt.C) { |
| 30 |
c.Patch(&err, newErr) |
| 31 |
c.Assert(err, qt.Equals, newErr) |
| 32 |
}) |
| 33 |
c.Assert(err, qt.Equals, oldErr) |
| 34 |
} |
| 35 |
|
| 36 |
func TestCPatchSetErrorToNil(t *testing.T) { |
| 37 |
c := qt.New(t) |
| 38 |
oldErr := errors.New("foo") |
| 39 |
err := oldErr |
| 40 |
testCleanup(t, func(c *qt.C) { |
| 41 |
c.Patch(&err, nil) |
| 42 |
c.Assert(err, qt.IsNil) |
| 43 |
}) |
| 44 |
c.Assert(err, qt.Equals, oldErr) |
| 45 |
} |
| 46 |
|
| 47 |
func TestCPatchSetMapToNil(t *testing.T) { |
| 48 |
c := qt.New(t) |
| 49 |
oldMap := map[string]int{"foo": 1234} |
| 50 |
m := oldMap |
| 51 |
testCleanup(t, func(c *qt.C) { |
| 52 |
c.Patch(&m, nil) |
| 53 |
c.Assert(m, qt.IsNil) |
| 54 |
}) |
| 55 |
c.Assert(m, qt.DeepEquals, oldMap) |
| 56 |
} |
| 57 |
|
| 58 |
func TestCPatchPanicsWhenNotAssignable(t *testing.T) { |
| 59 |
c := qt.New(t) |
| 60 |
i := 99 |
| 61 |
type otherInt int |
| 62 |
c.Assert(func() { |
| 63 |
c.Patch(&i, otherInt(88)) |
| 64 |
}, qt.PanicMatches, `reflect\.Set: value of type quicktest_test\.otherInt is not assignable to type int`) |
| 65 |
} |
| 66 |
|
| 67 |
func TestCUnsetenv(t *testing.T) { |
| 68 |
c := qt.New(t) |
| 69 |
const envName = "SOME_VAR" |
| 70 |
os.Setenv(envName, "initial") |
| 71 |
testCleanup(t, func(c *qt.C) { |
| 72 |
c.Unsetenv(envName) |
| 73 |
_, ok := os.LookupEnv(envName) |
| 74 |
c.Assert(ok, qt.IsFalse) |
| 75 |
}) |
| 76 |
c.Check(os.Getenv(envName), qt.Equals, "initial") |
| 77 |
} |
| 78 |
|
| 79 |
func TestCUnsetenvWithUnsetVariable(t *testing.T) { |
| 80 |
c := qt.New(t) |
| 81 |
const envName = "SOME_VAR" |
| 82 |
os.Unsetenv(envName) |
| 83 |
testCleanup(t, func(c *qt.C) { |
| 84 |
c.Unsetenv(envName) |
| 85 |
_, ok := os.LookupEnv(envName) |
| 86 |
c.Assert(ok, qt.IsFalse) |
| 87 |
}) |
| 88 |
_, ok := os.LookupEnv(envName) |
| 89 |
c.Assert(ok, qt.IsFalse) |
| 90 |
} |
| 91 |
|
| 92 |
func TestCMkdir(t *testing.T) { |
| 93 |
c := qt.New(t) |
| 94 |
var dir string |
| 95 |
testCleanup(t, func(c *qt.C) { |
| 96 |
dir = c.Mkdir() |
| 97 |
c.Assert(dir, qt.Not(qt.Equals), "") |
| 98 |
info, err := os.Stat(dir) |
| 99 |
c.Assert(err, qt.IsNil) |
| 100 |
c.Assert(info.IsDir(), qt.IsTrue) |
| 101 |
f, err := os.Create(filepath.Join(dir, "hello")) |
| 102 |
c.Assert(err, qt.IsNil) |
| 103 |
f.Close() |
| 104 |
}) |
| 105 |
_, err := os.Stat(dir) |
| 106 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 107 |
} |
| 108 |
|
| 109 |
func testCleanup(t *testing.T, f func(c *qt.C)) { |
| 110 |
t.Run("subtest", func(t *testing.T) { |
| 111 |
c := qt.New(t) |
| 112 |
if _, ok := c.TB.(cleaner); !ok { |
| 113 |
// Calling Done is required when testing on Go < 1.14. |
| 114 |
defer c.Done() |
| 115 |
} |
| 116 |
f(c) |
| 117 |
}) |
| 118 |
} |
| 119 |
|
| 120 |
type cleaner interface { |
| 121 |
Cleanup(func()) |
| 122 |
} |