forked from free-gate/wordcount
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
48 lines (43 loc) · 1.09 KB
/
main_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
package main
import (
"io/ioutil"
"os"
"strings"
"testing"
)
func TestWordcount(t *testing.T) {
tests := []struct {
name, in, want string
}{
{"empty", "", "0"},
{"single", "ok", "1"},
{"several", "go is awesome", "3"},
{"even more", "php - not so much", "5"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
old, reader, writer := captureStdout()
os.Args = []string{"main.go", test.in}
main()
got := restoreStdout(old, reader, writer)
if got != test.want {
t.Errorf("got '%s', want '%s'", got, test.want)
}
})
}
}
// captureStdout redirects os.Stdout into the custom writer
func captureStdout() (old *os.File, reader *os.File, writer *os.File) {
old = os.Stdout
reader, writer, _ = os.Pipe()
os.Stdout = writer
return old, reader, writer
}
// restoreStdout returns everything printed since the last captureStdout()
// and restores the old os.Stdout
func restoreStdout(old *os.File, reader *os.File, writer *os.File) string {
writer.Close()
out, _ := ioutil.ReadAll(reader)
os.Stdout = old
return strings.TrimSuffix(string(out), "\n")
}