From 599121067b25f64c3be687eae54ed2bfb9cca819 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Tue, 15 Feb 2022 07:44:48 -0500 Subject: Various updates for Go 1.18 Signed-off-by: Dave Henderson --- strings/strings_fuzz_test.go | 76 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 strings/strings_fuzz_test.go (limited to 'strings/strings_fuzz_test.go') diff --git a/strings/strings_fuzz_test.go b/strings/strings_fuzz_test.go new file mode 100644 index 00000000..e82c4427 --- /dev/null +++ b/strings/strings_fuzz_test.go @@ -0,0 +1,76 @@ +package strings + +import ( + "strings" + "testing" + "unicode" + + "github.com/stretchr/testify/assert" +) + +func FuzzIndent(f *testing.F) { + f.Add(0, " ", "foo\n") + f.Add(1, " ", "bar\nbaz\nqux\n") + f.Add(1, " ", "\n") + f.Add(3, " ", "quux\n") + f.Add(15, "\n0", "\n0") + + f.Fuzz(func(t *testing.T, width int, indent, s string) { + out := Indent(width, indent, s) + + // out should be equal to s when both have the indent character + // completely removed. + assert.Equal(t, + strings.ReplaceAll(s, indent, ""), + strings.ReplaceAll(out, indent, ""), + ) + }) +} + +func FuzzTrunc(f *testing.F) { + f.Add(0, "foo") + + f.Fuzz(func(t *testing.T, length int, s string) { + out := Trunc(length, s) + + assert.True(t, len(out) <= len(s)) + if length >= 0 { + assert.True(t, len(out) <= length) + } + + assert.Equal(t, s[0:len(out)], out) + }) +} + +func FuzzWordWrap(f *testing.F) { + out := `There shouldn't be any wrapping of long words or URLs because that would break +things very badly. To wit: +https://example.com/a/super-long/url/that-shouldnt-be?wrapped=for+fear+of#the-breaking-of-functionality +should appear on its own line, regardless of the desired word-wrapping width +that has been set.` + f.Add(out, "", uint(0)) + f.Add(out, "\n", uint(80)) + f.Add(out, "\v", uint(10)) + + f.Fuzz(func(t *testing.T, in, lbSeq string, width uint) { + for _, r := range lbSeq { + if !unicode.IsSpace(r) { + t.Skip("ignore non-whitespace sequences") + } + } + + out := WordWrap(in, WordWrapOpts{ + LBSeq: lbSeq, + Width: width, + }) + + if lbSeq == "" { + lbSeq = "\n" + } + // compare by stripping both the line-break sequence and spaces + assert.Equal(t, + strings.ReplaceAll(strings.ReplaceAll(in, lbSeq, ""), " ", ""), + strings.ReplaceAll(strings.ReplaceAll(out, lbSeq, ""), " ", ""), + ) + }) +} -- cgit v1.2.3