Skip to content

Commit

Permalink
gh-63: Return error if other is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed Jan 21, 2024
1 parent d68e89b commit 8be490c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.6.x, 1.7.x, 1.8.x, 1.9.x, 1.10.x, 1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x, 1.18.x, 1.19.x, 1.20.x]
go-version: [1.6.x, 1.7.x, 1.8.x, 1.9.x, 1.10.x, 1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x, 1.18.x, 1.19.x, 1.20.x, 1.21.x]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_gomodule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x, 1.18.x, 1.19.x, 1.20.x]
go-version: [1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x, 1.18.x, 1.19.x, 1.20.x, 1.21.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down
3 changes: 3 additions & 0 deletions imagehash.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func (h *ImageHash) Bits() int {

// Distance method returns a distance between two hashes.
func (h *ImageHash) Distance(other *ImageHash) (int, error) {
if other == nil {
return -1, errors.New("other should not be nil")
}
if h.GetKind() != other.GetKind() {
return -1, errors.New("Image hashes's kind should be identical")
}
Expand Down
12 changes: 12 additions & 0 deletions imagehash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ func TestNewImageHash(t *testing.T) {
}
}

func TestNil(t *testing.T) {
hash := NewImageHash(0, AHash)
dis, err := hash.Distance(nil)
if dis != -1 {
t.Errorf("Distance is expected as %d but got %d", -1, dis)
}
expectedError := errors.New("other should not be nil")
if err != nil && err.Error() != expectedError.Error() {
t.Errorf("Expected err %s, actual %s", expectedError, err)
}
}

func TestSerialization(t *testing.T) {
checkErr := func(err error) {
if err != nil {
Expand Down

0 comments on commit 8be490c

Please sign in to comment.