1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00
ferret/pkg/stdlib/strings/regex_test.go
Tim Voronov e6d692010c stdlib.strings
Added string functions to standard library
2018-09-21 20:37:09 -04:00

139 lines
3.4 KiB
Go

package strings_test
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/stdlib/strings"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestRegexMatch(t *testing.T) {
Convey("When args are not passed", t, func() {
Convey("It should return an error", func() {
var err error
_, err = strings.RegexMatch(context.Background())
So(err, ShouldBeError)
_, err = strings.RegexMatch(context.Background(), values.NewString(""))
So(err, ShouldBeError)
})
})
Convey("Should match with case insensitive regexp", t, func() {
out, err := strings.RegexMatch(
context.Background(),
values.NewString("My-us3r_n4m3"),
values.NewString("[a-z0-9_-]{3,16}$"),
values.True,
)
So(err, ShouldBeNil)
So(out.String(), ShouldEqual, `["My-us3r_n4m3"]`)
})
Convey("Should match with case sensitive regexp", t, func() {
out, err := strings.RegexMatch(
context.Background(),
values.NewString("john@doe.com"),
values.NewString(`([a-z0-9_\.-]+)@([\da-z-]+)\.([a-z\.]{2,6})$`),
)
So(err, ShouldBeNil)
So(out.String(), ShouldEqual, `["john@doe.com","john","doe","com"]`)
})
}
func TestRegexSplit(t *testing.T) {
Convey("When args are not passed", t, func() {
Convey("It should return an error", func() {
var err error
_, err = strings.RegexSplit(context.Background())
So(err, ShouldBeError)
_, err = strings.RegexSplit(context.Background(), values.NewString(""))
So(err, ShouldBeError)
})
})
Convey("Should split with regexp", t, func() {
out, err := strings.RegexSplit(
context.Background(),
values.NewString("This is a line.\n This is yet another line\r\n This again is a line.\r Mac line "),
values.NewString(`\.?(\n|\r)`),
)
So(err, ShouldBeNil)
So(out.String(), ShouldEqual, `["This is a line"," This is yet another line",""," This again is a line"," Mac line "]`)
})
}
func TestRegexTest(t *testing.T) {
Convey("When args are not passed", t, func() {
Convey("It should return an error", func() {
var err error
_, err = strings.RegexTest(context.Background())
So(err, ShouldBeError)
_, err = strings.RegexTest(context.Background(), values.NewString(""))
So(err, ShouldBeError)
})
})
Convey("Should return true when matches", t, func() {
out, _ := strings.RegexTest(
context.Background(),
values.NewString("the quick brown fox"),
values.NewString("the.*fox"),
)
So(out, ShouldEqual, true)
})
}
func TestRegexReplace(t *testing.T) {
Convey("When args are not passed", t, func() {
Convey("It should return an error", func() {
var err error
_, err = strings.RegexReplace(context.Background())
So(err, ShouldBeError)
_, err = strings.RegexReplace(context.Background(), values.NewString(""))
So(err, ShouldBeError)
_, err = strings.RegexReplace(context.Background(), values.NewString(""), values.NewString(""))
So(err, ShouldBeError)
})
})
Convey("Should replace with regexp", t, func() {
out, _ := strings.RegexReplace(
context.Background(),
values.NewString("the quick brown fox"),
values.NewString("the.*fox"),
values.NewString("jumped over"),
)
So(out, ShouldEqual, "jumped over")
out, _ = strings.RegexReplace(
context.Background(),
values.NewString("the quick brown fox"),
values.NewString("o"),
values.NewString("i"),
)
So(out, ShouldEqual, "the quick briwn fix")
})
}