...

Source file src/k8s.io/kubectl/pkg/util/term/term_writer_test.go

Documentation: k8s.io/kubectl/pkg/util/term

     1  /*
     2  Copyright 2016 The Kubernetes 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 term
    18  
    19  import (
    20  	"bytes"
    21  	"strings"
    22  	"testing"
    23  )
    24  
    25  const test = "Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube Kube"
    26  
    27  func TestWordWrapWriter(t *testing.T) {
    28  	testcases := map[string]struct {
    29  		input    string
    30  		maxWidth uint
    31  	}{
    32  		"max 10":   {input: test, maxWidth: 10},
    33  		"max 80":   {input: test, maxWidth: 80},
    34  		"max 120":  {input: test, maxWidth: 120},
    35  		"max 5000": {input: test, maxWidth: 5000},
    36  	}
    37  	for k, tc := range testcases {
    38  		b := bytes.NewBufferString("")
    39  		w := NewWordWrapWriter(b, tc.maxWidth)
    40  		_, err := w.Write([]byte(tc.input))
    41  		if err != nil {
    42  			t.Errorf("%s: Unexpected error: %v", k, err)
    43  		}
    44  		result := b.String()
    45  		if !strings.Contains(result, "Kube") {
    46  			t.Errorf("%s: Expected to contain \"Kube\"", k)
    47  		}
    48  		if len(result) < len(tc.input) {
    49  			t.Errorf("%s: Unexpectedly short string, got %d wanted at least %d chars: %q", k, len(result), len(tc.input), result)
    50  		}
    51  		for _, line := range strings.Split(result, "\n") {
    52  			if len(line) > int(tc.maxWidth) {
    53  				t.Errorf("%s: Every line must be at most %d chars long, got %d: %q", k, tc.maxWidth, len(line), line)
    54  			}
    55  		}
    56  		for _, word := range strings.Split(result, " ") {
    57  			if !strings.Contains(word, "Kube") {
    58  				t.Errorf("%s: Unexpected broken word: %q", k, word)
    59  			}
    60  		}
    61  	}
    62  }
    63  
    64  func TestMaxWidthWriter(t *testing.T) {
    65  	testcases := map[string]struct {
    66  		input    string
    67  		maxWidth uint
    68  	}{
    69  		"max 10":   {input: test, maxWidth: 10},
    70  		"max 80":   {input: test, maxWidth: 80},
    71  		"max 120":  {input: test, maxWidth: 120},
    72  		"max 5000": {input: test, maxWidth: 5000},
    73  	}
    74  	for k, tc := range testcases {
    75  		b := bytes.NewBufferString("")
    76  		w := NewMaxWidthWriter(b, tc.maxWidth)
    77  		_, err := w.Write([]byte(tc.input))
    78  		if err != nil {
    79  			t.Errorf("%s: Unexpected error: %v", k, err)
    80  		}
    81  		result := b.String()
    82  		if !strings.Contains(result, "Kube") {
    83  			t.Errorf("%s: Expected to contain \"Kube\"", k)
    84  		}
    85  		if len(result) < len(tc.input) {
    86  			t.Errorf("%s: Unexpectedly short string, got %d wanted at least %d chars: %q", k, len(result), len(tc.input), result)
    87  		}
    88  		lines := strings.Split(result, "\n")
    89  		for i, line := range lines {
    90  			if len(line) > int(tc.maxWidth) {
    91  				t.Errorf("%s: Every line must be at most %d chars long, got %d: %q", k, tc.maxWidth, len(line), line)
    92  			}
    93  			if i < len(lines)-1 && len(line) != int(tc.maxWidth) {
    94  				t.Errorf("%s: Lines except the last one are expected to be exactly %d chars long, got %d: %q", k, tc.maxWidth, len(line), line)
    95  			}
    96  		}
    97  	}
    98  }
    99  

View as plain text