diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2024-03-24 19:18:58 +0000 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2024-03-24 19:18:58 +0000 |
commit | 81dcb87b2158f625ca10a20df5a93a42bbcaf26b (patch) | |
tree | 24d80db1428e5a61d2d96d9c929eaa82b1e40b92 /json/write_test.go | |
parent | 0e4179fabbd0a66826f1375dae86ca7f681fb29d (diff) | |
download | stred-go-81dcb87b2158f625ca10a20df5a93a42bbcaf26b.tar |
Implements helper function navigateTo in json/write.go
Diffstat (limited to 'json/write_test.go')
-rw-r--r-- | json/write_test.go | 254 |
1 files changed, 88 insertions, 166 deletions
diff --git a/json/write_test.go b/json/write_test.go index 60ad609..508ccfa 100644 --- a/json/write_test.go +++ b/json/write_test.go @@ -5,179 +5,101 @@ import ( "main/walk" "strings" "testing" - "encoding/json" ) -type writeTester struct { - writer *JSONWriter - output *strings.Builder - t *testing.T -} - -func (t writeTester) write(value walk.Value, path ...interface{}) writeTester { - var pathValues []walk.Value - for _, segment := range path { - switch s := segment.(type) { - case int: - pathValues = append(pathValues, walk.NumberScalar(s)) - case string: - pathValues = append(pathValues, walk.StringStructure(s)) - default: - panic("Invalid path segment type") - } - } - - t.writer.Write(walk.WalkItem { - Value: []walk.Value{value}, - Path: pathValues, - }) - - return t -} - -func (t writeTester) expect(expected interface{}) writeTester { - t.writer.AssertDone() - output := t.output.String() - var actual interface{} - err := json.Unmarshal([]byte(output), &actual) - if err != nil { - t.t.Log("Produced invalid JSON:") - t.t.Log(output) - t.t.FailNow() - } - - expectedBytes, err1 := json.Marshal(expected) - actualBytes, err2 := json.Marshal(actual) - - if err1 != nil || err2 != nil { - panic("Error marshalling") - } - - expectedString := string(expectedBytes) - actualString := string(actualBytes) - - if expectedString != actualString { - t.t.Log("Expected:") - t.t.Log(expectedString) - t.t.Log("Found:") - t.t.Log(actualString) - t.t.FailNow() +func TestNavigateTo(t *testing.T) { + type testCase struct { + fromPath []walk.PathSegment + fromState JSONWriterState + toKeep int + toPath []walk.PathSegment + toState JSONWriterState + expected string } - return t -} - -func tester(t *testing.T) writeTester { - var output strings.Builder - return writeTester { - writer: NewJSONWriter(bufio.NewWriter(&output)), - output: &output, - t: t, - } -} - -func TestImplicitStructures(t *testing.T) { - tester(t).write( - walk.NullScalar{}, - 0, "test", 0, - ).expect( - []interface{}{ - map[string]interface{}{ - "test": []interface{}{ - nil, - }, - }, + tests := []testCase { + { + fromPath: []walk.PathSegment{}, + fromState: JSONWriterStateBeforeValue, + toKeep: 0, + toPath: []walk.PathSegment{"a", "b", "c"}, + toState: JSONWriterStateBeforeValue, + expected: `{"a":{"b":{"c":`, }, - ) -} - -func TestExplicitMap(t *testing.T) { - tester(t).write( - make(walk.MapStructure), - ).write( - walk.NullScalar{}, - "test", - ).expect( - map[string]interface{}{ - "test": nil, + { + fromPath: []walk.PathSegment{}, + fromState: JSONWriterStateBeforeValue, + toKeep: 0, + toPath: []walk.PathSegment{0, "a", "a", 0, 1}, + toState: JSONWriterStateInMap, + expected: `[{"a":{"a":[[{`, }, - ) -} - -func TestExplicitNested(t *testing.T) { - tester(t).write( - make(walk.MapStructure), - ).write( - make(walk.MapStructure), - "first", - ).write( - make(walk.MapStructure), - "first", "second", - ).write( - walk.StringStructure("test"), - "first", "second", "third", - ).expect( - map[string]interface{}{ - "first": map[string]interface{}{ - "second": map[string]interface{}{ - "third": "test", - }, - }, + { + fromPath: []walk.PathSegment{}, + fromState: JSONWriterStateInMap, + toKeep: 0, + toPath: []walk.PathSegment{}, + toState: JSONWriterStateInArray, + expected: "}\n[", }, - ) -} - -func TestArrayOfMaps(t *testing.T) { - tester(t).write( - walk.ArrayStructure{}, - ).write( - make(walk.MapStructure), - 0, - ).write( - walk.NumberScalar(0), - 0, "number", - ).write( - make(walk.MapStructure), - 1, - ).write( - walk.NumberScalar(1), - 1, "nested", "number", - ).write( - make(walk.MapStructure), - 2, - ).write( - walk.NumberScalar(2), - 2, "number", - ).expect( - []interface{}{ - map[string]interface{}{ - "number": 0, - }, - map[string]interface{}{ - "nested": map[string]interface{}{ - "number": 1, - }, - }, - map[string]interface{}{ - "number": 2, - }, + { + fromPath: []walk.PathSegment{0, 0}, + fromState: JSONWriterStateBeforeValue, + toKeep: 2, + toPath: []walk.PathSegment{"a"}, + toState: JSONWriterStateInArray, + expected: `{"a":[`, }, - ) -} - -func TestStructures1(t *testing.T) { - tester(t).write( - make(walk.MapStructure), - ).write( - make(walk.MapStructure), - "map", - ).write( - walk.ArrayStructure{}, - "array", - ).expect( - map[string]interface{}{ - "map": map[string]interface{}{}, - "array": []interface{}{}, + { + fromPath: []walk.PathSegment{"a", "b"}, + fromState: JSONWriterStateAfterValue, + toKeep: 1, + toPath: []walk.PathSegment{"c"}, + toState: JSONWriterStateBeforeValue, + expected: `,"c":`, }, - ) + { + fromPath: []walk.PathSegment{0, "a"}, + fromState: JSONWriterStateInArray, + toKeep: 0, + toPath: []walk.PathSegment{"b", 1}, + toState: JSONWriterStateInMap, + expected: `]}]` + "\n" + `{"b":[{`, + }, + { + fromPath: []walk.PathSegment{"a", "b", "c", "d", "e"}, + fromState: JSONWriterStateAfterValue, + toKeep: 2, + toPath: []walk.PathSegment{"f", "g", "h"}, + toState: JSONWriterStateBeforeValue, + expected: `}},"f":{"g":{"h":`, + }, + { + fromPath: []walk.PathSegment{"a", 0, "b"}, + fromState: JSONWriterStateAfterValue, + toKeep: 2, + toPath: []walk.PathSegment{0}, + toState: JSONWriterStateBeforeValue, + expected: `},[`, + }, + } + + for i, test := range tests { + var writer strings.Builder + jsonWriter := &JSONWriter { + path: test.fromPath, + writer: bufio.NewWriter(&writer), + state: test.fromState, + } + jsonWriter.navigateTo( + test.toKeep, + test.toPath, + test.toState, + ) + jsonWriter.writer.Flush() + res := writer.String() + if res != test.expected { + t.Errorf(`Test %d: Expected '%s' found '%s'`, i, test.expected, res) + } + } } |