package opt_test import ( "testing" "go.fifitido.net/cmd/opt" ) func TestParseUnknownLongOption(t *testing.T) { set := opt.Set{} args := []string{"--unknown"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") } else if err.Error() != "unknown option: --unknown" { t.Errorf("Expected error: unknown option, got: %s", err.Error()) } } func TestParseUnknownShortOption(t *testing.T) { set := opt.Set{} args := []string{"-u"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") } else if err.Error() != "unknown option: -u" { t.Errorf("Expected error: unknown option, got: %s", err.Error()) } } func TestParseUnknownShortChainedOption1(t *testing.T) { set := opt.Set{ opt.Bool("banana", "b", false, ""), opt.Bool("cucumber", "c", false, ""), } args := []string{"-abc"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") } else if err.Error() != "unknown option: -a" { t.Errorf("Expected error: unknown option, got: %s", err.Error()) } } func TestParseUnknownShortChainedOption2(t *testing.T) { set := opt.Set{ opt.Bool("apple", "a", false, ""), opt.Bool("cucumber", "c", false, ""), } args := []string{"-abc"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") } else if err.Error() != "unknown option: -b" { t.Errorf("Expected error: unknown option, got: %s", err.Error()) } } func TestParseUnknownShortChainedOption3(t *testing.T) { set := opt.Set{ opt.Bool("apple", "a", false, ""), opt.Bool("banana", "b", false, ""), } args := []string{"-abc"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") } else if err.Error() != "unknown option: -c" { t.Errorf("Expected error: unknown option, got: %s", err.Error()) } } func TestParseUnchaninableOption(t *testing.T) { set := opt.Set{ opt.Bool("apple", "a", false, ""), opt.String("banana", "b", "", ""), } args := []string{"-ab"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") } else if err.Error() != "cannot chain option: -b" { t.Errorf("Expected error: cannot chain option, got: %s", err.Error()) } } func TestParseShortOptionWithValueAndNoSpace(t *testing.T) { o := opt.String("fruit", "f", "", "") set := opt.Set{o} args := []string{"-fapple"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) } if o.Value() != "apple" { t.Errorf("Expected fruit to be 'apple', got: %s", o.Value()) } } func TestParseShortOptionWithValueAndSpace(t *testing.T) { o := opt.String("fruit", "f", "", "") set := opt.Set{o} args := []string{"-f", "apple"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) } if o.Value() != "apple" { t.Errorf("Expected fruit to be 'apple', got: %s", o.Value()) } } func TestParseShortOptionWithValueAndEqual(t *testing.T) { o := opt.String("fruit", "f", "", "") set := opt.Set{o} args := []string{"-f=apple"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) } if o.Value() != "apple" { t.Errorf("Expected fruit to be 'apple', got: %s", o.Value()) } } func TestParseLongOptionWithValueAndEqual(t *testing.T) { o := opt.String("fruit", "f", "", "") set := opt.Set{o} args := []string{"--fruit=apple"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) } if o.Value() != "apple" { t.Errorf("Expected fruit to be 'apple', got: %s", o.Value()) } } func TestParseLongOptionWithValueAndSpace(t *testing.T) { o := opt.String("fruit", "f", "", "") set := opt.Set{o} args := []string{"--fruit", "apple"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) } if o.Value() != "apple" { t.Errorf("Expected fruit to be 'apple', got: %s", o.Value()) } } func TestParseOptionTerminator(t *testing.T) { o := opt.String("fruit", "f", "banana", "") set := opt.Set{o} args := []string{"--fruit", "apple", "--", "hello", "world"} parser := opt.NewParser(args, set, false) restArgs, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) } if o.Value() != "apple" { t.Errorf("Expected fruit to be 'apple', got: %s", o.Value()) } if len(restArgs) != 2 { t.Errorf("Expected restArgs to be 2, got: %d", len(restArgs)) } if restArgs[0] != "hello" { t.Errorf("Expected restArgs[0] to be 'hello', got: %s", restArgs[0]) } if restArgs[1] != "world" { t.Errorf("Expected restArgs[1] to be 'world', got: %s", restArgs[1]) } } func TestParseLongOptionIgnoreUnknown(t *testing.T) { set := opt.Set{} args := []string{"--unknown", "hello", "world"} parser := opt.NewParser(args, set, true) restArgs, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) } if len(restArgs) != 2 { t.Errorf("Expected restArgs to be 2, got: %d", len(restArgs)) } if restArgs[0] != "hello" { t.Errorf("Expected restArgs[0] to be 'hello', got: %s", restArgs[0]) } if restArgs[1] != "world" { t.Errorf("Expected restArgs[1] to be 'world', got: %s", restArgs[1]) } } func TestParseShortOptionIgnoreUnknown(t *testing.T) { set := opt.Set{} args := []string{"-q"} parser := opt.NewParser(args, set, true) _, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) } } func TestParseChainedShortOptionIgnoreUnknown(t *testing.T) { apple := opt.Bool("apple", "a", false, "") banana := opt.Bool("banana", "b", false, "") cucumber := opt.Bool("cucumber", "c", false, "") set := opt.Set{apple, banana, cucumber} args := []string{"-adc"} parser := opt.NewParser(args, set, true) _, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) } if apple.Value() != true { t.Errorf("Expected apple to be true, got: %t", apple.Value()) } if banana.Value() != false { t.Errorf("Expected banana to be false, got: %t", banana.Value()) } if cucumber.Value() != true { t.Errorf("Expected cucumber to be true, got: %t", cucumber.Value()) } } func TestParseSingleDashValue(t *testing.T) { o := opt.String("fruit", "f", "", "") set := opt.Set{o} args := []string{"-f-"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) } if o.Value() != "-" { t.Errorf("Expected fruit to be '-', got: %s", o.Value()) } } func TestParseLongOptionBadValue(t *testing.T) { o := opt.Int("fruit", "f", 0, "") set := opt.Set{o} args := []string{"--fruit=five"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") } } func TestParseShortOptionWithSpaceBadValue(t *testing.T) { o := opt.Int("fruit", "f", 0, "") set := opt.Set{o} args := []string{"-f", "five"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") } } func TestParseShortOptionWithoutEqualBadValue(t *testing.T) { og := opt.Bool("green", "g", false, "") of := opt.Int("fruit", "f", 0, "") set := opt.Set{og, of} args := []string{"-ffive"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") } } func TestParseShortOptionWithEqualBadValue(t *testing.T) { og := opt.Bool("green", "g", false, "") of := opt.Int("fruit", "f", 0, "") set := opt.Set{og, of} args := []string{"-f=five"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") } } func TestParseLongOptionMissingValue(t *testing.T) { o := opt.Float("fruit-price", "f", 0, "") set := opt.Set{o} args := []string{"--fruit-price"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") } } func TestParseShortOptionMissingValue(t *testing.T) { o := opt.Float("fruit-price", "f", 0, "") set := opt.Set{o} args := []string{"-f"} parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") } }