2023-11-12 17:19:34 -05:00
|
|
|
package opt_test
|
2023-11-12 14:27:27 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-11-12 17:19:34 -05:00
|
|
|
"go.fifitido.net/cmd/opt"
|
2023-11-12 14:27:27 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseUnknownLongOption(t *testing.T) {
|
2023-11-12 17:19:34 -05:00
|
|
|
set := opt.Set{}
|
2023-11-12 14:27:27 -05:00
|
|
|
args := []string{"--unknown"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 14:27:27 -05:00
|
|
|
_, 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) {
|
2023-11-12 17:19:34 -05:00
|
|
|
set := opt.Set{}
|
2023-11-12 14:27:27 -05:00
|
|
|
args := []string{"-u"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 14:27:27 -05:00
|
|
|
_, 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) {
|
2023-11-12 17:19:34 -05:00
|
|
|
set := opt.Set{
|
|
|
|
opt.Bool("banana", "b", false, ""),
|
|
|
|
opt.Bool("cucumber", "c", false, ""),
|
2023-11-12 14:27:27 -05:00
|
|
|
}
|
|
|
|
args := []string{"-abc"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 14:27:27 -05:00
|
|
|
_, 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) {
|
2023-11-12 17:19:34 -05:00
|
|
|
set := opt.Set{
|
|
|
|
opt.Bool("apple", "a", false, ""),
|
|
|
|
opt.Bool("cucumber", "c", false, ""),
|
2023-11-12 14:27:27 -05:00
|
|
|
}
|
|
|
|
args := []string{"-abc"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 14:27:27 -05:00
|
|
|
_, 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) {
|
2023-11-12 17:19:34 -05:00
|
|
|
set := opt.Set{
|
|
|
|
opt.Bool("apple", "a", false, ""),
|
|
|
|
opt.Bool("banana", "b", false, ""),
|
2023-11-12 14:27:27 -05:00
|
|
|
}
|
|
|
|
args := []string{"-abc"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 14:27:27 -05:00
|
|
|
_, 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) {
|
2023-11-12 17:19:34 -05:00
|
|
|
set := opt.Set{
|
|
|
|
opt.Bool("apple", "a", false, ""),
|
|
|
|
opt.String("banana", "b", "", ""),
|
2023-11-12 14:27:27 -05:00
|
|
|
}
|
|
|
|
args := []string{"-ab"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 14:27:27 -05:00
|
|
|
_, 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) {
|
2023-11-12 17:19:34 -05:00
|
|
|
o := opt.String("fruit", "f", "", "")
|
|
|
|
set := opt.Set{o}
|
2023-11-12 14:27:27 -05:00
|
|
|
args := []string{"-fapple"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 14:27:27 -05:00
|
|
|
_, err := parser.Parse()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no error, got: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2023-11-12 17:19:34 -05:00
|
|
|
if o.Value() != "apple" {
|
|
|
|
t.Errorf("Expected fruit to be 'apple', got: %s", o.Value())
|
2023-11-12 14:27:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseShortOptionWithValueAndSpace(t *testing.T) {
|
2023-11-12 17:19:34 -05:00
|
|
|
o := opt.String("fruit", "f", "", "")
|
|
|
|
set := opt.Set{o}
|
2023-11-12 14:27:27 -05:00
|
|
|
args := []string{"-f", "apple"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 14:27:27 -05:00
|
|
|
_, err := parser.Parse()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no error, got: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2023-11-12 17:19:34 -05:00
|
|
|
if o.Value() != "apple" {
|
|
|
|
t.Errorf("Expected fruit to be 'apple', got: %s", o.Value())
|
2023-11-12 14:27:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseShortOptionWithValueAndEqual(t *testing.T) {
|
2023-11-12 17:19:34 -05:00
|
|
|
o := opt.String("fruit", "f", "", "")
|
|
|
|
set := opt.Set{o}
|
2023-11-12 14:27:27 -05:00
|
|
|
args := []string{"-f=apple"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 14:27:27 -05:00
|
|
|
_, err := parser.Parse()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no error, got: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2023-11-12 17:19:34 -05:00
|
|
|
if o.Value() != "apple" {
|
|
|
|
t.Errorf("Expected fruit to be 'apple', got: %s", o.Value())
|
2023-11-12 14:27:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseLongOptionWithValueAndEqual(t *testing.T) {
|
2023-11-12 17:19:34 -05:00
|
|
|
o := opt.String("fruit", "f", "", "")
|
|
|
|
set := opt.Set{o}
|
2023-11-12 14:27:27 -05:00
|
|
|
args := []string{"--fruit=apple"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 14:27:27 -05:00
|
|
|
_, err := parser.Parse()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no error, got: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2023-11-12 17:19:34 -05:00
|
|
|
if o.Value() != "apple" {
|
|
|
|
t.Errorf("Expected fruit to be 'apple', got: %s", o.Value())
|
2023-11-12 14:27:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseLongOptionWithValueAndSpace(t *testing.T) {
|
2023-11-12 17:19:34 -05:00
|
|
|
o := opt.String("fruit", "f", "", "")
|
|
|
|
set := opt.Set{o}
|
2023-11-12 14:27:27 -05:00
|
|
|
args := []string{"--fruit", "apple"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 14:27:27 -05:00
|
|
|
_, err := parser.Parse()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no error, got: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2023-11-12 17:19:34 -05:00
|
|
|
if o.Value() != "apple" {
|
|
|
|
t.Errorf("Expected fruit to be 'apple', got: %s", o.Value())
|
2023-11-12 14:27:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseOptionTerminator(t *testing.T) {
|
2023-11-12 17:19:34 -05:00
|
|
|
o := opt.String("fruit", "f", "banana", "")
|
|
|
|
set := opt.Set{o}
|
2023-11-12 17:06:43 -05:00
|
|
|
args := []string{"--fruit", "apple", "--", "hello", "world"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 14:27:27 -05:00
|
|
|
restArgs, err := parser.Parse()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no error, got: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2023-11-12 17:19:34 -05:00
|
|
|
if o.Value() != "apple" {
|
|
|
|
t.Errorf("Expected fruit to be 'apple', got: %s", o.Value())
|
2023-11-12 14:27:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-11-12 17:19:34 -05:00
|
|
|
set := opt.Set{}
|
2023-11-12 14:27:27 -05:00
|
|
|
args := []string{"--unknown", "hello", "world"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, true)
|
2023-11-12 14:27:27 -05:00
|
|
|
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) {
|
2023-11-12 17:19:34 -05:00
|
|
|
set := opt.Set{}
|
2023-11-12 14:27:27 -05:00
|
|
|
args := []string{"-q"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, true)
|
2023-11-12 14:27:27 -05:00
|
|
|
|
|
|
|
_, err := parser.Parse()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no error, got: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseChainedShortOptionIgnoreUnknown(t *testing.T) {
|
2023-11-12 17:19:34 -05:00
|
|
|
apple := opt.Bool("apple", "a", false, "")
|
|
|
|
banana := opt.Bool("banana", "b", false, "")
|
|
|
|
cucumber := opt.Bool("cucumber", "c", false, "")
|
|
|
|
set := opt.Set{apple, banana, cucumber}
|
2023-11-12 14:27:27 -05:00
|
|
|
args := []string{"-adc"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, true)
|
2023-11-12 14:27:27 -05:00
|
|
|
|
|
|
|
_, 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) {
|
2023-11-12 17:19:34 -05:00
|
|
|
o := opt.String("fruit", "f", "", "")
|
|
|
|
set := opt.Set{o}
|
2023-11-12 14:27:27 -05:00
|
|
|
args := []string{"-f-"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 14:27:27 -05:00
|
|
|
_, err := parser.Parse()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no error, got: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2023-11-12 17:19:34 -05:00
|
|
|
if o.Value() != "-" {
|
|
|
|
t.Errorf("Expected fruit to be '-', got: %s", o.Value())
|
2023-11-12 14:27:27 -05:00
|
|
|
}
|
|
|
|
}
|
2023-11-12 17:00:01 -05:00
|
|
|
|
|
|
|
func TestParseLongOptionBadValue(t *testing.T) {
|
2023-11-12 17:19:34 -05:00
|
|
|
o := opt.Int("fruit", "f", 0, "")
|
|
|
|
set := opt.Set{o}
|
2023-11-12 17:00:01 -05:00
|
|
|
args := []string{"--fruit=five"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 17:00:01 -05:00
|
|
|
_, err := parser.Parse()
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseShortOptionWithSpaceBadValue(t *testing.T) {
|
2023-11-12 17:19:34 -05:00
|
|
|
o := opt.Int("fruit", "f", 0, "")
|
|
|
|
set := opt.Set{o}
|
2023-11-12 17:00:01 -05:00
|
|
|
args := []string{"-f", "five"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 17:00:01 -05:00
|
|
|
_, err := parser.Parse()
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseShortOptionWithoutEqualBadValue(t *testing.T) {
|
2023-11-12 17:19:34 -05:00
|
|
|
og := opt.Bool("green", "g", false, "")
|
|
|
|
of := opt.Int("fruit", "f", 0, "")
|
|
|
|
set := opt.Set{og, of}
|
2023-11-12 17:00:01 -05:00
|
|
|
args := []string{"-ffive"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 17:00:01 -05:00
|
|
|
_, err := parser.Parse()
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseShortOptionWithEqualBadValue(t *testing.T) {
|
2023-11-12 17:19:34 -05:00
|
|
|
og := opt.Bool("green", "g", false, "")
|
|
|
|
of := opt.Int("fruit", "f", 0, "")
|
|
|
|
set := opt.Set{og, of}
|
2023-11-12 17:00:01 -05:00
|
|
|
args := []string{"-f=five"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 17:00:01 -05:00
|
|
|
_, err := parser.Parse()
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error")
|
|
|
|
}
|
|
|
|
}
|
2023-11-12 17:06:43 -05:00
|
|
|
|
|
|
|
func TestParseLongOptionMissingValue(t *testing.T) {
|
2023-11-12 17:19:34 -05:00
|
|
|
o := opt.Float("fruit-price", "f", 0, "")
|
|
|
|
set := opt.Set{o}
|
2023-11-12 17:09:33 -05:00
|
|
|
args := []string{"--fruit-price"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 17:06:43 -05:00
|
|
|
_, err := parser.Parse()
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseShortOptionMissingValue(t *testing.T) {
|
2023-11-12 17:19:34 -05:00
|
|
|
o := opt.Float("fruit-price", "f", 0, "")
|
|
|
|
set := opt.Set{o}
|
2023-11-12 17:06:43 -05:00
|
|
|
args := []string{"-f"}
|
2023-11-12 17:19:34 -05:00
|
|
|
parser := opt.NewParser(args, set, false)
|
2023-11-12 17:06:43 -05:00
|
|
|
_, err := parser.Parse()
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error")
|
|
|
|
}
|
|
|
|
}
|