-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
isoption_test.go
113 lines (100 loc) · 5.73 KB
/
isoption_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
// This file is part of go-getoptions.
//
// Copyright (C) 2015-2024 David Gamba Rios
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package getoptions
import (
"reflect"
"testing"
)
func TestIsOption(t *testing.T) {
cases := []struct {
name string
in string
mode Mode
optPair []optionPair
is bool
}{
{"lone dash", "-", Normal, []optionPair{{Option: "-"}}, true},
{"lone dash", "-", Bundling, []optionPair{{Option: "-"}}, true},
{"lone dash", "-", SingleDash, []optionPair{{Option: "-"}}, true},
{"double dash", "--", Normal, []optionPair{{Option: "--"}}, false},
{"double dash", "--", Bundling, []optionPair{{Option: "--"}}, false},
{"double dash", "--", SingleDash, []optionPair{{Option: "--"}}, false},
{"no option", "opt", Normal, []optionPair{}, false},
{"no option", "opt", Bundling, []optionPair{}, false},
{"no option", "opt", SingleDash, []optionPair{}, false},
{"Long option", "--opt", Normal, []optionPair{{Option: "opt"}}, true},
{"Long option", "--opt", Bundling, []optionPair{{Option: "opt"}}, true},
{"Long option", "--opt", SingleDash, []optionPair{{Option: "opt"}}, true},
{"Long option with arg", "--opt=arg", Normal, []optionPair{{Option: "opt", Args: []string{"arg"}}}, true},
{"Long option with arg", "--opt=arg", Bundling, []optionPair{{Option: "opt", Args: []string{"arg"}}}, true},
{"Long option with arg", "--opt=arg", SingleDash, []optionPair{{Option: "opt", Args: []string{"arg"}}}, true},
{"short option", "-opt", Normal, []optionPair{{Option: "opt"}}, true},
{"short option", "-opt", Bundling, []optionPair{{Option: "o"}, {Option: "p"}, {Option: "t"}}, true},
{"short option", "-opt", SingleDash, []optionPair{{Option: "o", Args: []string{"pt"}}}, true},
{"short option single letter", "-o", Normal, []optionPair{{Option: "o"}}, true},
{"short option single letter", "-o", Bundling, []optionPair{{Option: "o"}}, true},
{"short option single letter", "-o", SingleDash, []optionPair{{Option: "o"}}, true},
{"short option with arg", "-opt=arg", Normal, []optionPair{{Option: "opt", Args: []string{"arg"}}}, true},
{"short option with arg", "-opt=arg", Bundling, []optionPair{{Option: "o"}, {Option: "p"}, {Option: "t", Args: []string{"arg"}}}, true},
{"short option with arg", "-opt=arg", SingleDash, []optionPair{{Option: "o", Args: []string{"pt=arg"}}}, true},
}
windowsCases := []struct {
name string
in string
mode Mode
optPair []optionPair
is bool
}{
{"Long option", "/opt", Normal, []optionPair{{Option: "opt"}}, true},
{"Long option", "/opt", Bundling, []optionPair{{Option: "opt"}}, true},
{"Long option", "/opt", SingleDash, []optionPair{{Option: "opt"}}, true},
{"Long option with arg", "/opt:arg", Normal, []optionPair{{Option: "opt", Args: []string{"arg"}}}, true},
{"Long option with arg", "/opt:arg", Bundling, []optionPair{{Option: "opt", Args: []string{"arg"}}}, true},
{"Long option with arg", "/opt:arg", SingleDash, []optionPair{{Option: "opt", Args: []string{"arg"}}}, true},
{"Long option with arg", "/opt=arg", Normal, []optionPair{{Option: "opt", Args: []string{"arg"}}}, true},
{"Long option with arg", "/opt=arg", Bundling, []optionPair{{Option: "opt", Args: []string{"arg"}}}, true},
{"Long option with arg", "/opt=arg", SingleDash, []optionPair{{Option: "opt", Args: []string{"arg"}}}, true},
{"short option with arg", "-opt:arg", Normal, []optionPair{{Option: "opt", Args: []string{"arg"}}}, true},
{"short option with arg", "-opt:arg", Bundling, []optionPair{{Option: "o"}, {Option: "p"}, {Option: "t", Args: []string{"arg"}}}, true},
{"short option with arg", "-opt:arg", SingleDash, []optionPair{{Option: "o", Args: []string{"pt:arg"}}}, true},
{"Edge case", "/opt:=arg", Normal, []optionPair{{Option: "opt", Args: []string{"=arg"}}}, true},
{"Edge case", "/opt:=arg", Bundling, []optionPair{{Option: "opt", Args: []string{"=arg"}}}, true},
{"Edge case", "/opt:=arg", SingleDash, []optionPair{{Option: "opt", Args: []string{"=arg"}}}, true},
{"Edge case", "/opt=:arg", Normal, []optionPair{{Option: "opt", Args: []string{":arg"}}}, true},
{"Edge case", "/opt=:arg", Bundling, []optionPair{{Option: "opt", Args: []string{":arg"}}}, true},
{"Edge case", "/opt=:arg", SingleDash, []optionPair{{Option: "opt", Args: []string{":arg"}}}, true},
{"Edge case", "/opt==arg", Normal, []optionPair{{Option: "opt", Args: []string{"=arg"}}}, true},
{"Edge case", "/opt==arg", Bundling, []optionPair{{Option: "opt", Args: []string{"=arg"}}}, true},
{"Edge case", "/opt==arg", SingleDash, []optionPair{{Option: "opt", Args: []string{"=arg"}}}, true},
{"Edge case", "/opt::arg", Normal, []optionPair{{Option: "opt", Args: []string{":arg"}}}, true},
{"Edge case", "/opt::arg", Bundling, []optionPair{{Option: "opt", Args: []string{":arg"}}}, true},
{"Edge case", "/opt::arg", SingleDash, []optionPair{{Option: "opt", Args: []string{":arg"}}}, true},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
buf := setupLogging()
optPair, is := isOption(tt.in, tt.mode, false)
if !reflect.DeepEqual(optPair, tt.optPair) || is != tt.is {
t.Errorf("isOption(%q, %q) == (%q, %v), want (%q, %v)",
tt.in, tt.mode, optPair, is, tt.optPair, tt.is)
}
t.Log(buf.String())
})
}
for _, tt := range append(cases, windowsCases...) {
t.Run("windows "+tt.name, func(t *testing.T) {
buf := setupLogging()
optPair, is := isOption(tt.in, tt.mode, true)
if !reflect.DeepEqual(optPair, tt.optPair) || is != tt.is {
t.Errorf("isOption(%q, %q) == (%q, %v), want (%q, %v)",
tt.in, tt.mode, optPair, is, tt.optPair, tt.is)
}
t.Log(buf.String())
})
}
}