1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/stdlib/strings/fmt_test.go
3timeslazy acf2f13dcb Linter Cleanups (#294)
* sync with MontFerret/ferret

* fix --param handling

When params is converted to map it uses strings.Split,
which slices a string into all substrings separated by :.

* remove impossible conditions nil != nil

* delete ineffectual assignments

* replace '+= 1' with '++'

* remove useless comparison with nil

* merge variable declarations

* remove bool comparison

* fix imports

* fix imports

* delete unused file

* use copy instead of loop

* delete unused DummyInterface

* remove unnecassary break statements

* tidy modules
2019-05-03 17:10:34 -04:00

150 lines
3.1 KiB
Go

package strings_test
import (
"context"
"testing"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/stdlib/strings"
. "github.com/smartystreets/goconvey/convey"
)
type testCase struct {
Name string
Expected string
Format string
Args []core.Value
ShouldErr bool
}
func TestFmt(t *testing.T) {
tcs := []*testCase{
&testCase{
Name: `FMT("{}", 1) return "1"`,
Expected: "1",
Format: "{}",
Args: []core.Value{
values.NewInt(1),
},
},
&testCase{
Name: `FMT("{1} {} {0} {}", 1, 2) return "2 1 1 2"`,
Expected: "2 1 1 2",
Format: "{1} {} {0} {}",
Args: []core.Value{
values.NewInt(1),
values.NewInt(2),
},
},
&testCase{
Name: `FMT("{1} {} {0} {} {}", 1, 2, 3) return "2 1 1 2 3"`,
Expected: "2 1 1 2 3",
Format: "{1} {} {0} {} {}",
Args: []core.Value{
values.NewInt(1),
values.NewInt(2),
values.NewInt(3),
},
},
&testCase{
Name: `FMT("{2}{1} {0}", "World!", ",", "Hello") return "Hello, World!"`,
Expected: "Hello, World!",
Format: "{2}{1} {0}",
Args: []core.Value{
values.NewString("World!"),
values.NewString(","),
values.NewString("Hello"),
},
},
&testCase{
Name: `FMT({}, {key:"value"}) return "{"key":"value"}"`,
Expected: `{"key":"value"}`,
Format: "{}",
Args: []core.Value{
values.NewObjectWith(
values.NewObjectProperty(
"key", values.NewString("value"),
),
),
},
},
&testCase{
Name: `FMT({}, {key:"value"}) return "{"key":"value"}"`,
Expected: `{"key":"value","yek":"eulav"}`,
Format: "{}",
Args: []core.Value{
values.NewObjectWith(
values.NewObjectProperty(
"key", values.NewString("value"),
),
values.NewObjectProperty(
"yek", values.NewString("eulav"),
),
),
},
},
&testCase{
Name: `FMT("string") return "string"`,
Expected: "string",
Format: "string",
},
&testCase{
Name: `FMT("string") return "string"`,
Expected: "string",
Format: "string",
Args: []core.Value{
values.NewInt(1),
},
},
&testCase{
Name: `FMT("{}") return error`,
Format: "{}",
Args: []core.Value{},
ShouldErr: true,
},
&testCase{
Name: `FMT("{1}", 10) return error`,
Format: "{1}",
Args: []core.Value{
values.NewInt(10),
},
ShouldErr: true,
},
&testCase{
Name: `FMT("{1} {} {0} {}", 1, 2, 3) return error`,
Format: "{1} {} {0} {}",
Args: []core.Value{
values.NewInt(1),
values.NewInt(2),
values.NewInt(3),
},
ShouldErr: true,
},
}
for _, tc := range tcs {
tc.Do(t)
}
}
func (tc *testCase) Do(t *testing.T) {
Convey(tc.Name, t, func() {
var expected core.Value = values.NewString(tc.Expected)
args := []core.Value{values.NewString(tc.Format)}
args = append(args, tc.Args...)
formatted, err := strings.Fmt(context.Background(), args...)
if tc.ShouldErr {
So(err, ShouldBeError)
expected = values.None
} else {
So(err, ShouldBeNil)
}
So(formatted, ShouldEqual, expected)
})
}