summaryrefslogtreecommitdiff
path: root/src/lib/exvar/exvar_test.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2009-05-05 17:05:39 -0700
committerRob Pike <r@golang.org>2009-05-05 17:05:39 -0700
commitb6708b2dad4af458e69fde7fc596ecdce03280d0 (patch)
tree11cfc1b980ecce9cb0eec06f49cc53ce05faf350 /src/lib/exvar/exvar_test.go
parent658b10477a16151de7e4d51657993f95445f11ee (diff)
downloadgolang-b6708b2dad4af458e69fde7fc596ecdce03280d0.tar.gz
directory-per-package step 1: move files from lib/X.go to lib/X/X.go
no substantive changes except: - new Makefiles, all auto-generated - go/src/lib/Makefile has been extensively edited R=rsc OCL=28310 CL=28310
Diffstat (limited to 'src/lib/exvar/exvar_test.go')
-rw-r--r--src/lib/exvar/exvar_test.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/lib/exvar/exvar_test.go b/src/lib/exvar/exvar_test.go
new file mode 100644
index 000000000..28fbf3cf2
--- /dev/null
+++ b/src/lib/exvar/exvar_test.go
@@ -0,0 +1,80 @@
+// 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.
+
+package exvar
+
+import (
+ "exvar";
+ "fmt";
+ "json";
+ "testing";
+)
+
+func TestInt(t *testing.T) {
+ reqs := NewInt("requests");
+ if reqs.i != 0 {
+ t.Errorf("reqs.i = %v, want 4", reqs.i)
+ }
+ if reqs != Get("requests").(*Int) {
+ t.Errorf("Get() failed.")
+ }
+
+ reqs.Add(1);
+ reqs.Add(3);
+ if reqs.i != 4 {
+ t.Errorf("reqs.i = %v, want 4", reqs.i)
+ }
+
+ if s := reqs.String(); s != "4" {
+ t.Errorf("reqs.String() = %q, want \"4\"", s);
+ }
+}
+
+func TestString(t *testing.T) {
+ name := NewString("my-name");
+ if name.s != "" {
+ t.Errorf("name.s = %q, want \"\"", name.s)
+ }
+
+ name.Set("Mike");
+ if name.s != "Mike" {
+ t.Errorf("name.s = %q, want \"Mike\"", name.s)
+ }
+
+ if s := name.String(); s != "\"Mike\"" {
+ t.Errorf("reqs.String() = %q, want \"\"Mike\"\"", s);
+ }
+}
+
+func TestMapCounter(t *testing.T) {
+ colours := NewMap("bike-shed-colours");
+
+ colours.Add("red", 1);
+ colours.Add("red", 2);
+ colours.Add("blue", 4);
+ if x := colours.m["red"].(*Int).i; x != 3 {
+ t.Errorf("colours.m[\"red\"] = %v, want 3", x)
+ }
+ if x := colours.m["blue"].(*Int).i; x != 4 {
+ t.Errorf("colours.m[\"blue\"] = %v, want 4", x)
+ }
+
+ // 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 {
+ t.Errorf("colours.String() isn't valid JSON: %v", errtok)
+ }
+ if j.Kind() != json.MapKind {
+ t.Error("colours.String() didn't produce a map.")
+ }
+ red := j.Get("red");
+ if red.Kind() != json.NumberKind {
+ t.Error("red.Kind() is not a NumberKind.")
+ }
+ if x := red.Number(); x != 3 {
+ t.Error("red = %v, want 3", x)
+ }
+}