github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/expvar/expvar_test.go (about) 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. 4 5 package expvar 6 7 import ( 8 "encoding/json" 9 "testing" 10 ) 11 12 // RemoveAll removes all exported variables. 13 // This is for tests only. 14 func RemoveAll() { 15 mutex.Lock() 16 defer mutex.Unlock() 17 vars = make(map[string]Var) 18 } 19 20 func TestInt(t *testing.T) { 21 RemoveAll() 22 reqs := NewInt("requests") 23 if reqs.i != 0 { 24 t.Errorf("reqs.i = %v, want 0", reqs.i) 25 } 26 if reqs != Get("requests").(*Int) { 27 t.Errorf("Get() failed.") 28 } 29 30 reqs.Add(1) 31 reqs.Add(3) 32 if reqs.i != 4 { 33 t.Errorf("reqs.i = %v, want 4", reqs.i) 34 } 35 36 if s := reqs.String(); s != "4" { 37 t.Errorf("reqs.String() = %q, want \"4\"", s) 38 } 39 40 reqs.Set(-2) 41 if reqs.i != -2 { 42 t.Errorf("reqs.i = %v, want -2", reqs.i) 43 } 44 } 45 46 func TestFloat(t *testing.T) { 47 RemoveAll() 48 reqs := NewFloat("requests-float") 49 if reqs.f != 0.0 { 50 t.Errorf("reqs.f = %v, want 0", reqs.f) 51 } 52 if reqs != Get("requests-float").(*Float) { 53 t.Errorf("Get() failed.") 54 } 55 56 reqs.Add(1.5) 57 reqs.Add(1.25) 58 if reqs.f != 2.75 { 59 t.Errorf("reqs.f = %v, want 2.75", reqs.f) 60 } 61 62 if s := reqs.String(); s != "2.75" { 63 t.Errorf("reqs.String() = %q, want \"4.64\"", s) 64 } 65 66 reqs.Add(-2) 67 if reqs.f != 0.75 { 68 t.Errorf("reqs.f = %v, want 0.75", reqs.f) 69 } 70 } 71 72 func TestString(t *testing.T) { 73 RemoveAll() 74 name := NewString("my-name") 75 if name.s != "" { 76 t.Errorf("name.s = %q, want \"\"", name.s) 77 } 78 79 name.Set("Mike") 80 if name.s != "Mike" { 81 t.Errorf("name.s = %q, want \"Mike\"", name.s) 82 } 83 84 if s := name.String(); s != "\"Mike\"" { 85 t.Errorf("reqs.String() = %q, want \"\"Mike\"\"", s) 86 } 87 } 88 89 func TestMapCounter(t *testing.T) { 90 RemoveAll() 91 colors := NewMap("bike-shed-colors") 92 93 colors.Add("red", 1) 94 colors.Add("red", 2) 95 colors.Add("blue", 4) 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) 99 } 100 if x := colors.m["blue"].(*Int).i; x != 4 { 101 t.Errorf("colors.m[\"blue\"] = %v, want 4", x) 102 } 103 if x := colors.m["green"].(*Float).f; x != 4.125 { 104 t.Errorf("colors.m[\"green\"] = %v, want 3.14", x) 105 } 106 107 // colors.String() should be '{"red":3, "blue":4}', 108 // though the order of red and blue could vary. 109 s := colors.String() 110 var j interface{} 111 err := json.Unmarshal([]byte(s), &j) 112 if err != nil { 113 t.Errorf("colors.String() isn't valid JSON: %v", err) 114 } 115 m, ok := j.(map[string]interface{}) 116 if !ok { 117 t.Error("colors.String() didn't produce a map.") 118 } 119 red := m["red"] 120 x, ok := red.(float64) 121 if !ok { 122 t.Error("red.Kind() is not a number.") 123 } 124 if x != 3 { 125 t.Errorf("red = %v, want 3", x) 126 } 127 } 128 129 func TestFunc(t *testing.T) { 130 RemoveAll() 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) 135 } 136 137 x = 17 138 if s, exp := f.String(), `17`; s != exp { 139 t.Errorf(`f.String() = %q, want %q`, s, exp) 140 } 141 }