X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/43ecc0f42eaeface5f640479df7372bfb8021f23..4fad3266d27d3d193c6e25298c6085dbf8a6b610:/hotline/file_name_with_info_test.go diff --git a/hotline/file_name_with_info_test.go b/hotline/file_name_with_info_test.go index 15ec0d3..3ca2c30 100644 --- a/hotline/file_name_with_info_test.go +++ b/hotline/file_name_with_info_test.go @@ -2,34 +2,83 @@ package hotline import ( "github.com/stretchr/testify/assert" + "reflect" "testing" ) -func TestFileNameWithInfo_Read(t *testing.T) { +func TestFileNameWithInfo_MarshalBinary(t *testing.T) { type fields struct { - Type []byte - Creator []byte - FileSize []byte - NameScript []byte - NameSize []byte - Name []byte + fileNameWithInfoHeader fileNameWithInfoHeader + name []byte + } + tests := []struct { + name string + fields fields + wantData []byte + wantErr bool + }{ + { + name: "returns expected bytes", + fields: fields{ + fileNameWithInfoHeader: fileNameWithInfoHeader{ + Type: [4]byte{0x54, 0x45, 0x58, 0x54}, // TEXT + Creator: [4]byte{0x54, 0x54, 0x58, 0x54}, // TTXT + FileSize: [4]byte{0x00, 0x43, 0x16, 0xd3}, // File Size + RSVD: [4]byte{0, 0, 0, 0}, + NameScript: [2]byte{0, 0}, + NameSize: [2]byte{0x00, 0x03}, + }, + name: []byte("foo"), + }, + wantData: []byte{ + 0x54, 0x45, 0x58, 0x54, + 0x54, 0x54, 0x58, 0x54, + 0x00, 0x43, 0x16, 0xd3, + 0, 0, 0, 0, + 0, 0, + 0x00, 0x03, + 0x66, 0x6f, 0x6f, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f := &FileNameWithInfo{ + fileNameWithInfoHeader: tt.fields.fileNameWithInfoHeader, + name: tt.fields.name, + } + gotData, err := f.MarshalBinary() + if (err != nil) != tt.wantErr { + t.Errorf("MarshalBinary() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotData, tt.wantData) { + t.Errorf("MarshalBinary() gotData = %v, want %v", gotData, tt.wantData) + } + }) + } +} + +func TestFileNameWithInfo_UnmarshalBinary(t *testing.T) { + type fields struct { + fileNameWithInfoHeader fileNameWithInfoHeader + name []byte } type args struct { - p []byte + data []byte } tests := []struct { name string fields fields args args - want *FileNameWithInfo - wantN int + want *FileNameWithInfo wantErr bool }{ { - name: "reads bytes into struct", - fields: fields{}, + name: "writes bytes into struct", args: args{ - p: []byte{ + data: []byte{ 0x54, 0x45, 0x58, 0x54, // TEXT 0x54, 0x54, 0x58, 0x54, // TTXT 0x00, 0x43, 0x16, 0xd3, // File Size @@ -40,40 +89,31 @@ func TestFileNameWithInfo_Read(t *testing.T) { }, }, want: &FileNameWithInfo{ - Type: []byte("TEXT"), - Creator: []byte("TTXT"), - FileSize: []byte{0x00, 0x43, 0x16, 0xd3}, - RSVD: []byte{0, 0, 0, 0}, - NameScript: []byte{0, 0}, - NameSize: []byte{0x00, 0x0e}, - Name: []byte("Audion.app.zip"), + fileNameWithInfoHeader: fileNameWithInfoHeader{ + Type: [4]byte{0x54, 0x45, 0x58, 0x54}, // TEXT + Creator: [4]byte{0x54, 0x54, 0x58, 0x54}, // TTXT + FileSize: [4]byte{0x00, 0x43, 0x16, 0xd3}, // File Size + RSVD: [4]byte{0, 0, 0, 0}, + NameScript: [2]byte{0, 0}, + NameSize: [2]byte{0x00, 0x0e}, + }, + name: []byte("Audion.app.zip"), }, - wantN: 34, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { f := &FileNameWithInfo{ - Type: tt.fields.Type, - Creator: tt.fields.Creator, - FileSize: tt.fields.FileSize, - NameScript: tt.fields.NameScript, - NameSize: tt.fields.NameSize, - Name: tt.fields.Name, + fileNameWithInfoHeader: tt.fields.fileNameWithInfoHeader, + name: tt.fields.name, } - gotN, err := f.Read(tt.args.p) - if (err != nil) != tt.wantErr { - t.Errorf("Read() error = %v, wantErr %v", err, tt.wantErr) - return - } - if gotN != tt.wantN { - t.Errorf("Read() gotN = %v, want %v", gotN, tt.wantN) + if err := f.UnmarshalBinary(tt.args.data); (err != nil) != tt.wantErr { + t.Errorf("UnmarshalBinary() error = %v, wantErr %v", err, tt.wantErr) } if !assert.Equal(t, tt.want, f) { t.Errorf("Read() got = %v, want %v", f, tt.want) - } }) } -} +} \ No newline at end of file