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 TestFloat(t *testing.T) {
38 reqs := NewFloat("requests-float")
40 t.Errorf("reqs.f = %v, want 0", reqs.f)
42 if reqs != Get("requests-float").(*Float) {
43 t.Errorf("Get() failed.")
49 t.Errorf("reqs.f = %v, want 2.75", reqs.f)
52 if s := reqs.String(); s != "2.75" {
53 t.Errorf("reqs.String() = %q, want \"4.64\"", s)
58 t.Errorf("reqs.f = %v, want 0.75", reqs.f)
62 func TestString(t *testing.T) {
63 name := NewString("my-name")
65 t.Errorf("name.s = %q, want \"\"", name.s)
70 t.Errorf("name.s = %q, want \"Mike\"", name.s)
73 if s := name.String(); s != "\"Mike\"" {
74 t.Errorf("reqs.String() = %q, want \"\"Mike\"\"", s)
78 func TestMapCounter(t *testing.T) {
79 colours := NewMap("bike-shed-colours")
83 colours.Add("blue", 4)
84 colours.AddFloat("green", 4.125)
85 if x := colours.m["red"].(*Int).i; x != 3 {
86 t.Errorf("colours.m[\"red\"] = %v, want 3", x)
88 if x := colours.m["blue"].(*Int).i; x != 4 {
89 t.Errorf("colours.m[\"blue\"] = %v, want 4", x)
91 if x := colours.m["green"].(*Float).f; x != 4.125 {
92 t.Errorf("colours.m[\"green\"] = %v, want 3.14", x)
95 // colours.String() should be '{"red":3, "blue":4}',
96 // though the order of red and blue could vary.
99 err := json.Unmarshal([]byte(s), &j)
101 t.Errorf("colours.String() isn't valid JSON: %v", err)
103 m, ok := j.(map[string]interface{})
105 t.Error("colours.String() didn't produce a map.")
108 x, ok := red.(float64)
110 t.Error("red.Kind() is not a number.")
113 t.Errorf("red = %v, want 3", x)
117 func TestIntFunc(t *testing.T) {
119 ix := IntFunc(func() int64 { return x })
120 if s := ix.String(); s != "4" {
121 t.Errorf("ix.String() = %v, want 4", s)
125 if s := ix.String(); s != "5" {
126 t.Errorf("ix.String() = %v, want 5", s)
130 func TestFloatFunc(t *testing.T) {
132 ix := FloatFunc(func() float64 { return x })
133 if s := ix.String(); s != "8.5" {
134 t.Errorf("ix.String() = %v, want 3.14", s)
138 if s := ix.String(); s != "7.25" {
139 t.Errorf("ix.String() = %v, want 4.34", s)
143 func TestStringFunc(t *testing.T) {
145 sx := StringFunc(func() string { return x })
146 if s, exp := sx.String(), `"hello"`; s != exp {
147 t.Errorf(`sx.String() = %q, want %q`, s, exp)
151 if s, exp := sx.String(), `"goodbye"`; s != exp {
152 t.Errorf(`sx.String() = %q, want %q`, s, exp)