knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/test/upgrade/shell/prefixer_test.go (about)

     1  /*
     2  Copyright 2020 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package shell_test
    18  
    19  import (
    20  	"bytes"
    21  	"strconv"
    22  	"testing"
    23  
    24  	"knative.dev/pkg/test/upgrade/shell"
    25  )
    26  
    27  func TestNewPrefixer(t *testing.T) {
    28  	assert := assertions{t: t}
    29  	var lineno int64
    30  	tests := []struct {
    31  		name   string
    32  		prefix func() string
    33  		want   string
    34  	}{{
    35  		"static",
    36  		func() string {
    37  			return "[prefix] "
    38  		},
    39  		`[prefix] test string 1
    40  [prefix] test string 2
    41  `,
    42  	}, {
    43  		"empty",
    44  		func() string {
    45  			return ""
    46  		},
    47  		`test string 1
    48  test string 2
    49  `,
    50  	}, {
    51  		"dynamic",
    52  		func() string {
    53  			lineno++
    54  			return strconv.FormatInt(lineno, 10) + ") "
    55  		},
    56  		`1) test string 1
    57  2) test string 2
    58  `,
    59  	}}
    60  	for _, tt := range tests {
    61  		t.Run(tt.name, func(t *testing.T) {
    62  			writer := &bytes.Buffer{}
    63  			wr := shell.NewPrefixer(writer, tt.prefix)
    64  			_, err := wr.Write([]byte("test string 1\ntest string 2\n"))
    65  			assert.NoError(err)
    66  			got := writer.String()
    67  			assert.Equal(tt.want, got)
    68  		})
    69  	}
    70  }