All checks were successful
Build / build (push) Successful in 2m32s
- call now supports calling invokables by string - call now takes as arguments a listable of positional args, and a hashable of keyword args - added strs:split, strs:join, and strs:has-prefix - added new lists module with lists:first - added time:sleep
204 lines
5.4 KiB
Go
204 lines
5.4 KiB
Go
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)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestStrs_HasPrefix(t *testing.T) {
|
|
tests := []struct {
|
|
desc string
|
|
eval string
|
|
want any
|
|
wantErr bool
|
|
}{
|
|
{desc: "has prefix 1", eval: `strs:has-prefix "hello, world" "hello"`, want: true},
|
|
{desc: "has prefix 2", eval: `strs:has-prefix "goodbye, world" "hello"`, want: false},
|
|
{desc: "has prefix 3", eval: `strs:has-prefix "" "world"`, want: false},
|
|
{desc: "has prefix 4", eval: `strs:has-prefix "hello" ""`, want: true},
|
|
|
|
{desc: "err 1", eval: `strs:has-prefix`, wantErr: true},
|
|
{desc: "err 1", eval: `strs:has-prefix "asd"`, 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_Split(t *testing.T) {
|
|
tests := []struct {
|
|
desc string
|
|
eval string
|
|
want any
|
|
wantErr bool
|
|
}{
|
|
{desc: "split 1", eval: `strs:split "1,2,3" ","`, want: builtins.StringSlice{"1", "2", "3"}},
|
|
{desc: "split 2", eval: `strs:split "1,2,3" ";"`, want: builtins.StringSlice{"1,2,3"}},
|
|
{desc: "split 3", eval: `strs:split "" ";"`, want: builtins.StringSlice{""}},
|
|
{desc: "split 4", eval: `strs:split " " ";"`, want: builtins.StringSlice{" "}},
|
|
|
|
{desc: "split by char 1", eval: `strs:split "123"`, want: builtins.StringSlice{"1", "2", "3"}},
|
|
|
|
{desc: "split max 1", eval: `strs:split "1,2,3" "," -max 2`, want: builtins.StringSlice{"1", "2,3"}},
|
|
{desc: "split max 2", eval: `strs:split "1,2,3" "," -max 5`, want: builtins.StringSlice{"1", "2", "3"}},
|
|
|
|
{desc: "split by char max 1", eval: `strs:split "12345" -max 3`, want: builtins.StringSlice{"1", "2", "345"}},
|
|
|
|
{desc: "err 1", eval: `strs:split "1,2,3" -max []`, 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_Join(t *testing.T) {
|
|
tests := []struct {
|
|
desc string
|
|
eval string
|
|
want any
|
|
wantErr bool
|
|
}{
|
|
{desc: "join 1", eval: `strs:join [1 2 3] ","`, want: "1,2,3"},
|
|
{desc: "join 2", eval: `strs:join [a b c] " "`, want: "a b c"},
|
|
{desc: "join 3", eval: `strs:join [a b c] ""`, want: "abc"},
|
|
{desc: "join 4", eval: `strs:join [a b c]`, want: "abc"},
|
|
{desc: "join 5", eval: `strs:join (itrs:from [a b c]) ","`, want: "a,b,c"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
inst := ucl.New(
|
|
ucl.WithModule(builtins.Itrs()),
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|