chanfunding: match error string when testing CoinSelectSubtractFees
This commit is contained in:
parent
493bc27ec2
commit
0e2d6dc0a9
@ -2,6 +2,7 @@ package chanfunding
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
@ -209,6 +210,12 @@ func TestCoinSelectSubtractFees(t *testing.T) {
|
||||
const dustLimit = btcutil.Amount(1000)
|
||||
const dust = btcutil.Amount(100)
|
||||
|
||||
// removeAmounts replaces any amounts in string with "<amt>".
|
||||
removeAmounts := func(s string) string {
|
||||
re := regexp.MustCompile(`[[:digit:]]+\.?[[:digit:]]*`)
|
||||
return re.ReplaceAllString(s, "<amt>")
|
||||
}
|
||||
|
||||
type testCase struct {
|
||||
name string
|
||||
highFee bool
|
||||
@ -218,7 +225,7 @@ func TestCoinSelectSubtractFees(t *testing.T) {
|
||||
expectedInput []btcutil.Amount
|
||||
expectedFundingAmt btcutil.Amount
|
||||
expectedChange btcutil.Amount
|
||||
expectErr bool
|
||||
expectErr string
|
||||
}
|
||||
|
||||
testCases := []testCase{
|
||||
@ -279,7 +286,8 @@ func TestCoinSelectSubtractFees(t *testing.T) {
|
||||
},
|
||||
spendValue: fundingFee(feeRate, 1, false) + dust,
|
||||
|
||||
expectErr: true,
|
||||
expectErr: "output amount(<amt> BTC) after subtracting " +
|
||||
"fees(<amt> BTC) below dust limit(<amt> BTC)",
|
||||
},
|
||||
{
|
||||
// After subtracting fees, the resulting change output
|
||||
@ -355,7 +363,7 @@ func TestCoinSelectSubtractFees(t *testing.T) {
|
||||
},
|
||||
spendValue: 5 * fundingFee(highFeeRate, 1, false),
|
||||
|
||||
expectErr: true,
|
||||
expectErr: "fee <amt> BTC on total output value <amt> BTC",
|
||||
},
|
||||
}
|
||||
|
||||
@ -371,18 +379,27 @@ func TestCoinSelectSubtractFees(t *testing.T) {
|
||||
selected, localFundingAmt, changeAmt, err := CoinSelectSubtractFees(
|
||||
feeRate, test.spendValue, dustLimit, test.coins,
|
||||
)
|
||||
if !test.expectErr && err != nil {
|
||||
if err != nil {
|
||||
switch {
|
||||
case test.expectErr == "":
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
if test.expectErr && err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
case test.expectErr != removeAmounts(err.Error()):
|
||||
t.Fatalf("expected error '%v', got '%v'",
|
||||
test.expectErr,
|
||||
removeAmounts(err.Error()))
|
||||
|
||||
// If we got an expected error, there is nothing more to test.
|
||||
if test.expectErr {
|
||||
// If we got an expected error, there is
|
||||
// nothing more to test.
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Check that there was no expected error we missed.
|
||||
if test.expectErr != "" {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
|
||||
// Check that the selected inputs match what we expect.
|
||||
if len(selected) != len(test.expectedInput) {
|
||||
|
Loading…
Reference in New Issue
Block a user