Hash :
a3337f10
Author :
Date :
2012-12-17T15:15:20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
#include "clar_libgit2.h"
#include "diff_helpers.h"
static git_repository *g_repo = NULL;
static diff_expects expected;
static git_diff_options opts;
static git_blob *d, *alien;
void test_diff_blob__initialize(void)
{
git_oid oid;
g_repo = cl_git_sandbox_init("attr");
GIT_INIT_STRUCTURE(&opts, GIT_DIFF_OPTIONS_VERSION);
opts.context_lines = 1;
opts.interhunk_lines = 0;
memset(&expected, 0, sizeof(expected));
/* tests/resources/attr/root_test4.txt */
cl_git_pass(git_oid_fromstrn(&oid, "a0f7217a", 8));
cl_git_pass(git_blob_lookup_prefix(&d, g_repo, &oid, 4));
/* alien.png */
cl_git_pass(git_oid_fromstrn(&oid, "edf3dcee", 8));
cl_git_pass(git_blob_lookup_prefix(&alien, g_repo, &oid, 4));
}
void test_diff_blob__cleanup(void)
{
git_blob_free(d);
d = NULL;
git_blob_free(alien);
alien = NULL;
cl_git_sandbox_cleanup();
}
void test_diff_blob__can_compare_text_blobs(void)
{
git_blob *a, *b, *c;
git_oid a_oid, b_oid, c_oid;
/* tests/resources/attr/root_test1 */
cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8));
cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4));
/* tests/resources/attr/root_test2 */
cl_git_pass(git_oid_fromstrn(&b_oid, "4d713dc4", 8));
cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 4));
/* tests/resources/attr/root_test3 */
cl_git_pass(git_oid_fromstrn(&c_oid, "c96bbb2c2557a832", 16));
cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 8));
/* Doing the equivalent of a `git diff -U1` on these files */
/* diff on tests/resources/attr/root_test1 */
cl_git_pass(git_diff_blobs(
a, b, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(1, expected.files);
cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, expected.files_binary);
cl_assert_equal_i(1, expected.hunks);
cl_assert_equal_i(6, expected.lines);
cl_assert_equal_i(1, expected.line_ctxt);
cl_assert_equal_i(5, expected.line_adds);
cl_assert_equal_i(0, expected.line_dels);
/* diff on tests/resources/attr/root_test2 */
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
b, c, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(1, expected.files);
cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, expected.files_binary);
cl_assert_equal_i(1, expected.hunks);
cl_assert_equal_i(15, expected.lines);
cl_assert_equal_i(3, expected.line_ctxt);
cl_assert_equal_i(9, expected.line_adds);
cl_assert_equal_i(3, expected.line_dels);
/* diff on tests/resources/attr/root_test3 */
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
a, c, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(1, expected.files);
cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, expected.files_binary);
cl_assert_equal_i(1, expected.hunks);
cl_assert_equal_i(13, expected.lines);
cl_assert_equal_i(0, expected.line_ctxt);
cl_assert_equal_i(12, expected.line_adds);
cl_assert_equal_i(1, expected.line_dels);
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
c, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(1, expected.files);
cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, expected.files_binary);
cl_assert_equal_i(2, expected.hunks);
cl_assert_equal_i(14, expected.lines);
cl_assert_equal_i(4, expected.line_ctxt);
cl_assert_equal_i(6, expected.line_adds);
cl_assert_equal_i(4, expected.line_dels);
git_blob_free(a);
git_blob_free(b);
git_blob_free(c);
}
void test_diff_blob__can_compare_against_null_blobs(void)
{
git_blob *e = NULL;
cl_git_pass(git_diff_blobs(
d, e, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(1, expected.files);
cl_assert_equal_i(1, expected.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(0, expected.files_binary);
cl_assert_equal_i(1, expected.hunks);
cl_assert_equal_i(14, expected.hunk_old_lines);
cl_assert_equal_i(14, expected.lines);
cl_assert_equal_i(14, expected.line_dels);
opts.flags |= GIT_DIFF_REVERSE;
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
d, e, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(1, expected.files);
cl_assert_equal_i(1, expected.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, expected.files_binary);
cl_assert_equal_i(1, expected.hunks);
cl_assert_equal_i(14, expected.hunk_new_lines);
cl_assert_equal_i(14, expected.lines);
cl_assert_equal_i(14, expected.line_adds);
opts.flags ^= GIT_DIFF_REVERSE;
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
alien, NULL, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(1, expected.files);
cl_assert_equal_i(1, expected.files_binary);
cl_assert_equal_i(1, expected.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(0, expected.hunks);
cl_assert_equal_i(0, expected.lines);
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
NULL, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(1, expected.files);
cl_assert_equal_i(1, expected.files_binary);
cl_assert_equal_i(1, expected.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, expected.hunks);
cl_assert_equal_i(0, expected.lines);
}
static void assert_identical_blobs_comparison(diff_expects *expected)
{
cl_assert_equal_i(1, expected->files);
cl_assert_equal_i(1, expected->file_status[GIT_DELTA_UNMODIFIED]);
cl_assert_equal_i(0, expected->hunks);
cl_assert_equal_i(0, expected->lines);
}
void test_diff_blob__can_compare_identical_blobs(void)
{
cl_git_pass(git_diff_blobs(
d, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(0, expected.files_binary);
assert_identical_blobs_comparison(&expected);
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
NULL, NULL, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(0, expected.files_binary);
assert_identical_blobs_comparison(&expected);
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
alien, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert(expected.files_binary > 0);
assert_identical_blobs_comparison(&expected);
}
static void assert_binary_blobs_comparison(diff_expects *expected)
{
cl_assert(expected->files_binary > 0);
cl_assert_equal_i(1, expected->files);
cl_assert_equal_i(1, expected->file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, expected->hunks);
cl_assert_equal_i(0, expected->lines);
}
void test_diff_blob__can_compare_two_binary_blobs(void)
{
git_blob *heart;
git_oid h_oid;
/* heart.png */
cl_git_pass(git_oid_fromstrn(&h_oid, "de863bff", 8));
cl_git_pass(git_blob_lookup_prefix(&heart, g_repo, &h_oid, 4));
cl_git_pass(git_diff_blobs(
alien, heart, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_binary_blobs_comparison(&expected);
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
heart, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_binary_blobs_comparison(&expected);
git_blob_free(heart);
}
void test_diff_blob__can_compare_a_binary_blob_and_a_text_blob(void)
{
cl_git_pass(git_diff_blobs(
alien, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_binary_blobs_comparison(&expected);
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
d, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_binary_blobs_comparison(&expected);
}
/*
* $ git diff fe773770 a0f7217
* diff --git a/fe773770 b/a0f7217
* index fe77377..a0f7217 100644
* --- a/fe773770
* +++ b/a0f7217
* @@ -1,6 +1,6 @@
* Here is some stuff at the start
*
* -This should go in one hunk
* +This should go in one hunk (first)
*
* Some additional lines
*
* @@ -8,7 +8,7 @@ Down here below the other lines
*
* With even more at the end
*
* -Followed by a second hunk of stuff
* +Followed by a second hunk of stuff (second)
*
* That happens down here
*/
void test_diff_blob__comparing_two_text_blobs_honors_interhunkcontext(void)
{
git_blob *old_d;
git_oid old_d_oid;
opts.context_lines = 3;
/* tests/resources/attr/root_test1 from commit f5b0af1 */
cl_git_pass(git_oid_fromstrn(&old_d_oid, "fe773770", 8));
cl_git_pass(git_blob_lookup_prefix(&old_d, g_repo, &old_d_oid, 4));
/* Test with default inter-hunk-context (not set) => default is 0 */
cl_git_pass(git_diff_blobs(
old_d, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(2, expected.hunks);
/* Test with inter-hunk-context explicitly set to 0 */
opts.interhunk_lines = 0;
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
old_d, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(2, expected.hunks);
/* Test with inter-hunk-context explicitly set to 1 */
opts.interhunk_lines = 1;
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
old_d, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(1, expected.hunks);
git_blob_free(old_d);
}
void test_diff_blob__checks_options_version_too_low(void)
{
const git_error *err;
opts.version = 0;
cl_git_fail(git_diff_blobs(
d, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
err = giterr_last();
cl_assert_equal_i(GITERR_INVALID, err->klass);
}
void test_diff_blob__checks_options_version_too_high(void)
{
const git_error *err;
opts.version = 1024;
cl_git_fail(git_diff_blobs(
d, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
err = giterr_last();
cl_assert_equal_i(GITERR_INVALID, err->klass);
}
void test_diff_blob__can_correctly_detect_a_binary_blob_as_binary(void)
{
/* alien.png */
cl_assert_equal_i(true, git_blob_is_binary(alien));
}
void test_diff_blob__can_correctly_detect_a_textual_blob_as_non_binary(void)
{
/* tests/resources/attr/root_test4.txt */
cl_assert_equal_i(false, git_blob_is_binary(d));
}