summaryrefslogtreecommitdiff
path: root/tests-clar/diff/blob.c
blob: 7aa8ceb22b6a58442e17b384297400dcb1644564 (plain)
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
#include "clar_libgit2.h"
#include "diff_helpers.h"

static git_repository *g_repo = NULL;

void test_diff_blob__initialize(void)
{
	cl_fixture_sandbox("attr");
	cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
	cl_git_pass(p_rename("attr/gitattributes", "attr/.gitattributes"));
	cl_git_pass(git_repository_open(&g_repo, "attr/.git"));
}

void test_diff_blob__cleanup(void)
{
	git_repository_free(g_repo);
	g_repo = NULL;
	cl_fixture_cleanup("attr");
}

void test_diff_blob__0(void)
{
	git_blob *a, *b, *c, *d;
	git_oid a_oid, b_oid, c_oid, d_oid;
	git_diff_options opts = {0};
	diff_expects exp;

	/* 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));

	/* tests/resources/attr/root_test4.txt */
	cl_git_pass(git_oid_fromstrn(&d_oid, "fe773770c5a6", 12));
	cl_git_pass(git_blob_lookup_prefix(&d, g_repo, &d_oid, 6));

	/* Doing the equivalent of a `git diff -U1` on these files */

	opts.context_lines = 1;
	opts.interhunk_lines = 1;

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_blobs(
		g_repo, a, b, &opts, &exp, diff_hunk_fn, diff_line_fn));

	cl_assert(exp.hunks == 1);
	cl_assert(exp.lines == 6);
	cl_assert(exp.line_ctxt == 1);
	cl_assert(exp.line_adds == 5);
	cl_assert(exp.line_dels == 0);

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_blobs(
		g_repo, b, c, &opts, &exp, diff_hunk_fn, diff_line_fn));

	cl_assert(exp.hunks == 1);
	cl_assert(exp.lines == 15);
	cl_assert(exp.line_ctxt == 3);
	cl_assert(exp.line_adds == 9);
	cl_assert(exp.line_dels == 3);

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_blobs(
		g_repo, a, c, &opts, &exp, diff_hunk_fn, diff_line_fn));

	cl_assert(exp.hunks == 1);
	cl_assert(exp.lines == 13);
	cl_assert(exp.line_ctxt == 0);
	cl_assert(exp.line_adds == 12);
	cl_assert(exp.line_dels == 1);

	opts.context_lines = 1;

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_blobs(
		g_repo, c, d, &opts, &exp, diff_hunk_fn, diff_line_fn));

	cl_assert(exp.hunks == 2);
	cl_assert(exp.lines == 14);
	cl_assert(exp.line_ctxt == 4);
	cl_assert(exp.line_adds == 6);
	cl_assert(exp.line_dels == 4);

	git_blob_free(a);
	git_blob_free(b);
	git_blob_free(c);
	git_blob_free(d);
}