1
// Copyright Istio Authors
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
22
func TestToYAML(t *testing.T) {
30
inVals: map[string]any{
36
expectedOut: `foo: bar
43
inVals: map[string]any{
52
desc: "expected-err-nil",
54
expectedOut: "null\n",
57
for _, tt := range tests {
58
t.Run(tt.desc, func(t *testing.T) {
59
if got := ToYAML(tt.inVals); got != tt.expectedOut {
60
t.Errorf("%s: expected out %v got %s", tt.desc, tt.expectedOut, got)
66
func TestOverlayTrees(t *testing.T) {
70
inOverlays map[string]any
71
expectedOverlay map[string]any
75
desc: "overlay-valid",
76
inBase: map[string]any{
80
inOverlays: map[string]any{
83
expectedOverlay: map[string]any{
90
desc: "overlay-key-does-not-exist",
91
inBase: map[string]any{
95
inOverlays: map[string]any{
96
"i-dont-exist": "i-really-dont-exist",
98
expectedOverlay: map[string]any{
101
"i-dont-exist": "i-really-dont-exist",
106
desc: "remove-key-val",
107
inBase: map[string]any{
110
inOverlays: map[string]any{
113
expectedOverlay: map[string]any{},
117
for _, tt := range tests {
118
t.Run(tt.desc, func(t *testing.T) {
119
if gotOverlays, err := OverlayTrees(tt.inBase, tt.inOverlays); !reflect.DeepEqual(gotOverlays, tt.expectedOverlay) ||
120
((err != nil && tt.expectedErr == nil) || (err == nil && tt.expectedErr != nil)) {
121
t.Errorf("%s: expected overlay & err %v %v got %v %v", tt.desc, tt.expectedOverlay, tt.expectedErr,
128
func TestOverlayYAML(t *testing.T) {
137
desc: "overlay-yaml",
148
desc: "combine-yaml",
150
overlay: `baz: razmatazz`,
151
expect: `baz: razmatazz
159
overlay: `FM#)M#F(*#M`,
160
expect: "FM#)M#F(*#M\n",
164
for _, tt := range tests {
165
t.Run(tt.desc, func(t *testing.T) {
166
if got, err := OverlayYAML(tt.base, tt.overlay); got != tt.expect || ((tt.err != nil && err == nil) || (tt.err == nil && err != nil)) {
167
t.Errorf("%s: expected overlay&err %v %v got %v %v", tt.desc, tt.expect, tt.err, got, err)
173
func TestYAMLDiff(t *testing.T) {
203
desc: "invalid-yaml",
206
expect: "error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type map[string]interface {}",
209
for _, tt := range tests {
210
t.Run(tt.desc, func(t *testing.T) {
211
if got := YAMLDiff(tt.diff1, tt.diff2); got != tt.expect {
212
t.Errorf("%s: expect %v got %v", tt.desc, tt.expect, got)
218
func TestMultipleYAMLDiff(t *testing.T) {
263
for _, tt := range tests {
264
t.Run(tt.desc, func(t *testing.T) {
265
if got := YAMLDiff(tt.diff1, tt.diff2); got != tt.expect {
266
t.Errorf("%s: expect %v got %v", tt.desc, tt.expect, got)
272
func TestIsYAMLEqual(t *testing.T) {
298
desc: "yaml-not-equal",
300
stoichiometry: avagadro
308
for _, tt := range tests {
309
t.Run(tt.desc, func(t *testing.T) {
310
if got := IsYAMLEqual(tt.in1, tt.in2); got != tt.expect {
311
t.Errorf("%v: got %v want %v", tt.desc, got, tt.expect)
317
func TestIsYAMLEmpty(t *testing.T) {
324
desc: "completely-empty",
329
desc: "comment-logically-empty",
330
in: `# this is a comment
331
# this is another comment that serves no purpose
332
# (like all comments usually do)
338
in: `--- I dont mean anything`,
342
desc: "combine-comments-and-yaml",
343
in: `#this is another comment
345
# ^ that serves purpose
350
desc: "yaml-not-empty",
355
for _, tt := range tests {
356
t.Run(tt.desc, func(t *testing.T) {
357
if got := IsYAMLEmpty(tt.in); got != tt.expect {
358
t.Errorf("%v: expect %v got %v", tt.desc, tt.expect, got)