generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathio_test.go
147 lines (129 loc) · 2.52 KB
/
io_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
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
package u_test
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/require"
"moul.io/u"
)
type closer struct {
called bool
}
func (c *closer) Close() error {
c.called = true
return nil
}
func TestSilentClose(t *testing.T) {
c := &closer{}
require.False(t, c.called)
u.SilentClose(c)
require.True(t, c.called)
require.NotPanics(t, func() { u.SilentClose(nil) })
}
func ExampleSilentClose() {
f, _ := os.Open("file.txt")
defer u.SilentClose(f)
}
func ExampleCaptureStdout() {
fmt.Println("AAA")
closer, err := u.CaptureStdout()
if err != nil {
panic(err)
}
fmt.Println("BBB")
ret := closer()
fmt.Println("CCC")
fmt.Println(ret)
// Output:
// AAA
// CCC
// BBB
}
func ExampleMustCaptureStdout() {
fmt.Println("AAA")
closer := u.MustCaptureStdout()
fmt.Println("BBB")
ret := closer()
fmt.Println("CCC")
fmt.Println(ret)
// Output:
// AAA
// CCC
// BBB
}
func ExampleCaptureStderr() {
os.Stderr = os.Stdout // hack to run this test as an example test
fmt.Fprintln(os.Stderr, "AAA")
closer, err := u.CaptureStderr()
if err != nil {
panic(err)
}
fmt.Fprintln(os.Stderr, "BBB")
ret := closer()
fmt.Fprintln(os.Stderr, "CCC")
fmt.Fprintln(os.Stderr, ret)
// Output:
// AAA
// CCC
// BBB
}
func ExampleMustCaptureStderr() {
os.Stderr = os.Stdout // hack to run this test as an example test
fmt.Fprintln(os.Stderr, "AAA")
closer := u.MustCaptureStderr()
fmt.Fprintln(os.Stderr, "BBB")
ret := closer()
fmt.Fprintln(os.Stderr, "CCC")
fmt.Fprintln(os.Stderr, ret)
// Output:
// AAA
// CCC
// BBB
}
func ExampleCaptureStdoutAndStderr() {
os.Stderr = os.Stdout // hack to run this test as an example test
fmt.Fprintln(os.Stderr, "AAA")
fmt.Println("BBB")
closer, err := u.CaptureStdoutAndStderr()
if err != nil {
panic(err)
}
fmt.Fprintln(os.Stderr, "CCC")
fmt.Println("DDD")
ret := closer()
fmt.Fprintln(os.Stderr, "EEE")
fmt.Println("FFF")
fmt.Println(ret)
// Output:
// AAA
// BBB
// EEE
// FFF
// CCC
// DDD
}
func ExampleMustCaptureStdoutAndStderr() {
os.Stderr = os.Stdout // hack to run this test as an example test
fmt.Fprintln(os.Stderr, "AAA")
fmt.Println("BBB")
closer := u.MustCaptureStdoutAndStderr()
fmt.Fprintln(os.Stderr, "CCC")
fmt.Println("DDD")
ret := closer()
fmt.Fprintln(os.Stderr, "EEE")
fmt.Println("FFF")
fmt.Println(ret)
// Output:
// AAA
// BBB
// EEE
// FFF
// CCC
// DDD
}
func TestCaptureStderr(t *testing.T) {
closer := u.MustCaptureStderr()
fmt.Fprintln(os.Stderr, "hello world")
ret := closer()
require.Equal(t, ret, "hello world\n")
}