2011-02-15 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / libgo / go / flag / flag_test.go
blobb91a8b567956e7f762f9c1c140a2468ebc268054
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.
5 package flag_test
7 import (
8 . "flag"
9 "fmt"
10 "os"
11 "testing"
14 var (
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 {
25 if s == "0" {
26 return "false"
28 return "true"
31 func TestEverything(t *testing.T) {
32 m := make(map[string]*Flag)
33 desired := "0"
34 visitor := func(f *Flag) {
35 if len(f.Name) > 5 && f.Name[0:5] == "test_" {
36 m[f.Name] = f
37 ok := false
38 switch {
39 case f.Value.String() == desired:
40 ok = true
41 case f.Name == "test_bool" && f.Value.String() == boolString(desired):
42 ok = true
44 if !ok {
45 t.Error("Visit: bad value", f.Value.String(), "for", f.Name)
49 VisitAll(visitor)
50 if len(m) != 7 {
51 t.Error("VisitAll misses some flags")
52 for k, v := range m {
53 t.Log(k, *v)
56 m = make(map[string]*Flag)
57 Visit(visitor)
58 if len(m) != 0 {
59 t.Errorf("Visit sees unset flags")
60 for k, v := range m {
61 t.Log(k, *v)
64 // Now set all flags
65 Set("test_bool", "true")
66 Set("test_int", "1")
67 Set("test_int64", "1")
68 Set("test_uint", "1")
69 Set("test_uint64", "1")
70 Set("test_string", "1")
71 Set("test_float64", "1")
72 desired = "1"
73 Visit(visitor)
74 if len(m) != 7 {
75 t.Error("Visit fails after set")
76 for k, v := range m {
77 t.Log(k, *v)
82 func TestUsage(t *testing.T) {
83 called := false
84 ResetForTesting(func() { called = true })
85 if ParseForTesting([]string{"a.out", "-x"}) {
86 t.Error("parse did not fail for unknown flag")
88 if !called {
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"
104 args := []string{
105 "a.out",
106 "-bool",
107 "-bool2=true",
108 "--int", "22",
109 "--int64", "23",
110 "-uint", "24",
111 "--uint64", "25",
112 "-string", "hello",
113 "-float64", "2718e28",
114 extra,
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)
125 if *intFlag != 22 {
126 t.Error("int flag should be 22, is ", *intFlag)
128 if *int64Flag != 23 {
129 t.Error("int64 flag should be 23, is ", *int64Flag)
131 if *uintFlag != 24 {
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)
159 return true
162 func TestUserDefined(t *testing.T) {
163 ResetForTesting(func() { t.Fatal("bad parse") })
164 var v flagVar
165 Var(&v, "v", "usage")
166 if !ParseForTesting([]string{"a.out", "-v", "1", "-v", "2", "-v=3"}) {
167 t.Error("parse failed")
169 if len(v) != 3 {
170 t.Fatal("expected 3 args; got ", len(v))
172 expect := "[1 2 3]"
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") })
180 oldArgs := os.Args
181 defer func() { os.Args = oldArgs }()
182 os.Args = []string{"cmd", "-before", "subcmd", "-after", "args"}
183 before := Bool("before", false, "")
184 Parse()
185 cmd := Arg(0)
186 os.Args = Args()
187 after := Bool("after", false, "")
188 Parse()
189 args := Args()
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)