146 lines
5.5 KiB
Go
146 lines
5.5 KiB
Go
package wand
|
|
|
|
import (
|
|
"bytes"
|
|
"code.squareroundforest.org/arpio/wand/internal/tests/testlib"
|
|
"io"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestReflect(t *testing.T) {
|
|
t.Run("unpack", func(t *testing.T) {
|
|
f := func(a []int) int { return a[0] }
|
|
t.Run("slice", testExec(testCase{impl: f, command: "foo 42"}, "", "42"))
|
|
ps := func(a *[]*[]int) int { return (*((*a)[0]))[0] }
|
|
t.Run("pointer and slice", testExec(testCase{impl: ps, command: "foo 42"}, "", "42"))
|
|
type s struct {
|
|
Foo int
|
|
Bar *s
|
|
}
|
|
c := func(v s) int { return v.Foo }
|
|
t.Run("circular type", testExec(testCase{impl: c, command: "foo --foo 42"}, "unsupported parameter type", ""))
|
|
fp := func(a int) int { return a }
|
|
t.Run("function pointer", testExec(testCase{impl: &fp, command: "foo 42"}, "", "42"))
|
|
var p any
|
|
p = &p
|
|
t.Run("circular reference", testExec(testCase{impl: p, command: "foo"}, "must be a function or a pointer to a function", ""))
|
|
var i any
|
|
t.Run("nil interface", testExec(testCase{impl: &i, command: "foo"}, "must be a function or a pointer to a function", ""))
|
|
})
|
|
|
|
t.Run("io params", func(t *testing.T) {
|
|
f := func(in io.Reader) string { b, _ := io.ReadAll(in); return string(b) }
|
|
t.Run("in", testExec(testCase{impl: f, stdin: "foo", command: "bar"}, "", "foo"))
|
|
g := func(a string) io.Reader { return bytes.NewBufferString(a) }
|
|
t.Run("out", testExec(testCase{impl: g, command: "foo bar"}, "", "bar"))
|
|
h := func(out io.Writer, a string) { out.Write([]byte(a)) }
|
|
t.Run("stdout param", testExec(testCase{impl: h, command: "foo bar"}, "", "bar"))
|
|
})
|
|
|
|
t.Run("struct param", func(t *testing.T) {
|
|
f := func(s struct{ Bar int }) int { return s.Bar }
|
|
t.Run("basic", testExec(testCase{impl: f, command: "foo --bar 42"}, "", "42"))
|
|
})
|
|
|
|
t.Run("basic types", func(t *testing.T) {
|
|
t.Run("signed int", func(t *testing.T) {
|
|
f := func(a int) int { return a }
|
|
t.Run("decimal", testExec(testCase{impl: f, command: "foo 42"}, "", "42"))
|
|
t.Run("hexa", testExec(testCase{impl: f, command: "foo 0x2a"}, "", "42"))
|
|
t.Run("octal", testExec(testCase{impl: f, command: "foo 052"}, "", "42"))
|
|
t.Run("binary", testExec(testCase{impl: f, command: "foo 0b101010"}, "", "42"))
|
|
t.Run("fail", testExec(testCase{impl: f, command: "foo bar"}, "expecting int", ""))
|
|
g := func(a int32) int32 { return a }
|
|
t.Run("sized", testExec(testCase{impl: g, command: "foo 42"}, "", "42"))
|
|
})
|
|
|
|
t.Run("unsigned int", func(t *testing.T) {
|
|
f := func(a uint) uint { return a }
|
|
t.Run("decimal", testExec(testCase{impl: f, command: "foo 42"}, "", "42"))
|
|
t.Run("hexa", testExec(testCase{impl: f, command: "foo 0x2a"}, "", "42"))
|
|
t.Run("octal", testExec(testCase{impl: f, command: "foo 052"}, "", "42"))
|
|
t.Run("binary", testExec(testCase{impl: f, command: "foo 0b101010"}, "", "42"))
|
|
t.Run("fail", testExec(testCase{impl: f, command: "foo bar"}, "expecting uint", ""))
|
|
g := func(a uint32) uint32 { return a }
|
|
t.Run("sized", testExec(testCase{impl: g, command: "foo 42"}, "", "42"))
|
|
})
|
|
|
|
t.Run("bool", func(t *testing.T) {
|
|
f := func(a bool) bool { return a }
|
|
t.Run("true", testExec(testCase{impl: f, command: "foo true"}, "", "true"))
|
|
t.Run("false", testExec(testCase{impl: f, command: "foo false"}, "", "false"))
|
|
t.Run("fail", testExec(testCase{impl: f, command: "foo yes"}, "expecting bool", ""))
|
|
})
|
|
|
|
t.Run("float", func(t *testing.T) {
|
|
f := func(a float64) float64 { return a }
|
|
t.Run("accept", testExec(testCase{impl: f, command: "foo 3.14"}, "", "3.14"))
|
|
t.Run("fail", testExec(testCase{impl: f, command: "foo bar"}, "expecting float", ""))
|
|
})
|
|
|
|
t.Run("string", func(t *testing.T) {
|
|
f := func(a string) string { return a }
|
|
t.Run("accept", testExec(testCase{impl: f, command: "foo bar"}, "", "bar"))
|
|
})
|
|
|
|
t.Run("interface", func(t *testing.T) {
|
|
f := func(a any) any { return a }
|
|
t.Run("any", testExec(testCase{impl: f, command: "foo bar"}, "", "bar"))
|
|
type i interface{ Foo() }
|
|
g := func(a i) any { return a }
|
|
t.Run("unscannable", testExec(testCase{impl: g, command: "foo bar"}, "unsupported parameter type", ""))
|
|
})
|
|
})
|
|
|
|
t.Run("compatible types", func(t *testing.T) {
|
|
type s0 struct {
|
|
FooBar int
|
|
Foo struct{ Bar string }
|
|
}
|
|
type s1 struct {
|
|
FooBar int
|
|
Foo struct{ Bar int }
|
|
}
|
|
f := func(a s0) int { return a.FooBar + len(a.Foo.Bar) }
|
|
g := func(a s1) int { return a.FooBar + a.Foo.Bar }
|
|
t.Run("incompatible", testExec(testCase{impl: f, command: "foo --foo-bar 42"}, "duplicate fields with different types", ""))
|
|
t.Run("compatible", testExec(testCase{impl: g, command: "foo --foo-bar 42"}, "", "84"))
|
|
type s2 struct {
|
|
FooBar any
|
|
Foo struct{ Bar int }
|
|
}
|
|
h := func(a s2) int { return len(a.FooBar.(string)) + a.Foo.Bar }
|
|
t.Run("any interface", testExec(testCase{impl: h, command: "foo --foo-bar 42"}, "", "44"))
|
|
})
|
|
|
|
t.Run("bind failure", func(t *testing.T) {
|
|
f := func(s struct{ Foo time.Duration }) time.Duration { return s.Foo }
|
|
t.Run("no parse", testExec(testCase{impl: f, conf: "bar=baz", command: "foo"}, "", "0s"))
|
|
})
|
|
|
|
t.Run("indices of positional parameters", func(t *testing.T) {
|
|
t.Run(
|
|
"show function params",
|
|
testExec(testCase{impl: testlib.Foo, command: "foo help", contains: true}, "", "foo help"),
|
|
)
|
|
|
|
t.Run(
|
|
"skip non-positional params",
|
|
testExec(testCase{impl: testlib.Bar, command: "bar help", contains: true}, "", "bar help"),
|
|
)
|
|
})
|
|
|
|
t.Run("scalar type string", func(t *testing.T) {
|
|
t.Run(
|
|
"show help with options",
|
|
testExec(
|
|
testCase{impl: testlib.Baz, command: "baz help", contains: true},
|
|
"",
|
|
"baz help",
|
|
"--foo int",
|
|
),
|
|
)
|
|
})
|
|
}
|