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.
12 func TestInt(t *testing.T) {
13 reqs := NewInt("requests")
15 t.Errorf("reqs.i = %v, want 0", reqs.i)
17 if reqs != Get("requests").(*Int) {
18 t.Errorf("Get() failed.")
24 t.Errorf("reqs.i = %v, want 4", reqs.i)
27 if s := reqs.String(); s != "4" {
28 t.Errorf("reqs.String() = %q, want \"4\"", s)
33 t.Errorf("reqs.i = %v, want -2", reqs.i)
37 func TestString(t *testing.T) {
38 name := NewString("my-name")
40 t.Errorf("name.s = %q, want \"\"", name.s)
45 t.Errorf("name.s = %q, want \"Mike\"", name.s)
48 if s := name.String(); s != "\"Mike\"" {
49 t.Errorf("reqs.String() = %q, want \"\"Mike\"\"", s)
53 func TestMapCounter(t *testing.T) {
54 colours := NewMap("bike-shed-colours")
58 colours.Add("blue", 4)
59 if x := colours.m["red"].(*Int).i; x != 3 {
60 t.Errorf("colours.m[\"red\"] = %v, want 3", x)
62 if x := colours.m["blue"].(*Int).i; x != 4 {
63 t.Errorf("colours.m[\"blue\"] = %v, want 4", x)
66 // colours.String() should be '{"red":3, "blue":4}',
67 // though the order of red and blue could vary.
70 err := json.Unmarshal([]byte(s), &j)
72 t.Errorf("colours.String() isn't valid JSON: %v", err)
74 m, ok := j.(map[string]interface{})
76 t.Error("colours.String() didn't produce a map.")
79 x, ok := red.(float64)
81 t.Error("red.Kind() is not a number.")
84 t.Errorf("red = %v, want 3", x)
88 func TestIntFunc(t *testing.T) {
90 ix := IntFunc(func() int64 { return int64(x) })
91 if s := ix.String(); s != "4" {
92 t.Errorf("ix.String() = %v, want 4", s)
96 if s := ix.String(); s != "5" {
97 t.Errorf("ix.String() = %v, want 5", s)