-
Notifications
You must be signed in to change notification settings - Fork 0
/
getopt_test.go
89 lines (79 loc) · 3.03 KB
/
getopt_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
package getopt
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSimpleCase(t *testing.T) {
assert := assert.New(t)
opts, i, err := Getopts([]string{
"test_bin", "-afo", "output-file", "normal arg"}, "afo:")
assert.Nil(err, "Expected err to be nil")
assert.Equal(len(opts), 3, "Expected 3 options to be parsed")
assert.Equal(i, 3, "Expected non-option args to start at index 2")
assert.Equal(opts[0], Option{'a', ""})
assert.Equal(opts[1], Option{'f', ""})
assert.Equal(opts[2], Option{'o', "output-file"})
}
func TestShortFormArgument(t *testing.T) {
assert := assert.New(t)
opts, i, err := Getopts([]string{
"test_bin", "-afooutput-file", "normal arg"}, "afo:")
assert.Nil(err, "Expected err to be nil")
assert.Equal(len(opts), 3, "Expected 3 options to be parsed")
assert.Equal(i, 2, "Expected non-option args to start at index 2")
assert.Equal(opts[0], Option{'a', ""})
assert.Equal(opts[1], Option{'f', ""})
assert.Equal(opts[2], Option{'o', "output-file"})
}
func TestSeparateArgs(t *testing.T) {
assert := assert.New(t)
opts, i, err := Getopts([]string{
"test_bin", "-a", "-f", "-o", "output-file", "normal arg"}, "afo:")
assert.Nil(err, "Expected err to be nil")
assert.Equal(len(opts), 3, "Expected 3 options to be parsed")
assert.Equal(i, 5, "Expected non-option args to start at index 5")
assert.Equal(opts[0], Option{'a', ""})
assert.Equal(opts[1], Option{'f', ""})
assert.Equal(opts[2], Option{'o', "output-file"})
}
func TestTwoDashes(t *testing.T) {
assert := assert.New(t)
opts, i, err := Getopts([]string{
"test_bin", "-afo", "output-file", "--", "-f", "normal arg"}, "afo:")
assert.Nil(err, "Expected err to be nil")
assert.Equal(len(opts), 3, "Expected 3 options to be parsed")
assert.Equal(i, 4, "Expected non-option args to start at index 4")
assert.Equal(opts[0], Option{'a', ""})
assert.Equal(opts[1], Option{'f', ""})
assert.Equal(opts[2], Option{'o', "output-file"})
}
func TestUnknownOption(t *testing.T) {
assert := assert.New(t)
_, _, err := Getopts([]string{"test_bin", "-x"}, "y")
var errt UnknownOptionError
assert.IsType(err, errt, "Expected unknown option error")
assert.Equal(err.Error(), fmt.Sprintf("%s: unknown option -x", os.Args[0]),
"Expected POSIX-compatible error message")
}
func TestMissingOption(t *testing.T) {
assert := assert.New(t)
_, _, err := Getopts([]string{"test_bin", "-x"}, "x:")
var errt MissingOptionError
assert.IsType(err, errt, "Expected missing option error")
assert.Equal(err.Error(), fmt.Sprintf("%s: expected argument for -x",
os.Args[0]), "Expected POSIX-compatible error message")
}
func TestExpectedMissingOption(t *testing.T) {
assert := assert.New(t)
opts, _, err := Getopts([]string{"test_bin", "-x"}, ":x:")
assert.Nil(err, "Expected err to be nil")
assert.Equal(len(opts), 1, "Expected 1 option to be parsed")
assert.Equal(opts[0], Option{':', "x"})
}
func TestNoOption(t *testing.T) {
assert := assert.New(t)
_, i, _ := Getopts([]string{"test_bin"}, "")
assert.Equal(i, 1, "Expected non-option args to start at index 1")
}