-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
44 lines (40 loc) · 869 Bytes
/
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
package main
import (
"bytes"
"fmt"
"io"
"os"
"regexp"
"strings"
"testing"
"time"
)
func Test_parse(t *testing.T) {
out := new(bytes.Buffer)
defer func(w io.Writer) { stdout = w }(stdout)
stdout = out
opts := parse("-v")
want := fmt.Sprintf("%s v%s", name, version)
got := strings.TrimSpace(out.String())
if !regexp.MustCompile(want).MatchString(got) {
t.Errorf("expected %q, got %q", want, got)
}
if !opts.v {
t.Errorf("expected opts.v to be set")
}
opts = parse("-h")
want = fmt.Sprintf("-h")
got = strings.TrimSpace(out.String())
if !regexp.MustCompile(want).MatchString(got) {
t.Errorf("expected %q, got %q", want, got)
}
if !opts.h {
t.Errorf("expected opts.h to be set")
}
}
func Test_main(t *testing.T) {
defer func(a []string) { os.Args = a }(os.Args)
os.Args = []string{""}
go main()
time.Sleep(5 * time.Millisecond)
}