]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/file_name_with_info_test.go
Extensive refactor, quality of life enhancements
[rbdr/mobius] / hotline / file_name_with_info_test.go
index 15ec0d379fd7613fbc5675c01ec4d055e47fae65..d1b03c20f842884a812a394b9da595d64538cb16 100644 (file)
@@ -2,34 +2,84 @@ package hotline
 
 import (
        "github.com/stretchr/testify/assert"
+       "io"
+       "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 := io.ReadAll(f)
+                       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
                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,39 +90,30 @@ 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.Write(tt.args.data); (err != nil) != tt.wantErr {
+                               t.Errorf("Write() error = %v, wantErr %v", err, tt.wantErr)
                        }
                        if !assert.Equal(t, tt.want, f) {
                                t.Errorf("Read() got = %v, want %v", f, tt.want)
-
                        }
                })
        }