2009-04-19 22:17:27 -06: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.
|
|
|
|
|
2009-09-30 14:11:33 -06:00
|
|
|
package expvar
|
2009-04-19 22:17:27 -06:00
|
|
|
|
|
|
|
import (
|
2009-12-15 16:27:16 -07:00
|
|
|
"json"
|
|
|
|
"testing"
|
2009-04-19 22:17:27 -06:00
|
|
|
)
|
|
|
|
|
2009-05-04 16:14:22 -06:00
|
|
|
func TestInt(t *testing.T) {
|
2009-12-15 16:27:16 -07:00
|
|
|
reqs := NewInt("requests")
|
2009-05-04 16:14:22 -06:00
|
|
|
if reqs.i != 0 {
|
2010-01-22 15:24:17 -07:00
|
|
|
t.Errorf("reqs.i = %v, want 0", reqs.i)
|
2009-04-19 22:17:27 -06:00
|
|
|
}
|
2009-05-04 16:14:22 -06:00
|
|
|
if reqs != Get("requests").(*Int) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("Get() failed.")
|
2009-04-19 22:17:27 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:27:16 -07:00
|
|
|
reqs.Add(1)
|
|
|
|
reqs.Add(3)
|
2009-05-04 16:14:22 -06:00
|
|
|
if reqs.i != 4 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("reqs.i = %v, want 4", reqs.i)
|
2009-04-26 21:57:01 -06:00
|
|
|
}
|
|
|
|
|
2009-05-04 16:14:22 -06:00
|
|
|
if s := reqs.String(); s != "4" {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("reqs.String() = %q, want \"4\"", s)
|
2009-04-26 21:57:01 -06:00
|
|
|
}
|
2010-10-11 11:14:07 -06:00
|
|
|
|
|
|
|
reqs.Set(-2)
|
|
|
|
if reqs.i != -2 {
|
|
|
|
t.Errorf("reqs.i = %v, want -2", reqs.i)
|
|
|
|
}
|
2009-04-26 21:57:01 -06:00
|
|
|
}
|
|
|
|
|
2009-05-04 16:14:22 -06:00
|
|
|
func TestString(t *testing.T) {
|
2009-12-15 16:27:16 -07:00
|
|
|
name := NewString("my-name")
|
2009-05-04 16:14:22 -06:00
|
|
|
if name.s != "" {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("name.s = %q, want \"\"", name.s)
|
2009-04-20 23:38:14 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:27:16 -07:00
|
|
|
name.Set("Mike")
|
2009-05-04 16:14:22 -06:00
|
|
|
if name.s != "Mike" {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("name.s = %q, want \"Mike\"", name.s)
|
2009-04-26 21:57:01 -06:00
|
|
|
}
|
|
|
|
|
2009-05-04 16:14:22 -06:00
|
|
|
if s := name.String(); s != "\"Mike\"" {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("reqs.String() = %q, want \"\"Mike\"\"", s)
|
2009-04-20 23:38:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMapCounter(t *testing.T) {
|
2009-12-15 16:27:16 -07:00
|
|
|
colours := NewMap("bike-shed-colours")
|
2009-04-20 23:38:14 -06:00
|
|
|
|
2009-12-15 16:27:16 -07:00
|
|
|
colours.Add("red", 1)
|
|
|
|
colours.Add("red", 2)
|
|
|
|
colours.Add("blue", 4)
|
2009-05-04 16:14:22 -06:00
|
|
|
if x := colours.m["red"].(*Int).i; x != 3 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("colours.m[\"red\"] = %v, want 3", x)
|
2009-04-20 23:38:14 -06:00
|
|
|
}
|
2009-05-04 16:14:22 -06:00
|
|
|
if x := colours.m["blue"].(*Int).i; x != 4 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("colours.m[\"blue\"] = %v, want 4", x)
|
2009-04-20 23:38:14 -06:00
|
|
|
}
|
|
|
|
|
2009-05-04 16:14:22 -06:00
|
|
|
// colours.String() should be '{"red":3, "blue":4}',
|
|
|
|
// though the order of red and blue could vary.
|
2009-12-15 16:27:16 -07:00
|
|
|
s := colours.String()
|
2010-04-21 17:40:53 -06:00
|
|
|
var j interface{}
|
|
|
|
err := json.Unmarshal([]byte(s), &j)
|
2009-11-30 14:55:09 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("colours.String() isn't valid JSON: %v", err)
|
2009-04-19 22:17:27 -06:00
|
|
|
}
|
2009-12-15 16:27:16 -07:00
|
|
|
m, ok := j.(map[string]interface{})
|
2009-11-30 14:55:09 -07:00
|
|
|
if !ok {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("colours.String() didn't produce a map.")
|
2009-05-04 16:14:22 -06:00
|
|
|
}
|
2009-12-15 16:27:16 -07:00
|
|
|
red := m["red"]
|
|
|
|
x, ok := red.(float64)
|
2009-11-30 14:55:09 -07:00
|
|
|
if !ok {
|
|
|
|
t.Error("red.Kind() is not a number.")
|
2009-05-04 16:14:22 -06:00
|
|
|
}
|
2009-11-30 14:55:09 -07:00
|
|
|
if x != 3 {
|
2010-09-22 21:48:56 -06:00
|
|
|
t.Errorf("red = %v, want 3", x)
|
2009-04-19 22:17:27 -06:00
|
|
|
}
|
|
|
|
}
|
2009-05-24 16:04:43 -06:00
|
|
|
|
|
|
|
func TestIntFunc(t *testing.T) {
|
2010-12-19 23:35:21 -07:00
|
|
|
x := int64(4)
|
|
|
|
ix := IntFunc(func() int64 { return x })
|
2009-05-24 16:04:43 -06:00
|
|
|
if s := ix.String(); s != "4" {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("ix.String() = %v, want 4", s)
|
2009-05-24 16:04:43 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:27:16 -07:00
|
|
|
x++
|
2009-05-24 16:04:43 -06:00
|
|
|
if s := ix.String(); s != "5" {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("ix.String() = %v, want 5", s)
|
2009-05-24 16:04:43 -06:00
|
|
|
}
|
|
|
|
}
|
2010-12-19 23:35:21 -07:00
|
|
|
|
|
|
|
func TestStringFunc(t *testing.T) {
|
|
|
|
x := "hello"
|
|
|
|
sx := StringFunc(func() string { return x })
|
|
|
|
if s, exp := sx.String(), `"hello"`; s != exp {
|
|
|
|
t.Errorf(`sx.String() = %q, want %q`, s, exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
x = "goodbye"
|
|
|
|
if s, exp := sx.String(), `"goodbye"`; s != exp {
|
|
|
|
t.Errorf(`sx.String() = %q, want %q`, s, exp)
|
|
|
|
}
|
|
|
|
}
|