...

Source file src/github.com/mitchellh/go-wordwrap/wordwrap_test.go

Documentation: github.com/mitchellh/go-wordwrap

     1  package wordwrap
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestWrapString(t *testing.T) {
     9  	cases := []struct {
    10  		Input, Output string
    11  		Lim           uint
    12  	}{
    13  		// A simple word passes through.
    14  		{
    15  			"foo",
    16  			"foo",
    17  			4,
    18  		},
    19  		// A single word that is too long passes through.
    20  		// We do not break words.
    21  		{
    22  			"foobarbaz",
    23  			"foobarbaz",
    24  			4,
    25  		},
    26  		// Lines are broken at whitespace.
    27  		{
    28  			"foo bar baz",
    29  			"foo\nbar\nbaz",
    30  			4,
    31  		},
    32  		// Lines are broken at whitespace, even if words
    33  		// are too long. We do not break words.
    34  		{
    35  			"foo bars bazzes",
    36  			"foo\nbars\nbazzes",
    37  			4,
    38  		},
    39  		// A word that would run beyond the width is wrapped.
    40  		{
    41  			"fo sop",
    42  			"fo\nsop",
    43  			4,
    44  		},
    45  		// Do not break on non-breaking space.
    46  		{
    47  			"foo bar\u00A0baz",
    48  			"foo\nbar\u00A0baz",
    49  			10,
    50  		},
    51  		// Whitespace that trails a line and fits the width
    52  		// passes through, as does whitespace prefixing an
    53  		// explicit line break. A tab counts as one character.
    54  		{
    55  			"foo\nb\t r\n baz",
    56  			"foo\nb\t r\n baz",
    57  			4,
    58  		},
    59  		// Trailing whitespace is removed if it doesn't fit the width.
    60  		// Runs of whitespace on which a line is broken are removed.
    61  		{
    62  			"foo    \nb   ar   ",
    63  			"foo\nb\nar",
    64  			4,
    65  		},
    66  		// An explicit line break at the end of the input is preserved.
    67  		{
    68  			"foo bar baz\n",
    69  			"foo\nbar\nbaz\n",
    70  			4,
    71  		},
    72  		// Explicit break are always preserved.
    73  		{
    74  			"\nfoo bar\n\n\nbaz\n",
    75  			"\nfoo\nbar\n\n\nbaz\n",
    76  			4,
    77  		},
    78  		// Complete example:
    79  		{
    80  			" This is a list: \n\n\t* foo\n\t* bar\n\n\n\t* baz  \nBAM    ",
    81  			" This\nis a\nlist: \n\n\t* foo\n\t* bar\n\n\n\t* baz\nBAM",
    82  			6,
    83  		},
    84  		// Multi-byte characters
    85  		{
    86  			strings.Repeat("\u2584 ", 4),
    87  			"\u2584 \u2584" + "\n" +
    88  				strings.Repeat("\u2584 ", 2),
    89  			4,
    90  		},
    91  	}
    92  
    93  	for i, tc := range cases {
    94  		actual := WrapString(tc.Input, tc.Lim)
    95  		if actual != tc.Output {
    96  			t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`%s`\n\nActual Output:\n\n`%s`", i, tc.Input, tc.Output, actual)
    97  		}
    98  	}
    99  }
   100  

View as plain text