github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/base/breflect/isnil_test.go (about)

     1  package breflect
     2  
     3  import "testing"
     4  
     5  type tInterface interface {
     6  	test()
     7  }
     8  
     9  type tStruct struct {
    10  	t any
    11  }
    12  
    13  func (t tStruct) test() {
    14  }
    15  
    16  func TestIsNil(t *testing.T) {
    17  	var (
    18  		baseInt    int     = 1
    19  		baseString string  = "1"
    20  		baseFloat  float64 = 1.1
    21  	)
    22  	type args struct {
    23  		value interface{}
    24  	}
    25  	tests := []struct {
    26  		name string
    27  		args args
    28  		want bool
    29  	}{
    30  		{
    31  			name: "base",
    32  			args: args{
    33  				value: baseInt,
    34  			},
    35  			want: false,
    36  		},
    37  		{
    38  			name: "base",
    39  			args: args{
    40  				value: baseString,
    41  			},
    42  			want: false,
    43  		},
    44  		{
    45  			name: "base",
    46  			args: args{
    47  				value: baseFloat,
    48  			},
    49  			want: false,
    50  		},
    51  		{
    52  			name: "point",
    53  			args: args{
    54  				value: &baseInt,
    55  			},
    56  			want: false,
    57  		},
    58  		{
    59  			name: "point",
    60  			args: args{
    61  				value: &baseString,
    62  			},
    63  			want: false,
    64  		},
    65  		{
    66  			name: "point",
    67  			args: args{
    68  				value: &baseFloat,
    69  			},
    70  			want: false,
    71  		},
    72  		{
    73  			name: "point",
    74  			args: args{
    75  				value: (*int)(nil),
    76  			},
    77  			want: true,
    78  		},
    79  		{
    80  			name: "point",
    81  			args: args{
    82  				value: (*string)(nil),
    83  			},
    84  			want: true,
    85  		},
    86  		{
    87  			name: "point",
    88  			args: args{
    89  				value: (*float64)(nil),
    90  			},
    91  			want: true,
    92  		},
    93  		{
    94  			name: "point",
    95  			args: args{
    96  				value: (*float64)(nil),
    97  			},
    98  			want: true,
    99  		},
   100  		{
   101  			name: "struct",
   102  			args: args{
   103  				value: tStruct{t: "1"},
   104  			},
   105  			want: false,
   106  		},
   107  		{
   108  			name: "point struct",
   109  			args: args{
   110  				value: &tStruct{t: "1"},
   111  			},
   112  			want: false,
   113  		},
   114  		{
   115  			name: "point struct",
   116  			args: args{
   117  				value: (*tStruct)(nil),
   118  			},
   119  			want: true,
   120  		},
   121  		{
   122  			name: "interface",
   123  			args: args{
   124  				value: (tInterface)(tStruct{t: "1"}),
   125  			},
   126  			want: false,
   127  		},
   128  		{
   129  			name: "interface",
   130  			args: args{
   131  				value: (tInterface)(nil),
   132  			},
   133  			want: true,
   134  		},
   135  		{
   136  			name: "point interface",
   137  			args: args{
   138  				value: (*tInterface)(nil),
   139  			},
   140  			want: true,
   141  		},
   142  	}
   143  	for _, tt := range tests {
   144  		t.Run(tt.name, func(t *testing.T) {
   145  			if got := IsNil(tt.args.value); got != tt.want {
   146  				t.Errorf("IsNil() = %v, want %v", got, tt.want)
   147  			}
   148  		})
   149  	}
   150  }