github.com/elves/elvish@v0.15.0/pkg/eval/vals/conversion_test.go (about) 1 package vals 2 3 import ( 4 "reflect" 5 "testing" 6 7 . "github.com/elves/elvish/pkg/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(TypeOf(dstInit)) 19 err := ScanToGo(src, ptr.Interface()) 20 return ptr.Elem().Interface(), err 21 } 22 23 func TestScanToGo(t *testing.T) { 24 Test(t, Fn("ScanToGo", scanToGo2), Table{ 25 Args("12", 0).Rets(12), 26 Args("0x12", 0).Rets(0x12), 27 Args(12.0, 0).Rets(12), 28 Args("23", 0.0).Rets(23.0), 29 Args("0x23", 0.0).Rets(float64(0x23)), 30 Args("x", ' ').Rets('x'), 31 Args("foo", "").Rets("foo"), 32 Args(someType{"foo"}, someType{}).Rets(someType{"foo"}), 33 Args(nil, nil).Rets(nil), 34 35 Args(0.5, 0).Rets(0, errMustBeInteger), 36 Args("x", someType{}).Rets(Any, wrongType{"!!vals.someType", "string"}), 37 Args(someType{}, 0).Rets(Any, errMustBeInteger), 38 Args("x", 0).Rets(Any, cannotParseAs{"integer", "x"}), 39 Args(someType{}, 0.0).Rets(Any, errMustBeNumber), 40 Args("x", 0.0).Rets(Any, cannotParseAs{"number", "x"}), 41 Args(someType{}, ' ').Rets(Any, errMustBeString), 42 Args("\xc3\x28", ' ').Rets(Any, errMustBeValidUTF8), // Invalid UTF8 43 Args("ab", ' ').Rets(Any, errMustHaveSingleRune), 44 }) 45 } 46 47 func TestFromGo(t *testing.T) { 48 Test(t, Fn("FromGo", FromGo), Table{ 49 Args(12).Rets("12"), 50 Args(1.5).Rets(1.5), 51 Args('x').Rets("x"), 52 Args(nil).Rets(nil), 53 Args(someType{"foo"}).Rets(someType{"foo"}), 54 }) 55 }