add unit-tests (#137)
All checks were successful
coverage / build (push) Successful in 1m36s
test / test (push) Successful in 1m54s

This commit is contained in:
2025-05-14 01:58:09 +05:00
committed by GitHub
parent b020114dc5
commit 9f81375698
5 changed files with 122 additions and 4 deletions

4
go.mod
View File

@@ -2,18 +2,20 @@ module go.unistack.org/micro-codec-xml/v4
go 1.22.0
require (
github.com/stretchr/testify v1.10.0
go.unistack.org/micro-proto/v4 v4.1.0
go.unistack.org/micro/v4 v4.1.3
)
require (
github.com/ash3in/uuidv8 v1.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/matoous/go-nanoid v1.5.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
google.golang.org/protobuf v1.36.3 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

6
go.sum
View File

@@ -3,6 +3,8 @@ github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ
github.com/ash3in/uuidv8 v1.2.0 h1:2oogGdtCPwaVtyvPPGin4TfZLtOGE5F+W++E880G6SI=
github.com/ash3in/uuidv8 v1.2.0/go.mod h1:BnU0wJBxnzdEKmVg4xckBkD+VZuecTFTUP3M0dWgyY4=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
@@ -14,9 +16,13 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/matoous/go-nanoid v1.5.1 h1:aCjdvTyO9LLnTIi0fgdXhOPPvOHjpXN6Ik9DaNjIct4=
github.com/matoous/go-nanoid v1.5.1/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.unistack.org/micro-proto/v4 v4.1.0 h1:qPwL2n/oqh9RE3RTTDgt28XK3QzV597VugQPaw9lKUk=
go.unistack.org/micro-proto/v4 v4.1.0/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
go.unistack.org/micro/v4 v4.1.3 h1:9QHiLHBTfPtfKyoY3HsLPNVSK0wlAAoMV+9saLz2iR4=

40
reader_test.go Normal file
View File

@@ -0,0 +1,40 @@
package xml
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestReader_Read(t *testing.T) {
tests := []struct {
name string
input []byte
expected []byte
}{
{
name: "with replacement",
input: []byte(`<?xml version="1.1" encoding="UTF-8"?><root name="NAME"><item>ITEM</item></root>`),
expected: []byte(`<?xml version="1.0" encoding="UTF-8"?><root name="NAME"><item>ITEM</item></root>`),
},
{
name: "without replacement",
input: []byte(`<?xml version="1.0" encoding="UTF-8"?><root name="NAME"><item>ITEM</item></root>`),
expected: []byte(`<?xml version="1.0" encoding="UTF-8"?><root name="NAME"><item>ITEM</item></root>`),
},
{
name: "check invalid replacement",
input: []byte(`<?xml version="1.0" encoding="UTF-8"?><root version="1.1"><item>ITEM</item></root>`),
expected: []byte(`<?xml version="1.0" encoding="UTF-8"?><root version="1.1"><item>ITEM</item></root>`),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := make([]byte, len(tt.input))
n, err := newReader(tt.input).Read(buf)
require.NoError(t, err)
require.Equal(t, tt.expected, buf[:n])
})
}
}

6
xml.go
View File

@@ -134,9 +134,9 @@ func newReader(b []byte) io.Reader {
}
var (
srcP = []byte(`version="1.1"`)
srcP = []byte(`<?xml version="1.1"`)
srcL = len(srcP)
dstP = []byte(`version="1.0"`)
dstP = []byte(`<?xml version="1.0"`)
)
func (r *reader) Read(b []byte) (n int, err error) {
@@ -146,7 +146,7 @@ func (r *reader) Read(b []byte) (n int, err error) {
r.prevRune = -1
n = copy(b, r.s[r.i:])
if !r.skip {
if idx := bytes.Index(b, srcP); idx > 0 {
if idx := bytes.Index(b, srcP); idx >= 0 {
copy(b[idx:idx+srcL], dstP)
r.skip = true
}

70
xml_test.go Normal file
View File

@@ -0,0 +1,70 @@
package xml
import (
"encoding/xml"
"testing"
"github.com/stretchr/testify/require"
"go.unistack.org/micro/v4/codec"
)
func TestXmlCodec_Unmarshal(t *testing.T) {
type result struct {
XMLName xml.Name `xml:"root"`
Name string `xml:"name,attr"`
Item string `xml:"item"`
}
tests := []struct {
name string
input []byte
isXML11 bool
expected result
}{
{
name: "without version",
input: []byte(`<root name="NAME"><item>ITEM</item></root>`),
expected: result{
XMLName: xml.Name{Local: "root"},
Name: "NAME",
Item: "ITEM",
},
},
{
name: "version 1.0",
input: []byte(`<?xml version="1.0" encoding="UTF-8"?><root name="NAME"><item>ITEM</item></root>`),
expected: result{
XMLName: xml.Name{Local: "root"},
Name: "NAME",
Item: "ITEM",
},
},
{
name: "version 1.1",
input: []byte(`<?xml version="1.1" encoding="UTF-8"?><root name="NAME"><item>ITEM</item></root>`),
isXML11: true,
expected: result{
XMLName: xml.Name{Local: "root"},
Name: "NAME",
Item: "ITEM",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var (
r result
opts []codec.Option
)
if tt.isXML11 {
opts = append(opts, UnmarshalXML11(true))
}
err := NewCodec(opts...).Unmarshal(tt.input, &r)
require.NoError(t, err)
require.Equal(t, tt.expected, r)
})
}
}