k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/generators/rules/omitempty_match_case_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 rules
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"k8s.io/gengo/v2/types"
    24  )
    25  
    26  func TestOmitEmptyMatchCase(t *testing.T) {
    27  	tcs := []struct {
    28  		// name of test case
    29  		name string
    30  		t    *types.Type
    31  
    32  		// expected list of violation fields
    33  		expected []string
    34  	}{
    35  		{
    36  			name: "simple",
    37  			t: &types.Type{
    38  				Kind: types.Struct,
    39  				Members: []types.Member{
    40  					{
    41  						Name: "PodSpec",
    42  						Tags: `json:"podSpec"`,
    43  					},
    44  				},
    45  			},
    46  			expected: []string{},
    47  		},
    48  		{
    49  			name: "unserialized",
    50  			t: &types.Type{
    51  				Kind: types.Struct,
    52  				Members: []types.Member{
    53  					{
    54  						Name: "PodSpec",
    55  						Tags: `json:"-,inline"`,
    56  					},
    57  				},
    58  			},
    59  			expected: []string{},
    60  		},
    61  		{
    62  			name: "named_omitEmpty",
    63  			t: &types.Type{
    64  				Kind: types.Struct,
    65  				Members: []types.Member{
    66  					{
    67  						Name: "OmitEmpty",
    68  						Tags: `json:"omitEmpty,inline"`,
    69  					},
    70  				},
    71  			},
    72  			expected: []string{},
    73  		},
    74  		{
    75  			name: "valid",
    76  			t: &types.Type{
    77  				Kind: types.Struct,
    78  				Members: []types.Member{
    79  					{
    80  						Name: "PodSpec",
    81  						Tags: `json:"podSpec,omitempty"`,
    82  					},
    83  				},
    84  			},
    85  			expected: []string{},
    86  		},
    87  		{
    88  			name: "invalid",
    89  			t: &types.Type{
    90  				Kind: types.Struct,
    91  				Members: []types.Member{
    92  					{
    93  						Name: "PodSpec",
    94  						Tags: `json:"podSpec,omitEmpty"`,
    95  					},
    96  				},
    97  			},
    98  			expected: []string{"PodSpec"},
    99  		},
   100  	}
   101  
   102  	n := &OmitEmptyMatchCase{}
   103  	for _, tc := range tcs {
   104  		t.Run(tc.name, func(t *testing.T) {
   105  			if violations, _ := n.Validate(tc.t); !reflect.DeepEqual(violations, tc.expected) {
   106  				t.Errorf("unexpected validation result: want: %v, got: %v", tc.expected, violations)
   107  			}
   108  		})
   109  	}
   110  }