mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
fuzz: add custom function to fuzzer
This commit is contained in:
+4
-1
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/expr-lang/expr/test/fuzz"
|
||||
|
||||
"github.com/bettercap/readline"
|
||||
|
||||
"github.com/expr-lang/expr"
|
||||
"github.com/expr-lang/expr/builtin"
|
||||
"github.com/expr-lang/expr/debug"
|
||||
@@ -29,6 +30,8 @@ func main() {
|
||||
for name := range env {
|
||||
keywords = append(keywords, name)
|
||||
}
|
||||
fn := fuzz.Func()
|
||||
keywords = append(keywords, "fn")
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -81,7 +84,7 @@ func main() {
|
||||
continue
|
||||
}
|
||||
|
||||
program, err = expr.Compile(line, expr.Env(env))
|
||||
program, err = expr.Compile(line, expr.Env(env), fn)
|
||||
if err != nil {
|
||||
fmt.Printf("compile error: %s\n", err)
|
||||
continue
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
package fuzz
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/expr-lang/expr"
|
||||
)
|
||||
|
||||
func NewEnv() map[string]any {
|
||||
return map[string]any{
|
||||
"ok": true,
|
||||
@@ -25,6 +31,12 @@ func NewEnv() map[string]any {
|
||||
}
|
||||
}
|
||||
|
||||
func Func() expr.Option {
|
||||
return expr.Function("fn", func(params ...any) (any, error) {
|
||||
return fmt.Sprintf("fn(%v)", params), nil
|
||||
})
|
||||
}
|
||||
|
||||
type Foo struct {
|
||||
Bar string
|
||||
}
|
||||
|
||||
@@ -54,13 +54,14 @@ func FuzzExpr(f *testing.F) {
|
||||
}
|
||||
|
||||
env := NewEnv()
|
||||
fn := Func()
|
||||
|
||||
f.Fuzz(func(t *testing.T, code string) {
|
||||
if len(code) > 1000 {
|
||||
t.Skip("too long code")
|
||||
}
|
||||
|
||||
program, err := expr.Compile(code, expr.Env(env))
|
||||
program, err := expr.Compile(code, expr.Env(env), fn)
|
||||
if err != nil {
|
||||
t.Skipf("compile error: %s", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user