diff options
author | Russell Belfer <rb@github.com> | 2012-08-04 04:08:01 +0400 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2012-08-04 04:08:01 +0400 |
commit | 5dca201072724e4230141796d7c9f8836a277de8 (patch) | |
tree | 7eb6b8f3e08c8abc608ee43c0ddc32d4d043a2c6 /tests-clar/diff/index.c | |
parent | 2031760c626711cc69b4d63ac9798ff333583ca0 (diff) |
Update iterators for consistency across library
This updates all the `foreach()` type functions across the library
that take callbacks from the user to have a consistent behavior.
The rules are:
* A callback terminates the loop by returning any non-zero value
* Once the callback returns non-zero, it will not be called again
(i.e. the loop stops all iteration regardless of state)
* If the callback returns non-zero, the parent fn returns GIT_EUSER
* Although the parent returns GIT_EUSER, no error will be set in
the library and `giterr_last()` will return NULL if called.
This commit makes those changes across the library and adds tests
for most of the iteration APIs to make sure that they follow the
above rules.
Diffstat (limited to 'tests-clar/diff/index.c')
-rw-r--r-- | tests-clar/diff/index.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/tests-clar/diff/index.c b/tests-clar/diff/index.c index 171815df5..89e65e3b7 100644 --- a/tests-clar/diff/index.c +++ b/tests-clar/diff/index.c @@ -90,3 +90,53 @@ void test_diff_index__0(void) git_tree_free(a); git_tree_free(b); } + +static int diff_stop_after_2_files( + void *cb_data, + git_diff_delta *delta, + float progress) +{ + diff_expects *e = cb_data; + + GIT_UNUSED(progress); + GIT_UNUSED(delta); + + e->files++; + + return (e->files == 2); +} + +void test_diff_index__1(void) +{ + /* grabbed a couple of commit oids from the history of the attr repo */ + const char *a_commit = "26a125ee1bf"; /* the current HEAD */ + const char *b_commit = "0017bd4ab1ec3"; /* the start */ + git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit); + git_tree *b = resolve_commit_oid_to_tree(g_repo, b_commit); + git_diff_options opts = {0}; + git_diff_list *diff = NULL; + diff_expects exp; + + cl_assert(a); + cl_assert(b); + + opts.context_lines = 1; + opts.interhunk_lines = 1; + + memset(&exp, 0, sizeof(exp)); + + cl_git_pass(git_diff_index_to_tree(g_repo, &opts, a, &diff)); + + cl_assert_equal_i( + GIT_EUSER, + git_diff_foreach(diff, &exp, diff_stop_after_2_files, NULL, NULL) + ); + + cl_assert(exp.files == 2); + + git_diff_list_free(diff); + diff = NULL; + + git_tree_free(a); + git_tree_free(b); +} |