ucl/ucl/inst_test.go

344 lines
12 KiB
Go

package ucl_test
import (
"bytes"
"context"
"strings"
"ucl.lmika.dev/ucl"
"github.com/stretchr/testify/assert"
"testing"
)
func TestInst_Eval(t *testing.T) {
tests := []struct {
desc string
expr string
want any
wantObj bool
wantErr error
}{
{desc: "simple string", expr: `firstarg "hello"`, want: "hello"},
{desc: "simple int 1", expr: `firstarg 123`, want: 123},
{desc: "simple int 2", expr: `firstarg -234`, want: -234},
{desc: "simple ident 1", expr: `firstarg a-test`, want: "a-test"},
// String interpolation
{desc: "interpolate string 1", expr: `$what = "world" ; firstarg "hello $what"`, want: "hello world"},
{desc: "interpolate string 2", expr: `$what = "world" ; $when = "now" ; firstarg "$when, hello $what"`, want: "now, hello world"},
{desc: "interpolate string 3", expr: `$what = "world" ; $when = "now" ; firstarg "${when}, hello ${what}"`, want: "now, hello world"},
{desc: "interpolate string 4", expr: `$crazy = [far: "unknown"] ; firstarg "hello ${crazy.far}"`, want: "hello unknown"},
{desc: "interpolate string 5", expr: `$oldWords = ["hither" "thither" "yonder"] ; firstarg "hello ${oldWords.(1)}"`, want: "hello thither"},
{desc: "interpolate string 6", expr: `$oldWords = ["hither" "thither" "yonder"] ; firstarg "hello ${oldWords.(add 1 1)}"`, want: "hello yonder"},
{desc: "interpolate string 7", expr: `$oldWords = ["hither" "thither" "yonder"] ; firstarg "hello ${oldWords.(add 2 | sub (sub 2 1) | sub 1)}"`, want: "hello hither"},
{desc: "interpolate string 8", expr: `$words = ["old": ["hither" "thither" "yonder"] "new": ["near" "far"]] ; firstarg "hello ${words.old.(2)}"`, want: "hello yonder"},
{desc: "interpolate string 9", expr: `$what = "world" ; firstarg "hello $($what)"`, want: "hello world"},
{desc: "interpolate string 10", expr: `firstarg "hello $([1 2 3] | len)"`, want: "hello 3"},
{desc: "interpolate string 11", expr: `firstarg "hello $(add (add 1 2) 3)"`, want: "hello 6"},
{desc: "interpolate string 12", expr: `firstarg ("$(add 2 (add 1 1)) + $([1 2 3].(1) | cat ("$("")")) = $(("$(add 2 (4))"))")`, want: "4 + 2 = 6"},
// Sub-expressions
{desc: "sub expression 1", expr: `firstarg (sjoin "hello")`, want: "hello"},
{desc: "sub expression 2", expr: `firstarg (sjoin "hello " "world")`, want: "hello world"},
{desc: "sub expression 3", expr: `firstarg (sjoin "hello" (sjoin " ") (sjoin "world"))`, want: "hello world"},
// Variables
{desc: "var 1", expr: `firstarg $a`, want: "alpha"},
{desc: "var 2", expr: `firstarg $bee`, want: "buzz"},
{desc: "var 3", expr: `firstarg (sjoin $bee " " $bee " " $bee)`, want: "buzz buzz buzz"},
// Pipeline
{desc: "pipe 1", expr: `list "aye" "bee" "see" | joinpipe`, want: "aye,bee,see"},
{desc: "pipe 2", expr: `list "aye" "bee" "see" | map { |x| toUpper $x } | joinpipe`, want: "AYE,BEE,SEE"},
{desc: "pipe 3", expr: `firstarg ["normal"] | map { |x| toUpper $x } | joinpipe`, want: "NORMAL"},
{desc: "pipe literal 1", expr: `"hello" | firstarg`, want: "hello"},
{desc: "pipe literal 2", expr: `["hello" "world"] | joinpipe`, want: "hello,world"},
{desc: "ignored pipe", expr: `(list "aye" | firstarg "ignore me") | joinpipe`, want: "aye"},
// Multi-statements
{desc: "multi 1", expr: `firstarg "hello" ; firstarg "world"`, want: "world"},
{desc: "multi 2", expr: `list "hello" | toUpper ; firstarg "world"`, want: "world"},
{desc: "multi 3", expr: `$new = "this is new" ; firstarg $new`, want: "this is new"},
// Lists
{desc: "list 1", expr: `firstarg ["1" "2" "3"]`, want: []any{"1", "2", "3"}},
{desc: "list 2", expr: `$one = "one" ; firstarg [$one (list "two" | map { |x| toUpper $x } | head) "three"]`, want: []any{"one", "TWO", "three"}},
{desc: "list 3", expr: `firstarg []`, want: []any{}},
{desc: "list 4", expr: `$x = ["a" "b" "c"] ; firstarg [$x.(2) $x.(1) $x.(0)]`, want: []any{"c", "b", "a"}},
// Maps
{desc: "map 1", expr: `firstarg [one:"1" two:"2" three:"3"]`, want: map[string]any{"one": "1", "two": "2", "three": "3"}},
{desc: "map 2", expr: `firstarg ["one":"1" "two":"2" "three":"3"]`, want: map[string]any{"one": "1", "two": "2", "three": "3"}},
{desc: "map 3", expr: `
$one = "one" ; $n1 = "1"
firstarg [
$one:$n1
(list "two" | map { |x| toUpper $x } | head):(list "2" | map { |x| toUpper $x } | head)
three:"3"
]`, want: map[string]any{"one": "1", "TWO": "2", "three": "3"}},
{desc: "map 4", expr: `firstarg [:]`, want: map[string]any{}},
{desc: "map 5", expr: `$x = ["a" "b" "c"] ; firstarg ["one":$x.(2) "two":$x.(1) "three":$x.(0)]`, want: map[string]any{"one": "c", "two": "b", "three": "a"}},
{desc: "map 6", expr: `$x = [a:"A" b:"B" c:"C"] ; firstarg ["one":$x.c "two":$x.b "three":$x.a]`, want: map[string]any{"one": "C", "two": "B", "three": "A"}},
// Dots
{desc: "dot expr 1", expr: `$x = [1 2 3] ; $x.(0)`, want: 1},
{desc: "dot expr 2", expr: `$x = [1 2 3] ; $x.(1)`, want: 2},
{desc: "dot expr 3", expr: `$x = [1 2 3] ; $x.(2)`, want: 3},
{desc: "dot expr 4", expr: `$x = [1 2 3] ; $x.(3)`, want: nil},
{desc: "dot expr 5", expr: `$x = [1 2 3] ; $x.(add 1 1)`, want: 3},
{desc: "dot expr 6", expr: `$x = [1 2 3] ; $x.(-1)`, want: 3},
{desc: "dot expr 7", expr: `$x = [1 2 3] ; $x.(-2)`, want: 2},
{desc: "dot expr 8", expr: `$x = [1 2 3] ; $x.(-3)`, want: 1},
{desc: "dot expr 9", expr: `$x = [1 2 3] ; $x.(-4)`, want: nil},
{desc: "dot idents 1", expr: `$x = [alpha:"hello" bravo:"world"] ; $x.alpha`, want: "hello"},
{desc: "dot idents 2", expr: `$x = [alpha:"hello" bravo:"world"] ; $x.bravo`, want: "world"},
{desc: "dot idents 3", expr: `$x = [alpha:"hello" bravo:"world"] ; $x.charlie`, want: nil},
{desc: "dot idents 4", expr: `$x = [alpha:"hello" bravo:"world"] ; $x.("alpha")`, want: "hello"},
{desc: "dot idents 5", expr: `$x = [alpha:"hello" bravo:"world"] ; $x.("bravo")`, want: "world"},
{desc: "dot idents 6", expr: `$x = [alpha:"hello" bravo:"world"] ; $x.("charlie")`, want: nil},
{desc: "dot idents 7", expr: `$x = [MORE:"stuff"] ; $x.("more" | toUpper)`, want: "stuff"},
{desc: "dot idents 8", expr: `$x = [MORE:"stuff"] ; $x.(toUpper ("more"))`, want: "stuff"},
{desc: "dot idents 9", expr: `$x = [MORE:"stuff"] ; x.y`, want: nil},
{desc: "parse comments 1", expr: parseComments1, wantObj: true, wantErr: nil},
{desc: "parse comments 2", expr: parseComments2, wantObj: true, wantErr: nil},
{desc: "parse comments 3", expr: parseComments3, wantObj: true, wantErr: nil},
{desc: "parse comments 4", expr: parseComments4, wantObj: true, wantErr: nil},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
ctx := context.Background()
outW := bytes.NewBuffer(nil)
inst := ucl.New(ucl.WithOut(outW), ucl.WithTestBuiltin())
res, err := inst.EvalString(ctx, tt.expr)
if tt.wantErr != nil {
assert.ErrorIs(t, err, tt.wantErr)
} else if tt.wantObj {
assert.NoError(t, err)
_, isObj := res.(ucl.Object)
assert.True(t, isObj)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.want, res)
}
})
}
}
func TestInst_Eval_WithSubEnv(t *testing.T) {
t.Run("global symbols should not leak across environments", func(t *testing.T) {
ctx := t.Context()
inst := ucl.New()
res, err := inst.Eval(ctx, strings.NewReader(`$a = "hello" ; $a`), ucl.WithSubEnv())
assert.NoError(t, err)
assert.Equal(t, "hello", res)
res, err = inst.Eval(ctx, strings.NewReader(`$a`), ucl.WithSubEnv())
assert.NoError(t, err)
assert.Nil(t, res)
})
t.Run("environments should not leak when using hooks", func(t *testing.T) {
tests := []struct {
descr string
eval1 string
eval2 string
want1 any
want2 any
}{
{
descr: "reading vars",
eval1: `$a = "hello" ; hook { $a }`,
eval2: `$a = "world" ; hook { $a }`,
want1: "hello",
want2: "world",
},
{
descr: "modifying vars",
eval1: `$a = "hello" ; hook { $a = "new value" ; $a }`,
eval2: `$a = "world" ; hook { $a }`,
want1: "new value",
want2: "world",
},
{
descr: "defining procs",
eval1: `proc say_hello { "hello" } ; hook { say_hello }`,
eval2: `proc say_hello { "world" } ; hook { say_hello }`,
want1: "hello",
want2: "world",
},
{
descr: "exporting procs 1",
eval1: `export say_hello { "hello" } ; hook { say_hello }`,
eval2: `hook { say_hello }`,
want1: "hello",
want2: "hello",
},
{
descr: "exporting procs 2",
eval1: `$a = "hello" ; export say_hello { $a = "world"; $a } ; hook { say_hello }`,
eval2: `$a = "other" ; hook { say_hello }`,
want1: "world",
want2: "world",
},
}
for _, tt := range tests {
t.Run(tt.descr, func(t *testing.T) {
ctx := t.Context()
hooks := make([]ucl.Invokable, 0)
inst := ucl.New()
inst.SetBuiltin("hook", func(ctx context.Context, args ucl.CallArgs) (any, error) {
var hookProc ucl.Invokable
if err := args.Bind(&hookProc); err != nil {
return nil, err
}
hooks = append(hooks, hookProc)
return nil, nil
})
inst.SetBuiltin("export", func(ctx context.Context, args ucl.CallArgs) (any, error) {
var (
name string
hookProc ucl.Invokable
)
if err := args.Bind(&name, &hookProc); err != nil {
return nil, err
}
inst.SetBuiltinInvokable(name, hookProc)
return nil, nil
})
res, err := inst.Eval(ctx, strings.NewReader(tt.eval1), ucl.WithSubEnv())
assert.NoError(t, err)
assert.Nil(t, res)
res, err = inst.Eval(ctx, strings.NewReader(tt.eval2), ucl.WithSubEnv())
assert.NoError(t, err)
assert.Nil(t, res)
h1, err := hooks[0].Invoke(ctx, ucl.CallArgs{})
assert.NoError(t, err)
assert.Equal(t, tt.want1, h1)
h2, err := hooks[1].Invoke(ctx, ucl.CallArgs{})
assert.NoError(t, err)
assert.Equal(t, tt.want2, h2)
})
}
})
}
func TestInst_SetPseudoVar(t *testing.T) {
tests := []struct {
desc string
expr string
wantRes any
wantBarVar string
wantErr bool
}{
{desc: "read var 1", expr: `@foo`, wantRes: "this is foo"},
{desc: "read var 2", expr: `@bar`, wantRes: "this is bar"},
{desc: "read var 3", expr: `@fla`, wantRes: "missing value of fla"},
{desc: "write var 1", expr: `@foo = "hello" ; @foo`, wantRes: "hello"},
{desc: "write var 2", expr: `@bar = "world" ; @bar`, wantRes: "world", wantBarVar: "world"},
{desc: "write var 3", expr: `@blong = "hello"`, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
bar := &stringPseudoVar{str: "this is bar"}
inst := ucl.New(ucl.WithTestBuiltin())
inst.SetPseudoVar("foo", &stringPseudoVar{str: "this is foo"})
inst.SetPseudoVar("bar", bar)
inst.SetMissingPseudoVarHandler(missingPseudoVarType{})
res, err := inst.EvalString(t.Context(), tt.expr)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.wantRes, res)
if tt.wantBarVar != "" {
assert.Equal(t, tt.wantBarVar, bar.str)
}
})
}
}
var parseComments1 = `
proc lookup { |file|
foreach { |toks|
}
# this use to fail
}
`
var parseComments2 = `
proc lookup { |file|
foreach { |toks|
}
# this use to fail
#
# And so did this
}
`
var parseComments3 = `
proc lookup { |file|
foreach { |toks|
}
# this use to fail
#
# And so did this
}
`
var parseComments4 = `
proc lookup { |file|
foreach { |toks|
}
}
# this use to fail`
type stringPseudoVar struct {
str string
}
func (s *stringPseudoVar) Get(ctx context.Context) (any, error) {
return s.str, nil
}
func (s *stringPseudoVar) Set(ctx context.Context, v any) error {
s.str = v.(string)
return nil
}
type missingPseudoVarType struct{}
func (missingPseudoVarType) Get(ctx context.Context, name string) (any, error) {
return "missing value of " + name, nil
}