]>
Commit | Line | Data |
---|---|---|
f73f760e SC |
1 | #include "clar_libgit2.h" |
2 | ||
3 | #include "tag.h" | |
4 | ||
5 | static git_repository *g_repo; | |
6 | ||
7604ddbf | 7 | #define MAX_USED_TAGS 6 |
e800bbe8 SC |
8 | |
9 | struct pattern_match_t | |
10 | { | |
daa70138 SC |
11 | const char* pattern; |
12 | const size_t expected_matches; | |
13 | const char* expected_results[MAX_USED_TAGS]; | |
e800bbe8 SC |
14 | }; |
15 | ||
ac3d33df | 16 | /* Helpers */ |
f73f760e | 17 | static void ensure_tag_pattern_match(git_repository *repo, |
daa70138 | 18 | const struct pattern_match_t* data) |
f73f760e | 19 | { |
daa70138 SC |
20 | int already_found[MAX_USED_TAGS] = { 0 }; |
21 | git_strarray tag_list; | |
22 | int error = 0; | |
23 | size_t sucessfully_found = 0; | |
24 | size_t i, j; | |
25 | ||
26 | cl_assert(data->expected_matches <= MAX_USED_TAGS); | |
27 | ||
28 | if ((error = git_tag_list_match(&tag_list, data->pattern, repo)) < 0) | |
29 | goto exit; | |
30 | ||
31 | if (tag_list.count != data->expected_matches) | |
32 | { | |
33 | error = GIT_ERROR; | |
34 | goto exit; | |
35 | } | |
36 | ||
ac3d33df | 37 | /* we have to be prepared that tags come in any order. */ |
daa70138 SC |
38 | for (i = 0; i < tag_list.count; i++) |
39 | { | |
40 | for (j = 0; j < data->expected_matches; j++) | |
41 | { | |
42 | if (!already_found[j] && !strcmp(data->expected_results[j], tag_list.strings[i])) | |
43 | { | |
44 | already_found[j] = 1; | |
45 | sucessfully_found++; | |
46 | break; | |
47 | } | |
48 | } | |
49 | } | |
50 | cl_assert_equal_i((int)sucessfully_found, (int)data->expected_matches); | |
f73f760e SC |
51 | |
52 | exit: | |
22a2d3d5 | 53 | git_strarray_dispose(&tag_list); |
daa70138 | 54 | cl_git_pass(error); |
f73f760e SC |
55 | } |
56 | ||
ac3d33df | 57 | /* Fixture setup and teardown */ |
f73f760e SC |
58 | void test_object_tag_list__initialize(void) |
59 | { | |
daa70138 | 60 | g_repo = cl_git_sandbox_init("testrepo"); |
f73f760e SC |
61 | } |
62 | ||
63 | void test_object_tag_list__cleanup(void) | |
64 | { | |
daa70138 | 65 | cl_git_sandbox_cleanup(); |
f73f760e SC |
66 | } |
67 | ||
68 | void test_object_tag_list__list_all(void) | |
69 | { | |
ac3d33df | 70 | /* list all tag names from the repository */ |
daa70138 | 71 | git_strarray tag_list; |
f73f760e | 72 | |
daa70138 | 73 | cl_git_pass(git_tag_list(&tag_list, g_repo)); |
f73f760e | 74 | |
daa70138 | 75 | cl_assert_equal_i((int)tag_list.count, 6); |
f73f760e | 76 | |
22a2d3d5 | 77 | git_strarray_dispose(&tag_list); |
f73f760e SC |
78 | } |
79 | ||
e800bbe8 | 80 | static const struct pattern_match_t matches[] = { |
ac3d33df | 81 | /* All tags, including a packed one and two namespaced ones. */ |
daa70138 | 82 | { "", 6, { "e90810b", "point_to_blob", "test", "packed-tag", "foo/bar", "foo/foo/bar" } }, |
45949b37 | 83 | |
ac3d33df | 84 | /* beginning with */ |
daa70138 | 85 | { "t*", 1, { "test" } }, |
45949b37 | 86 | |
ac3d33df | 87 | /* ending with */ |
daa70138 | 88 | { "*b", 2, { "e90810b", "point_to_blob" } }, |
45949b37 | 89 | |
ac3d33df | 90 | /* exact match */ |
daa70138 SC |
91 | { "e", 0 }, |
92 | { "e90810b", 1, { "e90810b" } }, | |
45949b37 | 93 | |
ac3d33df | 94 | /* either or */ |
daa70138 | 95 | { "e90810[ab]", 1, { "e90810b" } }, |
45949b37 | 96 | |
ac3d33df | 97 | /* glob in the middle */ |
daa70138 | 98 | { "foo/*/bar", 1, { "foo/foo/bar" } }, |
45949b37 | 99 | |
ac3d33df JK |
100 | /* |
101 | * The matching of '*' is based on plain string matching analog to the regular expression ".*" | |
102 | * => a '/' in the tag name has no special meaning. | |
103 | * Compare to `git tag -l "*bar"` | |
104 | */ | |
8469219e SC |
105 | { "*bar", 2, { "foo/bar", "foo/foo/bar" } }, |
106 | ||
ac3d33df | 107 | /* End of list */ |
daa70138 | 108 | { NULL } |
e800bbe8 SC |
109 | }; |
110 | ||
f73f760e SC |
111 | void test_object_tag_list__list_by_pattern(void) |
112 | { | |
ac3d33df | 113 | /* list all tag names from the repository matching a specified pattern */ |
daa70138 SC |
114 | size_t i = 0; |
115 | while (matches[i].pattern) | |
116 | ensure_tag_pattern_match(g_repo, &matches[i++]); | |
f73f760e | 117 | } |