github.com/gogf/gf@v1.16.9/internal/empty/empty_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). 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/gogf/gf.
     6  
     7  package empty_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/gogf/gf/frame/g"
    13  	"github.com/gogf/gf/internal/empty"
    14  	"github.com/gogf/gf/test/gtest"
    15  	"github.com/gogf/gf/util/gconv"
    16  )
    17  
    18  type TestInt int
    19  
    20  type TestString string
    21  
    22  type TestPerson interface {
    23  	Say() string
    24  }
    25  type TestWoman struct {
    26  }
    27  
    28  func (woman TestWoman) Say() string {
    29  	return "nice"
    30  }
    31  
    32  func TestIsEmpty(t *testing.T) {
    33  	gtest.C(t, func(t *gtest.T) {
    34  		tmpT1 := "0"
    35  		tmpT2 := func() {}
    36  		tmpT2 = nil
    37  		tmpT3 := make(chan int, 0)
    38  		var (
    39  			tmpT4 TestPerson  = nil
    40  			tmpT5 *TestPerson = nil
    41  			tmpT6 TestPerson  = TestWoman{}
    42  			tmpT7 TestInt     = 0
    43  			tmpT8 TestString  = ""
    44  		)
    45  		tmpF1 := "1"
    46  		tmpF2 := func(a string) string { return "1" }
    47  		tmpF3 := make(chan int, 1)
    48  		tmpF3 <- 1
    49  		var (
    50  			tmpF4 TestPerson = &TestWoman{}
    51  			tmpF5 TestInt    = 1
    52  			tmpF6 TestString = "1"
    53  		)
    54  
    55  		// true
    56  		t.Assert(empty.IsEmpty(nil), true)
    57  		t.Assert(empty.IsEmpty(0), true)
    58  		t.Assert(empty.IsEmpty(gconv.Int(tmpT1)), true)
    59  		t.Assert(empty.IsEmpty(gconv.Int8(tmpT1)), true)
    60  		t.Assert(empty.IsEmpty(gconv.Int16(tmpT1)), true)
    61  		t.Assert(empty.IsEmpty(gconv.Int32(tmpT1)), true)
    62  		t.Assert(empty.IsEmpty(gconv.Int64(tmpT1)), true)
    63  		t.Assert(empty.IsEmpty(gconv.Uint64(tmpT1)), true)
    64  		t.Assert(empty.IsEmpty(gconv.Uint(tmpT1)), true)
    65  		t.Assert(empty.IsEmpty(gconv.Uint16(tmpT1)), true)
    66  		t.Assert(empty.IsEmpty(gconv.Uint32(tmpT1)), true)
    67  		t.Assert(empty.IsEmpty(gconv.Uint64(tmpT1)), true)
    68  		t.Assert(empty.IsEmpty(gconv.Float32(tmpT1)), true)
    69  		t.Assert(empty.IsEmpty(gconv.Float64(tmpT1)), true)
    70  		t.Assert(empty.IsEmpty(false), true)
    71  		t.Assert(empty.IsEmpty([]byte("")), true)
    72  		t.Assert(empty.IsEmpty(""), true)
    73  		t.Assert(empty.IsEmpty(g.Map{}), true)
    74  		t.Assert(empty.IsEmpty(g.Slice{}), true)
    75  		t.Assert(empty.IsEmpty(g.Array{}), true)
    76  		t.Assert(empty.IsEmpty(tmpT2), true)
    77  		t.Assert(empty.IsEmpty(tmpT3), true)
    78  		t.Assert(empty.IsEmpty(tmpT3), true)
    79  		t.Assert(empty.IsEmpty(tmpT4), true)
    80  		t.Assert(empty.IsEmpty(tmpT5), true)
    81  		t.Assert(empty.IsEmpty(tmpT6), true)
    82  		t.Assert(empty.IsEmpty(tmpT7), true)
    83  		t.Assert(empty.IsEmpty(tmpT8), true)
    84  
    85  		// false
    86  		t.Assert(empty.IsEmpty(gconv.Int(tmpF1)), false)
    87  		t.Assert(empty.IsEmpty(gconv.Int8(tmpF1)), false)
    88  		t.Assert(empty.IsEmpty(gconv.Int16(tmpF1)), false)
    89  		t.Assert(empty.IsEmpty(gconv.Int32(tmpF1)), false)
    90  		t.Assert(empty.IsEmpty(gconv.Int64(tmpF1)), false)
    91  		t.Assert(empty.IsEmpty(gconv.Uint(tmpF1)), false)
    92  		t.Assert(empty.IsEmpty(gconv.Uint8(tmpF1)), false)
    93  		t.Assert(empty.IsEmpty(gconv.Uint16(tmpF1)), false)
    94  		t.Assert(empty.IsEmpty(gconv.Uint32(tmpF1)), false)
    95  		t.Assert(empty.IsEmpty(gconv.Uint64(tmpF1)), false)
    96  		t.Assert(empty.IsEmpty(gconv.Float32(tmpF1)), false)
    97  		t.Assert(empty.IsEmpty(gconv.Float64(tmpF1)), false)
    98  		t.Assert(empty.IsEmpty(true), false)
    99  		t.Assert(empty.IsEmpty(tmpT1), false)
   100  		t.Assert(empty.IsEmpty([]byte("1")), false)
   101  		t.Assert(empty.IsEmpty(g.Map{"a": 1}), false)
   102  		t.Assert(empty.IsEmpty(g.Slice{"1"}), false)
   103  		t.Assert(empty.IsEmpty(g.Array{"1"}), false)
   104  		t.Assert(empty.IsEmpty(tmpF2), false)
   105  		t.Assert(empty.IsEmpty(tmpF3), false)
   106  		t.Assert(empty.IsEmpty(tmpF4), false)
   107  		t.Assert(empty.IsEmpty(tmpF5), false)
   108  		t.Assert(empty.IsEmpty(tmpF6), false)
   109  	})
   110  }
   111  
   112  func TestIsNil(t *testing.T) {
   113  	gtest.C(t, func(t *gtest.T) {
   114  		t.Assert(empty.IsNil(nil), true)
   115  	})
   116  	gtest.C(t, func(t *gtest.T) {
   117  		var i int
   118  		t.Assert(empty.IsNil(i), false)
   119  	})
   120  	gtest.C(t, func(t *gtest.T) {
   121  		var i *int
   122  		t.Assert(empty.IsNil(i), true)
   123  	})
   124  	gtest.C(t, func(t *gtest.T) {
   125  		var i *int
   126  		t.Assert(empty.IsNil(&i), false)
   127  		t.Assert(empty.IsNil(&i, true), true)
   128  	})
   129  }