github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/strings/strings_fuzz_test.go (about)

     1  package strings
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  	"unicode"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func FuzzIndent(f *testing.F) {
    12  	f.Add(0, "  ", "foo\n")
    13  	f.Add(1, "  ", "bar\nbaz\nqux\n")
    14  	f.Add(1, "  ", "\n")
    15  	f.Add(3, "  ", "quux\n")
    16  	f.Add(15, "\n0", "\n0")
    17  
    18  	f.Fuzz(func(t *testing.T, width int, indent, s string) {
    19  		out := Indent(width, indent, s)
    20  
    21  		// out should be equal to s when both have the indent character
    22  		// completely removed.
    23  		assert.Equal(t,
    24  			strings.ReplaceAll(s, indent, ""),
    25  			strings.ReplaceAll(out, indent, ""),
    26  		)
    27  	})
    28  }
    29  
    30  func FuzzTrunc(f *testing.F) {
    31  	f.Add(0, "foo")
    32  
    33  	f.Fuzz(func(t *testing.T, length int, s string) {
    34  		out := Trunc(length, s)
    35  
    36  		assert.LessOrEqual(t, len(out), len(s))
    37  		if length >= 0 {
    38  			assert.LessOrEqual(t, len(out), length)
    39  		}
    40  
    41  		assert.Equal(t, s[0:len(out)], out)
    42  	})
    43  }
    44  
    45  func FuzzWordWrap(f *testing.F) {
    46  	out := `There shouldn't be any wrapping of long words or URLs because that would break
    47  things very badly. To wit:
    48  https://example.com/a/super-long/url/that-shouldnt-be?wrapped=for+fear+of#the-breaking-of-functionality
    49  should appear on its own line, regardless of the desired word-wrapping width
    50  that has been set.`
    51  	f.Add(out, "", uint(0))
    52  	f.Add(out, "\n", uint(80))
    53  	f.Add(out, "\v", uint(10))
    54  
    55  	f.Fuzz(func(t *testing.T, in, lbSeq string, width uint) {
    56  		for _, r := range lbSeq {
    57  			if !unicode.IsSpace(r) {
    58  				t.Skip("ignore non-whitespace sequences")
    59  			}
    60  		}
    61  
    62  		out := WordWrap(in, WordWrapOpts{
    63  			LBSeq: lbSeq,
    64  			Width: width,
    65  		})
    66  
    67  		if lbSeq == "" {
    68  			lbSeq = "\n"
    69  		}
    70  		// compare by stripping both the line-break sequence and spaces
    71  		assert.Equal(t,
    72  			strings.ReplaceAll(strings.ReplaceAll(in, lbSeq, ""), " ", ""),
    73  			strings.ReplaceAll(strings.ReplaceAll(out, lbSeq, ""), " ", ""),
    74  		)
    75  	})
    76  }