github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/strings/concat_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 TestConcat(t *testing.T) {
    14  	Convey("When args are not passed", t, func() {
    15  		Convey("It should return an error", func() {
    16  			_, err := strings.Concat(context.Background())
    17  
    18  			So(err, ShouldBeError)
    19  		})
    20  	})
    21  
    22  	Convey("When args are strings", t, func() {
    23  		Convey("Concat('foo', 'bar', 'qaz') should return 'foobarqaz'", func() {
    24  			out, _ := strings.Concat(
    25  				context.Background(),
    26  				values.NewString("foo"),
    27  				values.NewString("bar"),
    28  				values.NewString("qaz"),
    29  			)
    30  
    31  			So(out, ShouldEqual, "foobarqaz")
    32  		})
    33  	})
    34  
    35  	Convey("When args are not strings", t, func() {
    36  		Convey("Concat('foo', None, 'bar') should return 'foobar'", func() {
    37  			out, _ := strings.Concat(
    38  				context.Background(),
    39  				values.NewString("foo"),
    40  				values.None,
    41  				values.NewString("bar"),
    42  			)
    43  
    44  			So(out, ShouldEqual, "foobar")
    45  		})
    46  		Convey("Concat('foo', 1, false) should return 'foo1false'", func() {
    47  			out, _ := strings.Concat(
    48  				context.Background(),
    49  				values.NewString("foo"),
    50  				values.NewInt(1),
    51  				values.False,
    52  			)
    53  
    54  			So(out, ShouldEqual, "foo1false")
    55  		})
    56  
    57  		Convey("Concat(['foo', 'bar']) should return 'foobar'", func() {
    58  			out, _ := strings.Concat(
    59  				context.Background(),
    60  				values.NewArrayWith(values.NewString("foo"), values.NewString("bar")),
    61  			)
    62  
    63  			So(out, ShouldEqual, "foobar")
    64  		})
    65  
    66  		Convey("Concat([1,2,3]) should return '123'", func() {
    67  			out, _ := strings.Concat(
    68  				context.Background(),
    69  				values.NewArrayWith(
    70  					values.NewInt(1),
    71  					values.NewInt(2),
    72  					values.NewInt(3),
    73  				),
    74  			)
    75  
    76  			So(out, ShouldEqual, "123")
    77  		})
    78  	})
    79  }
    80  
    81  func TestConcatWithSeparator(t *testing.T) {
    82  	Convey("When args are not passed", t, func() {
    83  		Convey("It should return an error", func() {
    84  			_, err := strings.ConcatWithSeparator(context.Background())
    85  
    86  			So(err, ShouldBeError)
    87  		})
    88  	})
    89  
    90  	Convey("When args are strings", t, func() {
    91  		Convey("ConcatWithSeparator(',' 'foo', 'bar', 'qaz') should return 'foo,bar,qaz'", func() {
    92  			out, _ := strings.ConcatWithSeparator(
    93  				context.Background(),
    94  				values.NewString(","),
    95  				values.NewString("foo"),
    96  				values.NewString("bar"),
    97  				values.NewString("qaz"),
    98  			)
    99  
   100  			So(out, ShouldEqual, "foo,bar,qaz")
   101  		})
   102  	})
   103  
   104  	Convey("When args are not strings", t, func() {
   105  		Convey("ConcatWithSeparator(',' ['foo', 'bar', 'qaz']) should return 'foo,bar,qaz'", func() {
   106  			out, _ := strings.ConcatWithSeparator(
   107  				context.Background(),
   108  				values.NewString(","),
   109  				values.NewArrayWith(
   110  					values.NewString("foo"),
   111  					values.NewString("bar"),
   112  					values.NewString("qaz"),
   113  				),
   114  			)
   115  
   116  			So(out, ShouldEqual, "foo,bar,qaz")
   117  		})
   118  
   119  		Convey("ConcatWithSeparator(',' ['foo', None, 'qaz']) should return 'foo,qaz'", func() {
   120  			out, _ := strings.ConcatWithSeparator(
   121  				context.Background(),
   122  				values.NewString(","),
   123  				values.NewArrayWith(
   124  					values.NewString("foo"),
   125  					values.None,
   126  					values.NewString("qaz"),
   127  				),
   128  			)
   129  
   130  			So(out, ShouldEqual, "foo,qaz")
   131  		})
   132  
   133  		Convey("ConcatWithSeparator(',' 'foo', None, 'qaz') should return 'foo,qaz'", func() {
   134  			out, _ := strings.ConcatWithSeparator(
   135  				context.Background(),
   136  				values.NewString(","),
   137  				values.NewArrayWith(
   138  					values.NewString("foo"),
   139  					values.None,
   140  					values.NewString("qaz"),
   141  				),
   142  			)
   143  
   144  			So(out, ShouldEqual, "foo,qaz")
   145  		})
   146  	})
   147  }