Improve StringIterator test coverage
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/bf12331c Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/bf12331c Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/bf12331c Branch: refs/heads/master Commit: bf12331c6e4f87d87dc5f50af894e6def88072d8 Parents: fbef0da Author: Nick Wellnhofer <[email protected]> Authored: Sat May 14 19:52:30 2016 +0200 Committer: Nick Wellnhofer <[email protected]> Committed: Sun May 15 17:57:34 2016 +0200 ---------------------------------------------------------------------- runtime/core/Clownfish/Test/TestString.c | 81 +++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/bf12331c/runtime/core/Clownfish/Test/TestString.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Test/TestString.c b/runtime/core/Clownfish/Test/TestString.c index f57b3a6..d89b5fe 100644 --- a/runtime/core/Clownfish/Test/TestString.c +++ b/runtime/core/Clownfish/Test/TestString.c @@ -26,6 +26,7 @@ #include "Clownfish/Boolean.h" #include "Clownfish/ByteBuf.h" #include "Clownfish/CharBuf.h" +#include "Clownfish/Err.h" #include "Clownfish/Test.h" #include "Clownfish/TestHarness/TestBatchRunner.h" #include "Clownfish/TestHarness/TestUtils.h" @@ -545,6 +546,9 @@ test_iterator(TestBatchRunner *runner) { StringIterator *top = Str_Top(string); StringIterator *tail = Str_Tail(string); + TEST_FALSE(runner, StrIter_Equals(top, (Obj*)tail), + "StrIter_Equals returns false"); + TEST_INT_EQ(runner, StrIter_Compare_To(top, (Obj*)tail), -1, "Compare_To top < tail"); TEST_INT_EQ(runner, StrIter_Compare_To(tail, (Obj*)top), 1, @@ -668,6 +672,17 @@ test_iterator_whitespace(TestBatchRunner *runner) { DECREF(ws_smiley); } +typedef struct { + StringIterator *top; + StringIterator *tail; +} StrIterCropContext; + +static void +S_striter_crop(void *vcontext) { + StrIterCropContext *context = (StrIterCropContext*)vcontext; + StrIter_crop(context->top, context->tail); +} + static void test_iterator_substring(TestBatchRunner *runner) { String *string = Str_newf("a%sb%sc%sd", smiley, smiley, smiley); @@ -687,7 +702,9 @@ test_iterator_substring(TestBatchRunner *runner) { { String *substring = StrIter_crop(start, end); - String *wanted = Str_newf("b%sc", smiley); + static const char wanted_buf[] = "b" SMILEY "c"; + static const size_t wanted_size = sizeof(wanted_buf) - 1; + String *wanted = Str_new_from_utf8(wanted_buf, wanted_size); TEST_TRUE(runner, Str_Equals(substring, (Obj*)wanted), "StrIter_crop"); @@ -695,23 +712,45 @@ test_iterator_substring(TestBatchRunner *runner) { "Starts_With returns true"); TEST_TRUE(runner, StrIter_Ends_With(end, wanted), "Ends_With returns true"); + TEST_TRUE(runner, + StrIter_Starts_With_Utf8(start, wanted_buf, wanted_size), + "Starts_With_Utf8 returns true"); + TEST_TRUE(runner, + StrIter_Ends_With_Utf8(end, wanted_buf, wanted_size), + "Ends_With_Utf8 returns true"); DECREF(wanted); DECREF(substring); } { - String *short_str = Str_newf("b%sx", smiley); + static const char short_buf[] = "b" SMILEY "x"; + static const size_t short_size = sizeof(short_buf) - 1; + String *short_str = Str_new_from_utf8(short_buf, short_size); TEST_FALSE(runner, StrIter_Starts_With(start, short_str), "Starts_With returns false"); TEST_FALSE(runner, StrIter_Ends_With(start, short_str), "Ends_With returns false"); - - String *long_str = Str_newf("b%sxxxxxxxxxxxx%sc", smiley, smiley); + TEST_FALSE(runner, + StrIter_Starts_With_Utf8(start, short_buf, short_size), + "Starts_With_Utf8 returns false"); + TEST_FALSE(runner, + StrIter_Ends_With_Utf8(start, short_buf, short_size), + "Ends_With_Utf8 returns false"); + + static const char long_buf[] = "b" SMILEY "xxxxxxxxxxxx" SMILEY "c"; + static const size_t long_size = sizeof(long_buf) - 1; + String *long_str = Str_new_from_utf8(long_buf, long_size); TEST_FALSE(runner, StrIter_Starts_With(start, long_str), "Starts_With long string returns false"); TEST_FALSE(runner, StrIter_Ends_With(end, long_str), "Ends_With long string returns false"); + TEST_FALSE(runner, + StrIter_Starts_With_Utf8(start, long_buf, long_size), + "Starts_With_Utf8 long string returns false"); + TEST_FALSE(runner, + StrIter_Ends_With_Utf8(end, long_buf, long_size), + "Ends_With_Utf8 long string returns false"); DECREF(short_str); DECREF(long_str); @@ -735,6 +774,38 @@ test_iterator_substring(TestBatchRunner *runner) { DECREF(substring); } + { + StrIterCropContext context; + context.top = NULL; + context.tail = NULL; + Err *error = Err_trap(S_striter_crop, &context); + TEST_TRUE(runner, error != NULL, + "StrIter_crop throws if top and tail are NULL"); + DECREF(error); + } + + { + String *other = SSTR_WRAP_C("other"); + StrIterCropContext context; + context.top = start; + context.tail = Str_Tail(other); + Err *error = Err_trap(S_striter_crop, &context); + TEST_TRUE(runner, error != NULL, + "StrIter_crop throws if string don't match"); + DECREF(error); + DECREF(context.tail); + } + + { + StrIterCropContext context; + context.top = end; + context.tail = start; + Err *error = Err_trap(S_striter_crop, &context); + TEST_TRUE(runner, error != NULL, + "StrIter_crop throws if top is behind tail"); + DECREF(error); + } + DECREF(start); DECREF(end); DECREF(string); @@ -742,7 +813,7 @@ test_iterator_substring(TestBatchRunner *runner) { void TestStr_Run_IMP(TestString *self, TestBatchRunner *runner) { - TestBatchRunner_Plan(runner, (TestBatch*)self, 148); + TestBatchRunner_Plan(runner, (TestBatch*)self, 158); test_new(runner); test_Cat(runner); test_Clone(runner);
