Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test for object.go #62

Merged
merged 5 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add TestReadHeader (#44)
  • Loading branch information
JunNishimura committed May 26, 2023
commit 7c8cf16930e4d1227ca756fec6c9340f825b8859
4 changes: 2 additions & 2 deletions internal/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ func readHeader(r io.Reader) (Type, int, error) {

objType, err := NewType(objTypeString)
if err != nil {
return UndefinedObject, 0, err
return UndefinedObject, 0, ErrInvalidObject
}
var size int
if _, err := fmt.Sscanf(sizeString, "%d", &size); err != nil {
return UndefinedObject, 0, err
return UndefinedObject, 0, ErrInvalidObject
}

return objType, size, nil
Expand Down
66 changes: 66 additions & 0 deletions internal/object/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package object
import (
"encoding/hex"
"errors"
"io"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

"github.com/JunNishimura/Goit/internal/sha"
Expand Down Expand Up @@ -168,3 +170,67 @@ func TestGetObject(t *testing.T) {
})
}
}

func TestReadHeader(t *testing.T) {
type args struct {
r io.Reader
}
tests := []struct {
name string
args args
wantType Type
wantSize int
wantErr error
}{
{
name: "success",
args: args{
r: strings.NewReader("blob 12\x00Hello, World"),
},
wantType: BlobObject,
wantSize: 12,
wantErr: nil,
},
{
name: "fail: empty header",
args: args{
r: strings.NewReader(""),
},
wantType: UndefinedObject,
wantSize: 0,
wantErr: ErrInvalidObject,
},
{
name: "fail: invalid object type",
args: args{
r: strings.NewReader("blub 12\x00Hello, World"),
},
wantType: UndefinedObject,
wantSize: 0,
wantErr: ErrInvalidObject,
},
{
name: "fail: invalid size",
args: args{
r: strings.NewReader("blob xx\x00Hello, World"),
},
wantType: UndefinedObject,
wantSize: 0,
wantErr: ErrInvalidObject,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
getType, n, err := readHeader(tt.args.r)
if !errors.Is(err, tt.wantErr) {
t.Errorf("got = %v, want = %v", err, tt.wantErr)
}
if getType != tt.wantType {
t.Errorf("got = %v, want = %v", getType, tt.wantType)
}
if n != tt.wantSize {
t.Errorf("got = %d, want = %d", n, tt.wantSize)
}
})
}
}