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 // RemoveAll removes all exported variables.
13 // This is for tests only.
17 vars
= make(map[string]Var
)
20 func TestInt(t
*testing
.T
) {
22 reqs
:= NewInt("requests")
24 t
.Errorf("reqs.i = %v, want 0", reqs
.i
)
26 if reqs
!= Get("requests").(*Int
) {
27 t
.Errorf("Get() failed.")
33 t
.Errorf("reqs.i = %v, want 4", reqs
.i
)
36 if s
:= reqs
.String(); s
!= "4" {
37 t
.Errorf("reqs.String() = %q, want \"4\"", s
)
42 t
.Errorf("reqs.i = %v, want -2", reqs
.i
)
46 func TestFloat(t
*testing
.T
) {
48 reqs
:= NewFloat("requests-float")
50 t
.Errorf("reqs.f = %v, want 0", reqs
.f
)
52 if reqs
!= Get("requests-float").(*Float
) {
53 t
.Errorf("Get() failed.")
59 t
.Errorf("reqs.f = %v, want 2.75", reqs
.f
)
62 if s
:= reqs
.String(); s
!= "2.75" {
63 t
.Errorf("reqs.String() = %q, want \"4.64\"", s
)
68 t
.Errorf("reqs.f = %v, want 0.75", reqs
.f
)
72 func TestString(t
*testing
.T
) {
74 name
:= NewString("my-name")
76 t
.Errorf("name.s = %q, want \"\"", name
.s
)
81 t
.Errorf("name.s = %q, want \"Mike\"", name
.s
)
84 if s
:= name
.String(); s
!= "\"Mike\"" {
85 t
.Errorf("reqs.String() = %q, want \"\"Mike\"\"", s
)
89 func TestMapCounter(t
*testing
.T
) {
91 colors
:= NewMap("bike-shed-colors")
96 colors
.AddFloat("green", 4.125)
97 if x
:= colors
.m
["red"].(*Int
).i
; x
!= 3 {
98 t
.Errorf("colors.m[\"red\"] = %v, want 3", x
)
100 if x
:= colors
.m
["blue"].(*Int
).i
; x
!= 4 {
101 t
.Errorf("colors.m[\"blue\"] = %v, want 4", x
)
103 if x
:= colors
.m
["green"].(*Float
).f
; x
!= 4.125 {
104 t
.Errorf("colors.m[\"green\"] = %v, want 3.14", x
)
107 // colors.String() should be '{"red":3, "blue":4}',
108 // though the order of red and blue could vary.
111 err
:= json
.Unmarshal([]byte(s
), &j
)
113 t
.Errorf("colors.String() isn't valid JSON: %v", err
)
115 m
, ok
:= j
.(map[string]interface{})
117 t
.Error("colors.String() didn't produce a map.")
120 x
, ok
:= red
.(float64)
122 t
.Error("red.Kind() is not a number.")
125 t
.Errorf("red = %v, want 3", x
)
129 func TestFunc(t
*testing
.T
) {
131 var x
interface{} = []string{"a", "b"}
132 f
:= Func(func() interface{} { return x
})
133 if s
, exp
:= f
.String(), `["a","b"]`; s
!= exp
{
134 t
.Errorf(`f.String() = %q, want %q`, s
, exp
)
138 if s
, exp
:= f
.String(), `17`; s
!= exp
{
139 t
.Errorf(`f.String() = %q, want %q`, s
, exp
)