forked from n10v/id3v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding_test.go
59 lines (52 loc) · 1.5 KB
/
encoding_test.go
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
package id3v2
import (
"bufio"
"bytes"
"testing"
)
func TestDecodeText(t *testing.T) {
testCases := []struct {
src []byte
from Encoding
utf8 string
}{
{[]byte{0x48, 0xE9, 0x6C, 0x6C, 0xF6}, EncodingISO, "Héllö"},
{[]byte{0xFF, 0xFE, 0x48, 0x00, 0xE9, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0xF6, 0x00}, EncodingUTF16, "Héllö"}, // UTF-16LE with BOM
{[]byte{0x00, 0x48, 0x00, 0xE9, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0xF6}, EncodingUTF16BE, "Héllö"},
}
for _, tc := range testCases {
got := decodeText(tc.src, tc.from)
if got != tc.utf8 {
t.Errorf("Expected %q from %v encoding, got %q", tc.utf8, tc.from, got)
}
}
}
func TestEncodeWriteText(t *testing.T) {
testCases := []struct {
src string
to Encoding
expected []byte
size int
}{
{"Héllö", EncodingISO, []byte{0x48, 0xE9, 0x6C, 0x6C, 0xF6}, 5},
{"Héllö", EncodingUTF16, []byte{0xFE, 0xFF, 0x00, 0x48, 0x00, 0xE9, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0xF6}, 12},
{"Héllö", EncodingUTF16BE, []byte{0x00, 0x48, 0x00, 0xE9, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0xF6}, 10},
}
buf := new(bytes.Buffer)
bw := bufio.NewWriter(buf)
for _, tc := range testCases {
buf.Reset()
n, err := encodeWriteText(bw, tc.src, tc.to)
if err != nil {
t.Errorf("Error by encoding and writing text: %v", err)
}
bw.Flush()
got := buf.Bytes()
if !bytes.Equal(got, tc.expected) {
t.Errorf("Expected %q from %q encoding, got %q", tc.expected, tc.to, got)
}
if n != tc.size {
t.Errorf("Expected %v size, got %v", tc.size, n)
}
}
}