github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/testing/len.go (about) 1 package testing 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/MontFerret/ferret/pkg/runtime/core" 8 "github.com/MontFerret/ferret/pkg/stdlib/collections" 9 "github.com/MontFerret/ferret/pkg/stdlib/testing/base" 10 ) 11 12 // LEN asserts that a measurable value has a length or size with the expected value. 13 // @param {Measurable} actual - Measurable value. 14 // @param {Int} length - Target length. 15 // @param {String} [message] - Message to display on error. 16 var Len = base.Assertion{ 17 DefaultMessage: func(args []core.Value) string { 18 return fmt.Sprintf("has size %s", args[1]) 19 }, 20 MinArgs: 2, 21 MaxArgs: 3, 22 Fn: func(ctx context.Context, args []core.Value) (bool, error) { 23 col := args[0] 24 size := args[1] 25 26 out, err := collections.Length(ctx, col) 27 28 if err != nil { 29 return false, err 30 } 31 32 return out.Compare(size) == 0, nil 33 }, 34 }