github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/strings/contains_test.go (about) 1 package strings_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/strings" 11 ) 12 13 func TestContains(t *testing.T) { 14 Convey("When args are not passed", t, func() { 15 Convey("It should return an error", func() { 16 _, err := strings.Contains(context.Background()) 17 18 So(err, ShouldBeError) 19 }) 20 }) 21 22 Convey("When args are strings", t, func() { 23 Convey("Contains('foobar', 'foo') should return 'true'", func() { 24 out, _ := strings.Contains( 25 context.Background(), 26 values.NewString("foobar"), 27 values.NewString("bar"), 28 ) 29 30 So(out, ShouldEqual, values.True) 31 }) 32 33 Convey("Contains('foobar', 'qaz') should return 'false'", func() { 34 out, _ := strings.Contains( 35 context.Background(), 36 values.NewString("foobar"), 37 values.NewString("qaz"), 38 ) 39 40 So(out, ShouldEqual, values.False) 41 }) 42 43 Convey("Contains('foobar', 'foo', true) should return '3'", func() { 44 out, _ := strings.Contains( 45 context.Background(), 46 values.NewString("foobar"), 47 values.NewString("bar"), 48 values.True, 49 ) 50 51 So(out, ShouldEqual, 3) 52 }) 53 54 Convey("Contains('foobar', 'qaz', true) should return '-1'", func() { 55 out, _ := strings.Contains( 56 context.Background(), 57 values.NewString("foobar"), 58 values.NewString("bar"), 59 values.True, 60 ) 61 62 So(out, ShouldEqual, 3) 63 }) 64 }) 65 66 Convey("When args are not strings", t, func() { 67 Convey("Contains('foo123', 1) should return 'true'", func() { 68 out, _ := strings.Contains( 69 context.Background(), 70 values.NewString("foo123"), 71 values.NewInt(1), 72 ) 73 74 So(out, ShouldEqual, values.True) 75 }) 76 77 Convey("Contains(123, 1) should return 'true'", func() { 78 out, _ := strings.Contains( 79 context.Background(), 80 values.NewInt(123), 81 values.NewInt(1), 82 ) 83 84 So(out, ShouldEqual, values.True) 85 }) 86 87 Convey("Contains([1,2,3], 1) should return 'true'", func() { 88 out, _ := strings.Contains( 89 context.Background(), 90 values.NewArrayWith(values.NewInt(1), values.NewInt(2), values.NewInt(3)), 91 values.NewInt(1), 92 ) 93 94 So(out, ShouldEqual, values.True) 95 }) 96 }) 97 }