diff options
| author | Dave Henderson <dhenderson@gmail.com> | 2022-02-15 07:44:48 -0500 |
|---|---|---|
| committer | Dave Henderson <dhenderson@gmail.com> | 2022-03-22 09:23:40 -0400 |
| commit | 599121067b25f64c3be687eae54ed2bfb9cca819 (patch) | |
| tree | fd2d943f7ca1274c3b4606b04496f3f4542c9c8a /strings/strings_fuzz_test.go | |
| parent | 1a084d95b7567f7e2da33e61c3e4168f656a22b5 (diff) | |
Various updates for Go 1.18
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'strings/strings_fuzz_test.go')
| -rw-r--r-- | strings/strings_fuzz_test.go | 76 |
1 files changed, 76 insertions, 0 deletions
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, ""), " ", ""), + ) + }) +} |
