dcrd/gcs/error_test.go
Dave Collins 36fbba3e22
gcs: Prepare v3.0.0.
This updates the gcs module dependencies, the copyright year in the
files modified since the previous release, and serves as a base for
gcs/v3.0.0.

The updated direct dependencies in this commit are as follows:

- github.com/decred/dcrd/blockchain/stake/v4@v4.0.0

The full list of updated direct dependencies since the previous
gcs/v2.1.0 release are as follows:

- github.com/dchest/siphash@v1.2.2
- github.com/decred/dcrd/blockchain/stake/v4@v4.0.0
- github.com/decred/dcrd/chaincfg/chainhash@v1.0.3
- github.com/decred/dcrd/txscript/v4@v4.0.0
- github.com/decred/dcrd/wire@v1.5.0

Finally, all modules in the repository that depend on the module are
tidied to ensure they are updated to use the latest versions hoisted
forward as a result.
2021-11-22 10:37:05 -06:00

138 lines
3.3 KiB
Go

// Copyright (c) 2019-2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package gcs
import (
"errors"
"io"
"testing"
)
// TestErrorKindStringer tests the stringized output for the ErrorKind type.
func TestErrorKindStringer(t *testing.T) {
tests := []struct {
in ErrorKind
want string
}{
{ErrNTooBig, "ErrNTooBig"},
{ErrPTooBig, "ErrPTooBig"},
{ErrBTooBig, "ErrBTooBig"},
{ErrMisserialized, "ErrMisserialized"},
}
for i, test := range tests {
result := test.in.Error()
if result != test.want {
t.Errorf("%d: got: %s want: %s", i, result, test.want)
continue
}
}
}
// TestError tests the error output for the Error type.
func TestError(t *testing.T) {
tests := []struct {
in Error
want string
}{{
Error{Description: "duplicate block"},
"duplicate block",
}, {
Error{Description: "human-readable error"},
"human-readable error",
},
}
for i, test := range tests {
result := test.in.Error()
if result != test.want {
t.Errorf("%d: got: %s want: %s", i, result, test.want)
continue
}
}
}
// TestErrorKindIsAs ensures both ErrorKind and Error can be identified as being
// a specific error kind via errors.Is and unwrapped via errors.As.
func TestErrorKindIsAs(t *testing.T) {
tests := []struct {
name string
err error
target error
wantMatch bool
wantAs ErrorKind
}{{
name: "ErrNTooBig == ErrNTooBig",
err: ErrNTooBig,
target: ErrNTooBig,
wantMatch: true,
wantAs: ErrNTooBig,
}, {
name: "Error.ErrNTooBig == ErrNTooBig",
err: makeError(ErrNTooBig, ""),
target: ErrNTooBig,
wantMatch: true,
wantAs: ErrNTooBig,
}, {
name: "Error.ErrNTooBig == Error.ErrNTooBig",
err: makeError(ErrNTooBig, ""),
target: makeError(ErrNTooBig, ""),
wantMatch: true,
wantAs: ErrNTooBig,
}, {
name: "ErrNTooBig != ErrPTooBig",
err: ErrNTooBig,
target: ErrPTooBig,
wantMatch: false,
wantAs: ErrNTooBig,
}, {
name: "Error.ErrNTooBig != ErrPTooBig",
err: makeError(ErrNTooBig, ""),
target: ErrPTooBig,
wantMatch: false,
wantAs: ErrNTooBig,
}, {
name: "ErrNTooBig != Error.ErrPTooBig",
err: ErrNTooBig,
target: makeError(ErrPTooBig, ""),
wantMatch: false,
wantAs: ErrNTooBig,
}, {
name: "Error.ErrNTooBig != Error.ErrPTooBig",
err: makeError(ErrNTooBig, ""),
target: makeError(ErrPTooBig, ""),
wantMatch: false,
wantAs: ErrNTooBig,
}, {
name: "Error.ErrMisserialized != io.EOF",
err: makeError(ErrMisserialized, ""),
target: io.EOF,
wantMatch: false,
wantAs: ErrMisserialized,
}}
for _, test := range tests {
// Ensure the error matches or not depending on the expected result.
result := errors.Is(test.err, test.target)
if result != test.wantMatch {
t.Errorf("%s: incorrect error identification -- got %v, want %v",
test.name, result, test.wantMatch)
continue
}
// Ensure the underlying error kind can be unwrapped and is the
// expected kind.
var kind ErrorKind
if !errors.As(test.err, &kind) {
t.Errorf("%s: unable to unwrap to error kind", test.name)
continue
}
if kind != test.wantAs {
t.Errorf("%s: unexpected unwrapped error kind -- got %v, want %v",
test.name, kind, test.wantAs)
continue
}
}
}