gh-63: Return error if other is nil (#64)
This commit is contained in:
parent
d68e89bd8f
commit
d8115886f3
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@ -4,7 +4,7 @@ jobs:
|
|||||||
test:
|
test:
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
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]
|
platform: [ubuntu-latest]
|
||||||
runs-on: ${{ matrix.platform }}
|
runs-on: ${{ matrix.platform }}
|
||||||
steps:
|
steps:
|
||||||
|
2
.github/workflows/ci_gomodule.yml
vendored
2
.github/workflows/ci_gomodule.yml
vendored
@ -4,7 +4,7 @@ jobs:
|
|||||||
test:
|
test:
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
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]
|
platform: [ubuntu-latest, macos-latest, windows-latest]
|
||||||
runs-on: ${{ matrix.platform }}
|
runs-on: ${{ matrix.platform }}
|
||||||
steps:
|
steps:
|
||||||
|
@ -13,6 +13,8 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var errNoOther = errors.New("other should not be nil")
|
||||||
|
|
||||||
// Kind describes the kinds of hash.
|
// Kind describes the kinds of hash.
|
||||||
type Kind int
|
type Kind int
|
||||||
|
|
||||||
@ -54,6 +56,9 @@ func (h *ImageHash) Bits() int {
|
|||||||
|
|
||||||
// Distance method returns a distance between two hashes.
|
// Distance method returns a distance between two hashes.
|
||||||
func (h *ImageHash) Distance(other *ImageHash) (int, error) {
|
func (h *ImageHash) Distance(other *ImageHash) (int, error) {
|
||||||
|
if other == nil {
|
||||||
|
return -1, errNoOther
|
||||||
|
}
|
||||||
if h.GetKind() != other.GetKind() {
|
if h.GetKind() != other.GetKind() {
|
||||||
return -1, errors.New("Image hashes's kind should be identical")
|
return -1, errors.New("Image hashes's kind should be identical")
|
||||||
}
|
}
|
||||||
|
@ -56,6 +56,17 @@ func TestNewImageHash(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNil(t *testing.T) {
|
||||||
|
hash := NewImageHash(0, AHash)
|
||||||
|
dis, err := hash.Distance(nil)
|
||||||
|
if err != errNoOther {
|
||||||
|
t.Errorf("Expected err %s, actual %s", errNoOther, err)
|
||||||
|
}
|
||||||
|
if dis != -1 {
|
||||||
|
t.Errorf("Distance is expected as %d but got %d", -1, dis)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSerialization(t *testing.T) {
|
func TestSerialization(t *testing.T) {
|
||||||
checkErr := func(err error) {
|
checkErr := func(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user