-
Notifications
You must be signed in to change notification settings - Fork 0
/
argumentative_test.go
155 lines (124 loc) · 3.69 KB
/
argumentative_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
148
149
150
151
152
153
154
155
package argumentative
import (
"bytes"
"io"
"log"
"os"
"sync"
"testing"
)
func captureOutput(f func()) string {
reader, writer, err := os.Pipe()
if err != nil {
panic(err)
}
stdout := os.Stdout
stderr := os.Stderr
defer func() {
os.Stdout = stdout
os.Stderr = stderr
log.SetOutput(os.Stderr)
}()
os.Stdout = writer
os.Stderr = writer
log.SetOutput(writer)
out := make(chan string)
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
var buf bytes.Buffer
wg.Done()
io.Copy(&buf, reader)
out <- buf.String()
}()
wg.Wait()
f()
writer.Close()
return <-out
}
func TestFlagsIntegration(t *testing.T) {
flags := &Flags{}
stringflag := flags.Flags().AddString("stringname", "s", true, "", "stringdescription")
boolflag := flags.Flags().AddBool("boolname", "b", "booldescription")
positional := flags.Flags().AddPositional("positionalname", false, "positionaldefault", "positionaldescription")
optx := flags.Flags().AddBool("optx", "x", "Option X")
opty := flags.Flags().AddBool("opty", "y", "Option Y")
optz := flags.Flags().AddBool("optz", "z", "Option Z")
var args []string
args = append(args, "scriptname")
err := flags.Parse(args)
await := "required flag --stringname missing"
if err == nil {
t.Errorf("No error found, got [%p], want pointer", err)
} else if err.Error() != await {
t.Errorf("Wrong error message, got [%s], want [%s]", err, await)
}
if *stringflag != "" {
t.Errorf("Wrong stringflag value, got [%s], want [%s]", *stringflag, "")
}
if *boolflag != false {
t.Errorf("Wrong boolflag value, got [%t], want [%t]", *boolflag, false)
}
if *positional != "positionaldefault" {
t.Errorf("Wrong positional value, got [%s], want [%s]", *positional, "positionaldefault")
}
args = append(args, "-s", "stringvalue", "-b", "positionalvalue")
err = flags.Parse(args)
await = "required flag --stringname missing"
if err != nil {
t.Errorf("Error found, got [%s], want nil", err.Error())
}
if *stringflag != "stringvalue" {
t.Errorf("Wrong stringflag value, got [%s], want [%s]", *stringflag, "stringvalue")
}
if *boolflag != true {
t.Errorf("Wrong boolflag value, got [%t], want [%t]", *boolflag, false)
}
if *positional != "positionalvalue" {
t.Errorf("Wrong positional value, got [%s], want [%s]", *positional, "positionalvalue")
}
args = append(args, "-xy")
err = flags.Parse(args)
if err != nil {
t.Errorf("Error found, got [%s], want nil", err.Error())
}
if !*optx {
t.Errorf("Wrong combined boolflag value, got [%t], want [%t]", *optx, true)
}
if !*opty {
t.Errorf("Wrong combined boolflag value, got [%t], want [%t]", *opty, true)
}
args = append(args, "-sz")
err = flags.Parse(args)
await = "options with parameters can not be combined -sz"
if err == nil {
t.Errorf("No error found, got [%p], want pointer", err)
} else if err.Error() != await {
t.Errorf("Wrong error message, got [%s], want [%s]", err, await)
}
if *optz {
t.Errorf("Wrong combined boolflag value, got [%t], want [%t]", *optz, false)
}
}
func TestUsage(t *testing.T) {
flags := &Flags{}
flags.Flags().AddString("stringname", "s", true, "", "stringdescription")
flags.Flags().AddBool("boolname", "b", "booldescription")
flags.Flags().AddPositional("positionalname", false, "positionaldefault", "positionaldescription")
await := `title
description
Usage: title [-b] -s [positionalname]
Flags:
-b, --boolname booldescription
Options:
-s, --stringname stringdescription
Positional arguments:
positionalname positionaldescription (Default: positionaldefault)
`
result := captureOutput(func() {
flags.Usage("title", "description", nil)
})
if result != await {
t.Errorf("Wrong Usage output, got\n%s\n\nwant\n\n%s", result, await)
}
}