tag/id3v2_test.go
Xavier Henner 1e646522d6 Support for numeric genres in id3v2
TCON

    The 'Content type', which previously was stored as a one byte numeric value
only, is now a numeric string. You may use one or several of the types as
ID3v1.1 did or, since the category list would be impossible to maintain with
accurate and up to date categories, define your own.

    References to the ID3v1 genres can be made by, as first byte, enter "("
followed by a number from the genres list (appendix A) and ended with a ")"
character. This is optionally followed by a refinement, e.g. "(21)" or
"(4)Eurodisco". Several references can be made in the same frame, e.g.
"(51)(39)". If the refinement should begin with a "(" character it should be
replaced with "((", e.g. "((I can figure out any genre)" or "(55)((I
think...)". The following new content types is defined in ID3v2 and is
implemented in the same way as the numerig content types, e.g. "(RX)".

To test it, use the id3v2 tool

% id3v2 -g 79 test.mp3
% id3v2 -l test.mp3| grep TCON
TCON (Content type): Hard Rock (79)
% ./tag test.mp3| grep Genre
 Genre: (79)

With the patch :
% go build && ./tag test.mp3| grep Genre
 Genre: Hard Rock
2015-07-04 14:20:43 +02:00

153 lines
2.9 KiB
Go

// Copyright 2015, David Howden
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tag
import (
"bytes"
"reflect"
"testing"
)
func TestUnsynchroniser(t *testing.T) {
tests := []struct {
input []byte
output []byte
}{
{
input: []byte{},
output: []byte{},
},
{
input: []byte{0x00},
output: []byte{0x00},
},
{
input: []byte{0xFF},
output: []byte{0xFF},
},
{
input: []byte{0xFF, 0x00},
output: []byte{0xFF},
},
{
input: []byte{0xFF, 0x00, 0x00},
output: []byte{0xFF, 0x00},
},
{
input: []byte{0xFF, 0x00, 0x01},
output: []byte{0xFF, 0x01},
},
{
input: []byte{0xFF, 0x00, 0xFF, 0x00},
output: []byte{0xFF, 0xFF},
},
{
input: []byte{0xFF, 0x00, 0xFF, 0xFF, 0x00},
output: []byte{0xFF, 0xFF, 0xFF},
},
{
input: []byte{0x00, 0x01, 0x02},
output: []byte{0x00, 0x01, 0x02},
},
}
for ii, tt := range tests {
r := bytes.NewReader(tt.input)
ur := unsynchroniser{Reader: r}
got := make([]byte, len(tt.output))
n, err := ur.Read(got)
if n != len(got) || err != nil {
t.Errorf("[%d] got: n = %d, err = %v, expected: n = %d, err = nil", ii, n, err, len(got))
}
if !reflect.DeepEqual(got, tt.output) {
t.Errorf("[%d] got: %v, expected %v", ii, got, tt.output)
}
}
}
func TestUnsynchroniserSplitReads(t *testing.T) {
tests := []struct {
input []byte
output []byte
split []int
}{
{
input: []byte{0x00, 0xFF, 0x00},
output: []byte{0x00, 0xFF},
split: []int{1, 1},
},
{
input: []byte{0xFF, 0x00, 0x01},
output: []byte{0xFF, 0x01},
split: []int{1, 1},
},
{
input: []byte{0xFF, 0x00, 0x01, 0x02},
output: []byte{0xFF, 0x01, 0x02},
split: []int{1, 1, 1},
},
{
input: []byte{0xFF, 0x00, 0x01, 0x02},
output: []byte{0xFF, 0x01, 0x02},
split: []int{2, 1},
},
{
input: []byte{0xFF, 0x00, 0x01, 0x02},
output: []byte{0xFF, 0x01, 0x02},
split: []int{1, 2},
},
}
for ii, tt := range tests {
r := bytes.NewReader(tt.input)
ur := unsynchroniser{Reader: r}
var got []byte
for i, l := range tt.split {
chunk := make([]byte, l)
n, err := ur.Read(chunk)
if n != len(chunk) || err != nil {
t.Errorf("[%d : %d] got: n = %d, err = %v, expected: n = %d, err = nil", ii, i, n, err, l)
}
got = append(got, chunk...)
}
if !reflect.DeepEqual(got, tt.output) {
t.Errorf("[%d] got: %v, expected %v", ii, got, tt.output)
}
}
}
func TestGenreExpension(t *testing.T) {
var tests = map[string]string{
"Test": "Test",
"((17)": "(17)",
"(17) Test": "Rock Test",
"(17)Test": "Rock Test",
"(17)": "Rock",
"Test(17)": "Test Rock",
"Test (17)": "Test Rock",
"(17)(93)": "Rock Psychedelic Rock",
"(17)Test(93)": "Rock Test Psychedelic Rock",
}
for g, r := range tests {
got := id3v2genre(g)
if got != r {
t.Errorf("[%v] got: %v, expected %v", g, got, r)
}
}
}