1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 test_bool = Bool("test_bool", false, "bool value")
15 test_int = Int("test_int", 0, "int value")
16 test_int64 = Int64("test_int64", 0, "int64 value")
17 test_uint = Uint("test_uint", 0, "uint value")
18 test_uint64 = Uint64("test_uint64", 0, "uint64 value")
19 test_string = String("test_string", "0", "string value")
20 test_float = Float("test_float", 0, "float value")
21 test_float64 = Float("test_float64", 0, "float64 value")
24 func boolString(s string) string {
31 func TestEverything(t *testing.T) {
32 m := make(map[string]*Flag)
34 visitor := func(f *Flag) {
35 if len(f.Name) > 5 && f.Name[0:5] == "test_" {
39 case f.Value.String() == desired:
41 case f.Name == "test_bool" && f.Value.String() == boolString(desired):
45 t.Error("Visit: bad value", f.Value.String(), "for", f.Name)
51 t.Error("VisitAll misses some flags")
56 m = make(map[string]*Flag)
59 t.Errorf("Visit sees unset flags")
65 Set("test_bool", "true")
67 Set("test_int64", "1")
69 Set("test_uint64", "1")
70 Set("test_string", "1")
71 Set("test_float", "1")
72 Set("test_float64", "1")
76 t.Error("Visit fails after set")
83 func TestUsage(t *testing.T) {
85 ResetForTesting(func() { called = true })
86 if ParseForTesting([]string{"a.out", "-x"}) {
87 t.Error("parse did not fail for unknown flag")
90 t.Error("did not call Usage for unknown flag")
94 func TestParse(t *testing.T) {
95 ResetForTesting(func() { t.Error("bad parse") })
96 boolFlag := Bool("bool", false, "bool value")
97 bool2Flag := Bool("bool2", false, "bool2 value")
98 intFlag := Int("int", 0, "int value")
99 int64Flag := Int64("int64", 0, "int64 value")
100 uintFlag := Uint("uint", 0, "uint value")
101 uint64Flag := Uint64("uint64", 0, "uint64 value")
102 stringFlag := String("string", "0", "string value")
103 floatFlag := Float("float", 0, "float value")
104 float64Flag := Float("float64", 0, "float64 value")
105 extra := "one-extra-argument"
116 "-float64", "2718e28",
119 if !ParseForTesting(args) {
120 t.Fatal("parse failed")
122 if *boolFlag != true {
123 t.Error("bool flag should be true, is ", *boolFlag)
125 if *bool2Flag != true {
126 t.Error("bool2 flag should be true, is ", *bool2Flag)
129 t.Error("int flag should be 22, is ", *intFlag)
131 if *int64Flag != 23 {
132 t.Error("int64 flag should be 23, is ", *int64Flag)
135 t.Error("uint flag should be 24, is ", *uintFlag)
137 if *uint64Flag != 25 {
138 t.Error("uint64 flag should be 25, is ", *uint64Flag)
140 if *stringFlag != "hello" {
141 t.Error("string flag should be `hello`, is ", *stringFlag)
143 if *floatFlag != 3141.5 {
144 t.Error("float flag should be 3141.5, is ", *floatFlag)
146 if *float64Flag != 2718e28 {
147 t.Error("float64 flag should be 2718e28, is ", *float64Flag)
149 if len(Args()) != 1 {
150 t.Error("expected one argument, got", len(Args()))
151 } else if Args()[0] != extra {
152 t.Errorf("expected argument %q got %q", extra, Args()[0])
156 // Declare a user-defined flag.
157 type flagVar []string
159 func (f *flagVar) String() string {
160 return fmt.Sprint([]string(*f))
163 func (f *flagVar) Set(value string) bool {
164 *f = append(*f, value)
168 func TestUserDefined(t *testing.T) {
169 ResetForTesting(func() { t.Fatal("bad parse") })
171 Var(&v, "v", "usage")
172 if !ParseForTesting([]string{"a.out", "-v", "1", "-v", "2", "-v=3"}) {
173 t.Error("parse failed")
176 t.Fatal("expected 3 args; got ", len(v))
179 if v.String() != expect {
180 t.Errorf("expected value %q got %q", expect, v.String())