1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
package storage_test
import (
"bytes"
"fmt"
"github.com/tomwright/dasel/v2"
"github.com/tomwright/dasel/v2/storage"
"io"
"reflect"
"testing"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/encoding/unicode"
)
var xmlBytes = []byte(`<user>
<name>Tom</name>
</user>
`)
var xmlMap = map[string]interface{}{
"user": map[string]interface{}{
"name": "Tom",
},
}
var encodedXmlMap = map[string]interface{}{
"user": map[string]interface{}{
"name": "Tõm",
},
}
func TestXMLParser_FromBytes(t *testing.T) {
got, err := (&storage.XMLParser{}).FromBytes(xmlBytes)
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}
if !reflect.DeepEqual(xmlMap, got.Interface()) {
t.Errorf("expected %v, got %v", xmlMap, got)
}
}
func TestXMLParser_FromBytes_Empty(t *testing.T) {
got, err := (&storage.XMLParser{}).FromBytes([]byte{})
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}
if !got.IsEmpty() {
t.Errorf("expected %v, got %v", nil, got)
}
}
func TestXMLParser_FromBytes_Error(t *testing.T) {
_, err := (&storage.XMLParser{}).FromBytes(nil)
if err == nil {
t.Errorf("expected error but got none")
return
}
_, err = (&storage.XMLParser{}).FromBytes(yamlBytes)
if err == nil {
t.Errorf("expected error but got none")
return
}
}
func TestXMLParser_ToBytes_Default(t *testing.T) {
got, err := (&storage.XMLParser{}).ToBytes(dasel.ValueOf(xmlMap))
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}
if !reflect.DeepEqual(xmlBytes, got) {
t.Errorf("expected %v, got %v", string(xmlBytes), string(got))
}
}
func TestXMLParser_ToBytes_SingleDocument(t *testing.T) {
got, err := (&storage.XMLParser{}).ToBytes(dasel.ValueOf(xmlMap).WithMetadata("isSingleDocument", true))
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}
if !reflect.DeepEqual(xmlBytes, got) {
t.Errorf("expected %v, got %v", string(xmlBytes), string(got))
}
}
func TestXMLParser_ToBytes_SingleDocument_Colourise(t *testing.T) {
got, err := (&storage.XMLParser{}).ToBytes(dasel.ValueOf(xmlMap).WithMetadata("isSingleDocument", true), storage.ColouriseOption(true))
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}
expBuf, _ := storage.Colourise(string(xmlBytes), "xml")
exp := expBuf.Bytes()
if !reflect.DeepEqual(exp, got) {
t.Errorf("expected %v, got %v", exp, got)
}
}
func TestXMLParser_ToBytes_MultiDocument(t *testing.T) {
got, err := (&storage.XMLParser{}).ToBytes(dasel.ValueOf([]interface{}{xmlMap, xmlMap}).WithMetadata("isMultiDocument", true))
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}
exp := append([]byte{}, xmlBytes...)
exp = append(exp, xmlBytes...)
if !reflect.DeepEqual(exp, got) {
t.Errorf("expected %v, got %v", string(exp), string(got))
}
}
func TestXMLParser_ToBytes_DefaultValue(t *testing.T) {
got, err := (&storage.XMLParser{}).ToBytes(dasel.ValueOf("asd"))
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}
exp := []byte(`asd
`)
if !reflect.DeepEqual(exp, got) {
t.Errorf("expected %v, got %v", string(exp), string(got))
}
}
func TestXMLParser_ToBytes_SingleDocumentValue(t *testing.T) {
got, err := (&storage.XMLParser{}).ToBytes(dasel.ValueOf("asd"))
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}
exp := []byte(`asd
`)
if !reflect.DeepEqual(exp, got) {
t.Errorf("expected %v, got %v", string(exp), string(got))
}
}
func TestXMLParser_ToBytes_MultiDocumentValue(t *testing.T) {
got, err := (&storage.XMLParser{}).ToBytes(dasel.ValueOf([]interface{}{"asd", "123"}).WithMetadata("isMultiDocument", true))
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}
exp := []byte(`asd
123
`)
if !reflect.DeepEqual(exp, got) {
t.Errorf("expected %v, got %v", string(exp), string(got))
}
}
func TestXMLParser_ToBytes_Entities(t *testing.T) {
bytes := []byte(`<systemList>
<system>
<command>sudo /home/fozz/RetroPie-Setup/retropie_packages.sh retropiemenu launch %ROM% </dev/tty >/dev/tty</command>
<extension>.rp .sh</extension>
<fullname>RetroPie</fullname>
<name>retropie</name>
<path>/home/fozz/RetroPie/retropiemenu</path>
<platform/>
<theme>retropie</theme>
</system>
</systemList>
`)
p := &storage.XMLParser{}
var doc interface{}
t.Run("FromBytes", func(t *testing.T) {
res, err := p.FromBytes(bytes)
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}
doc = res.Interface()
got := doc.(map[string]interface{})["systemList"].(map[string]interface{})["system"].(map[string]interface{})["command"]
exp := "sudo /home/fozz/RetroPie-Setup/retropie_packages.sh retropiemenu launch %ROM% </dev/tty >/dev/tty"
if exp != got {
t.Errorf("expected %s, got %s", exp, got)
}
})
t.Run("ToBytes", func(t *testing.T) {
gotBytes, err := p.ToBytes(dasel.ValueOf(doc))
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}
got := string(gotBytes)
exp := string(bytes)
if exp != got {
t.Errorf("expected %s, got %s", exp, got)
}
})
}
func TestXMLParser_DifferentEncodings(t *testing.T) {
newXmlBytes := func(newWriter func(io.Writer) io.Writer, encoding, text string) []byte {
const encodedXmlBytesFmt = `<?xml version='1.0' encoding='%s'?>`
const xmlBody = `<user><name>%s</name></user>`
var buf bytes.Buffer
w := newWriter(&buf)
fmt.Fprintf(w, xmlBody, text)
return []byte(fmt.Sprintf(encodedXmlBytesFmt, encoding) + buf.String())
}
testCases := []struct {
name string
xml []byte
}{
{
name: "supports ISO-8859-1",
xml: newXmlBytes(charmap.ISO8859_1.NewEncoder().Writer, "ISO-8859-1", "Tõm"),
},
{
name: "supports UTF-8",
xml: newXmlBytes(unicode.UTF8.NewEncoder().Writer, "UTF-8", "Tõm"),
},
{
name: "supports latin1",
xml: newXmlBytes(charmap.Windows1252.NewEncoder().Writer, "latin1", "Tõm"),
},
{
name: "supports UTF-16",
xml: newXmlBytes(unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewEncoder().Writer, "UTF-16", "Tõm"),
},
{
name: "supports UTF-16 (big endian)",
xml: newXmlBytes(unicode.UTF16(unicode.BigEndian, unicode.UseBOM).NewEncoder().Writer, "UTF-16BE", "Tõm"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got, err := (&storage.XMLParser{}).FromBytes(tc.xml)
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}
if !reflect.DeepEqual(encodedXmlMap, got.Interface()) {
t.Errorf("expected %v, got %v", encodedXmlMap, got)
}
})
}
}
|