...

Source file src/k8s.io/kubectl/pkg/explain/formatter_test.go

Documentation: k8s.io/kubectl/pkg/explain

     1  /*
     2  Copyright 2017 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 explain
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  )
    25  
    26  func TestFormatterWrite(t *testing.T) {
    27  	buf := bytes.Buffer{}
    28  	f := Formatter{
    29  		Writer: &buf,
    30  	}
    31  
    32  	f.Write("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
    33  	// Indent creates a new Formatter
    34  	f.Indent(5).Write("Morbi at turpis faucibus, gravida dolor ut, fringilla velit.")
    35  	// So Indent(2) doesn't indent to 7 here.
    36  	f.Indent(2).Write("Etiam maximus urna at tellus faucibus mattis.")
    37  
    38  	want := `Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    39       Morbi at turpis faucibus, gravida dolor ut, fringilla velit.
    40    Etiam maximus urna at tellus faucibus mattis.
    41  `
    42  
    43  	if buf.String() != want {
    44  		t.Errorf("Got:\n%v\nWant:\n%v\n", buf.String(), want)
    45  	}
    46  }
    47  
    48  func TestFormatterWrappedWrite(t *testing.T) {
    49  	buf := bytes.Buffer{}
    50  	f := Formatter{
    51  		Writer: &buf,
    52  		Wrap:   50,
    53  	}
    54  
    55  	f.WriteWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi at turpis faucibus, gravida dolor ut, fringilla velit.")
    56  	f.Indent(10).WriteWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi at turpis faucibus, gravida dolor ut, fringilla velit.")
    57  	// Test long words (especially urls) on their own line.
    58  	f.Indent(20).WriteWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit. ThisIsAVeryLongWordThatDoesn'tFitOnALineOnItsOwn. Morbi at turpis faucibus, gravida dolor ut, fringilla velit.")
    59  	// Test content that includes newlines, bullet points, and blockquotes/code blocks
    60  	f.Indent(4).WriteWrapped(`
    61  This is an
    62  introductory paragraph
    63  that should end
    64  up on a continuous line.
    65  
    66  Example:
    67  Example text on its own line
    68  
    69  List:
    70  1.  Item with
    71   wrapping text
    72  11.  Another
    73   item with wrapping text
    74  * Bullet item
    75   with wrapping text
    76  - Dash item
    77   with wrapping text
    78  
    79  base64(
    80      code goes here
    81      and here
    82  )`)
    83  
    84  	want := `Lorem ipsum dolor sit amet, consectetur adipiscing
    85  elit. Morbi at turpis faucibus, gravida dolor ut,
    86  fringilla velit.
    87            Lorem ipsum dolor sit amet, consectetur
    88            adipiscing elit. Morbi at turpis
    89            faucibus, gravida dolor ut, fringilla
    90            velit.
    91                      Lorem ipsum dolor sit amet,
    92                      consectetur adipiscing elit.
    93                      ThisIsAVeryLongWordThatDoesn'tFitOnALineOnItsOwn.
    94                      Morbi at turpis faucibus,
    95                      gravida dolor ut, fringilla
    96                      velit.
    97      This is an introductory paragraph that should
    98      end up on a continuous line.
    99  
   100      Example:
   101      Example text on its own line
   102  
   103      List:
   104      1. Item with wrapping text
   105      11. Another item with wrapping text
   106      * Bullet item with wrapping text
   107      - Dash item with wrapping text
   108  
   109      base64(
   110          code goes here
   111          and here
   112      )
   113  `
   114  
   115  	if buf.String() != want {
   116  		t.Errorf("Diff:\n%s", cmp.Diff(buf.String(), want))
   117  	}
   118  }
   119  
   120  func TestDefaultWrap(t *testing.T) {
   121  	buf := bytes.Buffer{}
   122  	f := Formatter{
   123  		Writer: &buf,
   124  		// Wrap is not set
   125  	}
   126  
   127  	f.WriteWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi at turpis faucibus, gravida dolor ut, fringilla velit. Etiam maximus urna at tellus faucibus mattis.")
   128  	want := `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi at turpis faucibus, gravida dolor ut, fringilla velit. Etiam maximus urna at tellus faucibus mattis.
   129  `
   130  	if buf.String() != want {
   131  		t.Errorf("Got:\n%v\nWant:\n%v\n", buf.String(), want)
   132  	}
   133  }
   134  

View as plain text