From 02dbd0856a34629c22c86fc81535dd7e3bafe6a1 Mon Sep 17 00:00:00 2001 From: ascandone Date: Wed, 7 Aug 2024 14:13:39 +0200 Subject: [PATCH] impl specs --- interpreter/interpreter.go | 5 ++++ interpreter/interpreter_error.go | 6 +++++ interpreter/interpreter_test.go | 46 ++++++++++++++------------------ 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/interpreter/interpreter.go b/interpreter/interpreter.go index c66743a..d792f80 100644 --- a/interpreter/interpreter.go +++ b/interpreter/interpreter.go @@ -275,6 +275,11 @@ func (st *programState) runSendStatement(statement parser.SendStatement) ([]Post return nil, err } + monetaryAmt := big.Int(monetary.Amount) + if monetaryAmt.Cmp(big.NewInt(0)) == -1 { + return nil, NegativeAmountErr{Amount: monetary.Amount} + } + sentTotal, err := st.trySending(statement.Source, *monetary) if err != nil { return nil, err diff --git a/interpreter/interpreter_error.go b/interpreter/interpreter_error.go index 91b9c50..dc9cfaf 100644 --- a/interpreter/interpreter_error.go +++ b/interpreter/interpreter_error.go @@ -63,3 +63,9 @@ type InvalidTypeErr struct { func (e InvalidTypeErr) Error() string { return fmt.Sprintf("This type does not exist: %s", e.Name) } + +type NegativeAmountErr struct{ Amount MonetaryInt } + +func (e NegativeAmountErr) Error() string { + return fmt.Sprintf("Cannot send negative amount: %s", e.Amount.String()) +} diff --git a/interpreter/interpreter_test.go b/interpreter/interpreter_test.go index c2ad25e..58b6cb3 100644 --- a/interpreter/interpreter_test.go +++ b/interpreter/interpreter_test.go @@ -64,9 +64,6 @@ func test(t *testing.T, testCase TestCase) { expected := testCase.expected if expected.Error != nil { assert.Equal(t, err, expected.Error) - if expected.ErrorContains != "" { - require.ErrorContains(t, err, expected.ErrorContains) - } } else { require.NoError(t, err) } @@ -87,10 +84,9 @@ func test(t *testing.T, testCase TestCase) { } type CaseResult struct { - Postings []machine.Posting - Metadata map[string]machine.Value - Error error - ErrorContains string + Postings []machine.Posting + Metadata map[string]machine.Value + Error error } type Posting = machine.Posting @@ -1127,25 +1123,23 @@ func TestVariableBalance(t *testing.T) { test(t, tc) }) - // TODO - // t.Run("send negative monetary", func(t *testing.T) { - // tc := NewTestCase() - // script = ` - // vars { - // monetary $amount = balance(@world, USD/2) - // } - // send $amount ( - // source = @A - // destination = @B - // )` - // tc.compile(t, script) - // tc.setBalance("world", "USD/2", -40) - // tc.expected = CaseResult{ - // Error: &machine.ErrNegativeAmount{}, - // ErrorContains: "must be non-negative", - // } - // test(t, tc) - // }) + t.Run("send negative monetary", func(t *testing.T) { + tc := NewTestCase() + script = ` + vars { + monetary $amount = balance(@world, USD/2) + } + send $amount ( + source = @A + destination = @B + )` + tc.compile(t, script) + tc.setBalance("world", "USD/2", -40) + tc.expected = CaseResult{ + Error: machine.NegativeAmountErr{Amount: machine.NewMonetaryInt(-40)}, + } + test(t, tc) + }) } // TODO TestVariablesParsing, TestSetVarsFromJSON, TestResolveResources, TestResolveBalances, TestMachine