github.com/jhalter/mobius@v0.12.1/hotline/file_name_with_info_test.go (about) 1 package hotline 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "reflect" 6 "testing" 7 ) 8 9 func TestFileNameWithInfo_MarshalBinary(t *testing.T) { 10 type fields struct { 11 fileNameWithInfoHeader fileNameWithInfoHeader 12 name []byte 13 } 14 tests := []struct { 15 name string 16 fields fields 17 wantData []byte 18 wantErr bool 19 }{ 20 { 21 name: "returns expected bytes", 22 fields: fields{ 23 fileNameWithInfoHeader: fileNameWithInfoHeader{ 24 Type: [4]byte{0x54, 0x45, 0x58, 0x54}, // TEXT 25 Creator: [4]byte{0x54, 0x54, 0x58, 0x54}, // TTXT 26 FileSize: [4]byte{0x00, 0x43, 0x16, 0xd3}, // File Size 27 RSVD: [4]byte{0, 0, 0, 0}, 28 NameScript: [2]byte{0, 0}, 29 NameSize: [2]byte{0x00, 0x03}, 30 }, 31 name: []byte("foo"), 32 }, 33 wantData: []byte{ 34 0x54, 0x45, 0x58, 0x54, 35 0x54, 0x54, 0x58, 0x54, 36 0x00, 0x43, 0x16, 0xd3, 37 0, 0, 0, 0, 38 0, 0, 39 0x00, 0x03, 40 0x66, 0x6f, 0x6f, 41 }, 42 wantErr: false, 43 }, 44 } 45 for _, tt := range tests { 46 t.Run(tt.name, func(t *testing.T) { 47 f := &FileNameWithInfo{ 48 fileNameWithInfoHeader: tt.fields.fileNameWithInfoHeader, 49 name: tt.fields.name, 50 } 51 gotData, err := f.MarshalBinary() 52 if (err != nil) != tt.wantErr { 53 t.Errorf("MarshalBinary() error = %v, wantErr %v", err, tt.wantErr) 54 return 55 } 56 if !reflect.DeepEqual(gotData, tt.wantData) { 57 t.Errorf("MarshalBinary() gotData = %v, want %v", gotData, tt.wantData) 58 } 59 }) 60 } 61 } 62 63 func TestFileNameWithInfo_UnmarshalBinary(t *testing.T) { 64 type fields struct { 65 fileNameWithInfoHeader fileNameWithInfoHeader 66 name []byte 67 } 68 type args struct { 69 data []byte 70 } 71 tests := []struct { 72 name string 73 fields fields 74 args args 75 want *FileNameWithInfo 76 wantErr bool 77 }{ 78 { 79 name: "writes bytes into struct", 80 args: args{ 81 data: []byte{ 82 0x54, 0x45, 0x58, 0x54, // TEXT 83 0x54, 0x54, 0x58, 0x54, // TTXT 84 0x00, 0x43, 0x16, 0xd3, // File Size 85 0x00, 0x00, 0x00, 0x00, // RSVD 86 0x00, 0x00, // NameScript 87 0x00, 0x0e, // Name Size 88 0x41, 0x75, 0x64, 0x69, 0x6f, 0x6e, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x7a, 0x69, 0x70, 89 }, 90 }, 91 want: &FileNameWithInfo{ 92 fileNameWithInfoHeader: fileNameWithInfoHeader{ 93 Type: [4]byte{0x54, 0x45, 0x58, 0x54}, // TEXT 94 Creator: [4]byte{0x54, 0x54, 0x58, 0x54}, // TTXT 95 FileSize: [4]byte{0x00, 0x43, 0x16, 0xd3}, // File Size 96 RSVD: [4]byte{0, 0, 0, 0}, 97 NameScript: [2]byte{0, 0}, 98 NameSize: [2]byte{0x00, 0x0e}, 99 }, 100 name: []byte("Audion.app.zip"), 101 }, 102 wantErr: false, 103 }, 104 } 105 for _, tt := range tests { 106 t.Run(tt.name, func(t *testing.T) { 107 f := &FileNameWithInfo{ 108 fileNameWithInfoHeader: tt.fields.fileNameWithInfoHeader, 109 name: tt.fields.name, 110 } 111 if err := f.UnmarshalBinary(tt.args.data); (err != nil) != tt.wantErr { 112 t.Errorf("Write() error = %v, wantErr %v", err, tt.wantErr) 113 } 114 if !assert.Equal(t, tt.want, f) { 115 t.Errorf("Read() got = %v, want %v", f, tt.want) 116 } 117 }) 118 } 119 }