From c6eb586bb07be13f4c2efb0e1dbfff5aea34d83b Mon Sep 17 00:00:00 2001 From: Luca Bernstein Date: Sun, 4 Feb 2024 15:12:34 +0100 Subject: [PATCH] Allow negative values to invert transaction Solves #228 --- bot/transactionBuilder.go | 4 ---- bot/transactionBuilder_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/bot/transactionBuilder.go b/bot/transactionBuilder.go index 76707dd..32a8bfb 100644 --- a/bot/transactionBuilder.go +++ b/bot/transactionBuilder.go @@ -67,10 +67,6 @@ func HandleFloat(m *tb.Message) (string, error) { if err != nil { return "", fmt.Errorf("parsing failed at value '%s': %s", value, err.Error()) } - if v < 0 { - c.LogLocalf(INFO, nil, "Got negative value. Inverting.") - v *= -1 - } c.LogLocalf(TRACE, nil, "Handled float: '%s' -> %f", amount, v) values = append(values, v) } diff --git a/bot/transactionBuilder_test.go b/bot/transactionBuilder_test.go index b58860d..a748448 100644 --- a/bot/transactionBuilder_test.go +++ b/bot/transactionBuilder_test.go @@ -193,6 +193,35 @@ func TestTransactionBuilding(t *testing.T) { `, "Templated string should be filled with variables as expected.") } +func TestTransactionBuildingWithNegativeAmount(t *testing.T) { + const twoWayTemplate = `${date} * "${description}"${tag} + ${account:from:the money came *from*} ${-amount} + ${account:to:the money went *to*} ${amount}` + + tx, err := bot.CreateSimpleTx("", twoWayTemplate) + if err != nil { + t.Errorf("Error creating simple tx: %s", err.Error()) + } + tx.Input(&tb.Message{Text: "-34"}) // amount + tx.Input(&tb.Message{Text: "Birthday gift from grandma"}) // description + tx.Input(&tb.Message{Text: "Assets:Wallet"}) // from + tx.Input(&tb.Message{Text: "Income:Gifts"}) // to + + if !tx.IsDone() { + t.Errorf("With given input transaction data should be complete for SimpleTx") + } + + templated, err := tx.FillTemplate("USD", "", 0) + if err != nil { + t.Errorf("There should be no error raised during templating: %s", err.Error()) + } + today := time.Now().Format(helpers.BEANCOUNT_DATE_FORMAT) + helpers.TestExpect(t, templated, today+` * "Birthday gift from grandma" + Assets:Wallet 34.00 USD + Income:Gifts -34.00 USD +`, "Templated string should be filled with variables as expected.") +} + func TestTransactionBuildingCustomCurrencyInAmount(t *testing.T) { tx, err := bot.CreateSimpleTx("", bot.TEMPLATE_SIMPLE_DEFAULT) if err != nil {