github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/helper/value_test.go (about)

     1  // Copyright 2019 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package helper
    15  
    16  import (
    17  	"errors"
    18  	"testing"
    19  	"unsafe"
    20  
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  type fIsNil func()
    25  
    26  func fIsNil1() {}
    27  
    28  func TestIsNil(t *testing.T) {
    29  	// nil value
    30  	i := 123
    31  	require.True(t, IsNil(nil))
    32  	require.False(t, IsNil(i))
    33  
    34  	// chan
    35  	require.True(t, IsNil((chan int)(nil)))
    36  	require.False(t, IsNil(make(chan int)))
    37  
    38  	// func
    39  	require.True(t, IsNil((fIsNil)(nil)))
    40  	require.False(t, IsNil(fIsNil1))
    41  
    42  	// interface (error is an interface)
    43  	require.True(t, IsNil((error)(nil)))
    44  	require.False(t, IsNil(errors.New("")))
    45  
    46  	// map
    47  	require.True(t, IsNil((map[int]int)(nil)))
    48  	require.False(t, IsNil(make(map[int]int)))
    49  
    50  	// pointer
    51  	var piNil *int
    52  	piNotNil := &i
    53  	require.True(t, IsNil(piNil))
    54  	require.False(t, IsNil(piNotNil))
    55  
    56  	// unsafe pointer
    57  	var upiNil unsafe.Pointer
    58  	upiNotNil := unsafe.Pointer(piNotNil)
    59  	require.True(t, IsNil(upiNil))
    60  	require.False(t, IsNil(upiNotNil))
    61  
    62  	// slice
    63  	require.True(t, IsNil(([]int)(nil)))
    64  	require.False(t, IsNil(make([]int, 0)))
    65  }