Debian Codegraph @

kyaml/kyaml_omitzero_test.go

raw
1 //go:build go1.24
2 // +build go1.24
3
4 /*
5 Copyright 2025 The Kubernetes Authors.
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 */
19
20 package kyaml
21
22 import (
23 "strings"
24 "testing"
25 )
26
27 func TestKYAMLOutputSyntax_omitzero(t *testing.T) {
28 type testCase struct {
29 name string
30 input any
31 expected string
32 }
33
34 tests := []testCase{
35 {"omitzero struct", struct {
36 I int `json:",omitzero"`
37 S string `json:",omitzero"`
38 B bool `json:",omitzero"`
39 P *int `json:",omitzero"`
40 }{}, "{}"},
41 {"omitzero struct nil slice", struct {
42 S []int `json:",omitzero"`
43 }{}, "{}"},
44 {"omitzero struct zero slice", struct {
45 S []int `json:",omitzero"`
46 }{S: []int{}}, "{\n S: [],\n}"},
47 {"omitzero struct nil map", struct {
48 M map[int]int `json:",omitzero"`
49 }{}, "{}"},
50 {"omitzero struct zero map", struct {
51 M map[int]int `json:",omitzero"`
52 }{M: map[int]int{}}, "{\n M: {},\n}"},
53 }
54
55 for _, tt := range tests {
56 t.Run(tt.name, func(t *testing.T) {
57 ky := &Encoder{}
58 yb, err := ky.Marshal(tt.input)
59 if err != nil {
60 t.Fatalf("failed to render KYAML: %v", err)
61 }
62 // always has a newline at the end
63 if result := strings.TrimRight(string(yb), "\n"); result != tt.expected {
64 t.Errorf("Marshal(%v):\nwanted: %q\n got: %q", tt.input, tt.expected, result)
65 }
66 })
67 }
68 }