]> git.r.bdr.sh - rbdr/mobius/blame - hotline/file_header_test.go
Refactoring and cleanup
[rbdr/mobius] / hotline / file_header_test.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
9cf66aea 4 "io"
6988a057
JH
5 "reflect"
6 "testing"
7)
8
9func 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{
9cf66aea
JH
26 Size: [2]byte{0x00, 0x0a},
27 Type: [2]byte{0x00, 0x00},
6988a057
JH
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{
9cf66aea
JH
38 Size: [2]byte{0x00, 0x0a},
39 Type: [2]byte{0x00, 0x01},
6988a057
JH
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
53func TestFileHeader_Payload(t *testing.T) {
54 type fields struct {
9cf66aea
JH
55 Size [2]byte
56 Type [2]byte
6988a057
JH
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{
9cf66aea
JH
67 Size: [2]byte{0x00, 0x0a},
68 Type: [2]byte{0x00, 0x00},
6988a057
JH
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 }
9cf66aea
JH
88 got, _ := io.ReadAll(fh)
89 if !reflect.DeepEqual(got, tt.want) {
8ff2b66d 90 t.Errorf("Read() = %v, want %v", got, tt.want)
6988a057
JH
91 }
92 })
93 }
94}