2018-09-22 02:36:33 +02:00
|
|
|
package strings_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-07 17:04:18 +02:00
|
|
|
"testing"
|
|
|
|
|
2018-09-22 02:36:33 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
"github.com/MontFerret/ferret/pkg/stdlib/strings"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSubstring(t *testing.T) {
|
|
|
|
Convey("When args are not passed", t, func() {
|
|
|
|
Convey("It should return an error", func() {
|
|
|
|
var err error
|
|
|
|
_, err = strings.Substring(context.Background())
|
|
|
|
|
|
|
|
So(err, ShouldBeError)
|
|
|
|
|
|
|
|
_, err = strings.Substring(context.Background(), values.NewString("foo"))
|
|
|
|
|
|
|
|
So(err, ShouldBeError)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Substring('foobar', 3) should return 'bar'", t, func() {
|
|
|
|
out, err := strings.Substring(
|
|
|
|
context.Background(),
|
|
|
|
values.NewString("foobar"),
|
|
|
|
values.NewInt(3),
|
|
|
|
)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(out, ShouldEqual, "bar")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Substring('foobar', 3, 2) should return 'ba'", t, func() {
|
|
|
|
out, err := strings.Substring(
|
|
|
|
context.Background(),
|
|
|
|
values.NewString("foobar"),
|
|
|
|
values.NewInt(3),
|
|
|
|
values.NewInt(2),
|
|
|
|
)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(out, ShouldEqual, "ba")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Substring('foobar', 3, 5) should return 'bar'", t, func() {
|
|
|
|
out, err := strings.Substring(
|
|
|
|
context.Background(),
|
|
|
|
values.NewString("foobar"),
|
|
|
|
values.NewInt(3),
|
|
|
|
values.NewInt(5),
|
|
|
|
)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(out, ShouldEqual, "bar")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLeft(t *testing.T) {
|
|
|
|
Convey("When args are not passed", t, func() {
|
|
|
|
Convey("It should return an error", func() {
|
|
|
|
var err error
|
|
|
|
_, err = strings.Left(context.Background())
|
|
|
|
|
|
|
|
So(err, ShouldBeError)
|
|
|
|
|
|
|
|
_, err = strings.Left(context.Background(), values.NewString("foo"))
|
|
|
|
|
|
|
|
So(err, ShouldBeError)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-11-07 17:04:18 +02:00
|
|
|
Convey("Left('foobarfoobar', 3) should return 'foo'", t, func() {
|
2018-09-22 02:36:33 +02:00
|
|
|
out, _ := strings.Left(
|
|
|
|
context.Background(),
|
2018-11-07 17:04:18 +02:00
|
|
|
values.NewString("foobarfoobar"),
|
2018-09-22 02:36:33 +02:00
|
|
|
values.NewInt(3),
|
|
|
|
)
|
|
|
|
|
|
|
|
So(out, ShouldEqual, "foo")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Left('foobar', 10) should return 'foobar'", t, func() {
|
|
|
|
out, _ := strings.Left(
|
|
|
|
context.Background(),
|
|
|
|
values.NewString("foobar"),
|
|
|
|
values.NewInt(10),
|
|
|
|
)
|
|
|
|
|
|
|
|
So(out, ShouldEqual, "foobar")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRight(t *testing.T) {
|
|
|
|
Convey("When args are not passed", t, func() {
|
|
|
|
Convey("It should return an error", func() {
|
|
|
|
var err error
|
|
|
|
_, err = strings.Right(context.Background())
|
|
|
|
|
|
|
|
So(err, ShouldBeError)
|
|
|
|
|
|
|
|
_, err = strings.Right(context.Background(), values.NewString("foo"))
|
|
|
|
|
|
|
|
So(err, ShouldBeError)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-11-07 17:04:18 +02:00
|
|
|
Convey("Right('foobarfoobar', 3) should return 'bar'", t, func() {
|
2018-09-22 02:36:33 +02:00
|
|
|
out, _ := strings.Right(
|
|
|
|
context.Background(),
|
2018-11-07 17:04:18 +02:00
|
|
|
values.NewString("foobarfoobar"),
|
2018-09-22 02:36:33 +02:00
|
|
|
values.NewInt(3),
|
|
|
|
)
|
|
|
|
|
|
|
|
So(out, ShouldEqual, "bar")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Right('foobar', 10) should return 'foobar'", t, func() {
|
|
|
|
out, _ := strings.Right(
|
|
|
|
context.Background(),
|
|
|
|
values.NewString("foobar"),
|
|
|
|
values.NewInt(10),
|
|
|
|
)
|
|
|
|
|
|
|
|
So(out, ShouldEqual, "foobar")
|
|
|
|
})
|
|
|
|
}
|