aboutsummaryrefslogtreecommitdiff
path: root/opts_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'opts_test.go')
-rw-r--r--opts_test.go114
1 files changed, 111 insertions, 3 deletions
diff --git a/opts_test.go b/opts_test.go
index 1f71057..6ebaa56 100644
--- a/opts_test.go
+++ b/opts_test.go
@@ -9,6 +9,8 @@ func die(t *testing.T, name string, want, got any) {
t.Fatalf("Expected %s to be ‘%s’ but got ‘%s’", name, want, got)
}
+// SHORT OPTS
+
func assertGet(t *testing.T, args []string, fw, ow int, ew error) []Flag {
flags, optind, err := Get(args, "abλc:dßĦ::")
if err != ew {
@@ -35,7 +37,7 @@ func TestNoFlag(t *testing.T) {
func TestCNoArg(t *testing.T) {
args := []string{"foo", "-c"}
- assertGet(t, args, 0, 0, NoArgumentError('c'))
+ assertGet(t, args, 0, 0, NoArgumentError{r: 'c'})
}
func TestCWithArg(t *testing.T) {
@@ -200,12 +202,12 @@ func TestΛAsArgToC(t *testing.T) {
func TestInvalidFlag(t *testing.T) {
args := []string{"foo", "-X"}
- assertGet(t, args, 0, 0, BadOptionError('X'))
+ assertGet(t, args, 0, 0, BadOptionError{r: 'X'})
}
func TestInvalidFlagWithArg(t *testing.T) {
args := []string{"foo", "-X", "bar"}
- assertGet(t, args, 0, 0, BadOptionError('X'))
+ assertGet(t, args, 0, 0, BadOptionError{r: 'X'})
}
func TestAAfterArg(t *testing.T) {
@@ -256,3 +258,109 @@ func TestΛĦWithArg(t *testing.T) {
die(t, "flags[1].Value", "bar", flags[1].Value)
}
}
+
+// LONG OPTS
+
+func assertGetLong(t *testing.T, args []string, fw, ow int, ew error) []Flag {
+ opts := []LongOpt{
+ {Short: 'a', Long: "add", Arg: None},
+ {Short: 'b', Long: "back", Arg: None},
+ {Short: 'λ', Long: "λεωνίδας", Arg: None},
+ {Short: 'c', Long: "change", Arg: Required},
+ {Short: 'C', Long: "count", Arg: None},
+ {Short: 'd', Long: "delete", Arg: None},
+ {Short: 'ß', Long: "scheiße", Arg: None},
+ {Short: 'Ħ', Long: "Ħaġrat", Arg: Optional},
+ }
+ flags, optind, err := GetLong(args, opts)
+ if err != ew {
+ die(t, "err", ew, err)
+ }
+ if optind != ow {
+ die(t, "optind", ow, optind)
+ }
+ if len(flags) != fw {
+ die(t, "flags", fw, flags)
+ }
+ return flags
+}
+
+func TestNoArgL(t *testing.T) {
+ args := []string{}
+ assertGetLong(t, args, 0, 0, nil)
+}
+
+func TestNoFlagL(t *testing.T) {
+ args := []string{"foo"}
+ assertGetLong(t, args, 0, 1, nil)
+}
+
+func TestChangeNoArg(t *testing.T) {
+ args := []string{"foo", "--change"}
+ assertGetLong(t, args, 0, 0, NoArgumentError{s: "change"})
+}
+
+func TestChangeArgEqual(t *testing.T) {
+ args := []string{"foo", "--change=bar"}
+ flags := assertGetLong(t, args, 1, 2, nil)
+ if flags[0].Key != 'c' {
+ die(t, "flags[0].Key", 'c', flags[0].Key)
+ }
+ if flags[0].Value != "bar" {
+ die(t, "flags[0].Value", "bar", flags[0].Value)
+ }
+}
+
+func TestChangeArgSpace(t *testing.T) {
+ args := []string{"foo", "--change", "bar"}
+ flags := assertGetLong(t, args, 1, 3, nil)
+ if flags[0].Key != 'c' {
+ die(t, "flags[0].Key", 'c', flags[0].Key)
+ }
+ if flags[0].Value != "bar" {
+ die(t, "flags[0].Value", "bar", flags[0].Value)
+ }
+}
+
+func TestChangeArgSpaceShort(t *testing.T) {
+ args := []string{"foo", "--ch", "bar"}
+ flags := assertGetLong(t, args, 1, 3, nil)
+ if flags[0].Key != 'c' {
+ die(t, "flags[0].Key", 'c', flags[0].Key)
+ }
+ if flags[0].Value != "bar" {
+ die(t, "flags[0].Value", "bar", flags[0].Value)
+ }
+}
+
+func TestChangeArgSpaceShortFail(t *testing.T) {
+ args := []string{"foo", "--c", "bar"}
+ assertGetLong(t, args, 0, 0, BadOptionError{s: "c"})
+}
+
+func TestĦaġratNoArg(t *testing.T) {
+ args := []string{"foo", "--Ħ"}
+ assertGetLong(t, args, 1, 2, nil)
+}
+
+func TestĦaġratArgEqual(t *testing.T) {
+ args := []string{"foo", "--Ħ=bar"}
+ flags := assertGetLong(t, args, 1, 2, nil)
+ if flags[0].Key != 'Ħ' {
+ die(t, "flags[0].Key", 'Ħ', flags[0].Key)
+ }
+ if flags[0].Value != "bar" {
+ die(t, "flags[0].Value", "bar", flags[0].Value)
+ }
+}
+
+func TestĦaġratArgSpace(t *testing.T) {
+ args := []string{"foo", "--Ħ", "bar"}
+ flags := assertGetLong(t, args, 1, 2, nil)
+ if flags[0].Key != 'Ħ' {
+ die(t, "flags[0].Key", 'Ħ', flags[0].Key)
+ }
+ if flags[0].Value != "" {
+ die(t, "flags[0].Value", "", flags[0].Value)
+ }
+}