]>
Commit | Line | Data |
---|---|---|
43ecc0f4 JH |
1 | package hotline |
2 | ||
3 | import ( | |
4 | "github.com/stretchr/testify/assert" | |
72dd37f1 | 5 | "reflect" |
43ecc0f4 JH |
6 | "testing" |
7 | ) | |
8 | ||
72dd37f1 | 9 | func TestFileNameWithInfo_MarshalBinary(t *testing.T) { |
43ecc0f4 | 10 | type fields struct { |
72dd37f1 JH |
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 | |
43ecc0f4 JH |
67 | } |
68 | type args struct { | |
72dd37f1 | 69 | data []byte |
43ecc0f4 JH |
70 | } |
71 | tests := []struct { | |
72 | name string | |
73 | fields fields | |
74 | args args | |
72dd37f1 | 75 | want *FileNameWithInfo |
43ecc0f4 JH |
76 | wantErr bool |
77 | }{ | |
78 | { | |
72dd37f1 | 79 | name: "writes bytes into struct", |
43ecc0f4 | 80 | args: args{ |
72dd37f1 | 81 | data: []byte{ |
43ecc0f4 JH |
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{ | |
72dd37f1 JH |
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"), | |
43ecc0f4 | 101 | }, |
43ecc0f4 JH |
102 | wantErr: false, |
103 | }, | |
104 | } | |
105 | for _, tt := range tests { | |
106 | t.Run(tt.name, func(t *testing.T) { | |
107 | f := &FileNameWithInfo{ | |
72dd37f1 JH |
108 | fileNameWithInfoHeader: tt.fields.fileNameWithInfoHeader, |
109 | name: tt.fields.name, | |
43ecc0f4 | 110 | } |
72dd37f1 JH |
111 | if err := f.UnmarshalBinary(tt.args.data); (err != nil) != tt.wantErr { |
112 | t.Errorf("UnmarshalBinary() error = %v, wantErr %v", err, tt.wantErr) | |
43ecc0f4 JH |
113 | } |
114 | if !assert.Equal(t, tt.want, f) { | |
115 | t.Errorf("Read() got = %v, want %v", f, tt.want) | |
43ecc0f4 JH |
116 | } |
117 | }) | |
118 | } | |
72dd37f1 | 119 | } |