diff options
author | Michael Hoisie <hoisie@gmail.com> | 2010-05-18 23:01:05 -0700 |
---|---|---|
committer | Michael Hoisie <hoisie@gmail.com> | 2010-05-18 23:01:05 -0700 |
commit | f27fb53aa1aae00db1078056d72a0709b0118fd9 (patch) | |
tree | c31ef47d6a51cd941ce1435fe615c5d5cc6dd22c /src/pkg/bytes/bytes_test.go | |
parent | eb763b17e60460869dd2895fde5a7475991fa94c (diff) | |
download | golang-f27fb53aa1aae00db1078056d72a0709b0118fd9.tar.gz |
bytes: add Trim, TrimLeft, TrimRight, and generic functions
R=rsc, r
CC=golang-dev
http://codereview.appspot.com/946045
Committer: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/pkg/bytes/bytes_test.go')
-rw-r--r-- | src/pkg/bytes/bytes_test.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/pkg/bytes/bytes_test.go b/src/pkg/bytes/bytes_test.go index 139404421..2bea1737f 100644 --- a/src/pkg/bytes/bytes_test.go +++ b/src/pkg/bytes/bytes_test.go @@ -576,3 +576,72 @@ func TestRunes(t *testing.T) { } } } + + +type TrimTest struct { + f func([]byte, string) []byte + in, cutset, out string +} + +var trimTests = []TrimTest{ + TrimTest{Trim, "abba", "a", "bb"}, + TrimTest{Trim, "abba", "ab", ""}, + TrimTest{TrimLeft, "abba", "ab", ""}, + TrimTest{TrimRight, "abba", "ab", ""}, + TrimTest{TrimLeft, "abba", "a", "bba"}, + TrimTest{TrimRight, "abba", "a", "abb"}, + TrimTest{Trim, "<tag>", "<>", "tag"}, + TrimTest{Trim, "* listitem", " *", "listitem"}, + TrimTest{Trim, `"quote"`, `"`, "quote"}, + TrimTest{Trim, "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"}, + //empty string tests + TrimTest{Trim, "abba", "", "abba"}, + TrimTest{Trim, "", "123", ""}, + TrimTest{Trim, "", "", ""}, + TrimTest{TrimLeft, "abba", "", "abba"}, + TrimTest{TrimLeft, "", "123", ""}, + TrimTest{TrimLeft, "", "", ""}, + TrimTest{TrimRight, "abba", "", "abba"}, + TrimTest{TrimRight, "", "123", ""}, + TrimTest{TrimRight, "", "", ""}, +} + +func TestTrim(t *testing.T) { + for _, tc := range trimTests { + actual := string(tc.f([]byte(tc.in), tc.cutset)) + var name string + switch tc.f { + case Trim: + name = "Trim" + case TrimLeft: + name = "TrimLeft" + case TrimRight: + name = "TrimRight" + default: + t.Error("Undefined trim function") + } + if actual != tc.out { + t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.cutset, actual, tc.out) + } + } +} + +type TrimFuncTest struct { + f func(r int) bool + name, in, out string +} + +var trimFuncTests = []TrimFuncTest{ + TrimFuncTest{unicode.IsSpace, "IsSpace", space + " hello " + space, "hello"}, + TrimFuncTest{unicode.IsDigit, "IsDigit", "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"}, + TrimFuncTest{unicode.IsUpper, "IsUpper", "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"}, +} + +func TestTrimFunc(t *testing.T) { + for _, tc := range trimFuncTests { + actual := string(TrimFunc([]byte(tc.in), tc.f)) + if actual != tc.out { + t.Errorf("TrimFunc(%q, %q) = %q; want %q", tc.in, tc.name, actual, tc.out) + } + } +} |