aboutsummaryrefslogtreecommitdiff
path: root/src/bytes
diff options
context:
space:
mode:
authorBen Hoyt <benhoyt@gmail.com>2019-03-28 17:49:43 -0400
committerIan Lance Taylor <iant@golang.org>2019-03-29 14:16:35 +0000
commit154e5abfcdb8ffe193d2af4c8ef590f4d9a27008 (patch)
tree2cd1000a508699aba19b11cefbf57e1da7964ca6 /src/bytes
parentddef157813686870e26c3c2c83bd5af6bef71a61 (diff)
downloadgo-154e5abfcdb8ffe193d2af4c8ef590f4d9a27008.tar.gz
go-154e5abfcdb8ffe193d2af4c8ef590f4d9a27008.zip
bytes, strings: add tests for TrimLeftFunc and TrimRightFunc
When I was working on the fix for #31038 (make TrimSpace return nil on all-space input) I noticed that there were no tests for TrimLeftFunc and TrimRightFunc, including the funky nil behavior. So add some! I've just reused the existing TrimFunc test cases for TrimLeftFunc and TrimRightFunc, as well as adding new tests for the empty string and all-trimmed cases (which test the nil-returning behavior of TrimFunc and TrimLeftFunc). Change-Id: Ib580d4364e9b3c91350305f9d9873080d7862904 Reviewed-on: https://go-review.googlesource.com/c/go/+/170061 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/bytes')
-rw-r--r--src/bytes/bytes_test.go75
1 files changed, 63 insertions, 12 deletions
diff --git a/src/bytes/bytes_test.go b/src/bytes/bytes_test.go
index a29ad5f3a0..4b000a3d2b 100644
--- a/src/bytes/bytes_test.go
+++ b/src/bytes/bytes_test.go
@@ -1261,8 +1261,11 @@ var isValidRune = predicate{
}
type TrimFuncTest struct {
- f predicate
- in, out string
+ f predicate
+ in string
+ trimOut []byte
+ leftOut []byte
+ rightOut []byte
}
func not(p predicate) predicate {
@@ -1275,20 +1278,68 @@ func not(p predicate) predicate {
}
var trimFuncTests = []TrimFuncTest{
- {isSpace, space + " hello " + space, "hello"},
- {isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"},
- {isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"},
- {not(isSpace), "hello" + space + "hello", space},
- {not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo", "\u0e50\u0e521234\u0e50\u0e51"},
- {isValidRune, "ab\xc0a\xc0cd", "\xc0a\xc0"},
- {not(isValidRune), "\xc0a\xc0", "a"},
+ {isSpace, space + " hello " + space,
+ []byte("hello"),
+ []byte("hello " + space),
+ []byte(space + " hello")},
+ {isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51",
+ []byte("hello"),
+ []byte("hello34\u0e50\u0e51"),
+ []byte("\u0e50\u0e5212hello")},
+ {isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F",
+ []byte("hello"),
+ []byte("helloEF\u2C6F\u2C6FGH\u2C6F\u2C6F"),
+ []byte("\u2C6F\u2C6F\u2C6F\u2C6FABCDhello")},
+ {not(isSpace), "hello" + space + "hello",
+ []byte(space),
+ []byte(space + "hello"),
+ []byte("hello" + space)},
+ {not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo",
+ []byte("\u0e50\u0e521234\u0e50\u0e51"),
+ []byte("\u0e50\u0e521234\u0e50\u0e51helo"),
+ []byte("hello\u0e50\u0e521234\u0e50\u0e51")},
+ {isValidRune, "ab\xc0a\xc0cd",
+ []byte("\xc0a\xc0"),
+ []byte("\xc0a\xc0cd"),
+ []byte("ab\xc0a\xc0")},
+ {not(isValidRune), "\xc0a\xc0",
+ []byte("a"),
+ []byte("a\xc0"),
+ []byte("\xc0a")},
+ // The nils returned by TrimLeftFunc are odd behavior, but we need
+ // to preserve backwards compatibility.
+ {isSpace, "",
+ nil,
+ nil,
+ []byte("")},
+ {isSpace, " ",
+ nil,
+ nil,
+ []byte("")},
}
func TestTrimFunc(t *testing.T) {
for _, tc := range trimFuncTests {
- actual := string(TrimFunc([]byte(tc.in), tc.f.f))
- if actual != tc.out {
- t.Errorf("TrimFunc(%q, %q) = %q; want %q", tc.in, tc.f.name, actual, tc.out)
+ trimmers := []struct {
+ name string
+ trim func(s []byte, f func(r rune) bool) []byte
+ out []byte
+ }{
+ {"TrimFunc", TrimFunc, tc.trimOut},
+ {"TrimLeftFunc", TrimLeftFunc, tc.leftOut},
+ {"TrimRightFunc", TrimRightFunc, tc.rightOut},
+ }
+ for _, trimmer := range trimmers {
+ actual := trimmer.trim([]byte(tc.in), tc.f.f)
+ if actual == nil && trimmer.out != nil {
+ t.Errorf("%s(%q, %q) = nil; want %q", trimmer.name, tc.in, tc.f.name, trimmer.out)
+ }
+ if actual != nil && trimmer.out == nil {
+ t.Errorf("%s(%q, %q) = %q; want nil", trimmer.name, tc.in, tc.f.name, actual)
+ }
+ if !Equal(actual, trimmer.out) {
+ t.Errorf("%s(%q, %q) = %q; want %q", trimmer.name, tc.in, tc.f.name, actual, trimmer.out)
+ }
}
}
}