github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/elvish/eval/vals/conversion_test.go (about) 1 package vals 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/u-root/u-root/cmds/core/elvish/tt" 8 ) 9 10 type someType struct { 11 foo string 12 } 13 14 // A wrapper around ScanToGo, to make it easier to test. Instead of supplying a 15 // pointer to the destination, an initial value to the destination is supplied 16 // and the result is returned. 17 func scanToGo2(src interface{}, dstInit interface{}) (interface{}, error) { 18 ptr := reflect.New(reflect.TypeOf(dstInit)) 19 err := ScanToGo(src, ptr.Interface()) 20 return ptr.Elem().Interface(), err 21 } 22 23 var scanToGoTests = tt.Table{ 24 Args("12", 0).Rets(12), 25 Args("0x12", 0).Rets(0x12), 26 Args("23", 0.0).Rets(23.0), 27 Args("0x23", 0.0).Rets(float64(0x23)), 28 Args("x", ' ').Rets('x'), 29 Args("foo", "").Rets("foo"), 30 Args(someType{"foo"}, someType{}).Rets(someType{"foo"}), 31 32 Args("x", someType{}).Rets(any, anyError), 33 Args(someType{}, 0).Rets(any, anyError), 34 Args("x", 0).Rets(any, anyError), 35 Args(someType{}, 0.0).Rets(any, anyError), 36 Args("x", 0.0).Rets(any, anyError), 37 Args(someType{}, ' ').Rets(any, anyError), 38 Args("\xc3\x28", ' ').Rets(any, anyError), // Invalid UTF8 39 Args("ab", ' ').Rets(any, anyError), 40 } 41 42 func TestScanToGo(t *testing.T) { 43 tt.Test(t, tt.Fn("ScanToGo", scanToGo2), scanToGoTests) 44 } 45 46 var fromGoTests = tt.Table{ 47 tt.Args(12).Rets("12"), 48 tt.Args(1.5).Rets("1.5"), 49 tt.Args('x').Rets("x"), 50 tt.Args(someType{"foo"}).Rets(someType{"foo"}), 51 } 52 53 func TestFromGo(t *testing.T) { 54 tt.Test(t, tt.Fn("FromGo", FromGo), fromGoTests) 55 }