mirror of
				https://github.com/TomWright/dasel.git
				synced 2022-05-22 02:32:45 +03:00 
			
		
		
		
	add tests for other XML encodings
This commit is contained in:
		
							
								
								
									
										3
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								go.mod
									
									
									
									
									
								
							| @@ -7,6 +7,8 @@ require ( | ||||
| 	github.com/clbanning/mxj/v2 v2.3.3-0.20201214204241-e937bdee5a3e | ||||
| 	github.com/pelletier/go-toml v1.9.3 | ||||
| 	github.com/spf13/cobra v1.2.1 | ||||
| 	golang.org/x/net v0.0.0-20210924151903-3ad01bbaa167 | ||||
| 	golang.org/x/text v0.3.7 | ||||
| 	gopkg.in/yaml.v2 v2.4.0 | ||||
| ) | ||||
|  | ||||
| @@ -14,6 +16,5 @@ require ( | ||||
| 	github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect | ||||
| 	github.com/dlclark/regexp2 v1.4.0 // indirect | ||||
| 	github.com/inconshreveable/mousetrap v1.0.0 // indirect | ||||
| 	github.com/paulrosania/go-charset v0.0.0-20190326053356-55c9d7a5834c // indirect | ||||
| 	github.com/spf13/pflag v1.0.5 // indirect | ||||
| ) | ||||
|   | ||||
							
								
								
									
										8
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								go.sum
									
									
									
									
									
								
							| @@ -206,8 +206,6 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ | ||||
| github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= | ||||
| github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= | ||||
| github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= | ||||
| github.com/paulrosania/go-charset v0.0.0-20190326053356-55c9d7a5834c h1:P6XGcuPTigoHf4TSu+3D/7QOQ1MbL6alNwrGhcW7sKw= | ||||
| github.com/paulrosania/go-charset v0.0.0-20190326053356-55c9d7a5834c/go.mod h1:YnNlZP7l4MhyGQ4CBRwv6ohZTPrUJJZtEv4ZgADkbs4= | ||||
| github.com/pelletier/go-toml v1.9.3 h1:zeC5b1GviRUyKYd6OJPvBU/mcVDVoL1OhT17FCt5dSQ= | ||||
| github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= | ||||
| github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||||
| @@ -338,6 +336,8 @@ golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v | ||||
| golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= | ||||
| golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc= | ||||
| golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= | ||||
| golang.org/x/net v0.0.0-20210924151903-3ad01bbaa167 h1:eDd+TJqbgfXruGQ5sJRU7tEtp/58OAx4+Ayjxg4SM+4= | ||||
| golang.org/x/net v0.0.0-20210924151903-3ad01bbaa167/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= | ||||
| golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= | ||||
| golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= | ||||
| golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= | ||||
| @@ -403,6 +403,7 @@ golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7w | ||||
| golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||||
| golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||||
| golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||||
| golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||||
| golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE= | ||||
| golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||||
| golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||||
| @@ -413,6 +414,9 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= | ||||
| golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||||
| golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||||
| golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||||
| golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||||
| golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= | ||||
| golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= | ||||
| golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= | ||||
| golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= | ||||
| golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= | ||||
|   | ||||
| @@ -6,8 +6,7 @@ import ( | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/clbanning/mxj/v2" | ||||
| 	"github.com/paulrosania/go-charset/charset" | ||||
| 	_ "github.com/paulrosania/go-charset/data" | ||||
| 	"golang.org/x/net/html/charset" | ||||
| ) | ||||
|  | ||||
| func init() { | ||||
| @@ -15,7 +14,7 @@ func init() { | ||||
| 	mxj.XMLEscapeCharsDecoder(true) | ||||
|  | ||||
| 	// Required for https://github.com/TomWright/dasel/issues/164 | ||||
| 	mxj.XmlCharsetReader = charset.NewReader | ||||
| 	mxj.XmlCharsetReader = charset.NewReaderLabel | ||||
|  | ||||
| 	registerReadParser([]string{"xml"}, []string{".xml"}, &XMLParser{}) | ||||
| 	registerWriteParser([]string{"xml"}, []string{".xml"}, &XMLParser{}) | ||||
|   | ||||
| @@ -1,9 +1,15 @@ | ||||
| package storage_test | ||||
|  | ||||
| import ( | ||||
| 	"github.com/tomwright/dasel/internal/storage" | ||||
| 	"bytes" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"reflect" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/tomwright/dasel/internal/storage" | ||||
| 	"golang.org/x/text/encoding/charmap" | ||||
| 	"golang.org/x/text/encoding/unicode" | ||||
| ) | ||||
|  | ||||
| var xmlBytes = []byte(`<user> | ||||
| @@ -15,6 +21,11 @@ var xmlMap = 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) | ||||
| @@ -177,3 +188,56 @@ func TestXMLParser_ToBytes_Entities(t *testing.T) { | ||||
| 		} | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| 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(&storage.BasicSingleDocument{Value: encodedXmlMap}, got) { | ||||
| 				t.Errorf("expected %v, got %v", encodedXmlMap, got) | ||||
| 			} | ||||
| 		}) | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Breno Loyola
					Breno Loyola