-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
capture_test.go
73 lines (65 loc) · 2.6 KB
/
capture_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
package testza_test
import (
"fmt"
"io"
"os"
"strconv"
"testing"
. "github.com/MarvinJWendt/testza"
)
func TestCaptureStdout(t *testing.T) {
type args struct {
capture func(w io.Writer) error
}
tests := []struct {
args args
want string
}{
{args: args{capture: func(w io.Writer) error { fmt.Print("Hello, World!"); return nil }}, want: "Hello, World!"},
{args: args{capture: func(w io.Writer) error { fmt.Print(" Hello, World! "); return nil }}, want: " Hello, World! "},
{args: args{capture: func(w io.Writer) error { fmt.Fprint(w, "Hello, World!"); return nil }}, want: "Hello, World!"},
{args: args{capture: func(w io.Writer) error { fmt.Print("H\ne\nl\nl\nl\no\n\n\n\nWorld!"); return nil }}, want: "H\ne\nl\nl\nl\no\n\n\n\nWorld!"},
{args: args{capture: func(w io.Writer) error { fmt.Println("Hello, World!"); return nil }}, want: "Hello, World!\n"},
{args: args{capture: func(w io.Writer) error { fmt.Println("Hello, \nWorld!"); return nil }}, want: "Hello, \nWorld!\n"},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
got, err := CaptureStdout(tt.args.capture)
AssertNil(t, err)
AssertEqual(t, got, tt.want)
})
}
}
func TestCaptureStderr(t *testing.T) {
type args struct {
capture func(w io.Writer) error
}
tests := []struct {
args args
want string
}{
{args: args{capture: func(w io.Writer) error { fmt.Fprint(os.Stderr, "Hello, World!"); return nil }}, want: "Hello, World!"},
{args: args{capture: func(w io.Writer) error { fmt.Fprint(os.Stderr, " Hello, World! "); return nil }}, want: " Hello, World! "},
{args: args{capture: func(w io.Writer) error { fmt.Fprint(w, "Hello, World!"); return nil }}, want: "Hello, World!"},
{args: args{capture: func(w io.Writer) error { fmt.Fprint(os.Stderr, "H\ne\nl\nl\nl\no\n\n\n\nWorld!"); return nil }}, want: "H\ne\nl\nl\nl\no\n\n\n\nWorld!"},
{args: args{capture: func(w io.Writer) error { fmt.Fprintln(os.Stderr, "Hello, World!"); return nil }}, want: "Hello, World!\n"},
{args: args{capture: func(w io.Writer) error { fmt.Fprintln(os.Stderr, "Hello, \nWorld!"); return nil }}, want: "Hello, \nWorld!\n"},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
got, err := CaptureStderr(tt.args.capture)
AssertNil(t, err)
AssertEqual(t, got, tt.want)
})
}
}
func TestCaptureStdoutAndStderr(t *testing.T) {
stdout, stderr, err := CaptureStdoutAndStderr(func(stdoutWriter, stderrWriter io.Writer) error {
fmt.Fprint(os.Stdout, "Hello")
fmt.Fprint(os.Stderr, "World")
return nil
})
AssertEqual(t, stdout, "Hello")
AssertEqual(t, stderr, "World")
AssertNoError(t, err)
}