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