github.com/replicatedhq/ship@v0.55.0/pkg/util/marshalIndent_test.go (about)

     1  package util
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestMarshalIndent(t *testing.T) {
    11  	veryLongLineString := "this is a very long single-line string. It should be rendered in yaml as a single line string, and not split into multiple lines. By default, go-yaml/yaml.v2 and go-yaml/yaml.v3 will split long strings onto multiple lines. This breaks some template functions, and so a PR has been opened against go-yaml/yaml.v3 to allow setting the desired length: https://github.com/go-yaml/yaml/pull/455. Until this PR is merged, we will instead be using laverya/yaml to render such strings."
    12  
    13  	type recurse struct {
    14  		A   int
    15  		Rec *recurse
    16  	}
    17  	recurseA := recurse{A: 1}
    18  	recurseB := recurse{A: 1, Rec: &recurseA}
    19  	recurseA.Rec = &recurseB
    20  
    21  	type args struct {
    22  		indent int
    23  		in     interface{}
    24  	}
    25  	tests := []struct {
    26  		name    string
    27  		args    args
    28  		want    string
    29  		wantErr bool
    30  	}{
    31  		{
    32  			name: "basic struct",
    33  			args: args{
    34  				indent: 3,
    35  				in: struct {
    36  					Abc     string
    37  					Xyz     int
    38  					Hello   string `yaml:"world"`
    39  					Recurse interface{}
    40  				}{
    41  					Abc:   "a",
    42  					Xyz:   0,
    43  					Hello: "this",
    44  					Recurse: struct {
    45  						Nested string
    46  					}{
    47  						Nested: "nestedstring",
    48  					},
    49  				},
    50  			},
    51  			want: `abc: a
    52  xyz: 0
    53  world: this
    54  recurse:
    55     nested: nestedstring
    56  `,
    57  		},
    58  		{
    59  			name: "very long string line",
    60  			args: args{
    61  				indent: 2,
    62  				in: struct {
    63  					Top      string
    64  					Indented interface{}
    65  				}{
    66  					Top: "top",
    67  					Indented: struct {
    68  						Long string
    69  					}{
    70  						Long: veryLongLineString,
    71  					},
    72  				},
    73  			},
    74  			want: fmt.Sprintf(`top: top
    75  indented:
    76    long: '%s'
    77  `, veryLongLineString),
    78  		},
    79  		{
    80  			name: "very long multiline string line",
    81  			args: args{
    82  				indent: 2,
    83  				in: struct {
    84  					Top      string
    85  					Indented interface{}
    86  				}{
    87  					Top: "top",
    88  					Indented: struct {
    89  						Long string
    90  					}{
    91  						Long: `if not split into multiple lines, this would be a rather long string
    92  thankfully it can be split naturally where newlines already exist
    93  as demonstrated here
    94  otherwise things would be completely unreadable`,
    95  					},
    96  				},
    97  			},
    98  			want: `top: top
    99  indented:
   100    long: |-
   101      if not split into multiple lines, this would be a rather long string
   102      thankfully it can be split naturally where newlines already exist
   103      as demonstrated here
   104      otherwise things would be completely unreadable
   105  `,
   106  		},
   107  	}
   108  	for _, tt := range tests {
   109  		t.Run(tt.name, func(t *testing.T) {
   110  			req := require.New(t)
   111  			got, err := MarshalIndent(tt.args.indent, tt.args.in)
   112  			if tt.wantErr {
   113  				req.Error(err)
   114  			} else {
   115  				req.NoError(err)
   116  			}
   117  
   118  			req.Equal(tt.want, string(got))
   119  		})
   120  	}
   121  }