ucl/cmdlang/inst_test.go

172 lines
4.5 KiB
Go
Raw Normal View History

2024-04-11 12:05:05 +00:00
package cmdlang_test
import (
"bytes"
"context"
"github.com/lmika/cmdlang-proto/cmdlang"
"github.com/stretchr/testify/assert"
"testing"
)
func TestInst_Eval(t *testing.T) {
tests := []struct {
desc string
expr string
want string
}{
{desc: "simple string", expr: `firstarg "hello"`, want: "hello"},
// 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
{desc: "pipe 1", expr: `pipe "aye" "bee" "see" | joinpipe`, want: "aye,bee,see"},
{desc: "pipe 2", expr: `pipe "aye" "bee" "see" | toUpper | joinpipe`, want: "AYE,BEE,SEE"},
2024-04-11 12:32:38 +00:00
{desc: "pipe 3", expr: `firstarg "normal" | toUpper | joinpipe`, want: "NORMAL"},
2024-04-11 12:05:05 +00:00
{desc: "ignored pipe", expr: `pipe "aye" "bee" "see" | firstarg "ignore me"`, want: "ignore me"}, // TODO: check for leaks
// Multi-statements
{desc: "multi 1", expr: `firstarg "hello" ; firstarg "world"`, want: "world"},
{desc: "multi 2", expr: `pipe "hello" | toUpper ; firstarg "world"`, want: "world"}, // TODO: assert for leaks
{desc: "multi 3", expr: `set new "this is new" ; firstarg $new`, want: "this is new"},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
ctx := context.Background()
outW := bytes.NewBuffer(nil)
inst := cmdlang.New(cmdlang.WithOut(outW), cmdlang.WithTestBuiltin())
res, err := inst.Eval(ctx, tt.expr)
assert.NoError(t, err)
assert.Equal(t, tt.want, res)
})
}
}
2024-04-13 11:46:50 +00:00
func TestBuiltins_Echo(t *testing.T) {
2024-04-12 23:25:16 +00:00
tests := []struct {
desc string
expr string
want string
}{
{desc: "no args", expr: `echo`, want: "\n"},
{desc: "single arg", expr: `echo "hello"`, want: "hello\n"},
{desc: "dual args", expr: `echo "hello " "world"`, want: "hello world\n"},
2024-04-13 11:46:50 +00:00
{desc: "multi-line 1", expr: `
echo "Hello"
echo "world"
`, want: "Hello\nworld\n"},
{desc: "multi-line 2", expr: `
echo "Hello"
echo "world"
`, want: "Hello\nworld\n"},
{desc: "multi-line 3", expr: `
;;;
echo "Hello"
;
echo "world"
;
`, want: "Hello\nworld\n"},
2024-04-12 23:25:16 +00:00
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
ctx := context.Background()
outW := bytes.NewBuffer(nil)
inst := cmdlang.New(cmdlang.WithOut(outW), cmdlang.WithTestBuiltin())
2024-04-13 11:46:50 +00:00
res, err := inst.Eval(ctx, tt.expr)
2024-04-12 23:25:16 +00:00
assert.NoError(t, err)
2024-04-13 11:46:50 +00:00
assert.Nil(t, res)
2024-04-12 23:25:16 +00:00
assert.Equal(t, tt.want, outW.String())
})
}
}
2024-04-13 11:46:50 +00:00
func TestBuiltins_If(t *testing.T) {
tests := []struct {
desc string
expr string
want string
}{
{desc: "single then", expr: `
set x "Hello"
if $x {
echo "true"
}`, want: "true\n(nil)\n"},
{desc: "single then and else", expr: `
set x "Hello"
if $x {
echo "true"
} else {
echo "false"
}`, want: "true\n(nil)\n"},
{desc: "single then, elif and else", expr: `
set x "Hello"
if $y {
echo "y is true"
} elif $x {
echo "x is true"
} else {
echo "nothings x"
}`, want: "x is true\n(nil)\n"},
{desc: "single then and elif, no else", expr: `
set x "Hello"
if $y {
echo "y is true"
} elif $x {
echo "x is true"
}`, want: "x is true\n(nil)\n"},
{desc: "single then, two elif, and else", expr: `
set x "Hello"
if $z {
echo "z is true"
} elif $y {
echo "y is true"
} elif $x {
echo "x is true"
}`, want: "x is true\n(nil)\n"},
{desc: "single then, two elif, and else, expecting else", expr: `
if $z {
echo "z is true"
} elif $y {
echo "y is true"
} elif $x {
echo "x is true"
} else {
echo "none is true"
}`, want: "none is true\n(nil)\n"},
{desc: "compressed then", expr: `set x "Hello" ; if $x { echo "true" }`, want: "true\n(nil)\n"},
{desc: "compressed else", expr: `if $x { echo "true" } else { echo "false" }`, want: "false\n(nil)\n"},
{desc: "compressed if", expr: `if $x { echo "x" } elif $y { echo "y" } else { echo "false" }`, want: "false\n(nil)\n"},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
ctx := context.Background()
outW := bytes.NewBuffer(nil)
inst := cmdlang.New(cmdlang.WithOut(outW), cmdlang.WithTestBuiltin())
err := inst.EvalAndDisplay(ctx, tt.expr)
assert.NoError(t, err)
assert.Equal(t, tt.want, outW.String())
})
}
2024-04-11 12:05:05 +00:00
}