github.com/zhongdalu/gf@v1.0.0/g/container/gvar/gvar_test.go (about)

     1  // Copyright 2019 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  package gvar_test
     8  
     9  import (
    10  	"bytes"
    11  	"encoding/binary"
    12  	"testing"
    13  
    14  	"github.com/zhongdalu/gf/g/container/gvar"
    15  	"github.com/zhongdalu/gf/g/test/gtest"
    16  )
    17  
    18  func TestSet(t *testing.T) {
    19  	gtest.Case(t, func() {
    20  		objOne := gvar.New("old", true)
    21  		objOneOld, _ := objOne.Set("new").(string)
    22  		gtest.Assert(objOneOld, "old")
    23  
    24  		objTwo := gvar.New("old", false)
    25  		objTwoOld, _ := objTwo.Set("new").(string)
    26  		gtest.Assert(objTwoOld, "old")
    27  	})
    28  }
    29  
    30  func TestVal(t *testing.T) {
    31  	gtest.Case(t, func() {
    32  		objOne := gvar.New(1, true)
    33  		objOneOld, _ := objOne.Val().(int)
    34  		gtest.Assert(objOneOld, 1)
    35  
    36  		objTwo := gvar.New(1, false)
    37  		objTwoOld, _ := objTwo.Val().(int)
    38  		gtest.Assert(objTwoOld, 1)
    39  	})
    40  }
    41  func TestInterface(t *testing.T) {
    42  	gtest.Case(t, func() {
    43  		objOne := gvar.New(1, true)
    44  		objOneOld, _ := objOne.Interface().(int)
    45  		gtest.Assert(objOneOld, 1)
    46  
    47  		objTwo := gvar.New(1, false)
    48  		objTwoOld, _ := objTwo.Interface().(int)
    49  		gtest.Assert(objTwoOld, 1)
    50  	})
    51  }
    52  func TestIsNil(t *testing.T) {
    53  	gtest.Case(t, func() {
    54  		objOne := gvar.New(nil, true)
    55  		gtest.Assert(objOne.IsNil(), true)
    56  
    57  		objTwo := gvar.New("noNil", false)
    58  		gtest.Assert(objTwo.IsNil(), false)
    59  
    60  	})
    61  }
    62  
    63  func TestBytes(t *testing.T) {
    64  	gtest.Case(t, func() {
    65  		x := int32(1)
    66  		bytesBuffer := bytes.NewBuffer([]byte{})
    67  		binary.Write(bytesBuffer, binary.BigEndian, x)
    68  
    69  		objOne := gvar.New(bytesBuffer.Bytes(), true)
    70  
    71  		bBuf := bytes.NewBuffer(objOne.Bytes())
    72  		var y int32
    73  		binary.Read(bBuf, binary.BigEndian, &y)
    74  
    75  		gtest.Assert(x, y)
    76  
    77  	})
    78  }
    79  
    80  func TestString(t *testing.T) {
    81  	gtest.Case(t, func() {
    82  		var str string = "hello"
    83  		objOne := gvar.New(str, true)
    84  		gtest.Assert(objOne.String(), str)
    85  
    86  	})
    87  }
    88  func TestBool(t *testing.T) {
    89  	gtest.Case(t, func() {
    90  		var ok bool = true
    91  		objOne := gvar.New(ok, true)
    92  		gtest.Assert(objOne.Bool(), ok)
    93  
    94  		ok = false
    95  		objTwo := gvar.New(ok, true)
    96  		gtest.Assert(objTwo.Bool(), ok)
    97  
    98  	})
    99  }
   100  
   101  func TestInt(t *testing.T) {
   102  	gtest.Case(t, func() {
   103  		var num int = 1
   104  		objOne := gvar.New(num, true)
   105  		gtest.Assert(objOne.Int(), num)
   106  
   107  	})
   108  }
   109  
   110  func TestInt8(t *testing.T) {
   111  	gtest.Case(t, func() {
   112  		var num int8 = 1
   113  		objOne := gvar.New(num, true)
   114  		gtest.Assert(objOne.Int8(), num)
   115  
   116  	})
   117  }
   118  
   119  func TestInt16(t *testing.T) {
   120  	gtest.Case(t, func() {
   121  		var num int16 = 1
   122  		objOne := gvar.New(num, true)
   123  		gtest.Assert(objOne.Int16(), num)
   124  
   125  	})
   126  }
   127  
   128  func TestInt32(t *testing.T) {
   129  	gtest.Case(t, func() {
   130  		var num int32 = 1
   131  		objOne := gvar.New(num, true)
   132  		gtest.Assert(objOne.Int32(), num)
   133  
   134  	})
   135  }
   136  
   137  func TestInt64(t *testing.T) {
   138  	gtest.Case(t, func() {
   139  		var num int64 = 1
   140  		objOne := gvar.New(num, true)
   141  		gtest.Assert(objOne.Int64(), num)
   142  
   143  	})
   144  }
   145  
   146  func TestUint(t *testing.T) {
   147  	gtest.Case(t, func() {
   148  		var num uint = 1
   149  		objOne := gvar.New(num, true)
   150  		gtest.Assert(objOne.Uint(), num)
   151  
   152  	})
   153  }
   154  
   155  func TestUint8(t *testing.T) {
   156  	gtest.Case(t, func() {
   157  		var num uint8 = 1
   158  		objOne := gvar.New(num, true)
   159  		gtest.Assert(objOne.Uint8(), num)
   160  
   161  	})
   162  }
   163  
   164  func TestUint16(t *testing.T) {
   165  	gtest.Case(t, func() {
   166  		var num uint16 = 1
   167  		objOne := gvar.New(num, true)
   168  		gtest.Assert(objOne.Uint16(), num)
   169  
   170  	})
   171  }
   172  
   173  func TestUint32(t *testing.T) {
   174  	gtest.Case(t, func() {
   175  		var num uint32 = 1
   176  		objOne := gvar.New(num, true)
   177  		gtest.Assert(objOne.Uint32(), num)
   178  
   179  	})
   180  }
   181  
   182  func TestUint64(t *testing.T) {
   183  	gtest.Case(t, func() {
   184  		var num uint64 = 1
   185  		objOne := gvar.New(num, true)
   186  		gtest.Assert(objOne.Uint64(), num)
   187  
   188  	})
   189  }
   190  func TestFloat32(t *testing.T) {
   191  	gtest.Case(t, func() {
   192  		var num float32 = 1.1
   193  		objOne := gvar.New(num, true)
   194  		gtest.Assert(objOne.Float32(), num)
   195  
   196  	})
   197  }
   198  
   199  func TestFloat64(t *testing.T) {
   200  	gtest.Case(t, func() {
   201  		var num float64 = 1.1
   202  		objOne := gvar.New(num, true)
   203  		gtest.Assert(objOne.Float64(), num)
   204  
   205  	})
   206  }
   207  
   208  func TestInts(t *testing.T) {
   209  	gtest.Case(t, func() {
   210  		var arr = []int{1, 2, 3, 4, 5}
   211  		objOne := gvar.New(arr, true)
   212  		gtest.Assert(objOne.Ints()[0], arr[0])
   213  	})
   214  }
   215  func TestFloats(t *testing.T) {
   216  	gtest.Case(t, func() {
   217  		var arr = []float64{1, 2, 3, 4, 5}
   218  		objOne := gvar.New(arr, true)
   219  		gtest.Assert(objOne.Floats()[0], arr[0])
   220  	})
   221  }
   222  func TestStrings(t *testing.T) {
   223  	gtest.Case(t, func() {
   224  		var arr = []string{"hello", "world"}
   225  		objOne := gvar.New(arr, true)
   226  		gtest.Assert(objOne.Strings()[0], arr[0])
   227  	})
   228  }
   229  
   230  func TestTime(t *testing.T) {
   231  	gtest.Case(t, func() {
   232  		var timeUnix int64 = 1556242660
   233  		objOne := gvar.New(timeUnix, true)
   234  		gtest.Assert(objOne.Time().Unix(), timeUnix)
   235  	})
   236  }
   237  
   238  type StTest struct {
   239  	Test int
   240  }
   241  
   242  func TestStruct(t *testing.T) {
   243  	gtest.Case(t, func() {
   244  		Kv := make(map[string]int, 1)
   245  		Kv["Test"] = 100
   246  
   247  		testObj := &StTest{}
   248  
   249  		objOne := gvar.New(Kv, true)
   250  
   251  		objOne.Struct(testObj)
   252  
   253  		gtest.Assert(testObj.Test, Kv["Test"])
   254  	})
   255  }