github.com/expr-lang/expr@v1.16.9/test/operator/issues584/issues584_test.go (about) 1 package issues584_test 2 3 import ( 4 "testing" 5 6 "github.com/expr-lang/expr/internal/testify/assert" 7 8 "github.com/expr-lang/expr" 9 ) 10 11 type Env struct{} 12 13 type Program struct { 14 } 15 16 func (p *Program) Foo() Value { 17 return func(e *Env) float64 { 18 return 5 19 } 20 } 21 22 func (p *Program) Bar() Value { 23 return func(e *Env) float64 { 24 return 100 25 } 26 } 27 28 func (p *Program) AndCondition(a, b Condition) Conditions { 29 return Conditions{a, b} 30 } 31 32 func (p *Program) AndConditions(a Conditions, b Condition) Conditions { 33 return append(a, b) 34 } 35 36 func (p *Program) ValueGreaterThan_float(v Value, i float64) Condition { 37 return func(e *Env) bool { 38 realized := v(e) 39 return realized > i 40 } 41 } 42 43 func (p *Program) ValueLessThan_float(v Value, i float64) Condition { 44 return func(e *Env) bool { 45 realized := v(e) 46 return realized < i 47 } 48 } 49 50 type Condition func(e *Env) bool 51 type Conditions []Condition 52 53 type Value func(e *Env) float64 54 55 func TestIssue584(t *testing.T) { 56 code := `Foo() > 1.5 and Bar() < 200.0` 57 58 p := &Program{} 59 60 opt := []expr.Option{ 61 expr.Env(p), 62 expr.Operator("and", "AndCondition", "AndConditions"), 63 expr.Operator(">", "ValueGreaterThan_float"), 64 expr.Operator("<", "ValueLessThan_float"), 65 } 66 67 program, err := expr.Compile(code, opt...) 68 assert.Nil(t, err) 69 70 state, err := expr.Run(program, p) 71 assert.Nil(t, err) 72 assert.NotNil(t, state) 73 }