diff --git a/internal/utils/jsonutil_test.go b/internal/utils/jsonutil_test.go index d627ade..22591a4 100644 --- a/internal/utils/jsonutil_test.go +++ b/internal/utils/jsonutil_test.go @@ -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: "value", - depth: -1, - expected: `{"root":{"child":"value"}}`, - }, - { - name: "XML with attributes", - input: "text", - depth: -1, - expected: `{"root":{"@attr":"value","child":"text"}}`, - }, - { - name: "XML with mixed content", - input: "\n text value\n more text\n", - depth: -1, - expected: `{"root":{"#text":"text\nmore text","child":"value"}}`, - }, - { - name: "Depth limited XML", - input: "valuetext", - depth: 2, - expected: `{"root":{"child1":{"grandchild":"value"},"child2":"text"}}`, - }, - { - name: "Depth 1 XML", - input: "valuetext", - depth: 1, - expected: `{"root":{"child1":"value","child2":"text"}}`, - }, - { - name: "Depth 0 XML", - input: "valuetext", - depth: 0, - expected: `{"root":"value\ntext"}`, - }, - { - name: "mixed text and xml", - input: `Thank you - -1. woop - - -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()) } } diff --git a/test/data/xml2json/formatted.json b/test/data/xml2json/formatted.json new file mode 100644 index 0000000..9b2e477 --- /dev/null +++ b/test/data/xml2json/formatted.json @@ -0,0 +1,5 @@ +{ + "root": { + "child": "value" + } +} diff --git a/test/data/xml2json/formatted2.json b/test/data/xml2json/formatted2.json new file mode 100644 index 0000000..85fbce5 --- /dev/null +++ b/test/data/xml2json/formatted2.json @@ -0,0 +1,6 @@ +{ + "root": { + "@attr": "value", + "child": "text" + } +} diff --git a/test/data/xml2json/formatted3.json b/test/data/xml2json/formatted3.json new file mode 100644 index 0000000..9a72296 --- /dev/null +++ b/test/data/xml2json/formatted3.json @@ -0,0 +1,6 @@ +{ + "root": { + "#text": "text\nmore text", + "child": "value" + } +} diff --git a/test/data/xml2json/formatted4.json b/test/data/xml2json/formatted4.json new file mode 100644 index 0000000..3a4c8d0 --- /dev/null +++ b/test/data/xml2json/formatted4.json @@ -0,0 +1,6 @@ +{ + "root": { + "child1": "value", + "child2": "text" + } +} diff --git a/test/data/xml2json/unformatted.xml b/test/data/xml2json/unformatted.xml new file mode 100644 index 0000000..41ccfa1 --- /dev/null +++ b/test/data/xml2json/unformatted.xml @@ -0,0 +1,3 @@ + + value + diff --git a/test/data/xml2json/unformatted2.xml b/test/data/xml2json/unformatted2.xml new file mode 100644 index 0000000..255f2dc --- /dev/null +++ b/test/data/xml2json/unformatted2.xml @@ -0,0 +1,3 @@ + + text + diff --git a/test/data/xml2json/unformatted3.xml b/test/data/xml2json/unformatted3.xml new file mode 100644 index 0000000..a460b05 --- /dev/null +++ b/test/data/xml2json/unformatted3.xml @@ -0,0 +1,4 @@ + + text value + more text + diff --git a/test/data/xml2json/unformatted4.xml b/test/data/xml2json/unformatted4.xml new file mode 100644 index 0000000..6fda50f --- /dev/null +++ b/test/data/xml2json/unformatted4.xml @@ -0,0 +1,6 @@ + + + value + + text +