istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/config/file/util/kubeyaml/kubeyaml_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 kubeyaml
    16  
    17  import (
    18  	"bufio"
    19  	"fmt"
    20  	"io"
    21  	"strings"
    22  	"testing"
    23  
    24  	. "github.com/onsi/gomega"
    25  )
    26  
    27  var joinCases = []struct {
    28  	merged string
    29  	split  []string
    30  }{
    31  	{
    32  		merged: "",
    33  		split:  nil,
    34  	},
    35  	{
    36  		merged: `yaml: foo`,
    37  		split: []string{
    38  			`yaml: foo`,
    39  		},
    40  	},
    41  	{
    42  		merged: `
    43  yaml: foo
    44  ---
    45  bar: boo
    46  `,
    47  		split: []string{
    48  			`
    49  yaml: foo
    50  `,
    51  			`bar: boo
    52  `,
    53  		},
    54  	},
    55  	{
    56  		merged: `
    57  yaml: foo
    58  ---
    59  bar: boo
    60  `,
    61  		split: []string{
    62  			`
    63  yaml: foo
    64  `,
    65  			``,
    66  			`bar: boo
    67  `,
    68  		},
    69  	},
    70  	{
    71  		merged: `
    72  yaml: foo
    73  ---
    74  bar: boo`,
    75  		split: []string{
    76  			`
    77  yaml: foo`,
    78  			`bar: boo`,
    79  		},
    80  	},
    81  }
    82  
    83  func TestJoinBytes(t *testing.T) {
    84  	for i, c := range joinCases {
    85  		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
    86  			g := NewWithT(t)
    87  
    88  			var by [][]byte
    89  			for _, s := range c.split {
    90  				by = append(by, []byte(s))
    91  			}
    92  			actual := Join(by...)
    93  
    94  			g.Expect(actual).To(Equal([]byte(c.merged)))
    95  		})
    96  	}
    97  }
    98  
    99  func TestJoinString(t *testing.T) {
   100  	for i, c := range joinCases {
   101  		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
   102  			g := NewWithT(t)
   103  
   104  			actual := JoinString(c.split...)
   105  
   106  			g.Expect(actual).To(Equal(c.merged))
   107  		})
   108  	}
   109  }
   110  
   111  func TestLineNumber(t *testing.T) {
   112  	testCases := []struct {
   113  		input       string
   114  		lineNumbers []int
   115  	}{
   116  		{
   117  			input:       "foo: bar\n---\nfoo: baz",
   118  			lineNumbers: []int{1, 3},
   119  		},
   120  		{
   121  			input:       "\n\nfoo: bar\n---\n\n\nfoo: baz",
   122  			lineNumbers: []int{3, 7},
   123  		},
   124  		{
   125  			input:       "---\n\nfoo: bar\n---\n\n\nfoo: baz",
   126  			lineNumbers: []int{3, 7},
   127  		},
   128  	}
   129  	for i, tc := range testCases {
   130  		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
   131  			g := NewWithT(t)
   132  
   133  			reader := bufio.NewReader(strings.NewReader(tc.input))
   134  			decoder := NewYAMLReader(reader)
   135  			var expectedLineNumbers []int
   136  			for {
   137  				_, line, err := decoder.Read()
   138  				if err == io.EOF {
   139  					break
   140  				}
   141  				expectedLineNumbers = append(expectedLineNumbers, line)
   142  			}
   143  			g.Expect(expectedLineNumbers).To(Equal(tc.lineNumbers))
   144  		})
   145  	}
   146  }