github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/strings/find_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 TestFindFirst(t *testing.T) { 14 Convey("When args are not passed", t, func() { 15 Convey("It should return an error", func() { 16 var err error 17 _, err = strings.FindFirst(context.Background()) 18 19 So(err, ShouldBeError) 20 21 _, err = strings.FindFirst( 22 context.Background(), 23 values.NewString("foo"), 24 ) 25 26 So(err, ShouldBeError) 27 }) 28 }) 29 30 Convey("When args are strings", t, func() { 31 Convey("FindFirst('foobarbaz', 'ba') should return 3", func() { 32 out, _ := strings.FindFirst( 33 context.Background(), 34 values.NewString("foobarbaz"), 35 values.NewString("ba"), 36 ) 37 38 So(out, ShouldEqual, 3) 39 }) 40 41 Convey("FindFirst('foobarbaz', 'ba', 4) should return 6", func() { 42 out, _ := strings.FindFirst( 43 context.Background(), 44 values.NewString("foobarbaz"), 45 values.NewString("ba"), 46 values.NewInt(4), 47 ) 48 49 So(out, ShouldEqual, 6) 50 }) 51 52 Convey("FindFirst('foobarbaz', 'ba', 4) should return -1", func() { 53 out, _ := strings.FindFirst( 54 context.Background(), 55 values.NewString("foobarbaz"), 56 values.NewString("ba"), 57 values.NewInt(7), 58 ) 59 60 So(out, ShouldEqual, -1) 61 }) 62 63 Convey("FindFirst('foobarbaz', 'ba', 0, 3) should return -1", func() { 64 out, _ := strings.FindFirst( 65 context.Background(), 66 values.NewString("foobarbaz"), 67 values.NewString("ba"), 68 values.NewInt(0), 69 values.NewInt(3), 70 ) 71 72 So(out, ShouldEqual, -1) 73 }) 74 }) 75 } 76 77 func TestFindLast(t *testing.T) { 78 Convey("When args are not passed", t, func() { 79 Convey("It should return an error", func() { 80 var err error 81 _, err = strings.FindLast(context.Background()) 82 83 So(err, ShouldBeError) 84 85 _, err = strings.FindLast( 86 context.Background(), 87 values.NewString("foo"), 88 ) 89 90 So(err, ShouldBeError) 91 }) 92 }) 93 94 Convey("When args are strings", t, func() { 95 Convey("FindLast('foobarbaz', 'ba') should return 6", func() { 96 out, _ := strings.FindLast( 97 context.Background(), 98 values.NewString("foobarbaz"), 99 values.NewString("ba"), 100 ) 101 102 So(out, ShouldEqual, 6) 103 }) 104 105 Convey("FindLast('foobarbaz', 'ba', 7) should return -1", func() { 106 out, _ := strings.FindLast( 107 context.Background(), 108 values.NewString("foobarbaz"), 109 values.NewString("ba"), 110 values.NewInt(7), 111 ) 112 113 So(out, ShouldEqual, -1) 114 }) 115 116 Convey("FindLast('foobarbaz', 'ba', 0, 5) should return 3", func() { 117 out, _ := strings.FindLast( 118 context.Background(), 119 values.NewString("foobarbaz"), 120 values.NewString("ba"), 121 values.NewInt(0), 122 values.NewInt(5), 123 ) 124 125 So(out, ShouldEqual, 3) 126 }) 127 }) 128 }