<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/json/write_test.go
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2024-03-25 14:35:09 +0000
committerCharlie Stanton <charlie@shtanton.xyz>2024-03-25 14:35:09 +0000
commitc6c1c67402f89c94fee040c2c740d96e61cab669 (patch)
treed829c7990dd327b543fe943dc5b01752fa5ccdea /json/write_test.go
parent81dcb87b2158f625ca10a20df5a93a42bbcaf26b (diff)
downloadstred-go-c6c1c67402f89c94fee040c2c740d96e61cab669.tar
Finish implementing new JSON writer
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)
+ }
+ }
+}