-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintrp_test.go
238 lines (226 loc) · 5.98 KB
/
intrp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package calcu
import (
"bytes"
"fmt"
"reflect"
"strconv"
"testing"
)
func TestGetFuncInfo(t *testing.T) {
add := func(a int, b MeasureValue, c *MeasureValue) (n int, err error, mv *MeasureValue) {
return a, nil, nil
}
fi := getFuncInfo(add)
if len(fi.paramTypes) != 3 {
t.Fatalf("expect 3 params, got %d", len(fi.paramTypes))
}
if len(fi.returnTypes) != 3 {
t.Fatalf("expect 3 returns, got %d", len(fi.returnTypes))
}
}
func TestInterpreterSimple(t *testing.T) {
exprs := `
CO2 = activity_value * CO2Factor;
CH2 = activity_value * CH2Factor;
N2O = activity_value * N2OFactor;
GHG = CO2 + CH2 + N2O;
a = CO2 * CH2 * (1 + 2);
b = CO2 * CH2 * (1 - 2);
c = CO2 * CH2 * 2/1;
d = CO2 * CH2 * (2/1);
e = CO2 * CH2 / (2/1);
f = CO2 * CH2 / (2/1);
print(CO2, CH2, N2O, GHG, a, b, c, d, e, f);
`
vars := map[string]string{
"activity_value": "1(10^3m3)",
"CO2Factor": "1.1E-04Gg/10^3m3",
"CH2Factor": "7.2E-06Gg/10^3m3",
"N2OFactor": "1.1E-03Gg/10^3m3",
}
intrp, err := NewInterpreter(vars)
if err != nil {
t.Fatal(err)
}
rd := bytes.NewBufferString(exprs)
outvars, err := intrp.Interpret(rd)
if err != nil {
t.Fatal(err)
}
co2 := outvars["CO2"]
ch2 := outvars["CH2"]
n2o := outvars["N2O"]
ghg := outvars["GHG"]
a := outvars["a"]
b := outvars["b"]
c := outvars["c"]
d := outvars["d"]
e := outvars["e"]
f := outvars["f"]
gots := []string{co2.String(), ch2.String(), n2o.String(), ghg.String(),
a.String(), b.String(), c.String(), d.String(), e.String(), f.String()}
expected := []string{"110kg", "7.2kg", "1100kg", "1217.2kg",
"2376kg", "-792kg", "1584kg", "1584kg", "396kg", "396kg"}
if !reflect.DeepEqual(expected, gots) {
t.Fatalf("exptectd: %v, got: %v", expected, gots)
}
}
func TestInterpreterUnitless(t *testing.T) {
exprs := `
CH4 = activity_value * FractionofGassyCoalMines * CH4Factor * CH4ConversionFactor;
GHG = CH4;
a = 1 * 1;
print(CH4, GHG, a);
`
vars := map[string]string{
"activity_value": "30",
"FractionofGassyCoalMines": "0.1",
"CH4Factor": "0.402m3",
"CH4ConversionFactor": "1.1E-03Gg/m3",
}
intrp, err := NewInterpreter(vars)
if err != nil {
t.Fatal(err)
}
rd := bytes.NewBufferString(exprs)
outvars, err := intrp.Interpret(rd)
if err != nil {
t.Fatal(err)
}
ch4 := outvars["CH4"]
ghg := outvars["GHG"]
a := outvars["a"]
gots := []string{ch4.String(), ghg.String(), a.String()}
expected := []string{"1326.6kg", "1326.6kg", "1"}
if !reflect.DeepEqual(expected, gots) {
t.Fatalf("exptectd: %v, got: %v", expected, gots)
}
}
func TestInterpreterReuseVar(t *testing.T) {
exprs := `
a = a + 1kg;
a = a + 2kg;
b = a;
b = b * b + 2kg;
print(a, b);
`
vars := map[string]string{"a": "1kg"}
intrp, err := NewInterpreter(vars)
if err != nil {
t.Fatal(err)
}
rd := bytes.NewBufferString(exprs)
outvars, err := intrp.Interpret(rd)
if err != nil {
t.Fatal(err)
}
expected := []string{"4kg", "18kg"}
a := outvars["a"].String()
b := outvars["b"].String()
gots := []string{a, b}
if !reflect.DeepEqual(expected, gots) {
t.Fatalf("expected: %s, got: %s", expected, gots)
}
}
func TestInterpreterSyntaxError(t *testing.T) {
cases := []struct {
expr string
ok bool
hint string
}{
{expr: "print(a);", ok: true},
{expr: `a = a + 2kg;`, ok: true},
{expr: `b = b + "10(10^3m3)";`, ok: true},
{expr: `a = a + "2kg";`, ok: true},
{expr: "print(a)", ok: false, hint: "have no ; in the end"},
{expr: "a = a + 1kg \n a = a + a", hint: "missing ; between lines"},
{expr: "a=1print(a);", ok: false, hint: "unexpected ident, missing ;"},
}
vars := map[string]string{"a": "1kg", "b": "1(10^3m3)"}
for i, c := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
intrp, err := NewInterpreter(vars)
if err != nil {
t.Fatal(err)
}
rd := bytes.NewBufferString(c.expr)
_, err = intrp.Interpret(rd)
switch c.ok {
case false:
if err == nil {
t.Fatalf("expected err, got nil")
}
t.Log(err)
case true:
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
}
})
}
}
func fAny(args ...interface{}) (*MeasureValue, error) {
// just use the num of args as an indicator
// to test return error or not.
if len(args) == 0 {
return nil, fmt.Errorf("i'm an error")
}
var outs []string
for _, a := range args {
outs = append(outs, fmt.Sprintf("%v", a))
}
fmt.Println(outs)
return mustMV("1kg", false), nil
}
func fTypes(s string, mv1, mv2, mv3, mv4 *MeasureValue) *MeasureValue {
fmt.Println(s, mv1, mv2, mv3, mv4)
return mv1
}
type Stub struct {
s string
}
func (s *Stub) mTypesF(str string, mv1, mv2, mv3, mv4 *MeasureValue) *MeasureValue {
fmt.Println(s.s, str, mv1, mv2, mv3, mv4)
return mv1
}
func fInt(a *MeasureValue) *MeasureValue {
return a
}
func TestInterpreterFuncs(t *testing.T) {
cases := []struct {
expr string
ok bool
hint string
}{
{expr: "print(a);", ok: true},
{expr: "fInt(1);", ok: true, hint: "int literal 1, will be wrapped as *MeasureValue"},
{expr: `fAny("1kg");`, ok: true, hint: `"1kg" is pass as *MeasureValue to func(because it a LITERALMV token`},
{expr: `fTypes("hello world", a, 1kg, "1kg", "10(10^3m3)");`, ok: true, hint: `"hello world" is pass as str to func(because it a LITERALSTR token`},
{expr: `mTypesF("hello world", a, 1kg, "1kg", "10(10^3m3)");`, ok: true, hint: `"hello world" is pass as str to func(because it a LITERALSTR token`},
{expr: `fAny("1kg", a);`, ok: true},
{expr: `fAny();`, ok: false, hint: "error raise by func"},
}
vars := map[string]string{"a": "1kg", "b": "1(10^3m3)"}
for i, c := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
stub := &Stub{s: "stateful method call"}
intrp, err := NewInterpreter(vars, fInt, fAny, fTypes, stub.mTypesF)
if err != nil {
t.Fatal(err)
}
rd := bytes.NewBufferString(c.expr)
_, err = intrp.Interpret(rd)
switch c.ok {
case false:
if err == nil {
t.Fatalf("expected err, got nil")
}
t.Log(err)
case true:
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
}
})
}
}