Skip to content

Commit

Permalink
add Compare method (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 17, 2023
1 parent b189c64 commit 08618e8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
8 changes: 6 additions & 2 deletions internal/sha/sha.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ var (
ErrInvalidHash = errors.New("invalid hash")
)

func (sha1 SHA1) String() string {
return hex.EncodeToString(sha1)
func (s SHA1) String() string {
return hex.EncodeToString(s)
}

func (s SHA1) Compare(other SHA1) bool {
return s.String() == other.String()
}

func ReadHash(hashString string) (SHA1, error) {
Expand Down
56 changes: 56 additions & 0 deletions internal/sha/sha_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,66 @@
package sha

import (
"encoding/hex"
"errors"
"strings"
"testing"
)

func TestCompare(t *testing.T) {
type args struct {
sha SHA1
}
type fields struct {
sha SHA1
}
type test struct {
name string
args args
fields fields
want bool
}
tests := []*test{
func() *test {
s, _ := hex.DecodeString("87f3c49bccf2597484ece08746d3ee5defaba335")

return &test{
name: "success: true",
args: args{
sha: s,
},
fields: fields{
sha: s,
},
want: true,
}
}(),
func() *test {
s, _ := hex.DecodeString("87f3c49bccf2597484ece08746d3ee5defaba335")
s2, _ := hex.DecodeString(strings.Repeat("0", 40))

return &test{
name: "success: false",
args: args{
sha: s,
},
fields: fields{
sha: s2,
},
want: false,
}
}(),
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.fields.sha.Compare(tt.args.sha)
if got != tt.want {
t.Errorf("got = %v, want = %v", got, tt.want)
}
})
}
}

func TestReadHash(t *testing.T) {
type args struct {
hashString string
Expand Down

0 comments on commit 08618e8

Please sign in to comment.