2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package expvar
|
|
|
|
|
|
|
|
import (
|
2011-12-07 02:11:29 +01:00
|
|
|
"encoding/json"
|
2010-12-03 05:34:57 +01:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
// RemoveAll removes all exported variables.
|
|
|
|
// This is for tests only.
|
|
|
|
func RemoveAll() {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
vars = make(map[string]Var)
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
func TestInt(t *testing.T) {
|
2013-01-29 21:52:43 +01:00
|
|
|
RemoveAll()
|
2010-12-03 05:34:57 +01:00
|
|
|
reqs := NewInt("requests")
|
|
|
|
if reqs.i != 0 {
|
|
|
|
t.Errorf("reqs.i = %v, want 0", reqs.i)
|
|
|
|
}
|
|
|
|
if reqs != Get("requests").(*Int) {
|
|
|
|
t.Errorf("Get() failed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
reqs.Add(1)
|
|
|
|
reqs.Add(3)
|
|
|
|
if reqs.i != 4 {
|
|
|
|
t.Errorf("reqs.i = %v, want 4", reqs.i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := reqs.String(); s != "4" {
|
|
|
|
t.Errorf("reqs.String() = %q, want \"4\"", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
reqs.Set(-2)
|
|
|
|
if reqs.i != -2 {
|
|
|
|
t.Errorf("reqs.i = %v, want -2", reqs.i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-21 19:19:03 +01:00
|
|
|
func TestFloat(t *testing.T) {
|
2013-01-29 21:52:43 +01:00
|
|
|
RemoveAll()
|
2011-01-21 19:19:03 +01:00
|
|
|
reqs := NewFloat("requests-float")
|
|
|
|
if reqs.f != 0.0 {
|
|
|
|
t.Errorf("reqs.f = %v, want 0", reqs.f)
|
|
|
|
}
|
|
|
|
if reqs != Get("requests-float").(*Float) {
|
|
|
|
t.Errorf("Get() failed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
reqs.Add(1.5)
|
|
|
|
reqs.Add(1.25)
|
|
|
|
if reqs.f != 2.75 {
|
|
|
|
t.Errorf("reqs.f = %v, want 2.75", reqs.f)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := reqs.String(); s != "2.75" {
|
|
|
|
t.Errorf("reqs.String() = %q, want \"4.64\"", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
reqs.Add(-2)
|
|
|
|
if reqs.f != 0.75 {
|
|
|
|
t.Errorf("reqs.f = %v, want 0.75", reqs.f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
func TestString(t *testing.T) {
|
2013-01-29 21:52:43 +01:00
|
|
|
RemoveAll()
|
2010-12-03 05:34:57 +01:00
|
|
|
name := NewString("my-name")
|
|
|
|
if name.s != "" {
|
|
|
|
t.Errorf("name.s = %q, want \"\"", name.s)
|
|
|
|
}
|
|
|
|
|
|
|
|
name.Set("Mike")
|
|
|
|
if name.s != "Mike" {
|
|
|
|
t.Errorf("name.s = %q, want \"Mike\"", name.s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := name.String(); s != "\"Mike\"" {
|
|
|
|
t.Errorf("reqs.String() = %q, want \"\"Mike\"\"", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMapCounter(t *testing.T) {
|
2013-01-29 21:52:43 +01:00
|
|
|
RemoveAll()
|
2011-09-16 17:47:21 +02:00
|
|
|
colors := NewMap("bike-shed-colors")
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
colors.Add("red", 1)
|
|
|
|
colors.Add("red", 2)
|
|
|
|
colors.Add("blue", 4)
|
|
|
|
colors.AddFloat("green", 4.125)
|
|
|
|
if x := colors.m["red"].(*Int).i; x != 3 {
|
|
|
|
t.Errorf("colors.m[\"red\"] = %v, want 3", x)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
if x := colors.m["blue"].(*Int).i; x != 4 {
|
|
|
|
t.Errorf("colors.m[\"blue\"] = %v, want 4", x)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
if x := colors.m["green"].(*Float).f; x != 4.125 {
|
|
|
|
t.Errorf("colors.m[\"green\"] = %v, want 3.14", x)
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// colors.String() should be '{"red":3, "blue":4}',
|
2010-12-03 05:34:57 +01:00
|
|
|
// though the order of red and blue could vary.
|
2011-09-16 17:47:21 +02:00
|
|
|
s := colors.String()
|
2010-12-03 05:34:57 +01:00
|
|
|
var j interface{}
|
|
|
|
err := json.Unmarshal([]byte(s), &j)
|
|
|
|
if err != nil {
|
2011-09-16 17:47:21 +02:00
|
|
|
t.Errorf("colors.String() isn't valid JSON: %v", err)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
m, ok := j.(map[string]interface{})
|
|
|
|
if !ok {
|
2011-09-16 17:47:21 +02:00
|
|
|
t.Error("colors.String() didn't produce a map.")
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
red := m["red"]
|
|
|
|
x, ok := red.(float64)
|
|
|
|
if !ok {
|
|
|
|
t.Error("red.Kind() is not a number.")
|
|
|
|
}
|
|
|
|
if x != 3 {
|
|
|
|
t.Errorf("red = %v, want 3", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
func TestFunc(t *testing.T) {
|
2013-01-29 21:52:43 +01:00
|
|
|
RemoveAll()
|
2011-05-20 02:18:15 +02:00
|
|
|
var x interface{} = []string{"a", "b"}
|
|
|
|
f := Func(func() interface{} { return x })
|
|
|
|
if s, exp := f.String(), `["a","b"]`; s != exp {
|
|
|
|
t.Errorf(`f.String() = %q, want %q`, s, exp)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
x = 17
|
|
|
|
if s, exp := f.String(), `17`; s != exp {
|
|
|
|
t.Errorf(`f.String() = %q, want %q`, s, exp)
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
}
|