diff options
author | Vicent Martà <vicent@github.com> | 2012-06-21 20:12:50 -0700 |
---|---|---|
committer | Vicent Martà <vicent@github.com> | 2012-06-21 20:12:50 -0700 |
commit | fb8aa9e11ba47a979b87f9f05c1af6dcefc6f805 (patch) | |
tree | 55164fac2833c091786b830f78c07ec1b049409e /tests-clar | |
parent | 7e912dd659c0e45300307e77a25c7af62b76887c (diff) | |
parent | d4827081ea3ed869c92fcc414bea81be7aa3edfe (diff) | |
download | libgit2-fb8aa9e11ba47a979b87f9f05c1af6dcefc6f805.tar.gz |
Merge pull request #782 from nulltoken/topic/branch-foreach
Branch foreach
Diffstat (limited to 'tests-clar')
-rw-r--r-- | tests-clar/refs/branches/foreach.c | 121 | ||||
-rw-r--r-- | tests-clar/refs/branches/listall.c | 78 |
2 files changed, 121 insertions, 78 deletions
diff --git a/tests-clar/refs/branches/foreach.c b/tests-clar/refs/branches/foreach.c new file mode 100644 index 000000000..60bf50bd5 --- /dev/null +++ b/tests-clar/refs/branches/foreach.c @@ -0,0 +1,121 @@ +#include "clar_libgit2.h" +#include "refs.h" +#include "branch.h" + +static git_repository *repo; +static git_reference *fake_remote; + +void test_refs_branches_foreach__initialize(void) +{ + git_oid id; + + cl_fixture_sandbox("testrepo.git"); + cl_git_pass(git_repository_open(&repo, "testrepo.git")); + + cl_git_pass(git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644")); + cl_git_pass(git_reference_create_oid(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0)); +} + +void test_refs_branches_foreach__cleanup(void) +{ + git_reference_free(fake_remote); + git_repository_free(repo); + + cl_fixture_cleanup("testrepo.git"); +} + +static int count_branch_list_cb(const char *branch_name, git_branch_t branch_type, void *payload) +{ + int *count = (int *)payload; + + (*count)++; + + return 0; +} + +static void assert_retrieval(unsigned int flags, unsigned int expected_count) +{ + int count = 0; + + cl_git_pass(git_branch_foreach(repo, flags, count_branch_list_cb, &count)); + + cl_assert_equal_i(expected_count, count); +} + +void test_refs_branches_foreach__retrieve_all_branches(void) +{ + assert_retrieval(GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE, 9); +} + +void test_refs_branches_foreach__retrieve_remote_branches(void) +{ + assert_retrieval(GIT_BRANCH_REMOTE, 2); +} + +void test_refs_branches_foreach__retrieve_local_branches(void) +{ + assert_retrieval(GIT_BRANCH_LOCAL, 7); +} + +struct expectations { + const char *branch_name; + int encounters; +}; + +static void assert_branch_has_been_found(struct expectations *findings, const char* expected_branch_name) +{ + int pos = 0; + + while (findings[pos].branch_name) + { + if (strcmp(expected_branch_name, findings[pos].branch_name) == 0) { + cl_assert_equal_i(1, findings[pos].encounters); + return; + } + + pos++; + } + + cl_fail("expected branch not found in list."); +} + +static int contains_branch_list_cb(const char *branch_name, git_branch_t branch_type, void *payload) +{ + int pos = 0; + + struct expectations *exp = (struct expectations *)payload; + + while (exp[pos].branch_name) + { + if (strcmp(branch_name, exp[pos].branch_name) == 0) + exp[pos].encounters++; + + pos++; + } + + return 0; +} + +/* + * $ git branch -r + * nulltoken/HEAD -> nulltoken/master + * nulltoken/master + */ +void test_refs_branches_foreach__retrieve_remote_symbolic_HEAD_when_present(void) +{ + struct expectations exp[] = { + { "nulltoken/HEAD", 0 }, + { "nulltoken/master", 0 }, + { NULL, 0 } + }; + + git_reference_free(fake_remote); + cl_git_pass(git_reference_create_symbolic(&fake_remote, repo, "refs/remotes/nulltoken/HEAD", "refs/remotes/nulltoken/master", 0)); + + assert_retrieval(GIT_BRANCH_REMOTE, 3); + + cl_git_pass(git_branch_foreach(repo, GIT_BRANCH_REMOTE, contains_branch_list_cb, &exp)); + + assert_branch_has_been_found(exp, "nulltoken/HEAD"); + assert_branch_has_been_found(exp, "nulltoken/HEAD"); +} diff --git a/tests-clar/refs/branches/listall.c b/tests-clar/refs/branches/listall.c deleted file mode 100644 index 77f8270fb..000000000 --- a/tests-clar/refs/branches/listall.c +++ /dev/null @@ -1,78 +0,0 @@ -#include "clar_libgit2.h" -#include "refs.h" -#include "branch.h" - -static git_repository *repo; -static git_strarray branch_list; -static git_reference *fake_remote; - -void test_refs_branches_listall__initialize(void) -{ - git_oid id; - - cl_fixture_sandbox("testrepo.git"); - cl_git_pass(git_repository_open(&repo, "testrepo.git")); - - cl_git_pass(git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644")); - cl_git_pass(git_reference_create_oid(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0)); -} - -void test_refs_branches_listall__cleanup(void) -{ - git_strarray_free(&branch_list); - git_reference_free(fake_remote); - git_repository_free(repo); - - cl_fixture_cleanup("testrepo.git"); -} - -static void assert_retrieval(unsigned int flags, unsigned int expected_count) -{ - cl_git_pass(git_branch_list(&branch_list, repo, flags)); - - cl_assert_equal_i(branch_list.count, expected_count); -} - -void test_refs_branches_listall__retrieve_all_branches(void) -{ - assert_retrieval(GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE, 9); -} - -void test_refs_branches_listall__retrieve_remote_branches(void) -{ - assert_retrieval(GIT_BRANCH_REMOTE, 2); -} - -void test_refs_branches_listall__retrieve_local_branches(void) -{ - assert_retrieval(GIT_BRANCH_LOCAL, 7); -} - -static void assert_branch_list_contains(git_strarray *branches, const char* expected_branch_name) -{ - unsigned int i; - - for (i = 0; i < branches->count; i++) { - if (strcmp(expected_branch_name, branches->strings[i]) == 0) - return; - } - - cl_fail("expected branch not found in list."); -} - -/* - * $ git branch -r - * nulltoken/HEAD -> nulltoken/master - * nulltoken/master - */ -void test_refs_branches_listall__retrieve_remote_symbolic_HEAD_when_present(void) -{ - git_reference_free(fake_remote); - cl_git_pass(git_reference_create_symbolic(&fake_remote, repo, "refs/remotes/nulltoken/HEAD", "refs/remotes/nulltoken/master", 0)); - - cl_git_pass(git_branch_list(&branch_list, repo, GIT_BRANCH_REMOTE)); - - cl_assert_equal_i(3, branch_list.count); - assert_branch_list_contains(&branch_list, "remotes/nulltoken/HEAD"); - assert_branch_list_contains(&branch_list, "remotes/nulltoken/master"); -} |