dcrd/dcrjson/error_test.go
Dave Collins 36c1823969
dcrjson: Prepare v4.0.1.
This updates the dcrjson module dependencies, the copyright year in the
files modified since the previous release, and serves as a base for
dcrjson/v4.0.1.

The updated direct dependencies in this commit are as follows:

- github.com/decred/dcrd/chaincfg/chainhash@v1.0.4

The updated indirect dependencies in this commit are as follows:

- github.com/decred/dcrd/crypto/blake256@v1.0.1

The full list of updated direct and indirect dependencies since the
previous dcrjson/v4.0.0 release are the same as above.

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.
2023-04-14 10:04:08 -05:00

152 lines
4.1 KiB
Go

// Copyright (c) 2014 The btcsuite developers
// Copyright (c) 2015-2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package dcrjson
import (
"errors"
"io"
"testing"
)
// TestErrorKindStringer tests the stringized output for the ErrorKind type.
func TestErrorKindStringer(t *testing.T) {
t.Parallel()
tests := []struct {
in ErrorKind
want string
}{
{ErrDuplicateMethod, "ErrDuplicateMethod"},
{ErrInvalidUsageFlags, "ErrInvalidUsageFlags"},
{ErrInvalidType, "ErrInvalidType"},
{ErrEmbeddedType, "ErrEmbeddedType"},
{ErrUnexportedField, "ErrUnexportedField"},
{ErrUnsupportedFieldType, "ErrUnsupportedFieldType"},
{ErrNonOptionalField, "ErrNonOptionalField"},
{ErrNonOptionalDefault, "ErrNonOptionalDefault"},
{ErrMismatchedDefault, "ErrMismatchedDefault"},
{ErrUnregisteredMethod, "ErrUnregisteredMethod"},
{ErrNumParams, "ErrNumParams"},
{ErrMissingDescription, "ErrMissingDescription"},
}
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) {
t.Parallel()
tests := []struct {
in Error
want string
}{{
Error{Description: "some error"},
"some error",
}, {
Error{Description: "human-readable error"},
"human-readable error",
}}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
result := test.in.Error()
if result != test.want {
t.Errorf("Error #%d\n 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: "ErrDuplicateMethod == ErrDuplicateMethod",
err: ErrDuplicateMethod,
target: ErrDuplicateMethod,
wantMatch: true,
wantAs: ErrDuplicateMethod,
}, {
name: "Error.ErrDuplicateMethod == ErrDuplicateMethod",
err: makeError(ErrDuplicateMethod, ""),
target: ErrDuplicateMethod,
wantMatch: true,
wantAs: ErrDuplicateMethod,
}, {
name: "Error.ErrDuplicateMethod == Error.ErrDuplicateMethod",
err: makeError(ErrDuplicateMethod, ""),
target: makeError(ErrDuplicateMethod, ""),
wantMatch: true,
wantAs: ErrDuplicateMethod,
}, {
name: "ErrDuplicateMethod != ErrInvalidType",
err: ErrDuplicateMethod,
target: ErrInvalidType,
wantMatch: false,
wantAs: ErrDuplicateMethod,
}, {
name: "Error.ErrDuplicateMethod != ErrInvalidType",
err: makeError(ErrDuplicateMethod, ""),
target: ErrInvalidType,
wantMatch: false,
wantAs: ErrDuplicateMethod,
}, {
name: "ErrDuplicateMethod != Error.ErrInvalidType",
err: ErrDuplicateMethod,
target: makeError(ErrInvalidType, ""),
wantMatch: false,
wantAs: ErrDuplicateMethod,
}, {
name: "Error.ErrDuplicateMethod != Error.ErrInvalidType",
err: makeError(ErrDuplicateMethod, ""),
target: makeError(ErrInvalidType, ""),
wantMatch: false,
wantAs: ErrDuplicateMethod,
}, {
name: "Error.ErrUnregisteredMethod != io.EOF",
err: makeError(ErrUnregisteredMethod, ""),
target: io.EOF,
wantMatch: false,
wantAs: ErrUnregisteredMethod,
}}
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
}
}
}