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-05-04 16:14:22 -06:00
|
|
|
"json";
|
2009-04-19 22:17:27 -06:00
|
|
|
"testing";
|
|
|
|
)
|
|
|
|
|
2009-05-04 16:14:22 -06:00
|
|
|
func TestInt(t *testing.T) {
|
|
|
|
reqs := NewInt("requests");
|
|
|
|
if reqs.i != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("reqs.i = %v, want 4", 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-05-04 16:14:22 -06:00
|
|
|
reqs.Add(1);
|
|
|
|
reqs.Add(3);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-04 16:14:22 -06:00
|
|
|
func TestString(t *testing.T) {
|
|
|
|
name := NewString("my-name");
|
|
|
|
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-05-04 16:14:22 -06:00
|
|
|
name.Set("Mike");
|
|
|
|
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-05-04 16:14:22 -06:00
|
|
|
colours := NewMap("bike-shed-colours");
|
2009-04-20 23:38:14 -06:00
|
|
|
|
2009-05-04 16:14:22 -06:00
|
|
|
colours.Add("red", 1);
|
|
|
|
colours.Add("red", 2);
|
|
|
|
colours.Add("blue", 4);
|
|
|
|
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.
|
|
|
|
s := colours.String();
|
|
|
|
j, ok, errtok := json.StringToJson(s);
|
|
|
|
if !ok {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("colours.String() isn't valid JSON: %v", errtok)
|
2009-04-19 22:17:27 -06:00
|
|
|
}
|
2009-05-04 16:14:22 -06:00
|
|
|
if j.Kind() != json.MapKind {
|
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
|
|
|
}
|
|
|
|
red := j.Get("red");
|
|
|
|
if red.Kind() != json.NumberKind {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("red.Kind() is not a NumberKind.")
|
2009-05-04 16:14:22 -06:00
|
|
|
}
|
|
|
|
if x := red.Number(); x != 3 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("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) {
|
|
|
|
x := int(4);
|
|
|
|
ix := IntFunc(func() int64 { return int64(x) });
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
x++;
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|