dcrd/blockchain/standalone/error_test.go
Dave Collins baf32f0cfc
blockchain: Prepare v4.0.0.
This updates the blockchain module dependencies, the copyright year in
the files modified since the previous release, and serves as a base for
blockchain/v4.0.0.

The updated direct dependencies in this commit are as follows:

- github.com/decred/dcrd/blockchain/stake/v4@v4.0.0
- github.com/decred/dcrd/blockchain/standalone/v2@v2.1.0
- github.com/decred/dcrd/database/v3@v3.0.0
- github.com/decred/dcrd/dcrutil/v4@v4.0.0
- github.com/decred/dcrd/gcs/v3@v3.0.0
- github.com/decred/dcrd/lru@v1.1.1
- github.com/syndtr/goleveldb@v1.0.1-0.20210819022825-2ae1ddf74ef7

The full list of updated direct dependencies since the previous
blockchain/v3.0.3 release are as follows:

- github.com/decred/dcrd/blockchain/stake/v4@v4.0.0
- github.com/decred/dcrd/blockchain/standalone/v2@v2.1.0
- github.com/decred/dcrd/chaincfg/chainhash@v1.0.3
- github.com/decred/dcrd/chaincfg/v3@v3.1.0
- github.com/decred/dcrd/database/v3@v3.0.0
- github.com/decred/dcrd/dcrec/secp256k1/v4@v4.0.1
- github.com/decred/dcrd/dcrutil/v4@v4.0.0
- github.com/decred/dcrd/gcs/v3@v3.0.0
- github.com/decred/dcrd/lru@v1.1.1
- github.com/decred/dcrd/txscript/v4@v4.0.0
- github.com/decred/dcrd/wire@v1.5.0
- github.com/decred/slog@v1.2.0
- github.com/syndtr/goleveldb@v1.0.1-0.20210819022825-2ae1ddf74ef7

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-12-17 13:37:03 -06:00

141 lines
3.9 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 standalone
import (
"errors"
"io"
"testing"
)
// TestErrorKindStringer tests the stringized output for the ErrorKind type.
func TestErrorKindStringer(t *testing.T) {
tests := []struct {
in ErrorKind
want string
}{
{ErrUnexpectedDifficulty, "ErrUnexpectedDifficulty"},
{ErrHighHash, "ErrHighHash"},
{ErrInvalidTSpendExpiry, "ErrInvalidTSpendExpiry"},
}
t.Logf("Running %d tests", len(tests))
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
}
}
}
// TestRuleError tests the error output for the RuleError type.
func TestRuleError(t *testing.T) {
tests := []struct {
in RuleError
want string
}{{
RuleError{Description: "unexpected difficulty"},
"unexpected difficulty",
}, {
RuleError{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("%d: got: %s want: %s", i, result, test.want)
continue
}
}
}
// TestRuleErrorKindIsAs ensures both ErrorKind and RuleError can be
// identified as being a specific error kind via errors.Is and unwrapped
// via errors.As.
func TestRuleErrorKindIsAs(t *testing.T) {
tests := []struct {
name string
err error
target error
wantMatch bool
wantAs ErrorKind
}{{
name: "ErrUnexpectedDifficulty == ErrUnexpectedDifficulty",
err: ErrUnexpectedDifficulty,
target: ErrUnexpectedDifficulty,
wantMatch: true,
wantAs: ErrUnexpectedDifficulty,
}, {
name: "RuleError.ErrUnexpectedDifficulty == ErrUnexpectedDifficulty",
err: ruleError(ErrUnexpectedDifficulty, ""),
target: ErrUnexpectedDifficulty,
wantMatch: true,
wantAs: ErrUnexpectedDifficulty,
}, {
name: "RuleError.ErrUnexpectedDifficulty == RuleError.ErrUnexpectedDifficulty",
err: ruleError(ErrUnexpectedDifficulty, ""),
target: ruleError(ErrUnexpectedDifficulty, ""),
wantMatch: true,
wantAs: ErrUnexpectedDifficulty,
}, {
name: "ErrUnexpectedDifficulty != ErrHighHash",
err: ErrUnexpectedDifficulty,
target: ErrHighHash,
wantMatch: false,
wantAs: ErrUnexpectedDifficulty,
}, {
name: "RuleError.ErrUnexpectedDifficulty != ErrHighHash",
err: ruleError(ErrUnexpectedDifficulty, ""),
target: ErrHighHash,
wantMatch: false,
wantAs: ErrUnexpectedDifficulty,
}, {
name: "ErrUnexpectedDifficulty != RuleError.ErrHighHash",
err: ErrUnexpectedDifficulty,
target: ruleError(ErrHighHash, ""),
wantMatch: false,
wantAs: ErrUnexpectedDifficulty,
}, {
name: "RuleError.ErrUnexpectedDifficulty != RuleError.ErrHighHash",
err: ruleError(ErrUnexpectedDifficulty, ""),
target: ruleError(ErrHighHash, ""),
wantMatch: false,
wantAs: ErrUnexpectedDifficulty,
}, {
name: "RuleError.ErrUnexpectedDifficulty != io.EOF",
err: ruleError(ErrUnexpectedDifficulty, ""),
target: io.EOF,
wantMatch: false,
wantAs: ErrUnexpectedDifficulty,
}}
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
}
}
}