...

Source file src/github.com/go-openapi/swag/convert_types_test.go

Documentation: github.com/go-openapi/swag

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package swag
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestStringSlice(t *testing.T) {
    27  	testCasesStringSlice := [][]string{
    28  		{"a", "b", "c", "d", "e"},
    29  		{"a", "b", "", "", "e"},
    30  	}
    31  
    32  	for idx, in := range testCasesStringSlice {
    33  		if in == nil {
    34  			continue
    35  		}
    36  		out := StringSlice(in)
    37  		assertValues(t, in, out, true, idx)
    38  
    39  		out2 := StringValueSlice(out)
    40  		assertValues(t, in, out2, false, idx)
    41  	}
    42  }
    43  
    44  func TestStringValueSlice(t *testing.T) {
    45  	testCasesStringValueSlice := [][]*string{
    46  		{String("a"), String("b"), nil, String("c")},
    47  	}
    48  
    49  	for idx, in := range testCasesStringValueSlice {
    50  		if in == nil {
    51  			continue
    52  		}
    53  		out := StringValueSlice(in)
    54  		assertValues(t, in, out, false, idx)
    55  
    56  		out2 := StringSlice(out)
    57  		assertValues(t, in, out2, true, idx)
    58  	}
    59  }
    60  
    61  func TestStringMap(t *testing.T) {
    62  	testCasesStringMap := []map[string]string{
    63  		{"a": "1", "b": "2", "c": "3"},
    64  	}
    65  
    66  	for idx, in := range testCasesStringMap {
    67  		if in == nil {
    68  			continue
    69  		}
    70  		out := StringMap(in)
    71  		assertValues(t, in, out, true, idx)
    72  
    73  		out2 := StringValueMap(out)
    74  		assertValues(t, in, out2, false, idx)
    75  	}
    76  }
    77  
    78  func TestBoolSlice(t *testing.T) {
    79  	testCasesBoolSlice := [][]bool{
    80  		{true, true, false, false},
    81  	}
    82  
    83  	for idx, in := range testCasesBoolSlice {
    84  		if in == nil {
    85  			continue
    86  		}
    87  		out := BoolSlice(in)
    88  		assertValues(t, in, out, true, idx)
    89  
    90  		out2 := BoolValueSlice(out)
    91  		assertValues(t, in, out2, false, idx)
    92  	}
    93  }
    94  
    95  func TestBoolValueSlice(t *testing.T) {
    96  	testCasesBoolValueSlice := [][]*bool{
    97  		{Bool(true), Bool(true), Bool(false), Bool(false)},
    98  	}
    99  
   100  	for idx, in := range testCasesBoolValueSlice {
   101  		if in == nil {
   102  			continue
   103  		}
   104  		out := BoolValueSlice(in)
   105  		assertValues(t, in, out, false, idx)
   106  
   107  		out2 := BoolSlice(out)
   108  		assertValues(t, in, out2, true, idx)
   109  	}
   110  }
   111  
   112  func TestBoolMap(t *testing.T) {
   113  	testCasesBoolMap := []map[string]bool{
   114  		{"a": true, "b": false, "c": true},
   115  	}
   116  
   117  	for idx, in := range testCasesBoolMap {
   118  		if in == nil {
   119  			continue
   120  		}
   121  		out := BoolMap(in)
   122  		assertValues(t, in, out, true, idx)
   123  
   124  		out2 := BoolValueMap(out)
   125  		assertValues(t, in, out2, false, idx)
   126  	}
   127  }
   128  
   129  func TestIntSlice(t *testing.T) {
   130  	testCasesIntSlice := [][]int{
   131  		{1, 2, 3, 4},
   132  	}
   133  
   134  	for idx, in := range testCasesIntSlice {
   135  		if in == nil {
   136  			continue
   137  		}
   138  		out := IntSlice(in)
   139  		assertValues(t, in, out, true, idx)
   140  
   141  		out2 := IntValueSlice(out)
   142  		assertValues(t, in, out2, false, idx)
   143  	}
   144  }
   145  
   146  func TestIntValueSlice(t *testing.T) {
   147  	testCasesIntValueSlice := [][]*int{
   148  		{Int(1), Int(2), Int(3), Int(4)},
   149  	}
   150  
   151  	for idx, in := range testCasesIntValueSlice {
   152  		if in == nil {
   153  			continue
   154  		}
   155  		out := IntValueSlice(in)
   156  		assertValues(t, in, out, false, idx)
   157  
   158  		out2 := IntSlice(out)
   159  		assertValues(t, in, out2, true, idx)
   160  	}
   161  }
   162  
   163  func TestIntMap(t *testing.T) {
   164  	testCasesIntMap := []map[string]int{
   165  		{"a": 3, "b": 2, "c": 1},
   166  	}
   167  
   168  	for idx, in := range testCasesIntMap {
   169  		if in == nil {
   170  			continue
   171  		}
   172  		out := IntMap(in)
   173  		assertValues(t, in, out, true, idx)
   174  
   175  		out2 := IntValueMap(out)
   176  		assertValues(t, in, out2, false, idx)
   177  	}
   178  }
   179  
   180  func TestInt64Slice(t *testing.T) {
   181  	testCasesInt64Slice := [][]int64{
   182  		{1, 2, 3, 4},
   183  	}
   184  
   185  	for idx, in := range testCasesInt64Slice {
   186  		if in == nil {
   187  			continue
   188  		}
   189  		out := Int64Slice(in)
   190  		assertValues(t, in, out, true, idx)
   191  
   192  		out2 := Int64ValueSlice(out)
   193  		assertValues(t, in, out2, false, idx)
   194  	}
   195  }
   196  
   197  func TestInt64ValueSlice(t *testing.T) {
   198  	testCasesInt64ValueSlice := [][]*int64{
   199  		{Int64(1), Int64(2), Int64(3), Int64(4)},
   200  	}
   201  
   202  	for idx, in := range testCasesInt64ValueSlice {
   203  		if in == nil {
   204  			continue
   205  		}
   206  		out := Int64ValueSlice(in)
   207  		assertValues(t, in, out, false, idx)
   208  
   209  		out2 := Int64Slice(out)
   210  		assertValues(t, in, out2, true, idx)
   211  	}
   212  }
   213  
   214  func TestInt64Map(t *testing.T) {
   215  	testCasesInt64Map := []map[string]int64{
   216  		{"a": 3, "b": 2, "c": 1},
   217  	}
   218  
   219  	for idx, in := range testCasesInt64Map {
   220  		if in == nil {
   221  			continue
   222  		}
   223  		out := Int64Map(in)
   224  		assertValues(t, in, out, true, idx)
   225  
   226  		out2 := Int64ValueMap(out)
   227  		assertValues(t, in, out2, false, idx)
   228  	}
   229  }
   230  
   231  func TestFloat32Slice(t *testing.T) {
   232  	testCasesFloat32Slice := [][]float32{
   233  		{1, 2, 3, 4},
   234  	}
   235  
   236  	for idx, in := range testCasesFloat32Slice {
   237  		if in == nil {
   238  			continue
   239  		}
   240  
   241  		out := Float32Slice(in)
   242  		assertValues(t, in, out, true, idx)
   243  
   244  		out2 := Float32ValueSlice(out)
   245  		assertValues(t, in, out2, false, idx)
   246  	}
   247  }
   248  
   249  func TestFloat64Slice(t *testing.T) {
   250  	testCasesFloat64Slice := [][]float64{
   251  		{1, 2, 3, 4},
   252  	}
   253  
   254  	for idx, in := range testCasesFloat64Slice {
   255  		if in == nil {
   256  			continue
   257  		}
   258  		out := Float64Slice(in)
   259  		assertValues(t, in, out, true, idx)
   260  
   261  		out2 := Float64ValueSlice(out)
   262  		assertValues(t, in, out2, false, idx)
   263  	}
   264  }
   265  
   266  func TestUintSlice(t *testing.T) {
   267  	testCasesUintSlice := [][]uint{
   268  		{1, 2, 3, 4},
   269  	}
   270  
   271  	for idx, in := range testCasesUintSlice {
   272  		if in == nil {
   273  			continue
   274  		}
   275  		out := UintSlice(in)
   276  		assertValues(t, in, out, true, idx)
   277  
   278  		out2 := UintValueSlice(out)
   279  		assertValues(t, in, out2, false, idx)
   280  	}
   281  }
   282  
   283  func TestUintValueSlice(t *testing.T) {
   284  	testCasesUintValueSlice := [][]*uint{}
   285  
   286  	for idx, in := range testCasesUintValueSlice {
   287  		if in == nil {
   288  			continue
   289  		}
   290  		out := UintValueSlice(in)
   291  		assertValues(t, in, out, true, idx)
   292  
   293  		out2 := UintSlice(out)
   294  		assertValues(t, in, out2, false, idx)
   295  	}
   296  }
   297  
   298  func TestUintMap(t *testing.T) {
   299  	testCasesUintMap := []map[string]uint{
   300  		{"a": 3, "b": 2, "c": 1},
   301  	}
   302  
   303  	for idx, in := range testCasesUintMap {
   304  		if in == nil {
   305  			continue
   306  		}
   307  		out := UintMap(in)
   308  		assertValues(t, in, out, true, idx)
   309  
   310  		out2 := UintValueMap(out)
   311  		assertValues(t, in, out2, false, idx)
   312  	}
   313  }
   314  
   315  func TestUint16Slice(t *testing.T) {
   316  	testCasesUint16Slice := [][]uint16{
   317  		{1, 2, 3, 4},
   318  	}
   319  
   320  	for idx, in := range testCasesUint16Slice {
   321  		if in == nil {
   322  			continue
   323  		}
   324  
   325  		out := Uint16Slice(in)
   326  		assertValues(t, in, out, true, idx)
   327  
   328  		out2 := Uint16ValueSlice(out)
   329  		assertValues(t, in, out2, false, idx)
   330  	}
   331  }
   332  
   333  func TestUint16ValueSlice(t *testing.T) {
   334  	testCasesUint16ValueSlice := [][]*uint16{}
   335  
   336  	for idx, in := range testCasesUint16ValueSlice {
   337  		if in == nil {
   338  			continue
   339  		}
   340  
   341  		out := Uint16ValueSlice(in)
   342  		assertValues(t, in, out, true, idx)
   343  
   344  		out2 := Uint16Slice(out)
   345  		assertValues(t, in, out2, false, idx)
   346  	}
   347  }
   348  
   349  func TestUint16Map(t *testing.T) {
   350  	testCasesUint16Map := []map[string]uint16{
   351  		{"a": 3, "b": 2, "c": 1},
   352  	}
   353  
   354  	for idx, in := range testCasesUint16Map {
   355  		if in == nil {
   356  			continue
   357  		}
   358  
   359  		out := Uint16Map(in)
   360  		assertValues(t, in, out, true, idx)
   361  
   362  		out2 := Uint16ValueMap(out)
   363  		assertValues(t, in, out2, false, idx)
   364  	}
   365  }
   366  
   367  func TestUint64Slice(t *testing.T) {
   368  	testCasesUint64Slice := [][]uint64{
   369  		{1, 2, 3, 4},
   370  	}
   371  
   372  	for idx, in := range testCasesUint64Slice {
   373  		if in == nil {
   374  			continue
   375  		}
   376  		out := Uint64Slice(in)
   377  		assertValues(t, in, out, true, idx)
   378  
   379  		out2 := Uint64ValueSlice(out)
   380  		assertValues(t, in, out2, false, idx)
   381  	}
   382  }
   383  
   384  func TestUint64ValueSlice(t *testing.T) {
   385  	testCasesUint64ValueSlice := [][]*uint64{}
   386  
   387  	for idx, in := range testCasesUint64ValueSlice {
   388  		if in == nil {
   389  			continue
   390  		}
   391  		out := Uint64ValueSlice(in)
   392  		assertValues(t, in, out, true, idx)
   393  
   394  		out2 := Uint64Slice(out)
   395  		assertValues(t, in, out2, false, idx)
   396  	}
   397  }
   398  
   399  func TestUint64Map(t *testing.T) {
   400  	testCasesUint64Map := []map[string]uint64{
   401  		{"a": 3, "b": 2, "c": 1},
   402  	}
   403  
   404  	for idx, in := range testCasesUint64Map {
   405  		if in == nil {
   406  			continue
   407  		}
   408  		out := Uint64Map(in)
   409  		assertValues(t, in, out, true, idx)
   410  
   411  		out2 := Uint64ValueMap(out)
   412  		assertValues(t, in, out2, false, idx)
   413  	}
   414  }
   415  
   416  func TestFloat32ValueSlice(t *testing.T) {
   417  	testCasesFloat32ValueSlice := [][]*float32{}
   418  
   419  	for idx, in := range testCasesFloat32ValueSlice {
   420  		if in == nil {
   421  			continue
   422  		}
   423  
   424  		out := Float32ValueSlice(in)
   425  		assertValues(t, in, out, true, idx)
   426  
   427  		out2 := Float32Slice(out)
   428  		assertValues(t, in, out2, false, idx)
   429  	}
   430  }
   431  
   432  func TestFloat32Map(t *testing.T) {
   433  	testCasesFloat32Map := []map[string]float32{
   434  		{"a": 3, "b": 2, "c": 1},
   435  	}
   436  
   437  	for idx, in := range testCasesFloat32Map {
   438  		if in == nil {
   439  			continue
   440  		}
   441  
   442  		out := Float32Map(in)
   443  		assertValues(t, in, out, true, idx)
   444  
   445  		out2 := Float32ValueMap(out)
   446  		assertValues(t, in, out2, false, idx)
   447  	}
   448  }
   449  
   450  func TestFloat64ValueSlice(t *testing.T) {
   451  	testCasesFloat64ValueSlice := [][]*float64{}
   452  
   453  	for idx, in := range testCasesFloat64ValueSlice {
   454  		if in == nil {
   455  			continue
   456  		}
   457  		out := Float64ValueSlice(in)
   458  		assertValues(t, in, out, true, idx)
   459  
   460  		out2 := Float64Slice(out)
   461  		assertValues(t, in, out2, false, idx)
   462  	}
   463  }
   464  
   465  func TestFloat64Map(t *testing.T) {
   466  	testCasesFloat64Map := []map[string]float64{
   467  		{"a": 3, "b": 2, "c": 1},
   468  	}
   469  
   470  	for idx, in := range testCasesFloat64Map {
   471  		if in == nil {
   472  			continue
   473  		}
   474  		out := Float64Map(in)
   475  		assertValues(t, in, out, true, idx)
   476  
   477  		out2 := Float64ValueMap(out)
   478  		assertValues(t, in, out2, false, idx)
   479  	}
   480  }
   481  
   482  func TestTimeSlice(t *testing.T) {
   483  	testCasesTimeSlice := [][]time.Time{
   484  		{time.Now(), time.Now().AddDate(100, 0, 0)},
   485  	}
   486  
   487  	for idx, in := range testCasesTimeSlice {
   488  		if in == nil {
   489  			continue
   490  		}
   491  		out := TimeSlice(in)
   492  		assertValues(t, in, out, true, idx)
   493  
   494  		out2 := TimeValueSlice(out)
   495  		assertValues(t, in, out2, false, idx)
   496  	}
   497  }
   498  
   499  func TestTimeValueSlice(t *testing.T) {
   500  	testCasesTimeValueSlice := [][]*time.Time{
   501  		{Time(time.Now()), Time(time.Now().AddDate(100, 0, 0))},
   502  	}
   503  
   504  	for idx, in := range testCasesTimeValueSlice {
   505  		if in == nil {
   506  			continue
   507  		}
   508  		out := TimeValueSlice(in)
   509  		assertValues(t, in, out, false, idx)
   510  
   511  		out2 := TimeSlice(out)
   512  		assertValues(t, in, out2, true, idx)
   513  	}
   514  }
   515  
   516  func TestTimeMap(t *testing.T) {
   517  	testCasesTimeMap := []map[string]time.Time{
   518  		{"a": time.Now().AddDate(-100, 0, 0), "b": time.Now()},
   519  	}
   520  
   521  	for idx, in := range testCasesTimeMap {
   522  		if in == nil {
   523  			continue
   524  		}
   525  		out := TimeMap(in)
   526  		assertValues(t, in, out, true, idx)
   527  
   528  		out2 := TimeValueMap(out)
   529  		assertValues(t, in, out2, false, idx)
   530  	}
   531  }
   532  
   533  func TestInt32Slice(t *testing.T) {
   534  	testCasesInt32Slice := [][]int32{
   535  		{1, 2, 3, 4},
   536  	}
   537  
   538  	for idx, in := range testCasesInt32Slice {
   539  		if in == nil {
   540  			continue
   541  		}
   542  		out := Int32Slice(in)
   543  		assertValues(t, in, out, true, idx)
   544  
   545  		out2 := Int32ValueSlice(out)
   546  		assertValues(t, in, out2, false, idx)
   547  	}
   548  }
   549  
   550  func TestInt32ValueSlice(t *testing.T) {
   551  	testCasesInt32ValueSlice := [][]*int32{
   552  		{Int32(1), Int32(2), Int32(3), Int32(4)},
   553  	}
   554  
   555  	for idx, in := range testCasesInt32ValueSlice {
   556  		if in == nil {
   557  			continue
   558  		}
   559  		out := Int32ValueSlice(in)
   560  		assertValues(t, in, out, false, idx)
   561  
   562  		out2 := Int32Slice(out)
   563  		assertValues(t, in, out2, true, idx)
   564  	}
   565  }
   566  
   567  func TestInt32Map(t *testing.T) {
   568  	testCasesInt32Map := []map[string]int32{
   569  		{"a": 3, "b": 2, "c": 1},
   570  	}
   571  
   572  	for idx, in := range testCasesInt32Map {
   573  		if in == nil {
   574  			continue
   575  		}
   576  		out := Int32Map(in)
   577  		assertValues(t, in, out, true, idx)
   578  
   579  		out2 := Int32ValueMap(out)
   580  		assertValues(t, in, out2, false, idx)
   581  	}
   582  }
   583  
   584  func TestUint32Slice(t *testing.T) {
   585  	testCasesUint32Slice := [][]uint32{
   586  		{1, 2, 3, 4},
   587  	}
   588  
   589  	for idx, in := range testCasesUint32Slice {
   590  		if in == nil {
   591  			continue
   592  		}
   593  		out := Uint32Slice(in)
   594  		assertValues(t, in, out, true, idx)
   595  
   596  		out2 := Uint32ValueSlice(out)
   597  		assertValues(t, in, out2, false, idx)
   598  	}
   599  }
   600  
   601  func TestUint32ValueSlice(t *testing.T) {
   602  	testCasesUint32ValueSlice := [][]*uint32{
   603  		{Uint32(1), Uint32(2), Uint32(3), Uint32(4)},
   604  	}
   605  
   606  	for idx, in := range testCasesUint32ValueSlice {
   607  		if in == nil {
   608  			continue
   609  		}
   610  		out := Uint32ValueSlice(in)
   611  		assertValues(t, in, out, false, idx)
   612  
   613  		out2 := Uint32Slice(out)
   614  		assertValues(t, in, out2, true, idx)
   615  	}
   616  }
   617  
   618  func TestUint32Map(t *testing.T) {
   619  	testCasesUint32Map := []map[string]uint32{
   620  		{"a": 3, "b": 2, "c": 1},
   621  	}
   622  
   623  	for idx, in := range testCasesUint32Map {
   624  		if in == nil {
   625  			continue
   626  		}
   627  		out := Uint32Map(in)
   628  		assertValues(t, in, out, true, idx)
   629  
   630  		out2 := Uint32ValueMap(out)
   631  		assertValues(t, in, out2, false, idx)
   632  	}
   633  }
   634  
   635  func TestStringValue(t *testing.T) {
   636  	testCasesString := []string{"a", "b", "c", "d", "e", ""}
   637  
   638  	for idx, in := range testCasesString {
   639  		out := String(in)
   640  		assertValues(t, in, out, true, idx)
   641  
   642  		out2 := StringValue(out)
   643  		assertValues(t, in, out2, false, idx)
   644  	}
   645  	assert.Zerof(t, StringValue(nil), "expected conversion from nil to return zero value")
   646  }
   647  
   648  func TestBoolValue(t *testing.T) {
   649  	testCasesBool := []bool{true, false}
   650  
   651  	for idx, in := range testCasesBool {
   652  		out := Bool(in)
   653  		assertValues(t, in, out, true, idx)
   654  
   655  		out2 := BoolValue(out)
   656  		assertValues(t, in, out2, false, idx)
   657  	}
   658  	assert.Zerof(t, BoolValue(nil), "expected conversion from nil to return zero value")
   659  }
   660  
   661  func TestIntValue(t *testing.T) {
   662  	testCasesInt := []int{1, 2, 3, 0}
   663  
   664  	for idx, in := range testCasesInt {
   665  		out := Int(in)
   666  		assertValues(t, in, out, true, idx)
   667  
   668  		out2 := IntValue(out)
   669  		assertValues(t, in, out2, false, idx)
   670  	}
   671  	assert.Zerof(t, IntValue(nil), "expected conversion from nil to return zero value")
   672  }
   673  
   674  func TestInt32Value(t *testing.T) {
   675  	testCasesInt32 := []int32{1, 2, 3, 0}
   676  
   677  	for idx, in := range testCasesInt32 {
   678  		out := Int32(in)
   679  		assertValues(t, in, out, true, idx)
   680  
   681  		out2 := Int32Value(out)
   682  		assertValues(t, in, out2, false, idx)
   683  	}
   684  	assert.Zerof(t, Int32Value(nil), "expected conversion from nil to return zero value")
   685  }
   686  
   687  func TestInt64Value(t *testing.T) {
   688  	testCasesInt64 := []int64{1, 2, 3, 0}
   689  
   690  	for idx, in := range testCasesInt64 {
   691  		out := Int64(in)
   692  		assertValues(t, in, out, true, idx)
   693  
   694  		out2 := Int64Value(out)
   695  		assertValues(t, in, out2, false, idx)
   696  	}
   697  	assert.Zerof(t, Int64Value(nil), "expected conversion from nil to return zero value")
   698  }
   699  
   700  func TestUintValue(t *testing.T) {
   701  	testCasesUint := []uint{1, 2, 3, 0}
   702  
   703  	for idx, in := range testCasesUint {
   704  		out := Uint(in)
   705  		assertValues(t, in, out, true, idx)
   706  
   707  		out2 := UintValue(out)
   708  		assertValues(t, in, out2, false, idx)
   709  	}
   710  	assert.Zerof(t, UintValue(nil), "expected conversion from nil to return zero value")
   711  }
   712  
   713  func TestUint32Value(t *testing.T) {
   714  	testCasesUint32 := []uint32{1, 2, 3, 0}
   715  
   716  	for idx, in := range testCasesUint32 {
   717  		out := Uint32(in)
   718  		assertValues(t, in, out, true, idx)
   719  
   720  		out2 := Uint32Value(out)
   721  		assertValues(t, in, out2, false, idx)
   722  	}
   723  	assert.Zerof(t, Uint32Value(nil), "expected conversion from nil to return zero value")
   724  }
   725  
   726  func TestUint64Value(t *testing.T) {
   727  	testCasesUint64 := []uint64{1, 2, 3, 0}
   728  
   729  	for idx, in := range testCasesUint64 {
   730  		out := Uint64(in)
   731  		assertValues(t, in, out, true, idx)
   732  
   733  		out2 := Uint64Value(out)
   734  		assertValues(t, in, out2, false, idx)
   735  	}
   736  	assert.Zerof(t, Uint64Value(nil), "expected conversion from nil to return zero value")
   737  }
   738  
   739  func TestFloat32Value(t *testing.T) {
   740  	testCasesFloat32 := []float32{1, 2, 3, 0}
   741  
   742  	for idx, in := range testCasesFloat32 {
   743  		out := Float32(in)
   744  		assertValues(t, in, out, true, idx)
   745  
   746  		out2 := Float32Value(out)
   747  		assertValues(t, in, out2, false, idx)
   748  	}
   749  
   750  	assert.Zerof(t, Float32Value(nil), "expected conversion from nil to return zero value")
   751  }
   752  
   753  func TestFloat64Value(t *testing.T) {
   754  	testCasesFloat64 := []float64{1, 2, 3, 0}
   755  
   756  	for idx, in := range testCasesFloat64 {
   757  		out := Float64(in)
   758  		assertValues(t, in, out, true, idx)
   759  
   760  		out2 := Float64Value(out)
   761  		assertValues(t, in, out2, false, idx)
   762  	}
   763  	assert.Zerof(t, Float64Value(nil), "expected conversion from nil to return zero value")
   764  }
   765  
   766  func TestTimeValue(t *testing.T) {
   767  	testCasesTime := []time.Time{
   768  		time.Now().AddDate(-100, 0, 0), time.Now(),
   769  	}
   770  
   771  	for idx, in := range testCasesTime {
   772  		out := Time(in)
   773  		assertValues(t, in, out, true, idx)
   774  
   775  		out2 := TimeValue(out)
   776  		assertValues(t, in, out2, false, idx)
   777  	}
   778  	assert.Zerof(t, TimeValue(nil), "expected conversion from nil to return zero value")
   779  }
   780  
   781  func assertSingleValue(t *testing.T, inElem, elem reflect.Value, expectPointer bool, idx int) {
   782  	require.Equalf(t,
   783  		expectPointer, (elem.Kind() == reflect.Ptr),
   784  		"unexpected expectPointer=%t value type", expectPointer,
   785  	)
   786  
   787  	if inElem.Kind() == reflect.Ptr && !inElem.IsNil() {
   788  		inElem = reflect.Indirect(inElem)
   789  	}
   790  
   791  	if elem.Kind() == reflect.Ptr && !elem.IsNil() {
   792  		elem = reflect.Indirect(elem)
   793  	}
   794  
   795  	require.Truef(t,
   796  		(elem.Kind() == reflect.Ptr && elem.IsNil()) ||
   797  			IsZero(elem.Interface()) == (inElem.Kind() == reflect.Ptr && inElem.IsNil()) ||
   798  			IsZero(inElem.Interface()),
   799  		"unexpected nil pointer at idx %d", idx,
   800  	)
   801  
   802  	if !((elem.Kind() == reflect.Ptr && elem.IsNil()) || IsZero(elem.Interface())) {
   803  		require.IsTypef(t, inElem.Interface(), elem.Interface(), "Expected in/out to match types")
   804  		assert.EqualValuesf(t, inElem.Interface(), elem.Interface(), "Unexpected value at idx %d: %v", idx, elem.Interface())
   805  	}
   806  }
   807  
   808  // assertValues checks equivalent representation pointer vs values for single var, slices and maps
   809  func assertValues(t *testing.T, in, out interface{}, expectPointer bool, idx int) {
   810  	vin := reflect.ValueOf(in)
   811  	vout := reflect.ValueOf(out)
   812  
   813  	switch vin.Kind() { //nolint:exhaustive
   814  	case reflect.Slice, reflect.Map:
   815  		require.Equalf(t, vin.Kind(), vout.Kind(), "Unexpected output type at idx %d", idx)
   816  		require.Equalf(t, vin.Len(), vout.Len(), "Unexpected len at idx %d", idx)
   817  
   818  		var elem, inElem reflect.Value
   819  		for i := 0; i < vin.Len(); i++ {
   820  			switch vin.Kind() { //nolint:exhaustive
   821  			case reflect.Slice:
   822  				elem = vout.Index(i)
   823  				inElem = vin.Index(i)
   824  			case reflect.Map:
   825  				keys := vin.MapKeys()
   826  				elem = vout.MapIndex(keys[i])
   827  				inElem = vout.MapIndex(keys[i])
   828  			default:
   829  			}
   830  
   831  			assertSingleValue(t, inElem, elem, expectPointer, idx)
   832  		}
   833  
   834  	default:
   835  		inElem := vin
   836  		elem := vout
   837  
   838  		assertSingleValue(t, inElem, elem, expectPointer, idx)
   839  	}
   840  }
   841  

View as plain text