diff --git a/lnwallet/chanfunding/coin_select_test.go b/lnwallet/chanfunding/coin_select_test.go index 3b02c34b0..c26dd7567 100644 --- a/lnwallet/chanfunding/coin_select_test.go +++ b/lnwallet/chanfunding/coin_select_test.go @@ -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 "". + removeAmounts := func(s string) string { + re := regexp.MustCompile(`[[:digit:]]+\.?[[:digit:]]*`) + return re.ReplaceAllString(s, "") + } + 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( BTC) after subtracting " + + "fees( BTC) below dust limit( 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 BTC on total output value 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",