2024-04-27 00:11:22 +00:00
|
|
|
package ucl_test
|
2024-04-11 12:05:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2024-04-27 00:11:22 +00:00
|
|
|
"github.com/lmika/ucl/ucl"
|
|
|
|
|
2024-04-11 12:05:05 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestInst_Eval(t *testing.T) {
|
|
|
|
tests := []struct {
|
2025-01-15 11:07:29 +00:00
|
|
|
desc string
|
|
|
|
expr string
|
|
|
|
want any
|
|
|
|
wantErr error
|
2024-04-11 12:05:05 +00:00
|
|
|
}{
|
|
|
|
{desc: "simple string", expr: `firstarg "hello"`, want: "hello"},
|
2024-04-24 10:12:39 +00:00
|
|
|
{desc: "simple int 1", expr: `firstarg 123`, want: 123},
|
|
|
|
{desc: "simple int 2", expr: `firstarg -234`, want: -234},
|
|
|
|
{desc: "simple ident", expr: `firstarg a-test`, want: "a-test"},
|
2024-04-11 12:05:05 +00:00
|
|
|
|
2025-01-15 11:07:29 +00:00
|
|
|
// String interpolation
|
|
|
|
{desc: "interpolate string 1", expr: `set what "world" ; firstarg "hello $what"`, want: "hello world"},
|
|
|
|
{desc: "interpolate string 2", expr: `set what "world" ; set when "now" ; firstarg "$when, hello $what"`, want: "now, hello world"},
|
|
|
|
{desc: "interpolate string 3", expr: `set what "world" ; set when "now" ; firstarg "${when}, hello ${what}"`, want: "now, hello world"},
|
|
|
|
{desc: "interpolate string 4", expr: `set "crazy var" "unknown" ; firstarg "hello ${crazy var}"`, want: "hello unknown"},
|
|
|
|
{desc: "interpolate string 5", expr: `set what "world" ; firstarg "hello $($what)"`, want: "hello world"},
|
|
|
|
{desc: "interpolate string 6", expr: `firstarg "hello $([1 2 3] | len)"`, want: "hello 3"},
|
|
|
|
{desc: "interpolate string 7", expr: `firstarg "hello $(add (add 1 2) 3)"`, want: "hello 6"},
|
|
|
|
{desc: "interpolate string 8", expr: `firstarg ("$(add 2 (add 1 1)) + $([1 2 3].(1) | cat ("$("")")) = $(("$(add 2 (4))"))")`, want: "4 + 2 = 6"},
|
|
|
|
|
2024-04-11 12:05:05 +00:00
|
|
|
// Sub-expressions
|
2024-04-12 23:25:16 +00:00
|
|
|
{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"},
|
2024-04-11 12:05:05 +00:00
|
|
|
|
|
|
|
// Variables
|
|
|
|
{desc: "var 1", expr: `firstarg $a`, want: "alpha"},
|
|
|
|
{desc: "var 2", expr: `firstarg $bee`, want: "buzz"},
|
2024-04-12 23:25:16 +00:00
|
|
|
{desc: "var 3", expr: `firstarg (sjoin $bee " " $bee " " $bee)`, want: "buzz buzz buzz"},
|
2024-04-11 12:05:05 +00:00
|
|
|
|
|
|
|
// Pipeline
|
2024-04-23 12:02:06 +00:00
|
|
|
{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"},
|
2024-04-11 12:05:05 +00:00
|
|
|
|
2024-04-23 12:02:06 +00:00
|
|
|
{desc: "ignored pipe", expr: `(list "aye" | firstarg "ignore me") | joinpipe`, want: "aye"},
|
2024-04-11 12:05:05 +00:00
|
|
|
|
|
|
|
// Multi-statements
|
|
|
|
{desc: "multi 1", expr: `firstarg "hello" ; firstarg "world"`, want: "world"},
|
2024-04-23 12:02:06 +00:00
|
|
|
{desc: "multi 2", expr: `list "hello" | toUpper ; firstarg "world"`, want: "world"},
|
2024-04-11 12:05:05 +00:00
|
|
|
{desc: "multi 3", expr: `set new "this is new" ; firstarg $new`, want: "this is new"},
|
2024-04-16 12:05:21 +00:00
|
|
|
|
|
|
|
// Lists
|
|
|
|
{desc: "list 1", expr: `firstarg ["1" "2" "3"]`, want: []any{"1", "2", "3"}},
|
2024-04-23 12:02:06 +00:00
|
|
|
{desc: "list 2", expr: `set one "one" ; firstarg [$one (list "two" | map { |x| toUpper $x } | head) "three"]`, want: []any{"one", "TWO", "three"}},
|
2024-04-16 12:05:21 +00:00
|
|
|
{desc: "list 3", expr: `firstarg []`, want: []any{}},
|
2025-01-15 11:07:29 +00:00
|
|
|
{desc: "list 4", expr: `set x ["a" "b" "c"] ; firstarg [$x.(2) $x.(1) $x.(0)]`, want: []any{"c", "b", "a"}},
|
2024-04-16 12:05:21 +00:00
|
|
|
|
|
|
|
// 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: `
|
|
|
|
set one "one" ; set n1 "1"
|
|
|
|
firstarg [
|
|
|
|
$one:$n1
|
2024-04-23 12:02:06 +00:00
|
|
|
(list "two" | map { |x| toUpper $x } | head):(list "2" | map { |x| toUpper $x } | head)
|
2024-04-16 12:05:21 +00:00
|
|
|
three:"3"
|
|
|
|
]`, want: map[string]any{"one": "1", "TWO": "2", "three": "3"}},
|
|
|
|
{desc: "map 4", expr: `firstarg [:]`, want: map[string]any{}},
|
2025-01-15 11:07:29 +00:00
|
|
|
{desc: "map 5", expr: `set 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: `set 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"}},
|
2024-05-10 23:16:34 +00:00
|
|
|
|
|
|
|
// Dots
|
|
|
|
{desc: "dot 1", expr: `set x [1 2 3] ; $x.(0)`, want: 1},
|
|
|
|
{desc: "dot 2", expr: `set x [1 2 3] ; $x.(1)`, want: 2},
|
|
|
|
{desc: "dot 3", expr: `set x [1 2 3] ; $x.(2)`, want: 3},
|
|
|
|
{desc: "dot 4", expr: `set x [1 2 3] ; $x.(3)`, want: nil},
|
|
|
|
{desc: "dot 5", expr: `set x [1 2 3] ; $x.(add 1 1)`, want: 3},
|
|
|
|
{desc: "dot 6", expr: `set x [alpha:"hello" bravo:"world"] ; $x.alpha`, want: "hello"},
|
|
|
|
{desc: "dot 7", expr: `set x [alpha:"hello" bravo:"world"] ; $x.bravo`, want: "world"},
|
|
|
|
{desc: "dot 8", expr: `set x [alpha:"hello" bravo:"world"] ; $x.charlie`, want: nil},
|
|
|
|
{desc: "dot 9", expr: `set x [alpha:"hello" bravo:"world"] ; $x.("alpha")`, want: "hello"},
|
|
|
|
{desc: "dot 10", expr: `set x [alpha:"hello" bravo:"world"] ; $x.("bravo")`, want: "world"},
|
|
|
|
{desc: "dot 11", expr: `set x [alpha:"hello" bravo:"world"] ; $x.("charlie")`, want: nil},
|
|
|
|
{desc: "dot 12", expr: `set x [MORE:"stuff"] ; $x.("more" | toUpper)`, want: "stuff"},
|
|
|
|
{desc: "dot 13", expr: `set x [MORE:"stuff"] ; $x.(toUpper ("more"))`, want: "stuff"},
|
|
|
|
{desc: "dot 14", expr: `set x [MORE:"stuff"] ; x.y`, want: nil},
|
2025-01-15 11:07:29 +00:00
|
|
|
|
|
|
|
{desc: "parse comments 1", expr: parseComments1, wantErr: ucl.ErrNotConvertable},
|
|
|
|
{desc: "parse comments 2", expr: parseComments2, wantErr: ucl.ErrNotConvertable},
|
|
|
|
{desc: "parse comments 3", expr: parseComments3, wantErr: ucl.ErrNotConvertable},
|
|
|
|
{desc: "parse comments 4", expr: parseComments4, wantErr: ucl.ErrNotConvertable},
|
2024-04-11 12:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
outW := bytes.NewBuffer(nil)
|
|
|
|
|
2024-04-27 00:11:22 +00:00
|
|
|
inst := ucl.New(ucl.WithOut(outW), ucl.WithTestBuiltin())
|
2024-04-11 12:05:05 +00:00
|
|
|
res, err := inst.Eval(ctx, tt.expr)
|
|
|
|
|
2025-01-15 11:07:29 +00:00
|
|
|
if tt.wantErr != nil {
|
|
|
|
assert.ErrorIs(t, err, tt.wantErr)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, tt.want, res)
|
|
|
|
}
|
2024-04-11 12:05:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2025-01-15 11:07:29 +00:00
|
|
|
|
|
|
|
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`
|