istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/util/yml/parts_test.go (about) 1 // Copyright Istio Authors 2 // 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 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 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. 14 15 package yml_test 16 17 import ( 18 "testing" 19 20 . "github.com/onsi/gomega" 21 22 "istio.io/istio/pkg/test/util/yml" 23 ) 24 25 func TestEmptyDoc(t *testing.T) { 26 g := NewWithT(t) 27 28 yaml := ` 29 ` 30 parts := yml.SplitString(yaml) 31 g.Expect(len(parts)).To(Equal(0)) 32 33 yaml = yml.JoinString(parts...) 34 g.Expect(yaml).To(Equal("")) 35 } 36 37 func TestSplitWithEmptyPart(t *testing.T) { 38 expected := []string{ 39 "a", 40 "b", 41 } 42 43 cases := []struct { 44 name string 45 doc string 46 }{ 47 { 48 name: "beginningNoCR", 49 doc: `--- 50 a 51 --- 52 b 53 `, 54 }, 55 { 56 name: "beginningWithCR", 57 doc: ` 58 --- 59 a 60 --- 61 b 62 `, 63 }, 64 { 65 name: "middle", 66 doc: `a 67 --- 68 --- 69 b 70 `, 71 }, 72 { 73 name: "end", 74 doc: ` 75 a 76 --- 77 b 78 --- 79 `, 80 }, 81 } 82 83 for _, c := range cases { 84 t.Run(c.name, func(t *testing.T) { 85 parts := yml.SplitString(c.doc) 86 g := NewWithT(t) 87 g.Expect(parts).To(Equal(expected)) 88 }) 89 } 90 } 91 92 func TestSplitWithNestedDocument(t *testing.T) { 93 doc := ` 94 b 95 b1 96 --- 97 b2 98 ` 99 expected := []string{ 100 `b 101 b1 102 --- 103 b2`, 104 } 105 106 g := NewWithT(t) 107 parts := yml.SplitString(doc) 108 g.Expect(parts).To(Equal(expected)) 109 } 110 111 func TestJoinRemovesEmptyParts(t *testing.T) { 112 parts := []string{ 113 `--- 114 --- 115 --- 116 `, 117 ` 118 --- 119 a 120 --- 121 `, 122 ` 123 b 124 --- 125 `, 126 } 127 128 expected := `a 129 --- 130 b` 131 132 g := NewWithT(t) 133 doc := yml.JoinString(parts...) 134 g.Expect(doc).To(Equal(expected)) 135 }