<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/json/write_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'json/write_test.go')
-rw-r--r--json/write_test.go141
1 files changed, 141 insertions, 0 deletions
diff --git a/json/write_test.go b/json/write_test.go
index 508ccfa..05b228e 100644
--- a/json/write_test.go
+++ b/json/write_test.go
@@ -103,3 +103,144 @@ func TestNavigateTo(t *testing.T) {
}
}
}
+
+func TestWrite(t *testing.T) {
+ type testCase struct {
+ values []walk.Value
+ expected string
+ }
+
+ tests := []testCase {
+ {
+ values: []walk.Value {
+ walk.MapValue {{
+ Key: "a",
+ Value: walk.MapValue {{
+ Key: "b",
+ Value: walk.StringValue("c"),
+ }},
+ }},
+ },
+ expected: `{"a":{"b":"c"}}`,
+ },
+ {
+ values: []walk.Value {
+ walk.MapValue {{
+ Key: "a",
+ Value: walk.MapValue {{
+ Key: "b",
+ Value: walk.StringValue("c"),
+ }},
+ }},
+ walk.MapValue {{
+ Key: "a",
+ Value: walk.MapValue {{
+ Key: "d",
+ Value: walk.NullValue{},
+ }},
+ }},
+ walk.MapValue {{
+ Key: "a",
+ Value: walk.MapValue{},
+ }},
+ walk.MapValue {{
+ Key: "a",
+ Value: walk.MapValue {{
+ Key: "e",
+ Value: walk.NumberValue(-4.3),
+ }},
+ }},
+ },
+ expected: `{"a":{"b":"c","d":null,"e":-4.3}}`,
+ },
+ {
+ values: []walk.Value {
+ walk.MapValue {{
+ Key: "a",
+ Value: walk.MapValue{{
+ Key: "aa",
+ Value: walk.StringValue("aav"),
+ }},
+ }},
+ walk.MapValue {{
+ Key: "a",
+ Value: walk.MapValue{},
+ }, {
+ Key: "b",
+ Value: walk.MapValue {{
+ Key: "bb",
+ Value: walk.StringValue("bbv"),
+ }},
+ }, {
+ Key: "c",
+ Value: walk.MapValue{},
+ }},
+ walk.MapValue {{
+ Key: "c",
+ Value: walk.MapValue {{
+ Key: "cc",
+ Value: walk.StringValue("ccv"),
+ }},
+ }},
+ },
+ expected: `{"a":{"aa":"aav"},"b":{"bb":"bbv"},"c":{"cc":"ccv"}}`,
+ },
+ {
+ values: []walk.Value {
+ walk.ArrayValue {{
+ Index: 0,
+ Value: walk.ArrayValue {{
+ Index: 5,
+ Value: walk.NumberValue(100),
+ }},
+ }},
+ walk.ArrayValue {{
+ Index: 0,
+ Value: walk.ArrayValue{},
+ }, {
+ Index: 0,
+ Value: walk.ArrayValue{},
+ }},
+ walk.ArrayValue {{
+ Index: 0,
+ Value: walk.NullValue{},
+ }, {
+ Index: 0,
+ Value: walk.ArrayValue{},
+ }},
+ walk.ArrayValue {{
+ Index: 0,
+ Value: walk.ArrayValue{},
+ }},
+ walk.ArrayValue {{
+ Index: 0,
+ Value: walk.ArrayValue {{
+ Index: 200,
+ Value: walk.NumberValue(200),
+ }},
+ }},
+ },
+ expected: `[[100],[],null,[200]]`,
+ },
+ }
+
+ for i, test := range tests {
+ var writer strings.Builder
+ jsonWriter := &JSONWriter {
+ path: nil,
+ writer: bufio.NewWriter(&writer),
+ state: JSONWriterStateBeforeValue,
+ }
+
+ for _, value := range test.values {
+ jsonWriter.Write(value)
+ }
+ jsonWriter.AssertDone()
+
+ jsonWriter.writer.Flush()
+ res := writer.String()
+ if res != test.expected {
+ t.Errorf(`Test %d: Expected '%s' found '%s'`, i, test.expected, res)
+ }
+ }
+}