{"TrimSuffix", "aabb", "b", "aab"},
}
+type TrimNilTest struct {
+ f string
+ in []byte
+ arg string
+ out []byte
+}
+
+var trimNilTests = []TrimNilTest{
+ {"Trim", nil, "", nil},
+ {"Trim", []byte{}, "", nil},
+ {"Trim", []byte{'a'}, "a", nil},
+ {"Trim", []byte{'a', 'a'}, "a", nil},
+ {"Trim", []byte{'a'}, "ab", nil},
+ {"Trim", []byte{'a', 'b'}, "ab", nil},
+ {"Trim", []byte("☺"), "☺", nil},
+ {"TrimLeft", nil, "", nil},
+ {"TrimLeft", []byte{}, "", nil},
+ {"TrimLeft", []byte{'a'}, "a", nil},
+ {"TrimLeft", []byte{'a', 'a'}, "a", nil},
+ {"TrimLeft", []byte{'a'}, "ab", nil},
+ {"TrimLeft", []byte{'a', 'b'}, "ab", nil},
+ {"TrimLeft", []byte("☺"), "☺", nil},
+ {"TrimRight", nil, "", nil},
+ {"TrimRight", []byte{}, "", []byte{}},
+ {"TrimRight", []byte{'a'}, "a", []byte{}},
+ {"TrimRight", []byte{'a', 'a'}, "a", []byte{}},
+ {"TrimRight", []byte{'a'}, "ab", []byte{}},
+ {"TrimRight", []byte{'a', 'b'}, "ab", []byte{}},
+ {"TrimRight", []byte("☺"), "☺", []byte{}},
+ {"TrimPrefix", nil, "", nil},
+ {"TrimPrefix", []byte{}, "", []byte{}},
+ {"TrimPrefix", []byte{'a'}, "a", []byte{}},
+ {"TrimPrefix", []byte("☺"), "☺", []byte{}},
+ {"TrimSuffix", nil, "", nil},
+ {"TrimSuffix", []byte{}, "", []byte{}},
+ {"TrimSuffix", []byte{'a'}, "a", []byte{}},
+ {"TrimSuffix", []byte("☺"), "☺", []byte{}},
+}
+
func TestTrim(t *testing.T) {
- for _, tc := range trimTests {
- name := tc.f
- var f func([]byte, string) []byte
- var fb func([]byte, []byte) []byte
+ toFn := func(name string) (func([]byte, string) []byte, func([]byte, []byte) []byte) {
switch name {
case "Trim":
- f = Trim
+ return Trim, nil
case "TrimLeft":
- f = TrimLeft
+ return TrimLeft, nil
case "TrimRight":
- f = TrimRight
+ return TrimRight, nil
case "TrimPrefix":
- fb = TrimPrefix
+ return nil, TrimPrefix
case "TrimSuffix":
- fb = TrimSuffix
+ return nil, TrimSuffix
default:
t.Errorf("Undefined trim function %s", name)
+ return nil, nil
+ }
+ }
+
+ for _, tc := range trimTests {
+ name := tc.f
+ f, fb := toFn(name)
+ if f == nil && fb == nil {
+ continue
}
var actual string
if f != nil {
t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
}
}
+
+ for _, tc := range trimNilTests {
+ name := tc.f
+ f, fb := toFn(name)
+ if f == nil && fb == nil {
+ continue
+ }
+ var actual []byte
+ if f != nil {
+ actual = f(tc.in, tc.arg)
+ } else {
+ actual = fb(tc.in, []byte(tc.arg))
+ }
+ report := func(s []byte) string {
+ if s == nil {
+ return "nil"
+ } else {
+ return fmt.Sprintf("%q", s)
+ }
+ }
+ if len(actual) != 0 {
+ t.Errorf("%s(%s, %q) returned non-empty value", name, report(tc.in), tc.arg)
+ } else {
+ actualNil := actual == nil
+ outNil := tc.out == nil
+ if actualNil != outNil {
+ t.Errorf("%s(%s, %q) got nil %t; want nil %t", name, report(tc.in), tc.arg, actualNil, outNil)
+ }
+ }
+ }
}
type predicate struct {