@@ -31,10 +31,10 @@ func resetDefaultCharsetsOrder() {
3131}
3232
3333func TestMaybeRemoveBOM (t * testing.T ) {
34- res := MaybeRemoveBOM ([]byte {0xc3 , 0xa1 , 0xc3 , 0xa9 , 0xc3 , 0xad , 0xc3 , 0xb3 , 0xc3 , 0xba }, ConvertOpts {})
34+ res := maybeRemoveBOM ([]byte {0xc3 , 0xa1 , 0xc3 , 0xa9 , 0xc3 , 0xad , 0xc3 , 0xb3 , 0xc3 , 0xba }, ConvertOpts {})
3535 assert .Equal (t , []byte {0xc3 , 0xa1 , 0xc3 , 0xa9 , 0xc3 , 0xad , 0xc3 , 0xb3 , 0xc3 , 0xba }, res )
3636
37- res = MaybeRemoveBOM ([]byte {0xef , 0xbb , 0xbf , 0xc3 , 0xa1 , 0xc3 , 0xa9 , 0xc3 , 0xad , 0xc3 , 0xb3 , 0xc3 , 0xba }, ConvertOpts {})
37+ res = maybeRemoveBOM ([]byte {0xef , 0xbb , 0xbf , 0xc3 , 0xa1 , 0xc3 , 0xa9 , 0xc3 , 0xad , 0xc3 , 0xb3 , 0xc3 , 0xba }, ConvertOpts {})
3838 assert .Equal (t , []byte {0xc3 , 0xa1 , 0xc3 , 0xa9 , 0xc3 , 0xad , 0xc3 , 0xb3 , 0xc3 , 0xba }, res )
3939}
4040
@@ -45,63 +45,54 @@ func TestToUTF8(t *testing.T) {
4545 // locale, so some conversions might behave differently. For that reason, we don't
4646 // depend on particular conversions but in expected behaviors.
4747
48- res , err := ToUTF8 ([]byte {0x41 , 0x42 , 0x43 }, ConvertOpts {})
49- assert .NoError (t , err )
50- assert .Equal (t , "ABC" , res )
48+ res := ToUTF8 ([]byte {0x41 , 0x42 , 0x43 }, ConvertOpts {})
49+ assert .Equal (t , "ABC" , string (res ))
5150
5251 // "áéíóú"
53- res , err = ToUTF8 ([]byte {0xc3 , 0xa1 , 0xc3 , 0xa9 , 0xc3 , 0xad , 0xc3 , 0xb3 , 0xc3 , 0xba }, ConvertOpts {})
54- assert .NoError (t , err )
52+ res = ToUTF8 ([]byte {0xc3 , 0xa1 , 0xc3 , 0xa9 , 0xc3 , 0xad , 0xc3 , 0xb3 , 0xc3 , 0xba }, ConvertOpts {})
5553 assert .Equal (t , []byte {0xc3 , 0xa1 , 0xc3 , 0xa9 , 0xc3 , 0xad , 0xc3 , 0xb3 , 0xc3 , 0xba }, []byte (res ))
5654
5755 // "áéíóú"
58- res , err = ToUTF8 ([]byte {
56+ res = ToUTF8 ([]byte {
5957 0xef , 0xbb , 0xbf , 0xc3 , 0xa1 , 0xc3 , 0xa9 , 0xc3 , 0xad , 0xc3 , 0xb3 ,
6058 0xc3 , 0xba ,
6159 }, ConvertOpts {})
62- assert .NoError (t , err )
6360 assert .Equal (t , []byte {0xc3 , 0xa1 , 0xc3 , 0xa9 , 0xc3 , 0xad , 0xc3 , 0xb3 , 0xc3 , 0xba }, []byte (res ))
6461
65- res , err = ToUTF8 ([]byte {
62+ res = ToUTF8 ([]byte {
6663 0x48 , 0x6F , 0x6C , 0x61 , 0x2C , 0x20 , 0x61 , 0x73 , 0xED , 0x20 , 0x63 ,
6764 0xF3 , 0x6D , 0x6F , 0x20 , 0xF1 , 0x6F , 0x73 , 0x41 , 0x41 , 0x41 , 0x2e ,
6865 }, ConvertOpts {})
69- assert .NoError (t , err )
7066 stringMustStartWith (t , "Hola," , res )
7167 stringMustEndWith (t , "AAA." , res )
7268
73- res , err = ToUTF8 ([]byte {
69+ res = ToUTF8 ([]byte {
7470 0x48 , 0x6F , 0x6C , 0x61 , 0x2C , 0x20 , 0x61 , 0x73 , 0xED , 0x20 , 0x63 ,
7571 0xF3 , 0x6D , 0x6F , 0x20 , 0x07 , 0xA4 , 0x6F , 0x73 , 0x41 , 0x41 , 0x41 , 0x2e ,
7672 }, ConvertOpts {})
77- assert .NoError (t , err )
7873 stringMustStartWith (t , "Hola," , res )
7974 stringMustEndWith (t , "AAA." , res )
8075
81- res , err = ToUTF8 ([]byte {
76+ res = ToUTF8 ([]byte {
8277 0x48 , 0x6F , 0x6C , 0x61 , 0x2C , 0x20 , 0x61 , 0x73 , 0xED , 0x20 , 0x63 ,
8378 0xF3 , 0x6D , 0x6F , 0x20 , 0x81 , 0xA4 , 0x6F , 0x73 , 0x41 , 0x41 , 0x41 , 0x2e ,
8479 }, ConvertOpts {})
85- assert .NoError (t , err )
8680 stringMustStartWith (t , "Hola," , res )
8781 stringMustEndWith (t , "AAA." , res )
8882
8983 // Japanese (Shift-JIS)
9084 // 日属秘ぞしちゅ。
91- res , err = ToUTF8 ([]byte {
85+ res = ToUTF8 ([]byte {
9286 0x93 , 0xFA , 0x91 , 0xAE , 0x94 , 0xE9 , 0x82 , 0xBC , 0x82 , 0xB5 , 0x82 ,
9387 0xBF , 0x82 , 0xE3 , 0x81 , 0x42 ,
9488 }, ConvertOpts {})
95- assert .NoError (t , err )
9689 assert .Equal (t , []byte {
9790 0xE6 , 0x97 , 0xA5 , 0xE5 , 0xB1 , 0x9E , 0xE7 , 0xA7 , 0x98 , 0xE3 ,
9891 0x81 , 0x9E , 0xE3 , 0x81 , 0x97 , 0xE3 , 0x81 , 0xA1 , 0xE3 , 0x82 , 0x85 , 0xE3 , 0x80 , 0x82 ,
99- },
100- []byte (res ))
92+ }, res )
10193
102- res , err = ToUTF8 ([]byte {0x00 , 0x00 , 0x00 , 0x00 }, ConvertOpts {})
103- assert .NoError (t , err )
104- assert .Equal (t , []byte {0x00 , 0x00 , 0x00 , 0x00 }, []byte (res ))
94+ res = ToUTF8 ([]byte {0x00 , 0x00 , 0x00 , 0x00 }, ConvertOpts {})
95+ assert .Equal (t , []byte {0x00 , 0x00 , 0x00 , 0x00 }, res )
10596}
10697
10798func TestToUTF8WithFallback (t * testing.T ) {
@@ -153,43 +144,44 @@ func TestToUTF8WithFallback(t *testing.T) {
153144
154145func TestToUTF8DropErrors (t * testing.T ) {
155146 resetDefaultCharsetsOrder ()
147+
156148 // "ABC"
157- res := ToUTF8DropErrors ([]byte {0x41 , 0x42 , 0x43 }, ConvertOpts {} )
149+ res := ToUTF8DropErrors ([]byte {0x41 , 0x42 , 0x43 })
158150 assert .Equal (t , []byte {0x41 , 0x42 , 0x43 }, res )
159151
160152 // "áéíóú"
161- res = ToUTF8DropErrors ([]byte {0xc3 , 0xa1 , 0xc3 , 0xa9 , 0xc3 , 0xad , 0xc3 , 0xb3 , 0xc3 , 0xba }, ConvertOpts {} )
153+ res = ToUTF8DropErrors ([]byte {0xc3 , 0xa1 , 0xc3 , 0xa9 , 0xc3 , 0xad , 0xc3 , 0xb3 , 0xc3 , 0xba })
162154 assert .Equal (t , []byte {0xc3 , 0xa1 , 0xc3 , 0xa9 , 0xc3 , 0xad , 0xc3 , 0xb3 , 0xc3 , 0xba }, res )
163155
164156 // UTF8 BOM + "áéíóú"
165- res = ToUTF8DropErrors ([]byte {0xef , 0xbb , 0xbf , 0xc3 , 0xa1 , 0xc3 , 0xa9 , 0xc3 , 0xad , 0xc3 , 0xb3 , 0xc3 , 0xba }, ConvertOpts {} )
157+ res = ToUTF8DropErrors ([]byte {0xef , 0xbb , 0xbf , 0xc3 , 0xa1 , 0xc3 , 0xa9 , 0xc3 , 0xad , 0xc3 , 0xb3 , 0xc3 , 0xba })
166158 assert .Equal (t , []byte {0xc3 , 0xa1 , 0xc3 , 0xa9 , 0xc3 , 0xad , 0xc3 , 0xb3 , 0xc3 , 0xba }, res )
167159
168160 // "Hola, así cómo ños"
169- res = ToUTF8DropErrors ([]byte {0x48 , 0x6F , 0x6C , 0x61 , 0x2C , 0x20 , 0x61 , 0x73 , 0xED , 0x20 , 0x63 , 0xF3 , 0x6D , 0x6F , 0x20 , 0xF1 , 0x6F , 0x73 }, ConvertOpts {} )
161+ res = ToUTF8DropErrors ([]byte {0x48 , 0x6F , 0x6C , 0x61 , 0x2C , 0x20 , 0x61 , 0x73 , 0xED , 0x20 , 0x63 , 0xF3 , 0x6D , 0x6F , 0x20 , 0xF1 , 0x6F , 0x73 })
170162 assert .Equal (t , []byte {0x48 , 0x6F , 0x6C , 0x61 , 0x2C , 0x20 , 0x61 , 0x73 }, res [:8 ])
171163 assert .Equal (t , []byte {0x73 }, res [len (res )- 1 :])
172164
173165 // "Hola, así cómo "
174166 minmatch := []byte {0x48 , 0x6F , 0x6C , 0x61 , 0x2C , 0x20 , 0x61 , 0x73 , 0xC3 , 0xAD , 0x20 , 0x63 , 0xC3 , 0xB3 , 0x6D , 0x6F , 0x20 }
175167
176- res = ToUTF8DropErrors ([]byte {0x48 , 0x6F , 0x6C , 0x61 , 0x2C , 0x20 , 0x61 , 0x73 , 0xED , 0x20 , 0x63 , 0xF3 , 0x6D , 0x6F , 0x20 , 0x07 , 0xA4 , 0x6F , 0x73 }, ConvertOpts {} )
168+ res = ToUTF8DropErrors ([]byte {0x48 , 0x6F , 0x6C , 0x61 , 0x2C , 0x20 , 0x61 , 0x73 , 0xED , 0x20 , 0x63 , 0xF3 , 0x6D , 0x6F , 0x20 , 0x07 , 0xA4 , 0x6F , 0x73 })
177169 // Do not fail for differences in invalid cases, as the library might change the conversion criteria for those
178170 assert .Equal (t , minmatch , res [0 :len (minmatch )])
179171
180- res = ToUTF8DropErrors ([]byte {0x48 , 0x6F , 0x6C , 0x61 , 0x2C , 0x20 , 0x61 , 0x73 , 0xED , 0x20 , 0x63 , 0xF3 , 0x6D , 0x6F , 0x20 , 0x81 , 0xA4 , 0x6F , 0x73 }, ConvertOpts {} )
172+ res = ToUTF8DropErrors ([]byte {0x48 , 0x6F , 0x6C , 0x61 , 0x2C , 0x20 , 0x61 , 0x73 , 0xED , 0x20 , 0x63 , 0xF3 , 0x6D , 0x6F , 0x20 , 0x81 , 0xA4 , 0x6F , 0x73 })
181173 // Do not fail for differences in invalid cases, as the library might change the conversion criteria for those
182174 assert .Equal (t , minmatch , res [0 :len (minmatch )])
183175
184176 // Japanese (Shift-JIS)
185177 // "日属秘ぞしちゅ。"
186- res = ToUTF8DropErrors ([]byte {0x93 , 0xFA , 0x91 , 0xAE , 0x94 , 0xE9 , 0x82 , 0xBC , 0x82 , 0xB5 , 0x82 , 0xBF , 0x82 , 0xE3 , 0x81 , 0x42 }, ConvertOpts {} )
178+ res = ToUTF8DropErrors ([]byte {0x93 , 0xFA , 0x91 , 0xAE , 0x94 , 0xE9 , 0x82 , 0xBC , 0x82 , 0xB5 , 0x82 , 0xBF , 0x82 , 0xE3 , 0x81 , 0x42 })
187179 assert .Equal (t , []byte {
188180 0xE6 , 0x97 , 0xA5 , 0xE5 , 0xB1 , 0x9E , 0xE7 , 0xA7 , 0x98 , 0xE3 ,
189181 0x81 , 0x9E , 0xE3 , 0x81 , 0x97 , 0xE3 , 0x81 , 0xA1 , 0xE3 , 0x82 , 0x85 , 0xE3 , 0x80 , 0x82 ,
190182 }, res )
191183
192- res = ToUTF8DropErrors ([]byte {0x00 , 0x00 , 0x00 , 0x00 }, ConvertOpts {} )
184+ res = ToUTF8DropErrors ([]byte {0x00 , 0x00 , 0x00 , 0x00 })
193185 assert .Equal (t , []byte {0x00 , 0x00 , 0x00 , 0x00 }, res )
194186}
195187
@@ -231,12 +223,12 @@ func TestDetectEncoding(t *testing.T) {
231223 assert .Error (t , err )
232224}
233225
234- func stringMustStartWith (t * testing.T , expected , value string ) {
235- assert .Equal (t , expected , value [:len (expected )])
226+ func stringMustStartWith (t * testing.T , expected string , value [] byte ) {
227+ assert .Equal (t , expected , string ( value [:len (expected )]) )
236228}
237229
238- func stringMustEndWith (t * testing.T , expected , value string ) {
239- assert .Equal (t , expected , value [len (value )- len (expected ):])
230+ func stringMustEndWith (t * testing.T , expected string , value [] byte ) {
231+ assert .Equal (t , expected , string ( value [len (value )- len (expected ):]) )
240232}
241233
242234func TestToUTF8WithFallbackReader (t * testing.T ) {
0 commit comments