OSDN Git Service

Add Go frontend, libgo library, and Go testsuite.
[pf3gnuchains/gcc-fork.git] / libgo / go / flag / flag_test.go
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.
4
5 package flag_test
6
7 import (
8         . "flag"
9         "fmt"
10         "testing"
11 )
12
13 var (
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")
22 )
23
24 func boolString(s string) string {
25         if s == "0" {
26                 return "false"
27         }
28         return "true"
29 }
30
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
43                         }
44                         if !ok {
45                                 t.Error("Visit: bad value", f.Value.String(), "for", f.Name)
46                         }
47                 }
48         }
49         VisitAll(visitor)
50         if len(m) != 8 {
51                 t.Error("VisitAll misses some flags")
52                 for k, v := range m {
53                         t.Log(k, *v)
54                 }
55         }
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)
62                 }
63         }
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_float", "1")
72         Set("test_float64", "1")
73         desired = "1"
74         Visit(visitor)
75         if len(m) != 8 {
76                 t.Error("Visit fails after set")
77                 for k, v := range m {
78                         t.Log(k, *v)
79                 }
80         }
81 }
82
83 func TestUsage(t *testing.T) {
84         called := false
85         ResetForTesting(func() { called = true })
86         if ParseForTesting([]string{"a.out", "-x"}) {
87                 t.Error("parse did not fail for unknown flag")
88         }
89         if !called {
90                 t.Error("did not call Usage for unknown flag")
91         }
92 }
93
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"
106         args := []string{
107                 "a.out",
108                 "-bool",
109                 "-bool2=true",
110                 "--int", "22",
111                 "--int64", "23",
112                 "-uint", "24",
113                 "--uint64", "25",
114                 "-string", "hello",
115                 "--float", "3141.5",
116                 "-float64", "2718e28",
117                 extra,
118         }
119         if !ParseForTesting(args) {
120                 t.Fatal("parse failed")
121         }
122         if *boolFlag != true {
123                 t.Error("bool flag should be true, is ", *boolFlag)
124         }
125         if *bool2Flag != true {
126                 t.Error("bool2 flag should be true, is ", *bool2Flag)
127         }
128         if *intFlag != 22 {
129                 t.Error("int flag should be 22, is ", *intFlag)
130         }
131         if *int64Flag != 23 {
132                 t.Error("int64 flag should be 23, is ", *int64Flag)
133         }
134         if *uintFlag != 24 {
135                 t.Error("uint flag should be 24, is ", *uintFlag)
136         }
137         if *uint64Flag != 25 {
138                 t.Error("uint64 flag should be 25, is ", *uint64Flag)
139         }
140         if *stringFlag != "hello" {
141                 t.Error("string flag should be `hello`, is ", *stringFlag)
142         }
143         if *floatFlag != 3141.5 {
144                 t.Error("float flag should be 3141.5, is ", *floatFlag)
145         }
146         if *float64Flag != 2718e28 {
147                 t.Error("float64 flag should be 2718e28, is ", *float64Flag)
148         }
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])
153         }
154 }
155
156 // Declare a user-defined flag.
157 type flagVar []string
158
159 func (f *flagVar) String() string {
160         return fmt.Sprint([]string(*f))
161 }
162
163 func (f *flagVar) Set(value string) bool {
164         *f = append(*f, value)
165         return true
166 }
167
168 func TestUserDefined(t *testing.T) {
169         ResetForTesting(func() { t.Fatal("bad parse") })
170         var v flagVar
171         Var(&v, "v", "usage")
172         if !ParseForTesting([]string{"a.out", "-v", "1", "-v", "2", "-v=3"}) {
173                 t.Error("parse failed")
174         }
175         if len(v) != 3 {
176                 t.Fatal("expected 3 args; got ", len(v))
177         }
178         expect := "[1 2 3]"
179         if v.String() != expect {
180                 t.Errorf("expected value %q got %q", expect, v.String())
181         }
182 }