From 03f17af97d5f2a8b61f58f74bea206ad23446a58 Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Mon, 21 Oct 2024 20:56:13 +1100 Subject: [PATCH 1/2] issue-2: added some more string functions --- ucl/builtins/strs.go | 45 +++++++++++++++++ ucl/builtins/strs_test.go | 102 ++++++++++++++++++++++++++++++++++++++ ucl/testbuiltins_test.go | 4 ++ 3 files changed, 151 insertions(+) create mode 100644 ucl/builtins/strs.go create mode 100644 ucl/builtins/strs_test.go diff --git a/ucl/builtins/strs.go b/ucl/builtins/strs.go new file mode 100644 index 0000000..33eb4e9 --- /dev/null +++ b/ucl/builtins/strs.go @@ -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 +} diff --git a/ucl/builtins/strs_test.go b/ucl/builtins/strs_test.go new file mode 100644 index 0000000..c6ef90a --- /dev/null +++ b/ucl/builtins/strs_test.go @@ -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) + } + }) + } +} diff --git a/ucl/testbuiltins_test.go b/ucl/testbuiltins_test.go index 7d0ae1c..3e6c68a 100644 --- a/ucl/testbuiltins_test.go +++ b/ucl/testbuiltins_test.go @@ -17,6 +17,10 @@ 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 From 504106b5efc11353bc44db034a3fe73f6a54ba0b Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Mon, 21 Oct 2024 20:59:37 +1100 Subject: [PATCH 2/2] issue-2: fixed trim function --- ucl/builtins/strs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ucl/builtins/strs.go b/ucl/builtins/strs.go index 33eb4e9..a10893d 100644 --- a/ucl/builtins/strs.go +++ b/ucl/builtins/strs.go @@ -41,5 +41,5 @@ func trim(ctx context.Context, args ucl.CallArgs) (any, error) { return nil, err } - return strings.ToLower(s), nil + return strings.TrimSpace(s), nil }