github.com/expr-lang/expr@v1.16.9/test/interface_method/interface_method_test.go (about) 1 package interface_method_test 2 3 import ( 4 "testing" 5 6 "github.com/expr-lang/expr/internal/testify/assert" 7 "github.com/expr-lang/expr/internal/testify/require" 8 9 "github.com/expr-lang/expr" 10 ) 11 12 type Bar interface { 13 Bar() int 14 } 15 16 type FooImpl struct{} 17 18 func (f FooImpl) Foo() Bar { 19 return BarImpl{} 20 } 21 22 type BarImpl struct{} 23 24 // Aba is a special method that is not part of the Bar interface, 25 // but is used to test that the correct method is called. "Aba" name 26 // is chosen to be before "Bar" in the alphabet. 27 func (b BarImpl) Aba() bool { 28 return true 29 } 30 31 func (b BarImpl) Bar() int { 32 return 42 33 } 34 35 func TestInterfaceMethod(t *testing.T) { 36 require.True(t, BarImpl{}.Aba()) 37 require.True(t, BarImpl{}.Bar() == 42) 38 39 env := map[string]any{ 40 "var": FooImpl{}, 41 } 42 p, err := expr.Compile(`var.Foo().Bar()`, expr.Env(env)) 43 assert.NoError(t, err) 44 45 out, err := expr.Run(p, env) 46 assert.NoError(t, err) 47 assert.Equal(t, 42, out) 48 }