-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
97 lines (72 loc) · 1.39 KB
/
example_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
package fakeio_test
import (
"bufio"
"fmt"
"github.com/rhysd/go-fakeio"
"os"
)
func ExampleStdout() {
f := fakeio.Stdout()
fmt.Print("Hello")
s, err := f.String()
if err != nil {
f.Restore()
panic(err)
}
// 'defer' is better, but here it's unavailable due to output test
f.Restore()
fmt.Println(s)
// Output:
// Hello
}
func ExampleStderr() {
f := fakeio.Stderr()
fmt.Fprint(os.Stderr, "Hello")
s, err := f.String()
if err != nil {
f.Restore()
panic(err)
}
// 'defer' is better, but here it's unavailable due to output test
f.Restore()
fmt.Println(s)
// Output:
// Hello
}
func ExampleStdin() {
f := fakeio.Stdin("Bye!")
s, err := bufio.NewReader(os.Stdin).ReadString('!')
if err != nil {
f.Restore()
panic(err)
}
// 'defer' is better, but here it's unavailable due to output test
f.Restore()
fmt.Println(s)
// Output:
// Bye!
}
func Example() {
f := fakeio.Stdout().Stderr().Stdin("from stdin!")
fromInput, err := bufio.NewReader(os.Stdin).ReadString('!')
if err != nil {
f.Restore()
panic(err)
}
fmt.Println("from stdout!")
fmt.Fprintln(os.Stderr, "from stderr!")
fromOutput, err := f.String()
if err != nil {
f.Restore()
panic(err)
}
// 'defer' is better, but here it's unavailable due to output test
f.Restore()
fmt.Println(fromInput)
fmt.Println(fromOutput)
// Output:
// from stdin!
// from stdout!
// from stderr!
//
}