github.com/sandwich-go/boost@v1.3.29/xconv/num_test.go (about)

     1  package xconv
     2  
     3  import (
     4  	. "github.com/smartystreets/goconvey/convey"
     5  	"testing"
     6  )
     7  
     8  func TestNum(t *testing.T) {
     9  	Convey(`test parse num`, t, func() {
    10  		for _, v := range []struct {
    11  			i      string
    12  			is     interface{}
    13  			hasErr bool
    14  		}{
    15  			{i: "3", is: 3}, {i: "3x", is: 3, hasErr: true},
    16  			{i: "3", is: uint(3)}, {i: "3x", is: uint(3), hasErr: true},
    17  			{i: "3", is: int8(3)}, {i: "3x", is: int8(3), hasErr: true},
    18  			{i: "3", is: int16(3)}, {i: "3x", is: int16(3), hasErr: true},
    19  			{i: "3", is: int32(3)}, {i: "3x", is: int32(3), hasErr: true},
    20  			{i: "3", is: int64(3)}, {i: "3x", is: int64(3), hasErr: true},
    21  			{i: "3", is: uint8(3)}, {i: "3x", is: uint8(3), hasErr: true},
    22  			{i: "3", is: uint16(3)}, {i: "3x", is: uint16(3), hasErr: true},
    23  			{i: "3", is: uint32(3)}, {i: "3x", is: uint32(3), hasErr: true},
    24  			{i: "3", is: uint64(3)}, {i: "3x", is: uint64(3), hasErr: true},
    25  		} {
    26  			var check = func(val, expected interface{}, err error, hasErr bool) {
    27  				if hasErr {
    28  					So(err, ShouldNotBeNil)
    29  				} else {
    30  					So(err, ShouldBeNil)
    31  					So(val, ShouldEqual, expected)
    32  				}
    33  			}
    34  			switch vv := v.is.(type) {
    35  			case int:
    36  				val, err := ParseInt(v.i)
    37  				check(val, vv, err, v.hasErr)
    38  			case uint:
    39  				val, err := ParseUint(v.i)
    40  				check(val, vv, err, v.hasErr)
    41  			case int8:
    42  				val, err := ParseInt8(v.i)
    43  				check(val, vv, err, v.hasErr)
    44  			case int16:
    45  				val, err := ParseInt16(v.i)
    46  				check(val, vv, err, v.hasErr)
    47  			case int32:
    48  				val, err := ParseInt32(v.i)
    49  				check(val, vv, err, v.hasErr)
    50  			case int64:
    51  				val, err := ParseInt64(v.i)
    52  				check(val, vv, err, v.hasErr)
    53  			case uint8:
    54  				val, err := ParseUint8(v.i)
    55  				check(val, vv, err, v.hasErr)
    56  			case uint16:
    57  				val, err := ParseUint16(v.i)
    58  				check(val, vv, err, v.hasErr)
    59  			case uint32:
    60  				val, err := ParseUint32(v.i)
    61  				check(val, vv, err, v.hasErr)
    62  			case uint64:
    63  				val, err := ParseUint64(v.i)
    64  				check(val, vv, err, v.hasErr)
    65  			}
    66  		}
    67  	})
    68  
    69  	Convey(`test format num`, t, func() {
    70  		for _, v := range []struct {
    71  			i  interface{}
    72  			is string
    73  		}{
    74  			{3, "3"}, {uint(3), "3"},
    75  			{int8(3), "3"}, {int16(3), "3"}, {int32(3), "3"}, {int64(3), "3"},
    76  			{uint8(3), "3"}, {uint16(3), "3"}, {uint32(3), "3"}, {uint64(3), "3"},
    77  		} {
    78  			switch vv := v.i.(type) {
    79  			case int:
    80  				So(FormatInt(vv), ShouldEqual, v.is)
    81  			case uint:
    82  				So(FormatUint(vv), ShouldEqual, v.is)
    83  			case int8:
    84  				So(FormatInt8(vv), ShouldEqual, v.is)
    85  			case int16:
    86  				So(FormatInt16(vv), ShouldEqual, v.is)
    87  			case int32:
    88  				So(FormatInt32(vv), ShouldEqual, v.is)
    89  			case int64:
    90  				So(FormatInt64(vv), ShouldEqual, v.is)
    91  			case uint8:
    92  				So(FormatUint8(vv), ShouldEqual, v.is)
    93  			case uint16:
    94  				So(FormatUint16(vv), ShouldEqual, v.is)
    95  			case uint32:
    96  				So(FormatUint32(vv), ShouldEqual, v.is)
    97  			case uint64:
    98  				So(FormatUint64(vv), ShouldEqual, v.is)
    99  			default:
   100  				t.Fatal("unsupported type")
   101  			}
   102  		}
   103  	})
   104  
   105  	Convey(`test format num to slice`, t, func() {
   106  		for _, v := range []struct {
   107  			i  interface{}
   108  			is []byte
   109  		}{
   110  			{int(3), []byte{51}}, {int8(3), []byte{51}}, {int16(3), []byte{51}}, {int32(3), []byte{51}}, {int64(3), []byte{51}},
   111  			{uint(3), []byte{51}}, {uint8(3), []byte{51}}, {uint16(3), []byte{51}}, {uint32(3), []byte{51}}, {uint64(3), []byte{51}},
   112  		} {
   113  			switch vv := v.i.(type) {
   114  			case int:
   115  				So(FormatIntToSlice(vv), ShouldResemble, v.is)
   116  			case uint:
   117  				So(FormatUintToSlice(vv), ShouldResemble, v.is)
   118  			case int8:
   119  				So(FormatInt8ToSlice(vv), ShouldResemble, v.is)
   120  			case int16:
   121  				So(FormatInt16ToSlice(vv), ShouldResemble, v.is)
   122  			case int32:
   123  				So(FormatInt32ToSlice(vv), ShouldResemble, v.is)
   124  			case int64:
   125  				So(FormatInt64ToSlice(vv), ShouldResemble, v.is)
   126  			case uint8:
   127  				So(FormatUint8ToSlice(vv), ShouldResemble, v.is)
   128  			case uint16:
   129  				So(FormatUint16ToSlice(vv), ShouldResemble, v.is)
   130  			case uint32:
   131  				So(FormatUint32ToSlice(vv), ShouldResemble, v.is)
   132  			case uint64:
   133  				So(FormatUint64ToSlice(vv), ShouldResemble, v.is)
   134  			default:
   135  				t.Fatal("unsupported type")
   136  			}
   137  		}
   138  	})
   139  }