github.com/kubernetes/utils@v0.0.0-20190308190857-21c4ce38f2a7/pointer/pointer_test.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package pointer
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  	"testing"
    23  )
    24  
    25  func TestAllPtrFieldsNil(t *testing.T) {
    26  	testCases := []struct {
    27  		obj      interface{}
    28  		expected bool
    29  	}{
    30  		{struct{}{}, true},
    31  		{struct{ Foo int }{12345}, true},
    32  		{&struct{ Foo int }{12345}, true},
    33  		{struct{ Foo *int }{nil}, true},
    34  		{&struct{ Foo *int }{nil}, true},
    35  		{struct {
    36  			Foo int
    37  			Bar *int
    38  		}{12345, nil}, true},
    39  		{&struct {
    40  			Foo int
    41  			Bar *int
    42  		}{12345, nil}, true},
    43  		{struct {
    44  			Foo *int
    45  			Bar *int
    46  		}{nil, nil}, true},
    47  		{&struct {
    48  			Foo *int
    49  			Bar *int
    50  		}{nil, nil}, true},
    51  		{struct{ Foo *int }{new(int)}, false},
    52  		{&struct{ Foo *int }{new(int)}, false},
    53  		{struct {
    54  			Foo *int
    55  			Bar *int
    56  		}{nil, new(int)}, false},
    57  		{&struct {
    58  			Foo *int
    59  			Bar *int
    60  		}{nil, new(int)}, false},
    61  		{(*struct{})(nil), true},
    62  	}
    63  	for i, tc := range testCases {
    64  		name := fmt.Sprintf("case[%d]", i)
    65  		t.Run(name, func(t *testing.T) {
    66  			actualErr := AllPtrFieldsNil(tc.obj)
    67  			if !reflect.DeepEqual(tc.expected, actualErr) {
    68  				t.Errorf("%s: expected %t, got %t", name, tc.expected, !tc.expected)
    69  			}
    70  		})
    71  	}
    72  }