package getopt_test import ( g "getopt" "regexp" "testing" "reflect" ) func TestStringArgNull(t *testing.T) { getopt := g.New() hello := getopt.StringLong(s("hello"), nil, false, "Try") getopt.Parse([]string{}) if hello == nil { t.Error("Unable to dereference hello") } if *hello != nil { t.Error("Hola was not set to nil") } } func TestStringArgNotNull(t *testing.T) { getopt := g.New() hello := getopt.StringLong(s("hello"), nil, false, "Try") getopt.Parse([]string{"--hello", "hello"}) if hello == nil { t.Error("Unable to dereference hello") } if *hello == nil { t.Error("--hello is not received.") } if **hello != "hello" { t.Error("Unexpected value in --hello " + **hello + ".") } } func TestStringArgNotNullWithRune(t *testing.T) { getopt := g.New() hello := getopt.StringLong(nil, r('h'), false, "Try") getopt.Parse([]string{"-hhello"}) if hello == nil { t.Error("Unable to dereference hello") } if *hello == nil { t.Error("-h is not received.") } if **hello != "hello" { t.Error("Unexpected value in --hello " + **hello + ".") } } func TestStringArgNotNullWithRuneUsingExternalArgument(t *testing.T) { getopt := g.New() hello := getopt.StringLong(nil, r('h'), false, "Try") getopt.Parse([]string{"-h", "hello"}) if hello == nil { t.Error("Unable to dereference hello") } if *hello == nil { t.Error("-h is not received.") } if **hello != "hello" { t.Error("Unexpected value in --hello " + **hello + ".") } } func TestParsingNonOptionAndNotOptionArgument(t *testing.T) { getopt := g.New() getopt.StringLong(nil, r('h'), false, "Try") getopt.Parse([]string{"-h", "a", "hello", "world"}) args := getopt.NoOptionArgs() if args[0] != "hello" { t.Error("Argument hello not received.") } if args[1] != "world" { t.Error("Argument world not received.") } } func TestParsingBoolOptionFalse(t *testing.T) { getopt := g.New() h := getopt.BoolLong(nil, r('h'), "Try") getopt.Parse([]string{""}) if *h { t.Error("Unexpected value of -h.") } } func TestParsingBoolOptionTrue(t *testing.T) { getopt := g.New() h := getopt.BoolLong(nil, r('h'), "Try") getopt.Parse([]string{"-h"}) if !*h { t.Error("Unexpected value of -h.") } } func TestParsingBoolOptionUsage(t *testing.T) { getopt := g.New() getopt.BoolLong(s("help"), r('h'), "Try") getopt.Parse([]string{""}) result_string := getopt.UsageString() expected := []string{"--help", "-h", "(required)", "Try"} re := regexp.MustCompile(`\t(\S+)\t(\S+)\t\t(\S+)\t(\S+)`) if reflect.DeepEqual(re.FindStringSubmatch(result_string), expected) { t.Error("Unmatched expected and result.") } } func s(a string) *string { return &a } func r(a rune) *rune { return &a }