2018-02-15 17:16:41 +08:00
|
|
|
package eval
|
|
|
|
|
2020-08-15 11:08:45 +08:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
)
|
2018-02-15 17:16:41 +08:00
|
|
|
|
2018-09-28 01:15:19 +08:00
|
|
|
type opts struct {
|
|
|
|
FooBar string
|
|
|
|
POSIX bool `name:"posix"`
|
|
|
|
Min int
|
2020-08-15 11:08:45 +08:00
|
|
|
ignore bool // this should be ignored since it isn't exported
|
2018-09-28 01:15:19 +08:00
|
|
|
}
|
2018-02-15 17:16:41 +08:00
|
|
|
|
2018-09-28 01:15:19 +08:00
|
|
|
var scanOptionsTests = []struct {
|
|
|
|
rawOpts RawOptions
|
|
|
|
preScan opts
|
|
|
|
postScan opts
|
2020-08-15 11:08:45 +08:00
|
|
|
err error
|
2018-09-28 01:15:19 +08:00
|
|
|
}{
|
|
|
|
{RawOptions{"foo-bar": "lorem ipsum"},
|
2020-08-15 11:08:45 +08:00
|
|
|
opts{}, opts{FooBar: "lorem ipsum"}, nil},
|
2018-09-28 01:15:19 +08:00
|
|
|
{RawOptions{"posix": true},
|
2020-08-15 11:08:45 +08:00
|
|
|
opts{}, opts{POSIX: true}, nil},
|
|
|
|
// Since "ignore" is not exported it will result in an error when used.
|
|
|
|
{RawOptions{"ignore": true},
|
|
|
|
opts{}, opts{ignore: false}, errors.New("unknown option ignore")},
|
2018-09-28 01:15:19 +08:00
|
|
|
}
|
2018-02-15 17:16:41 +08:00
|
|
|
|
2018-09-28 01:15:19 +08:00
|
|
|
func TestScanOptions(t *testing.T) {
|
2020-08-15 11:08:45 +08:00
|
|
|
// scanOptions requires a pointer to struct.
|
|
|
|
err := scanOptions(RawOptions{}, opts{})
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Scan should have reported invalid options arg error")
|
|
|
|
}
|
|
|
|
|
2018-09-28 01:15:19 +08:00
|
|
|
for _, test := range scanOptionsTests {
|
|
|
|
opts := test.preScan
|
2020-08-15 11:08:45 +08:00
|
|
|
err := scanOptions(test.rawOpts, &opts)
|
|
|
|
|
|
|
|
if ((err == nil) != (test.err == nil)) ||
|
|
|
|
(err != nil && test.err != nil && err.Error() != test.err.Error()) {
|
|
|
|
t.Errorf("Scan error mismatch %v: want %q, got %q", test.rawOpts, test.err, err)
|
|
|
|
}
|
2018-09-28 01:15:19 +08:00
|
|
|
if opts != test.postScan {
|
|
|
|
t.Errorf("Scan %v => %v, want %v", test.rawOpts, opts, test.postScan)
|
2018-09-27 08:48:44 +08:00
|
|
|
}
|
2018-02-15 17:16:41 +08:00
|
|
|
}
|
|
|
|
}
|