github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/arrays/position_test.go (about)

     1  package arrays_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	. "github.com/smartystreets/goconvey/convey"
     8  
     9  	"github.com/MontFerret/ferret/pkg/runtime/values"
    10  	"github.com/MontFerret/ferret/pkg/stdlib/arrays"
    11  )
    12  
    13  func TestPosition(t *testing.T) {
    14  	Convey("Should return TRUE when a value exists in a given array", t, func() {
    15  		arr := values.NewArrayWith(
    16  			values.NewInt(1),
    17  			values.NewInt(2),
    18  			values.NewInt(3),
    19  			values.NewInt(4),
    20  			values.NewInt(5),
    21  		)
    22  
    23  		out, err := arrays.Position(context.Background(), arr, values.NewInt(3))
    24  
    25  		So(err, ShouldBeNil)
    26  		So(out.String(), ShouldEqual, "true")
    27  	})
    28  
    29  	Convey("Should return FALSE when a value does not exist in a given array", t, func() {
    30  		arr := values.NewArrayWith(
    31  			values.NewInt(1),
    32  			values.NewInt(2),
    33  			values.NewInt(3),
    34  			values.NewInt(4),
    35  			values.NewInt(5),
    36  		)
    37  
    38  		out, err := arrays.Position(context.Background(), arr, values.NewInt(6))
    39  
    40  		So(err, ShouldBeNil)
    41  		So(out.String(), ShouldEqual, "false")
    42  	})
    43  
    44  	Convey("Should return index when a value exists in a given array", t, func() {
    45  		arr := values.NewArrayWith(
    46  			values.NewInt(1),
    47  			values.NewInt(2),
    48  			values.NewInt(3),
    49  			values.NewInt(4),
    50  			values.NewInt(5),
    51  		)
    52  
    53  		out, err := arrays.Position(
    54  			context.Background(),
    55  			arr,
    56  			values.NewInt(3),
    57  			values.NewBoolean(true),
    58  		)
    59  
    60  		So(err, ShouldBeNil)
    61  		So(out.String(), ShouldEqual, "2")
    62  	})
    63  
    64  	Convey("Should return -1 when a value does not exist in a given array", t, func() {
    65  		arr := values.NewArrayWith(
    66  			values.NewInt(1),
    67  			values.NewInt(2),
    68  			values.NewInt(3),
    69  			values.NewInt(4),
    70  			values.NewInt(5),
    71  		)
    72  
    73  		out, err := arrays.Position(
    74  			context.Background(),
    75  			arr,
    76  			values.NewInt(6),
    77  			values.NewBoolean(true),
    78  		)
    79  
    80  		So(err, ShouldBeNil)
    81  		So(out.String(), ShouldEqual, "-1")
    82  	})
    83  }