github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/strings/regex_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 TestRegexMatch(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.RegexMatch(context.Background())
    18  
    19  			So(err, ShouldBeError)
    20  
    21  			_, err = strings.RegexMatch(context.Background(), values.NewString(""))
    22  
    23  			So(err, ShouldBeError)
    24  		})
    25  	})
    26  
    27  	Convey("Should match with case insensitive regexp", t, func() {
    28  		out, err := strings.RegexMatch(
    29  			context.Background(),
    30  			values.NewString("My-us3r_n4m3"),
    31  			values.NewString("[a-z0-9_-]{3,16}$"),
    32  			values.True,
    33  		)
    34  
    35  		So(err, ShouldBeNil)
    36  		So(out.String(), ShouldEqual, `["My-us3r_n4m3"]`)
    37  	})
    38  
    39  	Convey("Should match with case sensitive regexp", t, func() {
    40  		out, err := strings.RegexMatch(
    41  			context.Background(),
    42  			values.NewString("john@doe.com"),
    43  			values.NewString(`([a-z0-9_\.-]+)@([\da-z-]+)\.([a-z\.]{2,6})$`),
    44  		)
    45  
    46  		So(err, ShouldBeNil)
    47  		So(out.String(), ShouldEqual, `["john@doe.com","john","doe","com"]`)
    48  	})
    49  }
    50  
    51  func TestRegexSplit(t *testing.T) {
    52  	Convey("When args are not passed", t, func() {
    53  		Convey("It should return an error", func() {
    54  			var err error
    55  			_, err = strings.RegexSplit(context.Background())
    56  
    57  			So(err, ShouldBeError)
    58  
    59  			_, err = strings.RegexSplit(context.Background(), values.NewString(""))
    60  
    61  			So(err, ShouldBeError)
    62  		})
    63  	})
    64  
    65  	Convey("Should split with regexp", t, func() {
    66  		out, err := strings.RegexSplit(
    67  			context.Background(),
    68  			values.NewString("This is a line.\n This is yet another line\r\n This again is a line.\r Mac line "),
    69  			values.NewString(`\.?(\n|\r)`),
    70  		)
    71  
    72  		So(err, ShouldBeNil)
    73  		So(out.String(), ShouldEqual, `["This is a line"," This is yet another line",""," This again is a line"," Mac line "]`)
    74  	})
    75  }
    76  
    77  func TestRegexTest(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.RegexTest(context.Background())
    82  
    83  			So(err, ShouldBeError)
    84  
    85  			_, err = strings.RegexTest(context.Background(), values.NewString(""))
    86  
    87  			So(err, ShouldBeError)
    88  
    89  		})
    90  	})
    91  
    92  	Convey("Should return true when matches", t, func() {
    93  		out, _ := strings.RegexTest(
    94  			context.Background(),
    95  			values.NewString("the quick brown fox"),
    96  			values.NewString("the.*fox"),
    97  		)
    98  
    99  		So(out, ShouldEqual, true)
   100  	})
   101  }
   102  
   103  func TestRegexReplace(t *testing.T) {
   104  	Convey("When args are not passed", t, func() {
   105  		Convey("It should return an error", func() {
   106  			var err error
   107  			_, err = strings.RegexReplace(context.Background())
   108  
   109  			So(err, ShouldBeError)
   110  
   111  			_, err = strings.RegexReplace(context.Background(), values.NewString(""))
   112  
   113  			So(err, ShouldBeError)
   114  
   115  			_, err = strings.RegexReplace(context.Background(), values.NewString(""), values.NewString(""))
   116  
   117  			So(err, ShouldBeError)
   118  		})
   119  	})
   120  
   121  	Convey("Should replace with regexp", t, func() {
   122  		out, _ := strings.RegexReplace(
   123  			context.Background(),
   124  			values.NewString("the quick brown fox"),
   125  			values.NewString("the.*fox"),
   126  			values.NewString("jumped over"),
   127  		)
   128  
   129  		So(out, ShouldEqual, "jumped over")
   130  
   131  		out, _ = strings.RegexReplace(
   132  			context.Background(),
   133  			values.NewString("the quick brown fox"),
   134  			values.NewString("o"),
   135  			values.NewString("i"),
   136  		)
   137  
   138  		So(out, ShouldEqual, "the quick briwn fix")
   139  	})
   140  }