github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/eval/options_test.go (about) 1 package eval 2 3 import ( 4 "errors" 5 "testing" 6 ) 7 8 type opts struct { 9 FooBar string 10 POSIX bool `name:"posix"` 11 Min int 12 ignore bool // this should be ignored since it isn't exported 13 } 14 15 var scanOptionsTests = []struct { 16 rawOpts RawOptions 17 preScan opts 18 postScan opts 19 err error 20 }{ 21 {RawOptions{"foo-bar": "lorem ipsum"}, 22 opts{}, opts{FooBar: "lorem ipsum"}, nil}, 23 {RawOptions{"posix": true}, 24 opts{}, opts{POSIX: true}, nil}, 25 // Since "ignore" is not exported it will result in an error when used. 26 {RawOptions{"ignore": true}, 27 opts{}, opts{ignore: false}, errors.New("unknown option ignore")}, 28 } 29 30 func TestScanOptions(t *testing.T) { 31 // scanOptions requires a pointer to struct. 32 err := scanOptions(RawOptions{}, opts{}) 33 if err == nil { 34 t.Errorf("Scan should have reported invalid options arg error") 35 } 36 37 for _, test := range scanOptionsTests { 38 opts := test.preScan 39 err := scanOptions(test.rawOpts, &opts) 40 41 if ((err == nil) != (test.err == nil)) || 42 (err != nil && test.err != nil && err.Error() != test.err.Error()) { 43 t.Errorf("Scan error mismatch %v: want %q, got %q", test.rawOpts, test.err, err) 44 } 45 if opts != test.postScan { 46 t.Errorf("Scan %v => %v, want %v", test.rawOpts, opts, test.postScan) 47 } 48 } 49 }