mirror of https://github.com/dsoprea/go-exif.git
type: Added encode and decode tests for all types but undefined.
- Updated `(TagType).Encode()` to use the byte-order it already has. - TODO: Add tests for TypeUndefined.pull/3/head
parent
7166faa0be
commit
98b3d60a12
4
type.go
4
type.go
|
@ -725,14 +725,14 @@ func (tt TagType) Resolve(valueContext ValueContext) (value interface{}, err err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encode knows how to encode the given value to a byte slice.
|
// Encode knows how to encode the given value to a byte slice.
|
||||||
func (tt TagType) Encode(byteOrder binary.ByteOrder, value interface{}) (encoded []byte, err error) {
|
func (tt TagType) Encode(value interface{}) (encoded []byte, err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if state := recover(); state != nil {
|
if state := recover(); state != nil {
|
||||||
err = log.Wrap(state.(error))
|
err = log.Wrap(state.(error))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ve := NewValueEncoder(byteOrder)
|
ve := NewValueEncoder(tt.byteOrder)
|
||||||
|
|
||||||
ed, err := ve.EncodeWithType(tt, value)
|
ed, err := ve.EncodeWithType(tt, value)
|
||||||
log.PanicIf(err)
|
log.PanicIf(err)
|
||||||
|
|
|
@ -0,0 +1,178 @@
|
||||||
|
package exif
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/dsoprea/go-logging"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_TagType_EncodeDecode_Byte(t *testing.T) {
|
||||||
|
tt := NewTagType(TypeByte, TestDefaultByteOrder)
|
||||||
|
|
||||||
|
data := []byte { 0x11, 0x22, 0x33, 0x44, 0x55 }
|
||||||
|
|
||||||
|
encoded, err := tt.Encode(data)
|
||||||
|
log.PanicIf(err)
|
||||||
|
|
||||||
|
if bytes.Compare(encoded, data) != 0 {
|
||||||
|
t.Fatalf("Data not encoded correctly.")
|
||||||
|
}
|
||||||
|
|
||||||
|
restored, err := tt.ParseBytes(encoded, uint32(len(data)))
|
||||||
|
log.PanicIf(err)
|
||||||
|
|
||||||
|
if bytes.Compare(restored, data) != 0 {
|
||||||
|
t.Fatalf("Data not decoded correctly.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_TagType_EncodeDecode_Ascii(t *testing.T) {
|
||||||
|
tt := NewTagType(TypeAscii, TestDefaultByteOrder)
|
||||||
|
|
||||||
|
data := "hello"
|
||||||
|
|
||||||
|
encoded, err := tt.Encode(data)
|
||||||
|
log.PanicIf(err)
|
||||||
|
|
||||||
|
if string(encoded) != fmt.Sprintf("%s\000", data) {
|
||||||
|
t.Fatalf("Data not encoded correctly.")
|
||||||
|
}
|
||||||
|
|
||||||
|
restored, err := tt.ParseAscii(encoded, uint32(len(data)))
|
||||||
|
log.PanicIf(err)
|
||||||
|
|
||||||
|
if restored != data {
|
||||||
|
t.Fatalf("Data not decoded correctly.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_TagType_EncodeDecode_Shorts(t *testing.T) {
|
||||||
|
tt := NewTagType(TypeShort, TestDefaultByteOrder)
|
||||||
|
|
||||||
|
data := []uint16 { 0x11, 0x22, 0x33 }
|
||||||
|
|
||||||
|
encoded, err := tt.Encode(data)
|
||||||
|
log.PanicIf(err)
|
||||||
|
|
||||||
|
if bytes.Compare(encoded, []byte { 0x00, 0x11, 0x00, 0x22, 0x00, 0x33 }) != 0 {
|
||||||
|
t.Fatalf("Data not encoded correctly.")
|
||||||
|
}
|
||||||
|
|
||||||
|
restored, err := tt.ParseShorts(encoded, uint32(len(data)))
|
||||||
|
log.PanicIf(err)
|
||||||
|
|
||||||
|
if reflect.DeepEqual(restored, data) != true {
|
||||||
|
t.Fatalf("Data not decoded correctly.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_TagType_EncodeDecode_Long(t *testing.T) {
|
||||||
|
tt := NewTagType(TypeLong, TestDefaultByteOrder)
|
||||||
|
|
||||||
|
data := []uint32 { 0x11, 0x22, 0x33 }
|
||||||
|
|
||||||
|
encoded, err := tt.Encode(data)
|
||||||
|
log.PanicIf(err)
|
||||||
|
|
||||||
|
if bytes.Compare(encoded, []byte { 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x33 }) != 0 {
|
||||||
|
t.Fatalf("Data not encoded correctly.")
|
||||||
|
}
|
||||||
|
|
||||||
|
restored, err := tt.ParseLongs(encoded, uint32(len(data)))
|
||||||
|
log.PanicIf(err)
|
||||||
|
|
||||||
|
if reflect.DeepEqual(restored, data) != true {
|
||||||
|
t.Fatalf("Data not decoded correctly.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_TagType_EncodeDecode_Rational(t *testing.T) {
|
||||||
|
tt := NewTagType(TypeRational, TestDefaultByteOrder)
|
||||||
|
|
||||||
|
data := []Rational {
|
||||||
|
Rational{ Numerator: 0x11, Denominator: 0x22 },
|
||||||
|
Rational{ Numerator: 0x33, Denominator: 0x44 },
|
||||||
|
}
|
||||||
|
|
||||||
|
encoded, err := tt.Encode(data)
|
||||||
|
log.PanicIf(err)
|
||||||
|
|
||||||
|
if bytes.Compare(encoded, []byte { 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x44 }) != 0 {
|
||||||
|
t.Fatalf("Data not encoded correctly.")
|
||||||
|
}
|
||||||
|
|
||||||
|
restored, err := tt.ParseRationals(encoded, uint32(len(data)))
|
||||||
|
log.PanicIf(err)
|
||||||
|
|
||||||
|
if reflect.DeepEqual(restored, data) != true {
|
||||||
|
t.Fatalf("Data not decoded correctly.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_TagType_EncodeDecode_SignedLong(t *testing.T) {
|
||||||
|
tt := NewTagType(TypeSignedLong, TestDefaultByteOrder)
|
||||||
|
|
||||||
|
data := []int32 { 0x11, 0x22, 0x33 }
|
||||||
|
|
||||||
|
encoded, err := tt.Encode(data)
|
||||||
|
log.PanicIf(err)
|
||||||
|
|
||||||
|
if bytes.Compare(encoded, []byte { 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x33 }) != 0 {
|
||||||
|
t.Fatalf("Data not encoded correctly.")
|
||||||
|
}
|
||||||
|
|
||||||
|
restored, err := tt.ParseSignedLongs(encoded, uint32(len(data)))
|
||||||
|
log.PanicIf(err)
|
||||||
|
|
||||||
|
if reflect.DeepEqual(restored, data) != true {
|
||||||
|
t.Fatalf("Data not decoded correctly.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_TagType_EncodeDecode_SignedRational(t *testing.T) {
|
||||||
|
tt := NewTagType(TypeSignedRational, TestDefaultByteOrder)
|
||||||
|
|
||||||
|
data := []SignedRational {
|
||||||
|
SignedRational{ Numerator: 0x11, Denominator: 0x22 },
|
||||||
|
SignedRational{ Numerator: 0x33, Denominator: 0x44 },
|
||||||
|
}
|
||||||
|
|
||||||
|
encoded, err := tt.Encode(data)
|
||||||
|
log.PanicIf(err)
|
||||||
|
|
||||||
|
if bytes.Compare(encoded, []byte { 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x44 }) != 0 {
|
||||||
|
t.Fatalf("Data not encoded correctly.")
|
||||||
|
}
|
||||||
|
|
||||||
|
restored, err := tt.ParseSignedRationals(encoded, uint32(len(data)))
|
||||||
|
log.PanicIf(err)
|
||||||
|
|
||||||
|
if reflect.DeepEqual(restored, data) != true {
|
||||||
|
t.Fatalf("Data not decoded correctly.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_TagType_EncodeDecode_AsciiNoNul(t *testing.T) {
|
||||||
|
tt := NewTagType(TypeAsciiNoNul, TestDefaultByteOrder)
|
||||||
|
|
||||||
|
data := "hello"
|
||||||
|
|
||||||
|
encoded, err := tt.Encode(data)
|
||||||
|
log.PanicIf(err)
|
||||||
|
|
||||||
|
if string(encoded) != data {
|
||||||
|
t.Fatalf("Data not encoded correctly.")
|
||||||
|
}
|
||||||
|
|
||||||
|
restored, err := tt.ParseAsciiNoNul(encoded, uint32(len(data)))
|
||||||
|
log.PanicIf(err)
|
||||||
|
|
||||||
|
if restored != data {
|
||||||
|
t.Fatalf("Data not decoded correctly.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(dustin): Add tests for TypeUndefined.
|
Loading…
Reference in New Issue