2018-02-15 17:16:41 +08:00
|
|
|
package eval
|
|
|
|
|
2020-08-15 11:08:45 +08:00
|
|
|
import (
|
2022-01-01 00:50:44 +08:00
|
|
|
"reflect"
|
2020-08-15 11:08:45 +08:00
|
|
|
"testing"
|
2022-01-01 00:50:44 +08:00
|
|
|
|
2022-03-25 10:41:49 +08:00
|
|
|
"src.elv.sh/pkg/tt"
|
2020-08-15 11:08:45 +08:00
|
|
|
)
|
2018-02-15 17:16:41 +08:00
|
|
|
|
2022-03-25 10:41:49 +08:00
|
|
|
var Args = tt.Args
|
|
|
|
|
2018-09-28 01:15:19 +08:00
|
|
|
type opts struct {
|
2022-01-01 00:50:44 +08:00
|
|
|
Foo string
|
|
|
|
bar int
|
2018-09-28 01:15:19 +08:00
|
|
|
}
|
2018-02-15 17:16:41 +08:00
|
|
|
|
2022-03-12 06:49:32 +08:00
|
|
|
// Equal is required by cmp.Diff, since opts contains unexported fields.
|
|
|
|
func (o opts) Equal(p opts) bool { return o == p }
|
|
|
|
|
2018-09-28 01:15:19 +08:00
|
|
|
func TestScanOptions(t *testing.T) {
|
2022-01-01 00:50:44 +08:00
|
|
|
// A wrapper of ScanOptions, to make it easier to test
|
2022-03-20 23:50:25 +08:00
|
|
|
wrapper := func(src RawOptions, dstInit any) (any, error) {
|
2022-01-01 00:50:44 +08:00
|
|
|
ptr := reflect.New(reflect.TypeOf(dstInit))
|
|
|
|
ptr.Elem().Set(reflect.ValueOf(dstInit))
|
|
|
|
err := scanOptions(src, ptr.Interface())
|
|
|
|
return ptr.Elem().Interface(), err
|
2020-08-15 11:08:45 +08:00
|
|
|
}
|
|
|
|
|
2024-01-11 23:42:43 +08:00
|
|
|
tt.Test(t, tt.Fn(wrapper).Named("scanOptions"),
|
2022-01-01 00:50:44 +08:00
|
|
|
Args(RawOptions{"foo": "lorem ipsum"}, opts{}).
|
|
|
|
Rets(opts{Foo: "lorem ipsum"}, nil),
|
|
|
|
Args(RawOptions{"bar": 20}, opts{bar: 10}).
|
|
|
|
Rets(opts{bar: 10}, UnknownOption{"bar"}),
|
2024-01-11 23:42:43 +08:00
|
|
|
)
|
2018-02-15 17:16:41 +08:00
|
|
|
}
|