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