diff options
| author | Russell Belfer <rb@github.com> | 2012-07-10 15:10:14 -0700 |
|---|---|---|
| committer | Russell Belfer <rb@github.com> | 2012-07-10 23:19:47 -0700 |
| commit | 039fc4067989a14a09784ed16ce7126ac75461cb (patch) | |
| tree | 798a1a34cd1de1dabdd462a5c1a017815d65b309 /tests-clar/core/buffer.c | |
| parent | 6b9a49cd5fd6addc5b9ce3281ea33a50934f5a94 (diff) | |
| download | libgit2-039fc4067989a14a09784ed16ce7126ac75461cb.tar.gz | |
Add a couple of useful git_buf utilities
* `git_buf_rfind` (with tests and tests for `git_buf_rfind_next`)
* `git_buf_puts_escaped` and `git_buf_puts_escaped_regex` (with tests)
to copy strings into a buffer while injecting an escape sequence
(e.g. '\') in front of particular characters.
Diffstat (limited to 'tests-clar/core/buffer.c')
| -rw-r--r-- | tests-clar/core/buffer.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/tests-clar/core/buffer.c b/tests-clar/core/buffer.c index 6a718f45..996cb7bc 100644 --- a/tests-clar/core/buffer.c +++ b/tests-clar/core/buffer.c @@ -611,3 +611,50 @@ void test_core_buffer__11(void) git_buf_free(&a); } + +void test_core_buffer__rfind_variants(void) +{ + git_buf a = GIT_BUF_INIT; + ssize_t len; + + cl_git_pass(git_buf_sets(&a, "/this/is/it/")); + + len = (ssize_t)git_buf_len(&a); + + cl_assert(git_buf_rfind(&a, '/') == len - 1); + cl_assert(git_buf_rfind_next(&a, '/') == len - 4); + + cl_assert(git_buf_rfind(&a, 'i') == len - 3); + cl_assert(git_buf_rfind_next(&a, 'i') == len - 3); + + cl_assert(git_buf_rfind(&a, 'h') == 2); + cl_assert(git_buf_rfind_next(&a, 'h') == 2); + + cl_assert(git_buf_rfind(&a, 'q') == -1); + cl_assert(git_buf_rfind_next(&a, 'q') == -1); + + git_buf_clear(&a); +} + +void test_core_buffer__puts_escaped(void) +{ + git_buf a = GIT_BUF_INIT; + + git_buf_clear(&a); + cl_git_pass(git_buf_puts_escaped(&a, "this is a test", "", "")); + cl_assert_equal_s("this is a test", a.ptr); + + git_buf_clear(&a); + cl_git_pass(git_buf_puts_escaped(&a, "this is a test", "t", "\\")); + cl_assert_equal_s("\\this is a \\tes\\t", a.ptr); + + git_buf_clear(&a); + cl_git_pass(git_buf_puts_escaped(&a, "this is a test", "i ", "__")); + cl_assert_equal_s("th__is__ __is__ a__ test", a.ptr); + + git_buf_clear(&a); + cl_git_pass(git_buf_puts_escape_regex(&a, "^match\\s*[A-Z]+.*")); + cl_assert_equal_s("\\^match\\\\s\\*\\[A-Z\\]\\+\\.\\*", a.ptr); + + git_buf_free(&a); +} |
