[Gd-chatter] r11650 - in trunk/libraries/strings: . tests
cgay at gwydiondylan.org
cgay at gwydiondylan.org
Mon Jan 21 22:42:23 CET 2008
Author: cgay
Date: Mon Jan 21 22:42:22 2008
New Revision: 11650
Modified:
trunk/libraries/strings/strings.dylan
trunk/libraries/strings/tests/strings-test-suite.dylan
Log:
job: minor
fix split bug. add more tests.
Modified: trunk/libraries/strings/strings.dylan
==============================================================================
--- trunk/libraries/strings/strings.dylan (original)
+++ trunk/libraries/strings/strings.dylan Mon Jan 21 22:42:22 2008
@@ -870,7 +870,7 @@
// bounds whenever possible. (In particular it may not always be
// possible when the separator is a regex.)
define method split
- (seq :: <sequence>, separator :: <function>,
+ (seq :: <sequence>, find-separator :: <function>,
#key start :: <integer> = 0,
end: _end :: false-or(<integer>),
count :: false-or(<integer>))
@@ -884,7 +884,7 @@
let num-parts :: <integer> = 0;
let separator-end = #f;
while (bpos & bpos < epos & num-parts < max-parts)
- let (sep-start, sep-end) = separator(seq, bpos, epos);
+ let (sep-start, sep-end) = find-separator(seq, bpos, epos);
if (sep-start)
parts := add!(parts, copy-sequence(seq, start: bpos, end: sep-start));
separator-end := sep-end;
@@ -918,7 +918,7 @@
let epos :: <integer> = epos | seq.size;
let max-separator-start :: <integer> = epos - separator.size;
block (exit-loop)
- for (seq-index from bpos below max-separator-start)
+ for (seq-index from bpos to max-separator-start)
if (looking-at?(separator, seq, seq-index))
exit-loop(seq-index, seq-index + separator.size);
end;
@@ -975,10 +975,9 @@
#f
end
end method find-regex;
- split(seq, find-regex, start: start, end: _end, count: count);
+ split(seq, find-regex, start: start, end: _end, count: count)
end method split;
-
// todo -- should this be exported?
define method looking-at?
(pattern :: <byte-string>, big :: <byte-string>, bpos :: <integer>)
Modified: trunk/libraries/strings/tests/strings-test-suite.dylan
==============================================================================
--- trunk/libraries/strings/tests/strings-test-suite.dylan (original)
+++ trunk/libraries/strings/tests/strings-test-suite.dylan Mon Jan 21 22:42:22 2008
@@ -94,8 +94,8 @@
list(as(<unicode-string>, ""), #f)))
let (val, expected-result) = apply(values, item);
check-equal(fmt("byte-string?(%=) => %=", val, expected-result),
- byte-string?(val),
- expected-result);
+ expected-result,
+ byte-string?(val));
end for;
end function-test byte-string?;
@@ -107,10 +107,10 @@
#("Abc", "Abc"),
#("one two", "One Two"),
#("_one,two", "_One,Two")))
- let (before, after) = apply(values, item);
+ let (before, expected) = apply(values, item);
check-equal(fmt("capitalize %=", before),
- capitalize(before),
- after);
+ expected,
+ capitalize(before));
end;
end function-test capitalize;
@@ -121,10 +121,10 @@
#("Abc", "Abc"),
#("one two", "One Two"),
#("_one,two", "_One,Two")))
- let (before, after) = apply(values, map(copy-sequence, item));
+ let (before, expected) = apply(values, map(copy-sequence, item));
check-equal(fmt("capitalize! %=", before),
- capitalize!(before),
- after);
+ expected,
+ capitalize!(before));
check-true(fmt("capitalize! %= retains identity", before),
capitalize!(before) == before);
end;
@@ -145,8 +145,8 @@
#("ABC", "abc"),
#("ONE TWO", "one two"),
#("_oNe,Two", "_one,two")))
- let (before, after) = apply(values, map(copy-sequence, item));
- check-equal(fmt("lowercase! %=", before), lowercase!(before), after);
+ let (before, expected) = apply(values, map(copy-sequence, item));
+ check-equal(fmt("lowercase! %=", before), expected, lowercase!(before));
check-true(fmt("lowercase! %= retains identity", before),
lowercase!(before) == before);
end;
@@ -159,8 +159,8 @@
#("ABC", "abc"),
#("ONE TWO", "one two"),
#("_oNe,Two", "_one,two")))
- let (before, after) = apply(values, item);
- check-equal(fmt("lowercase %=", before), lowercase(before), after);
+ let (before, expected) = apply(values, item);
+ check-equal(fmt("lowercase %=", before), expected, lowercase(before));
end;
end function-test lowercase;
@@ -171,8 +171,8 @@
#("abc", "ABC"),
#("one two", "ONE TWO"),
#("_oNe,Two", "_ONE,TWO")))
- let (before, after) = apply(values, map(copy-sequence, item));
- check-equal(fmt("uppercase! %=", before), uppercase!(before), after);
+ let (before, expected) = apply(values, map(copy-sequence, item));
+ check-equal(fmt("uppercase! %=", before), expected, uppercase!(before));
check-true(fmt("uppercase! %= retains identity", before),
uppercase!(before) == before);
end;
@@ -185,8 +185,8 @@
#("abc", "ABC"),
#("one two", "ONE TWO"),
#("_oNe,Two", "_ONE,TWO")))
- let (before, after) = apply(values, item);
- check-equal(fmt("uppercase %=", before), uppercase(before), after);
+ let (before, expected) = apply(values, item);
+ check-equal(fmt("uppercase %=", before), expected, uppercase(before));
end;
end function-test uppercase;
@@ -200,24 +200,30 @@
list(" a ", " a ", test:, method (c) #f end),
list("o", "xox", test:, method (c) c == 'x' end)
))
- let (after, before, #rest trim-args) = apply(values, item);
+ let (expected, before, #rest trim-args) = apply(values, item);
check-equal(fmt("trim %=", before),
- apply(trim, before, trim-args),
- after);
+ expected,
+ apply(trim, before, trim-args));
end for;
end function-test trim;
define strings function-test join ()
let abc = #["a", "b", "c"];
- check-equal("join one element", join(#["foo"], "-"), "foo");
- check-equal("join with empty separator", join(abc, ""), "abc");
- check-equal("join with non-empty separator", join(abc, "-"), "a-b-c");
+ check-equal("join one element",
+ "foo",
+ join(#["foo"], "-"));
+ check-equal("join with empty separator",
+ "abc",
+ join(abc, ""));
+ check-equal("join with non-empty separator",
+ "a-b-c",
+ join(abc, "-"));
check-equal("join with conjunction",
- join(abc, ", ", conjunction: " and "),
- "a, b and c");
+ "a, b and c",
+ join(abc, ", ", conjunction: " and "));
check-equal("join with conjunction and key",
- join(abc, ", ", conjunction: " and ", key: uppercase),
- "A, B and C");
+ "A, B and C",
+ join(abc, ", ", conjunction: " and ", key: uppercase));
check-condition("join an empty sequence is an error",
<error>,
join(#[], "-"));
@@ -234,45 +240,62 @@
define strings function-test split ()
// Tests for basic functionality with no keyword args
check-equal("split empty string with another string",
- split("", "-"),
- #[""]);
+ #[""],
+ split("", "-"));
check-equal("split empty sequence",
- split(#(), #t),
- #[#()]);
+ #[#()],
+ split(#(), #t));
check-equal("basic split on string separator",
- split("a b c", " "),
- #["a", "b", "c"]);
+ #["a", "b", "c"],
+ split("a b c", " "));
check-equal("basic split on object separator",
- split("a b c", ' '),
- #["a", "b", "c"]);
+ #["a", "b", "c"],
+ split("a b c", ' '));
check-equal("back-to-back separators",
- split("a b", ' '),
- #["a", "", "b"]);
+ #["a", "", "b"],
+ split("a b", ' '));
check-equal("separators on the ends",
- split(" x ", ' '),
- #["", "x", ""]);
+ #["", "x", ""],
+ split(" x ", ' '));
+ check-equal("split on entire string",
+ #["", ""],
+ split("abc", "abc"));
+ check-equal("split on a unfound separator",
+ #["abc"],
+ split("abc", "-"));
+ check-equal("split on something longer than entire string",
+ #["abc"],
+ split("abc", "abcd"));
// Tests for the count argument.
check-equal("basic count test",
- split("a,b,c,d", ',', count: 1),
- #["a", "b,c,d"]);
+ #["a", "b,c,d"],
+ split("a,b,c,d", ',', count: 1));
check-equal("basic count test",
- split("a,b,c,d", ',', count: 2),
- #["a", "b", "c,d"]);
+ #["a", "b", "c,d"],
+ split("a,b,c,d", ',', count: 2));
// Tests for the start and end arguments
check-equal("basic start/end test",
- split("a b c d", ' ', start: 1, end: 6),
- #["", "b", "c", ""]);
+ #["", "b", "c", ""],
+ split("a b c d", ' ', start: 1, end: 6));
check-equal("basic start/end test",
- split("a b c d", ' ', start: 1),
- #["", "b", "c", "d"]);
+ #["", "b", "c", "d"],
+ split("a b c d", ' ', start: 1));
// Tests for splitting on regular expressions
check-equal("basic regex split",
- split("a b c", compile-regex(" ")),
- #["a", "b", "c"]);
-
+ #["a", "b", "c"],
+ split("a b c", compile-regex(" ")));
+ check-equal("split on whitespace regex",
+ #["a", "b", "c"],
+ split("a b\t\n\fc", compile-regex("\\s+")));
+ check-equal("split on unfound regex",
+ #["abc"],
+ split("abc", compile-regex(" ")));
+ check-equal("split on entire string found by regex",
+ #["", ""],
+ split("abc", compile-regex("^.*$")));
end function-test split;
define function replacement-test (mutating?)
More information about the chatter
mailing list