Compare commits
No commits in common. "5b913266e934fd5b04c580e1c291e0f06f58c23d" and "bb78a39cdb6fd3adcbf55334104c63b883a095d6" have entirely different histories.
5b913266e9
...
bb78a39cdb
|
@ -1,45 +0,0 @@
|
|||
package builtins
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"ucl.lmika.dev/ucl"
|
||||
)
|
||||
|
||||
func Strs() ucl.Module {
|
||||
return ucl.Module{
|
||||
Name: "strs",
|
||||
Builtins: map[string]ucl.BuiltinHandler{
|
||||
"to-upper": toUpper,
|
||||
"to-lower": toLower,
|
||||
"trim": trim,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func toUpper(ctx context.Context, args ucl.CallArgs) (any, error) {
|
||||
var s string
|
||||
if err := args.Bind(&s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return strings.ToUpper(s), nil
|
||||
}
|
||||
|
||||
func toLower(ctx context.Context, args ucl.CallArgs) (any, error) {
|
||||
var s string
|
||||
if err := args.Bind(&s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return strings.ToLower(s), nil
|
||||
}
|
||||
|
||||
func trim(ctx context.Context, args ucl.CallArgs) (any, error) {
|
||||
var s string
|
||||
if err := args.Bind(&s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return strings.TrimSpace(s), nil
|
||||
}
|
|
@ -1,102 +0,0 @@
|
|||
package builtins_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
"ucl.lmika.dev/ucl"
|
||||
"ucl.lmika.dev/ucl/builtins"
|
||||
)
|
||||
|
||||
func TestStrs_ToUpper(t *testing.T) {
|
||||
tests := []struct {
|
||||
desc string
|
||||
eval string
|
||||
want any
|
||||
wantErr bool
|
||||
}{
|
||||
{desc: "to upper 1", eval: `strs:to-upper "hello"`, want: "HELLO"},
|
||||
{desc: "to upper 2", eval: `strs:to-upper ""`, want: ""},
|
||||
{desc: "to upper 3", eval: `strs:to-upper 123`, want: "123"},
|
||||
{desc: "to upper 4", eval: `strs:to-upper "foo Bar BaZ"`, want: "FOO BAR BAZ"},
|
||||
|
||||
{desc: "err to upper 1", eval: `strs:to-upper`, wantErr: true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.desc, func(t *testing.T) {
|
||||
inst := ucl.New(
|
||||
ucl.WithModule(builtins.Strs()),
|
||||
)
|
||||
res, err := inst.Eval(context.Background(), tt.eval)
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.want, res)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrs_ToLower(t *testing.T) {
|
||||
tests := []struct {
|
||||
desc string
|
||||
eval string
|
||||
want any
|
||||
wantErr bool
|
||||
}{
|
||||
{desc: "to lower 1", eval: `strs:to-lower "HeLLo"`, want: "hello"},
|
||||
{desc: "to lower 2", eval: `strs:to-lower ""`, want: ""},
|
||||
{desc: "to lower 3", eval: `strs:to-lower 123`, want: "123"},
|
||||
{desc: "to lower 4", eval: `strs:to-lower "foo Bar BaZ"`, want: "foo bar baz"},
|
||||
|
||||
{desc: "err to lower 1", eval: `strs:to-lower`, wantErr: true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.desc, func(t *testing.T) {
|
||||
inst := ucl.New(
|
||||
ucl.WithModule(builtins.Strs()),
|
||||
)
|
||||
res, err := inst.Eval(context.Background(), tt.eval)
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.want, res)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrs_Trim(t *testing.T) {
|
||||
tests := []struct {
|
||||
desc string
|
||||
eval string
|
||||
want any
|
||||
wantErr bool
|
||||
}{
|
||||
{desc: "trim space 1", eval: `strs:trim " hello "`, want: "hello"},
|
||||
{desc: "trim space 2", eval: `strs:trim ""`, want: ""},
|
||||
{desc: "trim space 3", eval: `strs:trim " 123"`, want: "123"},
|
||||
{desc: "trim space 4", eval: `strs:trim "foo Bar BaZ "`, want: "foo Bar BaZ"},
|
||||
|
||||
{desc: "err trim space 1", eval: `strs:trim`, wantErr: true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.desc, func(t *testing.T) {
|
||||
inst := ucl.New(
|
||||
ucl.WithModule(builtins.Strs()),
|
||||
)
|
||||
res, err := inst.Eval(context.Background(), tt.eval)
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.want, res)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -18,10 +18,6 @@ func WithTestBuiltin() InstOption {
|
|||
return args.args[0], nil
|
||||
}))
|
||||
|
||||
i.rootEC.addCmd("toUpper", invokableFunc(func(ctx context.Context, args invocationArgs) (object, error) {
|
||||
return strObject(strings.ToUpper(args.args[0].String())), nil
|
||||
}))
|
||||
|
||||
i.rootEC.addCmd("sjoin", invokableFunc(func(ctx context.Context, args invocationArgs) (object, error) {
|
||||
if len(args.args) == 0 {
|
||||
return strObject(""), nil
|
||||
|
|
Loading…
Reference in a new issue