mirror of
https://github.com/kubernetes/kubernetes.git
synced 2026-05-13 09:29:57 -04:00
The format package is used by ktesting, both to reconfigure Gomega and to format errors, therefore it has to be moved to staging together with ktesting, if or when we get to that because those are desirable features. Because format only has the YAML package as additional dependency and that should be okay for all other repos (except for the YAML package itself, of course), we can publish the format package as a sub-package of such a future ktesting module. Avoiding the dependency on apimachinery to detect unstructured.Unstructured is a bit tricky, but doable by relaxing what we check for. The test/utils/format package is kept to test ktesting/format with the actual packages that it cannot depend on (apimachinery, api).
94 lines
2.5 KiB
Go
94 lines
2.5 KiB
Go
/*
|
|
Copyright 2022 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package format_test
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"testing"
|
|
|
|
"github.com/onsi/gomega/format"
|
|
)
|
|
|
|
func TestGomegaFormatObject(t *testing.T) {
|
|
for name, test := range map[string]struct {
|
|
value interface{}
|
|
expected string
|
|
indentation uint
|
|
}{
|
|
"int": {value: 1, expected: `<int>: 1`},
|
|
"string": {value: "hello world", expected: `<string>: hello world`},
|
|
"struct": {value: myStruct{a: 1, b: 2}, expected: `<format_test.myStruct>: {a: 1, b: 2}`},
|
|
"gomegastringer": {value: typeWithGomegaStringer(2), expected: `<format_test.typeWithGomegaStringer>: my stringer 2`},
|
|
|
|
// v1.Pod is tested in test/utils/format.
|
|
|
|
// For unstructured.Unstructured, the corresponding testuses the real type.
|
|
// The expected result is a bit different because "our" myUnstructured has no
|
|
// special JSON encoding methods, but for testing that YAML is used at all that's
|
|
// sufficient.
|
|
"unstructured-pointer": {
|
|
value: &myUnstructured{
|
|
Object: map[string]any{
|
|
"metadata": map[string]any{
|
|
"name": "some-name",
|
|
},
|
|
},
|
|
},
|
|
expected: `<*format_test.myUnstructured | <hex>>:
|
|
Object:
|
|
metadata:
|
|
name: some-name`,
|
|
},
|
|
"unstructured-value": {
|
|
value: myUnstructured{
|
|
Object: map[string]any{
|
|
"metadata": map[string]any{
|
|
"name": "some-name",
|
|
},
|
|
},
|
|
},
|
|
expected: `<format_test.myUnstructured>:
|
|
Object:
|
|
metadata:
|
|
name: some-name`,
|
|
},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
actual := format.Object(test.value, test.indentation)
|
|
actual = regexp.MustCompile(`\| 0x[a-z0-9]+`).ReplaceAllString(actual, `| <hex>`)
|
|
if test.expected != actual {
|
|
t.Errorf("Expected:\n%s\nActual:\n%s\n", test.expected, actual)
|
|
}
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
type typeWithGomegaStringer int
|
|
|
|
func (v typeWithGomegaStringer) GomegaString() string {
|
|
return fmt.Sprintf("my stringer %d", v)
|
|
}
|
|
|
|
type myStruct struct {
|
|
a, b int
|
|
}
|
|
|
|
type myUnstructured struct {
|
|
Object map[string]any
|
|
}
|