|
| 1 | +#include "clar_libgit2.h" |
| 2 | +#include "object.h" |
| 3 | +#include "signature.h" |
| 4 | +#include "tag.h" |
| 5 | + |
| 6 | +static void assert_tag_parses(const char *data, size_t datalen, |
| 7 | + const char *expected_oid, |
| 8 | + const char *expected_name, |
| 9 | + const char *expected_tagger, |
| 10 | + const char *expected_message) |
| 11 | +{ |
| 12 | + git_tag *tag; |
| 13 | + |
| 14 | + if (!datalen) |
| 15 | + datalen = strlen(data); |
| 16 | + |
| 17 | + cl_git_pass(git_object__from_raw((git_object **) &tag, data, datalen, GIT_OBJ_TAG)); |
| 18 | + cl_assert_equal_i(tag->type, GIT_OBJ_TAG); |
| 19 | + |
| 20 | + if (expected_oid) { |
| 21 | + git_oid oid; |
| 22 | + cl_git_pass(git_oid_fromstr(&oid, expected_oid)); |
| 23 | + cl_assert_equal_oid(&oid, &tag->target); |
| 24 | + } |
| 25 | + |
| 26 | + if (expected_name) |
| 27 | + cl_assert_equal_s(expected_name, tag->tag_name); |
| 28 | + else |
| 29 | + cl_assert_equal_s(tag->message, NULL); |
| 30 | + |
| 31 | + if (expected_tagger) { |
| 32 | + git_signature *tagger; |
| 33 | + cl_git_pass(git_signature_from_buffer(&tagger, expected_tagger)); |
| 34 | + cl_assert_equal_s(tagger->name, tag->tagger->name); |
| 35 | + cl_assert_equal_s(tagger->email, tag->tagger->email); |
| 36 | + cl_assert_equal_i(tagger->when.time, tag->tagger->when.time); |
| 37 | + cl_assert_equal_i(tagger->when.offset, tag->tagger->when.offset); |
| 38 | + cl_assert_equal_i(tagger->when.sign, tag->tagger->when.sign); |
| 39 | + git_signature_free(tagger); |
| 40 | + } else { |
| 41 | + cl_assert_equal_s(tag->tagger, NULL); |
| 42 | + } |
| 43 | + |
| 44 | + if (expected_message) |
| 45 | + cl_assert_equal_s(expected_message, tag->message); |
| 46 | + else |
| 47 | + cl_assert_equal_s(tag->message, NULL); |
| 48 | + |
| 49 | + git_object__free(&tag->object); |
| 50 | +} |
| 51 | + |
| 52 | +static void assert_tag_fails(const char *data, size_t datalen) |
| 53 | +{ |
| 54 | + git_object *object; |
| 55 | + if (!datalen) |
| 56 | + datalen = strlen(data); |
| 57 | + cl_git_fail(git_object__from_raw(&object, data, datalen, GIT_OBJ_TAG)); |
| 58 | +} |
| 59 | + |
| 60 | +void test_object_tag_parse__valid_tag_parses(void) |
| 61 | +{ |
| 62 | + const char *tag = |
| 63 | + "object a8d447f68076d1520f69649bb52629941be7031f\n" |
| 64 | + "type tag\n" |
| 65 | + "tag tagname\n" |
| 66 | + "tagger Taggy Mr. Taggart <taggy@taggart.com>\n" |
| 67 | + "\n" |
| 68 | + "Message"; |
| 69 | + assert_tag_parses(tag, 0, |
| 70 | + "a8d447f68076d1520f69649bb52629941be7031f", |
| 71 | + "tagname", |
| 72 | + "Taggy Mr. Taggart <taggy@taggart.com>", |
| 73 | + "Message"); |
| 74 | +} |
| 75 | + |
| 76 | +void test_object_tag_parse__missing_tagger_parses(void) |
| 77 | +{ |
| 78 | + const char *tag = |
| 79 | + "object a8d447f68076d1520f69649bb52629941be7031f\n" |
| 80 | + "type tag\n" |
| 81 | + "tag tagname\n" |
| 82 | + "\n" |
| 83 | + "Message"; |
| 84 | + assert_tag_parses(tag, 0, |
| 85 | + "a8d447f68076d1520f69649bb52629941be7031f", |
| 86 | + "tagname", |
| 87 | + NULL, |
| 88 | + "Message"); |
| 89 | +} |
| 90 | + |
| 91 | +void test_object_tag_parse__missing_message_parses(void) |
| 92 | +{ |
| 93 | + const char *tag = |
| 94 | + "object a8d447f68076d1520f69649bb52629941be7031f\n" |
| 95 | + "type tag\n" |
| 96 | + "tag tagname\n" |
| 97 | + "tagger Taggy Mr. Taggart <taggy@taggart.com>\n"; |
| 98 | + assert_tag_parses(tag, 0, |
| 99 | + "a8d447f68076d1520f69649bb52629941be7031f", |
| 100 | + "tagname", |
| 101 | + "Taggy Mr. Taggart <taggy@taggart.com>", |
| 102 | + NULL); |
| 103 | +} |
| 104 | + |
| 105 | +void test_object_tag_parse__unknown_field_parses(void) |
| 106 | +{ |
| 107 | + const char *tag = |
| 108 | + "object a8d447f68076d1520f69649bb52629941be7031f\n" |
| 109 | + "type tag\n" |
| 110 | + "tag tagname\n" |
| 111 | + "tagger Taggy Mr. Taggart <taggy@taggart.com>\n" |
| 112 | + "foo bar\n" |
| 113 | + "frubble frabble\n" |
| 114 | + "\n" |
| 115 | + "Message"; |
| 116 | + assert_tag_parses(tag, 0, |
| 117 | + "a8d447f68076d1520f69649bb52629941be7031f", |
| 118 | + "tagname", |
| 119 | + "Taggy Mr. Taggart <taggy@taggart.com>", |
| 120 | + "Message"); |
| 121 | +} |
| 122 | + |
| 123 | +void test_object_tag_parse__missing_object_fails(void) |
| 124 | +{ |
| 125 | + const char *tag = |
| 126 | + "type tag\n" |
| 127 | + "tag tagname\n" |
| 128 | + "tagger Taggy Mr. Taggart <taggy@taggart.com>\n" |
| 129 | + "\n" |
| 130 | + "Message"; |
| 131 | + assert_tag_fails(tag, 0); |
| 132 | +} |
| 133 | + |
| 134 | +void test_object_tag_parse__malformatted_object_fails(void) |
| 135 | +{ |
| 136 | + const char *tag = |
| 137 | + "object a8d447f68076d15xxxxxxxxxxxxxxxx41be7031f\n" |
| 138 | + "type tag\n" |
| 139 | + "tag tagname\n" |
| 140 | + "tagger Taggy Mr. Taggart <taggy@taggart.com>\n" |
| 141 | + "\n" |
| 142 | + "Message"; |
| 143 | + assert_tag_fails(tag, 0); |
| 144 | +} |
| 145 | + |
| 146 | +void test_object_tag_parse__missing_type_fails(void) |
| 147 | +{ |
| 148 | + const char *tag = |
| 149 | + "object a8d447f68076d1520f69649bb52629941be7031f\n" |
| 150 | + "tag tagname\n" |
| 151 | + "tagger Taggy Mr. Taggart <taggy@taggart.com>\n" |
| 152 | + "\n" |
| 153 | + "Message"; |
| 154 | + assert_tag_fails(tag, 0); |
| 155 | +} |
| 156 | + |
| 157 | +void test_object_tag_parse__invalid_type_fails(void) |
| 158 | +{ |
| 159 | + const char *tag = |
| 160 | + "object a8d447f68076d1520f69649bb52629941be7031f\n" |
| 161 | + "type garbage\n" |
| 162 | + "tag tagname\n" |
| 163 | + "tagger Taggy Mr. Taggart <taggy@taggart.com>\n" |
| 164 | + "\n" |
| 165 | + "Message"; |
| 166 | + assert_tag_fails(tag, 0); |
| 167 | +} |
| 168 | + |
| 169 | +void test_object_tag_parse__missing_tagname_fails(void) |
| 170 | +{ |
| 171 | + const char *tag = |
| 172 | + "object a8d447f68076d1520f69649bb52629941be7031f\n" |
| 173 | + "type tag\n" |
| 174 | + "tagger Taggy Mr. Taggart <taggy@taggart.com>\n" |
| 175 | + "\n" |
| 176 | + "Message"; |
| 177 | + assert_tag_fails(tag, 0); |
| 178 | +} |
| 179 | + |
| 180 | +void test_object_tag_parse__misformatted_tagger_fails(void) |
| 181 | +{ |
| 182 | + const char *tag = |
| 183 | + "object a8d447f68076d1520f69649bb52629941be7031f\n" |
| 184 | + "type tag\n" |
| 185 | + "tag Tag\n" |
| 186 | + "tagger taggy@taggart.com>\n" |
| 187 | + "\n" |
| 188 | + "Message"; |
| 189 | + assert_tag_fails(tag, 0); |
| 190 | +} |
| 191 | + |
| 192 | +void test_object_tag_parse__missing_message_fails(void) |
| 193 | +{ |
| 194 | + const char *tag = |
| 195 | + "object a8d447f68076d1520f69649bb52629941be7031f\n" |
| 196 | + "type tag\n" |
| 197 | + "tag Tag\n" |
| 198 | + "tagger taggy@taggart.com>\n"; |
| 199 | + assert_tag_fails(tag, 0); |
| 200 | +} |
0 commit comments