diff options
author | Russell Belfer <rb@github.com> | 2013-01-07 15:44:22 -0800 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2013-01-07 15:44:22 -0800 |
commit | f2b7f7a6cb8678763ada70e75d5b38407770e269 (patch) | |
tree | 0fca2a3ed081c1aa3e98679468f1ca273d1a3280 /tests-clar/diff/blob.c | |
parent | f6234cd994ad01fb3aa8c2f0fd8e3d2cf89cf3f2 (diff) | |
download | libgit2-f2b7f7a6cb8678763ada70e75d5b38407770e269.tar.gz |
Share git_diff_blobs/git_diff_blob_to_buffer code
This moves the implementation of these two APIs into common code
that will be shared between the two. Also, this adds tests for
the `git_diff_blob_to_buffer` API. Lastly, this adds some extra
`const` to a few places that can use it.
Diffstat (limited to 'tests-clar/diff/blob.c')
-rw-r--r-- | tests-clar/diff/blob.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/tests-clar/diff/blob.c b/tests-clar/diff/blob.c index 8300cb716..4b29c9c94 100644 --- a/tests-clar/diff/blob.c +++ b/tests-clar/diff/blob.c @@ -347,3 +347,81 @@ 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)); } + +/* + * git_diff_blob_to_buffer tests + */ + +void test_diff_blob__can_compare_blob_to_buffer(void) +{ + git_blob *a; + git_oid a_oid; + const char *a_content = "Hello from the root\n"; + const char *b_content = "Hello from the root\n\nSome additional lines\n\nDown here below\n\n"; + + /* 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)); + + /* diff from blob a to content of b */ + cl_git_pass(git_diff_blob_to_buffer( + a, b_content, strlen(b_content), + &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 from blob a to content of a */ + memset(&expected, 0, sizeof(expected)); + cl_git_pass(git_diff_blob_to_buffer( + a, a_content, strlen(a_content), + &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); + + assert_identical_blobs_comparison(&expected); + + /* diff from NULL blob to content of b */ + memset(&expected, 0, sizeof(expected)); + cl_git_pass(git_diff_blob_to_buffer( + NULL, a_content, strlen(a_content), + &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(1, expected.hunks); + cl_assert_equal_i(1, expected.lines); + cl_assert_equal_i(1, expected.line_adds); + + /* diff from blob a to NULL buffer */ + memset(&expected, 0, sizeof(expected)); + cl_git_pass(git_diff_blob_to_buffer( + a, NULL, 0, + &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(1, expected.hunks); + cl_assert_equal_i(1, expected.lines); + cl_assert_equal_i(1, expected.line_dels); + + /* diff with reverse */ + opts.flags ^= GIT_DIFF_REVERSE; + + memset(&expected, 0, sizeof(expected)); + cl_git_pass(git_diff_blob_to_buffer( + a, NULL, 0, + &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(1, expected.hunks); + cl_assert_equal_i(1, expected.lines); + cl_assert_equal_i(1, expected.line_adds); + + git_blob_free(a); +} |