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 // RemoveAll removes all exported variables.
16 // This is for tests only.
20 vars
= make(map[string]Var
)
24 func TestInt(t
*testing
.T
) {
26 reqs
:= NewInt("requests")
28 t
.Errorf("reqs.i = %v, want 0", reqs
.i
)
30 if reqs
!= Get("requests").(*Int
) {
31 t
.Errorf("Get() failed.")
37 t
.Errorf("reqs.i = %v, want 4", reqs
.i
)
40 if s
:= reqs
.String(); s
!= "4" {
41 t
.Errorf("reqs.String() = %q, want \"4\"", s
)
46 t
.Errorf("reqs.i = %v, want -2", reqs
.i
)
50 func TestFloat(t
*testing
.T
) {
52 reqs
:= NewFloat("requests-float")
54 t
.Errorf("reqs.f = %v, want 0", reqs
.f
)
56 if reqs
!= Get("requests-float").(*Float
) {
57 t
.Errorf("Get() failed.")
63 t
.Errorf("reqs.f = %v, want 2.75", reqs
.f
)
66 if s
:= reqs
.String(); s
!= "2.75" {
67 t
.Errorf("reqs.String() = %q, want \"4.64\"", s
)
72 t
.Errorf("reqs.f = %v, want 0.75", reqs
.f
)
76 func TestString(t
*testing
.T
) {
78 name
:= NewString("my-name")
80 t
.Errorf("name.s = %q, want \"\"", name
.s
)
85 t
.Errorf("name.s = %q, want \"Mike\"", name
.s
)
88 if s
:= name
.String(); s
!= "\"Mike\"" {
89 t
.Errorf("reqs.String() = %q, want \"\"Mike\"\"", s
)
93 func TestMapCounter(t
*testing
.T
) {
95 colors
:= NewMap("bike-shed-colors")
100 colors
.AddFloat(`green "midori"`, 4.125)
101 if x
:= colors
.m
["red"].(*Int
).i
; x
!= 3 {
102 t
.Errorf("colors.m[\"red\"] = %v, want 3", x
)
104 if x
:= colors
.m
["blue"].(*Int
).i
; x
!= 4 {
105 t
.Errorf("colors.m[\"blue\"] = %v, want 4", x
)
107 if x
:= colors
.m
[`green "midori"`].(*Float
).f
; x
!= 4.125 {
108 t
.Errorf("colors.m[`green \"midori\"] = %v, want 3.14", x
)
111 // colors.String() should be '{"red":3, "blue":4}',
112 // though the order of red and blue could vary.
115 err
:= json
.Unmarshal([]byte(s
), &j
)
117 t
.Errorf("colors.String() isn't valid JSON: %v", err
)
119 m
, ok
:= j
.(map[string]interface{})
121 t
.Error("colors.String() didn't produce a map.")
124 x
, ok
:= red
.(float64)
126 t
.Error("red.Kind() is not a number.")
129 t
.Errorf("red = %v, want 3", x
)
133 func TestFunc(t
*testing
.T
) {
135 var x
interface{} = []string{"a", "b"}
136 f
:= Func(func() interface{} { return x
})
137 if s
, exp
:= f
.String(), `["a","b"]`; s
!= exp
{
138 t
.Errorf(`f.String() = %q, want %q`, s
, exp
)
142 if s
, exp
:= f
.String(), `17`; s
!= exp
{
143 t
.Errorf(`f.String() = %q, want %q`, s
, exp
)
147 func TestHandler(t
*testing
.T
) {
153 for i
:= 0; i
< 9; i
++ {
154 m2
.Add(strconv
.Itoa(i
), int64(i
))
156 rr
:= httptest
.NewRecorder()
157 rr
.Body
= new(bytes
.Buffer
)
158 expvarHandler(rr
, nil)
160 "map1": {"a": 1, "z": 2},
161 "map2": {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8}
164 if got
:= rr
.Body
.String(); got
!= want
{
165 t
.Errorf("HTTP handler wrote:\n%s\nWant:\n%s", got
, want
)