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 ( import (
"encoding/hex" "encoding/hex"
"regexp"
"testing" "testing"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
@ -209,6 +210,12 @@ func TestCoinSelectSubtractFees(t *testing.T) {
const dustLimit = btcutil.Amount(1000) const dustLimit = btcutil.Amount(1000)
const dust = btcutil.Amount(100) 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 { type testCase struct {
name string name string
highFee bool highFee bool
@ -218,7 +225,7 @@ func TestCoinSelectSubtractFees(t *testing.T) {
expectedInput []btcutil.Amount expectedInput []btcutil.Amount
expectedFundingAmt btcutil.Amount expectedFundingAmt btcutil.Amount
expectedChange btcutil.Amount expectedChange btcutil.Amount
expectErr bool expectErr string
} }
testCases := []testCase{ testCases := []testCase{
@ -279,7 +286,8 @@ func TestCoinSelectSubtractFees(t *testing.T) {
}, },
spendValue: fundingFee(feeRate, 1, false) + dust, 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 // After subtracting fees, the resulting change output
@ -355,7 +363,7 @@ func TestCoinSelectSubtractFees(t *testing.T) {
}, },
spendValue: 5 * fundingFee(highFeeRate, 1, false), 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( selected, localFundingAmt, changeAmt, err := CoinSelectSubtractFees(
feeRate, test.spendValue, dustLimit, test.coins, feeRate, test.spendValue, dustLimit, test.coins,
) )
if !test.expectErr && err != nil { if err != nil {
t.Fatalf(err.Error()) 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") 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. // Check that the selected inputs match what we expect.
if len(selected) != len(test.expectedInput) { if len(selected) != len(test.expectedInput) {
t.Fatalf("expected %v inputs, got %v", t.Fatalf("expected %v inputs, got %v",