issue-2: added some more string functions #6

Merged
lmika merged 2 commits from feature/issue-2 into main 2024-10-21 10:01:49 +00:00
3 changed files with 151 additions and 0 deletions
Showing only changes of commit 03f17af97d - Show all commits

45
ucl/builtins/strs.go Normal file
View File

@ -0,0 +1,45 @@
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.ToLower(s), nil
}

102
ucl/builtins/strs_test.go Normal file
View File

@ -0,0 +1,102 @@
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)
}
})
}
}

View File

@ -17,6 +17,10 @@ func WithTestBuiltin() InstOption {
return args.args[0], nil 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) { i.rootEC.addCmd("sjoin", invokableFunc(func(ctx context.Context, args invocationArgs) (object, error) {
if len(args.args) == 0 { if len(args.args) == 0 {
return strObject(""), nil return strObject(""), nil