]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "io" | |
5 | "reflect" | |
6 | "testing" | |
7 | ) | |
8 | ||
9 | func TestNewFileHeader(t *testing.T) { | |
10 | type args struct { | |
11 | fileName string | |
12 | isDir bool | |
13 | } | |
14 | tests := []struct { | |
15 | name string | |
16 | args args | |
17 | want FileHeader | |
18 | }{ | |
19 | { | |
20 | name: "when path is file", | |
21 | args: args{ | |
22 | fileName: "foo", | |
23 | isDir: false, | |
24 | }, | |
25 | want: FileHeader{ | |
26 | Size: [2]byte{0x00, 0x0a}, | |
27 | Type: [2]byte{0x00, 0x00}, | |
28 | FilePath: EncodeFilePath("foo"), | |
29 | }, | |
30 | }, | |
31 | { | |
32 | name: "when path is dir", | |
33 | args: args{ | |
34 | fileName: "foo", | |
35 | isDir: true, | |
36 | }, | |
37 | want: FileHeader{ | |
38 | Size: [2]byte{0x00, 0x0a}, | |
39 | Type: [2]byte{0x00, 0x01}, | |
40 | FilePath: EncodeFilePath("foo"), | |
41 | }, | |
42 | }, | |
43 | } | |
44 | for _, tt := range tests { | |
45 | t.Run(tt.name, func(t *testing.T) { | |
46 | if got := NewFileHeader(tt.args.fileName, tt.args.isDir); !reflect.DeepEqual(got, tt.want) { | |
47 | t.Errorf("NewFileHeader() = %v, want %v", got, tt.want) | |
48 | } | |
49 | }) | |
50 | } | |
51 | } | |
52 | ||
53 | func TestFileHeader_Payload(t *testing.T) { | |
54 | type fields struct { | |
55 | Size [2]byte | |
56 | Type [2]byte | |
57 | FilePath []byte | |
58 | } | |
59 | tests := []struct { | |
60 | name string | |
61 | fields fields | |
62 | want []byte | |
63 | }{ | |
64 | { | |
65 | name: "has expected payload bytes", | |
66 | fields: fields{ | |
67 | Size: [2]byte{0x00, 0x0a}, | |
68 | Type: [2]byte{0x00, 0x00}, | |
69 | FilePath: EncodeFilePath("foo"), | |
70 | }, | |
71 | want: []byte{ | |
72 | 0x00, 0x0a, // total size | |
73 | 0x00, 0x00, // type | |
74 | 0x00, 0x01, // path item count | |
75 | 0x00, 0x00, // path separator | |
76 | 0x03, // pathName len | |
77 | 0x66, 0x6f, 0x6f, // "foo" | |
78 | }, | |
79 | }, | |
80 | } | |
81 | for _, tt := range tests { | |
82 | t.Run(tt.name, func(t *testing.T) { | |
83 | fh := &FileHeader{ | |
84 | Size: tt.fields.Size, | |
85 | Type: tt.fields.Type, | |
86 | FilePath: tt.fields.FilePath, | |
87 | } | |
88 | got, _ := io.ReadAll(fh) | |
89 | if !reflect.DeepEqual(got, tt.want) { | |
90 | t.Errorf("Read() = %v, want %v", got, tt.want) | |
91 | } | |
92 | }) | |
93 | } | |
94 | } |