Skip to content

Commit 08f979a

Browse files
committed
added tests
1 parent 8279b97 commit 08f979a

File tree

2 files changed

+138
-2
lines changed

2 files changed

+138
-2
lines changed

internal/interpreter/interpreter.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func parseVar(type_ string, rawValue string, r parser.Range) (Value, Interpreter
9191
case analysis.TypeAccount:
9292
return AccountAddress(rawValue), nil
9393
case analysis.TypePortion:
94-
bi, err := parsePortionSpecific(rawValue)
94+
bi, err := ParsePortionSpecific(rawValue)
9595
if err != nil {
9696
return nil, err
9797
}
@@ -861,7 +861,7 @@ func (st *programState) evaluateSentAmt(sentValue parser.SentValue) (*string, *b
861861
// slightly edited copy-paste from:
862862
// https://github.com/formancehq/ledger/blob/b188d0c80eadaab5024d74edc967c7005e155f7c/internal/machine/portion.go#L57
863863

864-
func parsePortionSpecific(input string) (*big.Rat, InterpreterError) {
864+
func ParsePortionSpecific(input string) (*big.Rat, InterpreterError) {
865865
var res *big.Rat
866866
var ok bool
867867

@@ -890,5 +890,10 @@ func parsePortionSpecific(input string) (*big.Rat, InterpreterError) {
890890
if res == nil {
891891
return nil, BadPortionParsingErr{Reason: "invalid format", Source: input}
892892
}
893+
894+
if res.Cmp(big.NewRat(0, 1)) == -1 || res.Cmp(big.NewRat(1, 1)) == 1 {
895+
return nil, BadPortionParsingErr{Reason: "portion must be between 0% and 100% inclusive", Source: input}
896+
}
897+
893898
return res, nil
894899
}

internal/interpreter/portion_test.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package interpreter_test
2+
3+
import (
4+
"math/big"
5+
"strings"
6+
"testing"
7+
8+
"github.com/formancehq/numscript/internal/interpreter"
9+
)
10+
11+
func TestBetween0And1Inclusive(t *testing.T) {
12+
tests := []struct {
13+
in string
14+
want *big.Rat
15+
wantErr bool
16+
}{
17+
{
18+
in: "0%",
19+
want: big.NewRat(0, 1),
20+
},
21+
{
22+
in: "0.0%",
23+
want: big.NewRat(0, 1),
24+
},
25+
{
26+
in: "0/1",
27+
want: big.NewRat(0, 1),
28+
},
29+
{
30+
in: "0/25",
31+
want: big.NewRat(0, 1),
32+
},
33+
{
34+
in: "0/100",
35+
want: big.NewRat(0, 1),
36+
},
37+
{
38+
in: "1%",
39+
want: big.NewRat(1, 100),
40+
},
41+
{
42+
in: "1/100",
43+
want: big.NewRat(1, 100),
44+
},
45+
{
46+
in: "10/1000",
47+
want: big.NewRat(1, 100),
48+
},
49+
{
50+
in: "50/100",
51+
want: big.NewRat(50, 100),
52+
},
53+
{
54+
in: "50%",
55+
want: big.NewRat(50, 100),
56+
},
57+
{
58+
in: "50.0%",
59+
want: big.NewRat(50, 100),
60+
},
61+
{
62+
in: "1/1",
63+
want: big.NewRat(1, 1),
64+
},
65+
{
66+
in: "100/100",
67+
want: big.NewRat(1, 1),
68+
},
69+
{
70+
in: "100.0%",
71+
want: big.NewRat(1, 1),
72+
},
73+
{
74+
in: "100%",
75+
want: big.NewRat(1, 1),
76+
},
77+
// Now for the failures. We don't check negative numbers in this test because
78+
// those are a parsing failure, not a range failure.
79+
{
80+
in: "100.1%",
81+
wantErr: true,
82+
},
83+
{
84+
in: "101%",
85+
wantErr: true,
86+
},
87+
{
88+
in: "101/100",
89+
wantErr: true,
90+
},
91+
{
92+
in: "2/1",
93+
wantErr: true,
94+
},
95+
{
96+
in: "3/2",
97+
wantErr: true,
98+
},
99+
}
100+
101+
for _, test := range tests {
102+
t.Run(test.in, func(t *testing.T) {
103+
got, err := interpreter.ParsePortionSpecific(test.in)
104+
if test.wantErr {
105+
if err == nil {
106+
t.Fatal("should have errored")
107+
}
108+
if !strings.Contains(err.Error(), "between") {
109+
t.Fatal("wrong error")
110+
}
111+
return
112+
}
113+
if err != nil {
114+
t.Fatalf("ParsePortionSpecific(%q): %v", test.in, err)
115+
}
116+
if test.want.Cmp(got) != 0 {
117+
t.Fatalf("ParsePortionSpecific(%q) = %q, want %q", test.in, got, test.want)
118+
}
119+
})
120+
}
121+
}
122+
123+
func TestInvalidFormat(t *testing.T) {
124+
_, err := interpreter.ParsePortionSpecific("this is not a portion")
125+
if err == nil {
126+
t.Fatal("should have errored")
127+
}
128+
if !strings.Contains(err.Error(), "format") {
129+
t.Fatal("wrong error")
130+
}
131+
}

0 commit comments

Comments
 (0)