forked from expr-lang/expr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface_method_test.go
More file actions
48 lines (36 loc) · 924 Bytes
/
Copy pathinterface_method_test.go
File metadata and controls
48 lines (36 loc) · 924 Bytes
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
package interface_method_test
import (
"testing"
"github.com/expr-lang/expr/internal/testify/assert"
"github.com/expr-lang/expr/internal/testify/require"
"github.com/expr-lang/expr"
)
type Bar interface {
Bar() int
}
type FooImpl struct{}
func (f FooImpl) Foo() Bar {
return BarImpl{}
}
type BarImpl struct{}
// Aba is a special method that is not part of the Bar interface,
// but is used to test that the correct method is called. "Aba" name
// is chosen to be before "Bar" in the alphabet.
func (b BarImpl) Aba() bool {
return true
}
func (b BarImpl) Bar() int {
return 42
}
func TestInterfaceMethod(t *testing.T) {
require.True(t, BarImpl{}.Aba())
require.True(t, BarImpl{}.Bar() == 42)
env := map[string]any{
"var": FooImpl{},
}
p, err := expr.Compile(`var.Foo().Bar()`, expr.Env(env))
assert.NoError(t, err)
out, err := expr.Run(p, env)
assert.NoError(t, err)
assert.Equal(t, 42, out)
}