This repository has been archived by the owner on May 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_testup_test.go
93 lines (85 loc) · 2.75 KB
/
example_testup_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
package testup_test
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/devnev/testup"
)
// testOsStat emulates what a Test function for os.Stat might look like when using testup.Suite.
func testOsStat(t *testing.T) {
// The entire callback to testup.Suite is run three times: once as a prelude, and once for every
// callback to test. Any setup and teardown that should be executed once for the entire suite
// should be placed before the call to testup.Suite.
testup.Suite(t, func(t *testing.T, test testup.Register) {
test("of a directory", func() {
// This setup and teardown code is re-executed in each of the three runs.
dir, err := ioutil.TempDir(".", "test")
if err != nil {
t.Fatalf("unable to create test directory: %s", err)
}
// Teardown can be done using regular defer
defer func() {
err = os.RemoveAll(dir)
if err != nil {
t.Fatalf("unable to remove test directory: %s", err)
}
}()
// In the first "prelude" run, none of the test callbacks below are executed.
// Along with registering all the test cases, this allows testing the setup and teardown
// in isolation.
// In the second run, only this callback is executed
test("dir is a directory", func() {
fi, err := os.Stat(dir)
if err != nil {
t.Fatalf("unexpected error from stat: %s", err)
}
if !fi.IsDir() {
t.Fatal("expected FileInfo.IsDir() to return true")
}
})
// In the third run, only this callback is executed
test("dir contains created file", func() {
// We can safely mutate resource used in other test cases as each case is started
// with a fresh setup
err := ioutil.WriteFile(filepath.Join(dir, "testfile"), []byte("data"), 0644)
if err != nil {
t.Fatalf("unexpected error from writefile: %s", err)
}
items, err := ioutil.ReadDir(dir)
if err != nil {
t.Fatalf("unexpected error from readdir: %s", err)
}
if len(items) != 1 || items[0].Name() != "testfile" {
t.Fatalf("expected results to contain only testfile, got %+v", items)
}
})
})
test("of a file", func() {
// This setup and teardown code is executed twice, once without the sub-test and once
// with the sub-test.
file, err := ioutil.TempFile(".", "test")
if err != nil {
t.Fatalf("unable to create test directory: %s", err)
}
defer func() {
err = os.RemoveAll(file.Name())
if err != nil {
t.Fatalf("unable to remove test directory: %s", err)
}
}()
test("reports not a directory", func() {
fi, err := os.Stat(file.Name())
if err != nil {
t.Fatalf("unexpected error from stat: %v", err)
}
if fi.IsDir() {
t.Fatalf("expected FileInfo.IsDir() to return false")
}
})
})
})
}
func ExampleSuite() {
// See testOsStat
}