-
-
Notifications
You must be signed in to change notification settings - Fork 388
/
issues584_test.go
73 lines (55 loc) · 1.3 KB
/
issues584_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
package issues584_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/expr-lang/expr"
)
type Env struct{}
type Program struct {
}
func (p *Program) Foo() Value {
return func(e *Env) float64 {
return 5
}
}
func (p *Program) Bar() Value {
return func(e *Env) float64 {
return 100
}
}
func (p *Program) AndCondition(a, b Condition) Conditions {
return Conditions{a, b}
}
func (p *Program) AndConditions(a Conditions, b Condition) Conditions {
return append(a, b)
}
func (p *Program) ValueGreaterThan_float(v Value, i float64) Condition {
return func(e *Env) bool {
realized := v(e)
return realized > i
}
}
func (p *Program) ValueLessThan_float(v Value, i float64) Condition {
return func(e *Env) bool {
realized := v(e)
return realized < i
}
}
type Condition func(e *Env) bool
type Conditions []Condition
type Value func(e *Env) float64
func TestIssue584(t *testing.T) {
code := `Foo() > 1.5 and Bar() < 200.0`
p := &Program{}
opt := []expr.Option{
expr.Env(p),
expr.Operator("and", "AndCondition", "AndConditions"),
expr.Operator(">", "ValueGreaterThan_float"),
expr.Operator("<", "ValueLessThan_float"),
}
program, err := expr.Compile(code, opt...)
assert.Nil(t, err)
state, err := expr.Run(program, p)
assert.Nil(t, err)
assert.NotNil(t, state)
}