mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
+1
-1
@@ -1,4 +1,4 @@
|
||||
package interface_method_test
|
||||
package interface_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@@ -0,0 +1,40 @@
|
||||
package interface_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/expr-lang/expr"
|
||||
"github.com/expr-lang/expr/internal/testify/assert"
|
||||
)
|
||||
|
||||
type StoreInterface interface {
|
||||
Get(string) int
|
||||
}
|
||||
|
||||
type StoreImpt struct{}
|
||||
|
||||
func (f StoreImpt) Get(s string) int {
|
||||
return 42
|
||||
}
|
||||
|
||||
func (f StoreImpt) Set(s string, i int) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
type Env struct {
|
||||
Store StoreInterface `expr:"store"`
|
||||
}
|
||||
|
||||
func TestInterfaceHide(t *testing.T) {
|
||||
var env Env
|
||||
p, err := expr.Compile(`store.Get("foo")`, expr.Env(env))
|
||||
assert.NoError(t, err)
|
||||
|
||||
out, err := expr.Run(p, Env{Store: StoreImpt{}})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 42, out)
|
||||
|
||||
_, err = expr.Compile(`store.Set("foo", 100)`, expr.Env(env))
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "type interface_test.StoreInterface has no method Set")
|
||||
}
|
||||
Reference in New Issue
Block a user