Exemple #1
0
func TestUnsynchTextFrame_RoundTrip(t *testing.T) {
	var (
		err              error
		tempfile         *os.File
		f                *File
		tagger           *v2.Tag
		ft               v2.FrameType
		utextFrame       *v2.UnsynchTextFrame
		parsedFrame      v2.Framer
		resultFrame      *v2.UnsynchTextFrame
		ok               bool
		expected, actual string
	)

	tempfile, err = ioutil.TempFile("", "id3v2")
	if err != nil {
		t.Fatal(err)
	}

	tagger = v2.NewTag(3)
	ft = v2.V23FrameTypeMap["COMM"]
	utextFrame = v2.NewUnsynchTextFrame(ft, "Comment", "Foo")
	tagger.AddFrames(utextFrame)

	_, err = tempfile.Write(tagger.Bytes())
	tempfile.Close()
	if err != nil {
		t.Fatal(err)
	}

	f, err = Open(tempfile.Name())
	if err != nil {
		t.Fatal(err)
	}

	parsedFrame = f.Frame("COMM")
	if resultFrame, ok = parsedFrame.(*v2.UnsynchTextFrame); !ok {
		t.Error("Couldn't cast frame")
	} else {
		expected = utextFrame.Description()
		actual = resultFrame.Description()
		if expected != actual {
			t.Errorf("Expected %q, got %q", expected, actual)
		}
	}
}
Exemple #2
0
// Opens a new tagged file
func Open(name string) (*File, error) {
	fi, err := os.OpenFile(name, os.O_RDWR, 0666)
	if err != nil {
		return nil, err
	}

	file := &File{file: fi}

	if v2Tag := v2.ParseTag(fi); v2Tag != nil {
		file.Tagger = v2Tag
		file.originalSize = v2Tag.Size()
	} else if v1Tag := v1.ParseTag(fi); v1Tag != nil {
		file.Tagger = v1Tag
	} else {
		// Add a new tag if none exists
		file.Tagger = v2.NewTag(LatestVersion)
	}

	return file, nil
}