summaryrefslogtreecommitdiff
path: root/src/pkg/json/struct_test.go
diff options
context:
space:
mode:
authorMichael Hoisie <hoisie@gmail.com>2009-11-19 20:45:03 -0800
committerMichael Hoisie <hoisie@gmail.com>2009-11-19 20:45:03 -0800
commita97dd86d35e662c7fb0aa5b7d471f5801a092148 (patch)
treed22fc921709218819f2626ffeb75c3bb2f5b842b /src/pkg/json/struct_test.go
parentdda9fcd01acd38e9cf35deb89a452c0fb80c6ddc (diff)
downloadgolang-a97dd86d35e662c7fb0aa5b7d471f5801a092148.tar.gz
Add json.Marshal to json package
R=rsc CC=golang-dev http://codereview.appspot.com/157068 Committer: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/pkg/json/struct_test.go')
-rw-r--r--src/pkg/json/struct_test.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/pkg/json/struct_test.go b/src/pkg/json/struct_test.go
index 89d363d9e..b71c31a85 100644
--- a/src/pkg/json/struct_test.go
+++ b/src/pkg/json/struct_test.go
@@ -5,6 +5,7 @@
package json
import (
+ "bytes";
"reflect";
"strconv";
"testing";
@@ -157,3 +158,48 @@ func TestIssue114(t *testing.T) {
}
}
}
+
+type marshalTest struct {
+ val interface{};
+ out string;
+}
+
+var marshalTests = []marshalTest{
+ // basic string
+ marshalTest{true, "true"},
+ marshalTest{false, "false"},
+ marshalTest{123, "123"},
+ marshalTest{0.1, "0.1"},
+ marshalTest{1e-10, "1e-10"},
+ marshalTest{"teststring", `"teststring"`},
+ marshalTest{[4]int{1, 2, 3, 4}, "[1,2,3,4]"},
+ marshalTest{[]int{1, 2, 3, 4}, "[1,2,3,4]"},
+ marshalTest{[][]int{[]int{1, 2}, []int{3, 4}}, "[[1,2],[3,4]]"},
+ marshalTest{map[string]string{"one": "one"}, `{"one":"one"}`},
+ marshalTest{map[string]int{"one": 1}, `{"one":1}`},
+ marshalTest{struct{}{}, "{}"},
+ marshalTest{struct{ a int }{1}, `{"a":1}`},
+ marshalTest{struct {
+ a int;
+ b string;
+ }{1, "hello"},
+ `{"a":1,"b":"hello"}`,
+ },
+ marshalTest{map[string][]int{"3": []int{1, 2, 3}}, `{"3":[1,2,3]}`},
+}
+
+func TestJsonMarshal(t *testing.T) {
+ for _, tt := range marshalTests {
+ var buf bytes.Buffer;
+
+ err := Marshal(&buf, tt.val);
+ if err != nil {
+ t.Errorf("Error converting %s to JSON: \n", err.String())
+ }
+
+ s := buf.String();
+ if s != tt.out {
+ t.Errorf("Error converting to JSON. Expected: %q Actual %q\n", tt.out, s)
+ }
+ }
+}