Skip to content

Commit

Permalink
TECH Extract xml2json test data into files for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
sibprogrammer committed Dec 14, 2024
1 parent d086312 commit e796262
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 80 deletions.
111 changes: 31 additions & 80 deletions internal/utils/jsonutil_test.go
Original file line number Diff line number Diff line change
@@ -1,96 +1,47 @@
package utils

import (
"bytes"
"encoding/json"
"os"
"path"
"strings"
"testing"

"github.com/antchfx/xmlquery"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
)

func TestNodeToJSON(t *testing.T) {
func TestXmlToJSON(t *testing.T) {
tests := []struct {
name string
input string
depth int
expected string
unformattedFile string
expectedFile string
depth int
}{
{
name: "Simple XML",
input: "<root><child>value</child></root>",
depth: -1,
expected: `{"root":{"child":"value"}}`,
},
{
name: "XML with attributes",
input: "<root attr=\"value\"><child>text</child></root>",
depth: -1,
expected: `{"root":{"@attr":"value","child":"text"}}`,
},
{
name: "XML with mixed content",
input: "<root>\n text <child>value</child>\n more text\n</root>",
depth: -1,
expected: `{"root":{"#text":"text\nmore text","child":"value"}}`,
},
{
name: "Depth limited XML",
input: "<root><child1><grandchild>value</grandchild></child1><child2>text</child2></root>",
depth: 2,
expected: `{"root":{"child1":{"grandchild":"value"},"child2":"text"}}`,
},
{
name: "Depth 1 XML",
input: "<root><child1><grandchild>value</grandchild></child1><child2>text</child2></root>",
depth: 1,
expected: `{"root":{"child1":"value","child2":"text"}}`,
},
{
name: "Depth 0 XML",
input: "<root><child1><grandchild>value</grandchild></child1><child2>text</child2></root>",
depth: 0,
expected: `{"root":"value\ntext"}`,
},
{
name: "mixed text and xml",
input: `Thank you
<thinking>
1. woop
</thinking>
Bye`,
expected: `{"#text":"Thank you\nBye","thinking":"1. woop"}`,
},
{"unformatted.xml", "formatted.json", -1},
{"unformatted2.xml", "formatted2.json", -1},
{"unformatted3.xml", "formatted3.json", -1},
{"unformatted4.xml", "formatted4.json", 1},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
doc, err := xmlquery.Parse(strings.NewReader(tt.input))
if err != nil {
t.Fatalf("Failed to parse XML: %v", err)
}

result := NodeToJSON(doc, tt.depth)
resultJSON, err := json.Marshal(result)
if err != nil {
t.Fatalf("Failed to marshal result to JSON: %v", err)
}

var resultMap, expectedMap map[string]interface{}
err = json.Unmarshal(resultJSON, &resultMap)
if err != nil {
t.Fatalf("Failed to unmarshal result JSON: %v", err)
}
err = json.Unmarshal([]byte(tt.expected), &expectedMap)
if err != nil {
t.Fatalf("Failed to unmarshal expected JSON: %v", err)
}

t.Log(string(resultJSON))
if diff := cmp.Diff(expectedMap, resultMap); diff != "" {
t.Errorf("NodeToJSON mismatch (-want +got):\n%s", diff)
}
})
for _, testCase := range tests {
inputFileName := path.Join("..", "..", "test", "data", "xml2json", testCase.unformattedFile)
unformattedXmlReader := getFileReader(inputFileName)

outputFileName := path.Join("..", "..", "test", "data", "xml2json", testCase.expectedFile)
data, jsonReadErr := os.ReadFile(outputFileName)
assert.Nil(t, jsonReadErr)
expectedJson := string(data)

node, parseErr := xmlquery.Parse(unformattedXmlReader)
assert.Nil(t, parseErr)
result := NodeToJSON(node, testCase.depth)
jsonData, jsonMarshalErr := json.Marshal(result)
assert.Nil(t, jsonMarshalErr)

output := new(strings.Builder)
formatErr := FormatJson(bytes.NewReader(jsonData), output, " ", ColorsDisabled)
assert.Nil(t, formatErr)
assert.Equal(t, expectedJson, output.String())
}
}
5 changes: 5 additions & 0 deletions test/data/xml2json/formatted.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"root": {
"child": "value"
}
}
6 changes: 6 additions & 0 deletions test/data/xml2json/formatted2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"root": {
"@attr": "value",
"child": "text"
}
}
6 changes: 6 additions & 0 deletions test/data/xml2json/formatted3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"root": {
"#text": "text\nmore text",
"child": "value"
}
}
6 changes: 6 additions & 0 deletions test/data/xml2json/formatted4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"root": {
"child1": "value",
"child2": "text"
}
}
3 changes: 3 additions & 0 deletions test/data/xml2json/unformatted.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<root>
<child>value</child>
</root>
3 changes: 3 additions & 0 deletions test/data/xml2json/unformatted2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<root attr="value">
<child>text</child>
</root>
4 changes: 4 additions & 0 deletions test/data/xml2json/unformatted3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<root>
text <child>value</child>
more text
</root>
6 changes: 6 additions & 0 deletions test/data/xml2json/unformatted4.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<root>
<child1>
<grandchild>value</grandchild>
</child1>
<child2>text</child2>
</root>

0 comments on commit e796262

Please sign in to comment.