github.com/songzhibin97/gkit@v1.2.13/distributed/task/reflect_test.go (about)

     1  package task
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	json "github.com/json-iterator/go"
     8  )
     9  
    10  var reflectValuesTestCases = []struct {
    11  	name          string
    12  	value         interface{}
    13  	expectedType  string
    14  	expectedValue interface{}
    15  }{
    16  	// basic types
    17  	{
    18  		name:         "bool",
    19  		value:        false,
    20  		expectedType: "bool",
    21  	},
    22  	{
    23  		name:          "int",
    24  		value:         json.Number("123"),
    25  		expectedType:  "int",
    26  		expectedValue: int(123),
    27  	},
    28  	{
    29  		name:          "int8",
    30  		value:         json.Number("123"),
    31  		expectedType:  "int8",
    32  		expectedValue: int8(123),
    33  	},
    34  	{
    35  		name:          "int16",
    36  		value:         json.Number("123"),
    37  		expectedType:  "int16",
    38  		expectedValue: int16(123),
    39  	},
    40  	{
    41  		name:          "int32",
    42  		value:         json.Number("123"),
    43  		expectedType:  "int32",
    44  		expectedValue: int32(123),
    45  	},
    46  	{
    47  		name:          "int64",
    48  		value:         json.Number("185135722552891243"),
    49  		expectedType:  "int64",
    50  		expectedValue: int64(185135722552891243),
    51  	},
    52  	{
    53  		name:          "uint",
    54  		value:         json.Number("123"),
    55  		expectedType:  "uint",
    56  		expectedValue: uint(123),
    57  	},
    58  	{
    59  		name:          "uint8",
    60  		value:         json.Number("123"),
    61  		expectedType:  "uint8",
    62  		expectedValue: uint8(123),
    63  	},
    64  	{
    65  		name:          "uint16",
    66  		value:         json.Number("123"),
    67  		expectedType:  "uint16",
    68  		expectedValue: uint16(123),
    69  	},
    70  	{
    71  		name:          "uint32",
    72  		value:         json.Number("123"),
    73  		expectedType:  "uint32",
    74  		expectedValue: uint32(123),
    75  	},
    76  	{
    77  		name:          "uint64",
    78  		value:         json.Number("185135722552891243"),
    79  		expectedType:  "uint64",
    80  		expectedValue: uint64(185135722552891243),
    81  	},
    82  	{
    83  		name:          "float32",
    84  		value:         json.Number("0.5"),
    85  		expectedType:  "float32",
    86  		expectedValue: float32(0.5),
    87  	},
    88  	{
    89  		name:          "float64",
    90  		value:         json.Number("0.5"),
    91  		expectedType:  "float64",
    92  		expectedValue: float64(0.5),
    93  	},
    94  	{
    95  		name:          "string",
    96  		value:         "123",
    97  		expectedType:  "string",
    98  		expectedValue: "123",
    99  	},
   100  	// slices
   101  	{
   102  		name:          "[]bool",
   103  		value:         []interface{}{false, true},
   104  		expectedType:  "[]bool",
   105  		expectedValue: []bool{false, true},
   106  	},
   107  	{
   108  		name:          "[]int",
   109  		value:         []interface{}{json.Number("1"), json.Number("2")},
   110  		expectedType:  "[]int",
   111  		expectedValue: []int{1, 2},
   112  	},
   113  	{
   114  		name:          "[]int8",
   115  		value:         []interface{}{json.Number("1"), json.Number("2")},
   116  		expectedType:  "[]int8",
   117  		expectedValue: []int8{1, 2},
   118  	},
   119  	{
   120  		name:          "[]int16",
   121  		value:         []interface{}{json.Number("1"), json.Number("2")},
   122  		expectedType:  "[]int16",
   123  		expectedValue: []int16{1, 2},
   124  	},
   125  	{
   126  		name:          "[]int32",
   127  		value:         []interface{}{json.Number("1"), json.Number("2")},
   128  		expectedType:  "[]int32",
   129  		expectedValue: []int32{1, 2},
   130  	},
   131  	{
   132  		name:          "[]int64",
   133  		value:         []interface{}{json.Number("1"), json.Number("2")},
   134  		expectedType:  "[]int64",
   135  		expectedValue: []int64{1, 2},
   136  	},
   137  	{
   138  		name:          "[]uint",
   139  		value:         []interface{}{json.Number("1"), json.Number("2")},
   140  		expectedType:  "[]uint",
   141  		expectedValue: []uint{1, 2},
   142  	},
   143  	{
   144  		name:          "[]uint8",
   145  		value:         []interface{}{json.Number("1"), json.Number("2")},
   146  		expectedType:  "[]uint8",
   147  		expectedValue: []uint8{1, 2},
   148  	},
   149  	{
   150  		name:          "[]uint16",
   151  		value:         []interface{}{json.Number("1"), json.Number("2")},
   152  		expectedType:  "[]uint16",
   153  		expectedValue: []uint16{1, 2},
   154  	},
   155  	{
   156  		name:          "[]uint32",
   157  		value:         []interface{}{json.Number("1"), json.Number("2")},
   158  		expectedType:  "[]uint32",
   159  		expectedValue: []uint32{1, 2},
   160  	},
   161  	{
   162  		name:          "[]uint64",
   163  		value:         []interface{}{json.Number("1"), json.Number("2")},
   164  		expectedType:  "[]uint64",
   165  		expectedValue: []uint64{1, 2},
   166  	},
   167  	{
   168  		name:          "[]float32",
   169  		value:         []interface{}{json.Number("0.5"), json.Number("1.28")},
   170  		expectedType:  "[]float32",
   171  		expectedValue: []float32{0.5, 1.28},
   172  	},
   173  	{
   174  		name:          "[]float64",
   175  		value:         []interface{}{json.Number("0.5"), json.Number("1.28")},
   176  		expectedType:  "[]float64",
   177  		expectedValue: []float64{0.5, 1.28},
   178  	},
   179  	{
   180  		name:          "[]string",
   181  		value:         []interface{}{"foo", "bar"},
   182  		expectedType:  "[]string",
   183  		expectedValue: []string{"foo", "bar"},
   184  	},
   185  	// empty slices from NULL
   186  	{
   187  		name:          "[]bool",
   188  		value:         nil,
   189  		expectedType:  "[]bool",
   190  		expectedValue: []bool{},
   191  	},
   192  	{
   193  		name:          "[]int64",
   194  		value:         nil,
   195  		expectedType:  "[]int64",
   196  		expectedValue: []int64{},
   197  	},
   198  	{
   199  		name:          "[]uint64",
   200  		value:         nil,
   201  		expectedType:  "[]uint64",
   202  		expectedValue: []uint64{},
   203  	},
   204  	{
   205  		name:          "[]float64",
   206  		value:         nil,
   207  		expectedType:  "[]float64",
   208  		expectedValue: []float64{},
   209  	},
   210  	{
   211  		name:          "[]string",
   212  		value:         nil,
   213  		expectedType:  "[]string",
   214  		expectedValue: []string{},
   215  	},
   216  }
   217  
   218  func TestReflectValue(t *testing.T) {
   219  	t.Parallel()
   220  
   221  	for _, tc := range reflectValuesTestCases {
   222  		tc := tc // capture range variable
   223  		t.Run(tc.name, func(t *testing.T) {
   224  			t.Parallel()
   225  
   226  			value, err := ReflectValue(tc.name, tc.value)
   227  			if err != nil {
   228  				t.Error(err)
   229  			}
   230  			if value.Type().String() != tc.expectedType {
   231  				t.Errorf("type is %v, want %s", value.Type().String(), tc.expectedType)
   232  			}
   233  			if tc.expectedValue != nil {
   234  				if !reflect.DeepEqual(value.Interface(), tc.expectedValue) {
   235  					t.Errorf("value is %v, want %v", value.Interface(), tc.expectedValue)
   236  				}
   237  			}
   238  		})
   239  	}
   240  }