chanfunding: match error string when testing CoinSelectSubtractFees

This commit is contained in:
Bjarne Magnussen 2020-08-28 11:50:57 +02:00
parent 493bc27ec2
commit 0e2d6dc0a9
No known key found for this signature in database
GPG Key ID: B0A9ADF6B24CE67F

@ -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,19 +379,28 @@ func TestCoinSelectSubtractFees(t *testing.T) {
selected, localFundingAmt, changeAmt, err := CoinSelectSubtractFees(
feeRate, test.spendValue, dustLimit, test.coins,
)
if !test.expectErr && err != nil {
t.Fatalf(err.Error())
if err != nil {
switch {
case test.expectErr == "":
t.Fatalf(err.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.
default:
return
}
}
if test.expectErr && err == nil {
// Check that there was no expected error we missed.
if test.expectErr != "" {
t.Fatalf("expected error")
}
// If we got an expected error, there is nothing more to test.
if test.expectErr {
return
}
// Check that the selected inputs match what we expect.
if len(selected) != len(test.expectedInput) {
t.Fatalf("expected %v inputs, got %v",