libgo: Merge to master revision 19184.
[official-gcc.git] / libgo / go / expvar / expvar_test.go
blobd2ea484935e9c5fe08cb52e4c27c01a26b7cab79
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 expvar
7 import (
8 "bytes"
9 "encoding/json"
10 "net/http/httptest"
11 "strconv"
12 "testing"
15 // RemoveAll removes all exported variables.
16 // This is for tests only.
17 func RemoveAll() {
18 mutex.Lock()
19 defer mutex.Unlock()
20 vars = make(map[string]Var)
21 varKeys = nil
24 func TestInt(t *testing.T) {
25 RemoveAll()
26 reqs := NewInt("requests")
27 if reqs.i != 0 {
28 t.Errorf("reqs.i = %v, want 0", reqs.i)
30 if reqs != Get("requests").(*Int) {
31 t.Errorf("Get() failed.")
34 reqs.Add(1)
35 reqs.Add(3)
36 if reqs.i != 4 {
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)
44 reqs.Set(-2)
45 if reqs.i != -2 {
46 t.Errorf("reqs.i = %v, want -2", reqs.i)
50 func TestFloat(t *testing.T) {
51 RemoveAll()
52 reqs := NewFloat("requests-float")
53 if reqs.f != 0.0 {
54 t.Errorf("reqs.f = %v, want 0", reqs.f)
56 if reqs != Get("requests-float").(*Float) {
57 t.Errorf("Get() failed.")
60 reqs.Add(1.5)
61 reqs.Add(1.25)
62 if reqs.f != 2.75 {
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)
70 reqs.Add(-2)
71 if reqs.f != 0.75 {
72 t.Errorf("reqs.f = %v, want 0.75", reqs.f)
76 func TestString(t *testing.T) {
77 RemoveAll()
78 name := NewString("my-name")
79 if name.s != "" {
80 t.Errorf("name.s = %q, want \"\"", name.s)
83 name.Set("Mike")
84 if name.s != "Mike" {
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) {
94 RemoveAll()
95 colors := NewMap("bike-shed-colors")
97 colors.Add("red", 1)
98 colors.Add("red", 2)
99 colors.Add("blue", 4)
100 colors.AddFloat("green", 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"].(*Float).f; x != 4.125 {
108 t.Errorf("colors.m[\"green\"] = %v, want 3.14", x)
111 // colors.String() should be '{"red":3, "blue":4}',
112 // though the order of red and blue could vary.
113 s := colors.String()
114 var j interface{}
115 err := json.Unmarshal([]byte(s), &j)
116 if err != nil {
117 t.Errorf("colors.String() isn't valid JSON: %v", err)
119 m, ok := j.(map[string]interface{})
120 if !ok {
121 t.Error("colors.String() didn't produce a map.")
123 red := m["red"]
124 x, ok := red.(float64)
125 if !ok {
126 t.Error("red.Kind() is not a number.")
128 if x != 3 {
129 t.Errorf("red = %v, want 3", x)
133 func TestFunc(t *testing.T) {
134 RemoveAll()
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)
141 x = 17
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) {
148 RemoveAll()
149 m := NewMap("map1")
150 m.Add("a", 1)
151 m.Add("z", 2)
152 m2 := NewMap("map2")
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)
159 want := `{
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)