Skip to content

Commit

Permalink
impl specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Aug 7, 2024
1 parent 524f441 commit 02dbd08
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
5 changes: 5 additions & 0 deletions interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions interpreter/interpreter_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
46 changes: 20 additions & 26 deletions interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 02dbd08

Please sign in to comment.