github.com/containerd/nerdctl@v1.7.7/pkg/reflectutil/reflectutil_test.go (about)

     1  /*
     2     Copyright The containerd 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 reflectutil
    18  
    19  import (
    20  	"testing"
    21  
    22  	"gotest.tools/v3/assert"
    23  )
    24  
    25  func TestUnknownNonEmptyFields(t *testing.T) {
    26  	type foo struct {
    27  		FooBool    bool
    28  		FooBoolPtr *bool
    29  		FooMap     map[string]struct{}
    30  		FooStr     string
    31  		FooStr2    string
    32  		FooStr3    string
    33  	}
    34  
    35  	foo1 := foo{
    36  		FooBool:    true,
    37  		FooBoolPtr: nil,
    38  		FooMap:     nil,
    39  		FooStr:     "foo",
    40  		FooStr2:    "",
    41  		FooStr3:    "oof",
    42  	}
    43  	assert.DeepEqual(t,
    44  		[]string{"FooStr", "FooStr3"},
    45  		UnknownNonEmptyFields(&foo1, "FooBool"))
    46  	assert.DeepEqual(t,
    47  		[]string{"FooStr", "FooStr3"},
    48  		UnknownNonEmptyFields(foo1, "FooBool"))
    49  	assert.DeepEqual(t,
    50  		[]string{"FooStr", "FooStr3"},
    51  		UnknownNonEmptyFields(&foo1, "FooBool", "FooMap"))
    52  
    53  	foo2 := foo1
    54  	foo2.FooBoolPtr = &foo1.FooBool
    55  	foo2.FooMap = map[string]struct{}{
    56  		"blah": {},
    57  	}
    58  	assert.DeepEqual(t,
    59  		[]string{"FooBoolPtr", "FooMap", "FooStr", "FooStr3"},
    60  		UnknownNonEmptyFields(&foo2, "FooBool"))
    61  
    62  	foo3 := foo1
    63  	foo3.FooMap = make(map[string]struct{})
    64  	assert.DeepEqual(t,
    65  		[]string{"FooStr", "FooStr3"},
    66  		UnknownNonEmptyFields(&foo3, "FooBool"))
    67  }