-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator_test.go
94 lines (86 loc) · 1.87 KB
/
calculator_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
package govaluate
import (
"fmt"
"testing"
)
type CalculatorOutPut struct {
IntValue float64 `json:"int_value"` // 如果用默认的运算符,返回值都会是float64类型 如果需要指定类型,就需要编写元函数
FloatValue float64 `json:"float_value"`
StringValue string `json:"string_value"`
}
type CalculatorInPut struct {
IntValue float64 `json:"int_value"`
FloatValue float64 `json:"float_value"`
StringValue string `json:"string_value"`
}
func TestNewCalculator(t *testing.T) {
cal := NewCalculator()
cal.SetCalculatorRelations([]CalculatorRelation{
{
OutputField: "IntValue",
Expression: "IntValue+1",
},
{
OutputField: "FloatValue",
Expression: "FloatValue+3",
},
{
OutputField: "StringValue",
Expression: "'a'",
},
})
res := CalculatorOutPut{}
e := cal.Expression(CalculatorInPut{}, &res)
t.Logf("%+v", e)
fmt.Println(res)
}
type MrpDict struct {
Basic struct {
Level string
Status string
}
}
func TestNewCalculatorBool(t *testing.T) {
cal := NewCalculator()
cal.SetCalculatorRelations([]CalculatorRelation{
{
OutputField: "",
Expression: "[Basic.Level] == 'Top1' && [Basic.Status] == 'New'",
},
})
res := false
e := cal.Expression(MrpDict{
Basic: struct {
Level string
Status string
}{Level: "Top1", Status: "New"},
}, &res)
t.Logf("%+v %+v", e, res)
}
func BenchmarkNewCalculator(b *testing.B) {
cal := NewCalculator()
cal.SetCalculatorRelations([]CalculatorRelation{
{
OutputField: "IntValue",
Expression: "IntValue+1",
},
{
OutputField: "FloatValue",
Expression: "FloatValue+3",
},
{
OutputField: "StringValue",
Expression: "'a'",
},
{
OutputField: "StringValue",
Expression: "'c'",
},
})
res := CalculatorOutPut{}
for i := 0; i < b.N; i++ {
//3次运算 赋值
_ = cal.Expression(CalculatorInPut{}, &res)
}
b.ReportAllocs()
}