github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/testing/match.go (about) 1 package testing 2 3 import ( 4 "context" 5 6 "github.com/MontFerret/ferret/pkg/runtime/core" 7 "github.com/MontFerret/ferret/pkg/runtime/values" 8 "github.com/MontFerret/ferret/pkg/stdlib/strings" 9 "github.com/MontFerret/ferret/pkg/stdlib/testing/base" 10 ) 11 12 // MATCH asserts that value matches the regular expression. 13 // @param {Any} actual - Actual value. 14 // @param {String} expression - Regular expression. 15 // @param {String} [message] - Message to display on error. 16 var Match = base.Assertion{ 17 DefaultMessage: func(args []core.Value) string { 18 return "match regular expression" 19 }, 20 MinArgs: 2, 21 MaxArgs: 3, 22 Fn: func(ctx context.Context, args []core.Value) (bool, error) { 23 value := args[0] 24 regexp := args[1] 25 26 out, err := strings.RegexMatch(ctx, value, regexp) 27 28 if err != nil { 29 return false, err 30 } 31 32 return out.Compare(values.True) == 0, nil 33 }, 34 }