k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/utils/format/format_test.go (about)

     1  /*
     2  Copyright 2022 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 format_test
    18  
    19  import (
    20  	"fmt"
    21  	"regexp"
    22  	"testing"
    23  
    24  	"github.com/onsi/gomega/format"
    25  	"github.com/stretchr/testify/assert"
    26  
    27  	v1 "k8s.io/api/core/v1"
    28  )
    29  
    30  func TestGomegaFormatObject(t *testing.T) {
    31  	for name, test := range map[string]struct {
    32  		value       interface{}
    33  		expected    string
    34  		indentation uint
    35  	}{
    36  		"int":            {value: 1, expected: `<int>: 1`},
    37  		"string":         {value: "hello world", expected: `<string>: "hello world"`},
    38  		"struct":         {value: myStruct{a: 1, b: 2}, expected: `<format_test.myStruct>: {a: 1, b: 2}`},
    39  		"gomegastringer": {value: typeWithGomegaStringer(2), expected: `<format_test.typeWithGomegaStringer>: my stringer 2`},
    40  		"pod": {value: v1.Pod{}, expected: `<v1.Pod>: 
    41      metadata:
    42        creationTimestamp: null
    43      spec:
    44        containers: null
    45      status: {}`},
    46  		"pod-indented": {value: v1.Pod{}, indentation: 1, expected: `    <v1.Pod>: 
    47          metadata:
    48            creationTimestamp: null
    49          spec:
    50            containers: null
    51          status: {}`},
    52  		"pod-ptr": {value: &v1.Pod{}, expected: `<*v1.Pod | <hex>>: 
    53      metadata:
    54        creationTimestamp: null
    55      spec:
    56        containers: null
    57      status: {}`},
    58  		"pod-hash": {value: map[string]v1.Pod{}, expected: `<map[string]v1.Pod | len:0>: 
    59      {}`},
    60  		"podlist": {value: v1.PodList{}, expected: `<v1.PodList>: 
    61      items: null
    62      metadata: {}`},
    63  	} {
    64  		t.Run(name, func(t *testing.T) {
    65  			actual := format.Object(test.value, test.indentation)
    66  			actual = regexp.MustCompile(`\| 0x[a-z0-9]+`).ReplaceAllString(actual, `| <hex>`)
    67  			assert.Equal(t, test.expected, actual)
    68  		})
    69  	}
    70  
    71  }
    72  
    73  type typeWithGomegaStringer int
    74  
    75  func (v typeWithGomegaStringer) GomegaString() string {
    76  	return fmt.Sprintf("my stringer %d", v)
    77  }
    78  
    79  type myStruct struct {
    80  	a, b int
    81  }