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

130 lines
2.7 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 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)
})
})
Convey("Left('foobar', 3) should return 'foo'", t, func() {
out, _ := strings.Left(
context.Background(),
values.NewString("foobar"),
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)
})
})
Convey("Right('foobar', 3) should return 'bar'", t, func() {
out, _ := strings.Right(
context.Background(),
values.NewString("foobar"),
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")
})
}