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.
15 test_bool
= Bool("test_bool", false, "bool value")
16 test_int
= Int("test_int", 0, "int value")
17 test_int64
= Int64("test_int64", 0, "int64 value")
18 test_uint
= Uint("test_uint", 0, "uint value")
19 test_uint64
= Uint64("test_uint64", 0, "uint64 value")
20 test_string
= String("test_string", "0", "string value")
21 test_float64
= Float64("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_float64", "1")
75 t
.Error("Visit fails after set")
82 func TestUsage(t
*testing
.T
) {
84 ResetForTesting(func() { called
= true })
85 if ParseForTesting([]string{"a.out", "-x"}) {
86 t
.Error("parse did not fail for unknown flag")
89 t
.Error("did not call Usage for unknown flag")
93 func TestParse(t
*testing
.T
) {
94 ResetForTesting(func() { t
.Error("bad parse") })
95 boolFlag
:= Bool("bool", false, "bool value")
96 bool2Flag
:= Bool("bool2", false, "bool2 value")
97 intFlag
:= Int("int", 0, "int value")
98 int64Flag
:= Int64("int64", 0, "int64 value")
99 uintFlag
:= Uint("uint", 0, "uint value")
100 uint64Flag
:= Uint64("uint64", 0, "uint64 value")
101 stringFlag
:= String("string", "0", "string value")
102 float64Flag
:= Float64("float64", 0, "float64 value")
103 extra
:= "one-extra-argument"
113 "-float64", "2718e28",
116 if !ParseForTesting(args
) {
117 t
.Fatal("parse failed")
119 if *boolFlag
!= true {
120 t
.Error("bool flag should be true, is ", *boolFlag
)
122 if *bool2Flag
!= true {
123 t
.Error("bool2 flag should be true, is ", *bool2Flag
)
126 t
.Error("int flag should be 22, is ", *intFlag
)
128 if *int64Flag
!= 23 {
129 t
.Error("int64 flag should be 23, is ", *int64Flag
)
132 t
.Error("uint flag should be 24, is ", *uintFlag
)
134 if *uint64Flag
!= 25 {
135 t
.Error("uint64 flag should be 25, is ", *uint64Flag
)
137 if *stringFlag
!= "hello" {
138 t
.Error("string flag should be `hello`, is ", *stringFlag
)
140 if *float64Flag
!= 2718e28
{
141 t
.Error("float64 flag should be 2718e28, is ", *float64Flag
)
143 if len(Args()) != 1 {
144 t
.Error("expected one argument, got", len(Args()))
145 } else if Args()[0] != extra
{
146 t
.Errorf("expected argument %q got %q", extra
, Args()[0])
150 // Declare a user-defined flag.
151 type flagVar
[]string
153 func (f
*flagVar
) String() string {
154 return fmt
.Sprint([]string(*f
))
157 func (f
*flagVar
) Set(value
string) bool {
158 *f
= append(*f
, value
)
162 func TestUserDefined(t
*testing
.T
) {
163 ResetForTesting(func() { t
.Fatal("bad parse") })
165 Var(&v
, "v", "usage")
166 if !ParseForTesting([]string{"a.out", "-v", "1", "-v", "2", "-v=3"}) {
167 t
.Error("parse failed")
170 t
.Fatal("expected 3 args; got ", len(v
))
173 if v
.String() != expect
{
174 t
.Errorf("expected value %q got %q", expect
, v
.String())
178 func TestChangingArgs(t
*testing
.T
) {
179 ResetForTesting(func() { t
.Fatal("bad parse") })
181 defer func() { os
.Args
= oldArgs
}()
182 os
.Args
= []string{"cmd", "-before", "subcmd", "-after", "args"}
183 before
:= Bool("before", false, "")
187 after
:= Bool("after", false, "")
191 if !*before || cmd
!= "subcmd" ||
!*after ||
len(args
) != 1 || args
[0] != "args" {
192 t
.Fatalf("expected true subcmd true [args] got %v %v %v %v", *before
, cmd
, *after
, args
)