]>
Commit | Line | Data |
---|---|---|
3a437590 RB |
1 | #include "clar_libgit2.h" |
2 | #include "diff_helpers.h" | |
3 | ||
4 | static git_repository *g_repo = NULL; | |
4f806761 | 5 | static diff_expects exp; |
6 | static git_diff_options opts; | |
28ef7f9b | 7 | static git_blob *d, *alien; |
3a437590 RB |
8 | |
9 | void test_diff_blob__initialize(void) | |
10 | { | |
28ef7f9b | 11 | git_oid oid; |
4f806761 | 12 | |
854eccbb | 13 | g_repo = cl_git_sandbox_init("attr"); |
4f806761 | 14 | |
15 | memset(&opts, 0, sizeof(opts)); | |
16 | opts.context_lines = 1; | |
17 | opts.interhunk_lines = 1; | |
18 | ||
19 | memset(&exp, 0, sizeof(exp)); | |
20 | ||
21 | /* tests/resources/attr/root_test4.txt */ | |
28ef7f9b | 22 | cl_git_pass(git_oid_fromstrn(&oid, "fe773770c5a6", 12)); |
23 | cl_git_pass(git_blob_lookup_prefix(&d, g_repo, &oid, 6)); | |
24 | ||
25 | /* alien.png */ | |
26 | cl_git_pass(git_oid_fromstrn(&oid, "edf3dcee", 8)); | |
27 | cl_git_pass(git_blob_lookup_prefix(&alien, g_repo, &oid, 4)); | |
3a437590 RB |
28 | } |
29 | ||
30 | void test_diff_blob__cleanup(void) | |
31 | { | |
4f806761 | 32 | git_blob_free(d); |
28ef7f9b | 33 | git_blob_free(alien); |
4f806761 | 34 | |
854eccbb | 35 | cl_git_sandbox_cleanup(); |
3a437590 RB |
36 | } |
37 | ||
245c5eae | 38 | void test_diff_blob__can_compare_text_blobs(void) |
3a437590 | 39 | { |
4f806761 | 40 | git_blob *a, *b, *c; |
41 | git_oid a_oid, b_oid, c_oid; | |
3a437590 RB |
42 | |
43 | /* tests/resources/attr/root_test1 */ | |
44 | cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8)); | |
45 | cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4)); | |
46 | ||
47 | /* tests/resources/attr/root_test2 */ | |
48 | cl_git_pass(git_oid_fromstrn(&b_oid, "4d713dc4", 8)); | |
49 | cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 4)); | |
50 | ||
51 | /* tests/resources/attr/root_test3 */ | |
52 | cl_git_pass(git_oid_fromstrn(&c_oid, "c96bbb2c2557a832", 16)); | |
53 | cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 8)); | |
54 | ||
3a437590 RB |
55 | /* Doing the equivalent of a `git diff -U1` on these files */ |
56 | ||
3a437590 | 57 | cl_git_pass(git_diff_blobs( |
28ef7f9b | 58 | a, b, &opts, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); |
59 | ||
60 | cl_assert(exp.files == 1); | |
61 | cl_assert(exp.file_mods == 1); | |
62 | cl_assert(exp.at_least_one_of_them_is_binary == false); | |
3a437590 RB |
63 | |
64 | cl_assert(exp.hunks == 1); | |
65 | cl_assert(exp.lines == 6); | |
66 | cl_assert(exp.line_ctxt == 1); | |
67 | cl_assert(exp.line_adds == 5); | |
68 | cl_assert(exp.line_dels == 0); | |
69 | ||
70 | memset(&exp, 0, sizeof(exp)); | |
71 | cl_git_pass(git_diff_blobs( | |
28ef7f9b | 72 | b, c, &opts, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); |
73 | ||
74 | cl_assert(exp.files == 1); | |
75 | cl_assert(exp.file_mods == 1); | |
76 | cl_assert(exp.at_least_one_of_them_is_binary == false); | |
3a437590 RB |
77 | |
78 | cl_assert(exp.hunks == 1); | |
79 | cl_assert(exp.lines == 15); | |
80 | cl_assert(exp.line_ctxt == 3); | |
81 | cl_assert(exp.line_adds == 9); | |
82 | cl_assert(exp.line_dels == 3); | |
83 | ||
84 | memset(&exp, 0, sizeof(exp)); | |
85 | cl_git_pass(git_diff_blobs( | |
28ef7f9b | 86 | a, c, &opts, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); |
87 | ||
88 | cl_assert(exp.files == 1); | |
89 | cl_assert(exp.file_mods == 1); | |
90 | cl_assert(exp.at_least_one_of_them_is_binary == false); | |
3a437590 RB |
91 | |
92 | cl_assert(exp.hunks == 1); | |
93 | cl_assert(exp.lines == 13); | |
94 | cl_assert(exp.line_ctxt == 0); | |
95 | cl_assert(exp.line_adds == 12); | |
96 | cl_assert(exp.line_dels == 1); | |
97 | ||
98 | opts.context_lines = 1; | |
99 | ||
100 | memset(&exp, 0, sizeof(exp)); | |
101 | cl_git_pass(git_diff_blobs( | |
28ef7f9b | 102 | c, d, &opts, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); |
103 | ||
104 | cl_assert(exp.files == 1); | |
105 | cl_assert(exp.file_mods == 1); | |
106 | cl_assert(exp.at_least_one_of_them_is_binary == false); | |
3a437590 RB |
107 | |
108 | cl_assert(exp.hunks == 2); | |
109 | cl_assert(exp.lines == 14); | |
110 | cl_assert(exp.line_ctxt == 4); | |
111 | cl_assert(exp.line_adds == 6); | |
112 | cl_assert(exp.line_dels == 4); | |
113 | ||
114 | git_blob_free(a); | |
115 | git_blob_free(b); | |
116 | git_blob_free(c); | |
3a437590 RB |
117 | } |
118 | ||
4f806761 | 119 | void test_diff_blob__can_compare_against_null_blobs(void) |
120 | { | |
121 | git_blob *e = NULL; | |
122 | ||
123 | cl_git_pass(git_diff_blobs( | |
28ef7f9b | 124 | d, e, &opts, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); |
125 | ||
126 | cl_assert(exp.files == 1); | |
127 | cl_assert(exp.file_dels == 1); | |
128 | cl_assert(exp.at_least_one_of_them_is_binary == false); | |
4f806761 | 129 | |
130 | cl_assert(exp.hunks == 1); | |
131 | cl_assert(exp.hunk_old_lines == 14); | |
132 | cl_assert(exp.lines == 14); | |
133 | cl_assert(exp.line_dels == 14); | |
134 | ||
135 | opts.flags |= GIT_DIFF_REVERSE; | |
136 | memset(&exp, 0, sizeof(exp)); | |
137 | ||
138 | cl_git_pass(git_diff_blobs( | |
28ef7f9b | 139 | d, e, &opts, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); |
140 | ||
141 | cl_assert(exp.files == 1); | |
142 | cl_assert(exp.file_adds == 1); | |
143 | cl_assert(exp.at_least_one_of_them_is_binary == false); | |
4f806761 | 144 | |
145 | cl_assert(exp.hunks == 1); | |
146 | cl_assert(exp.hunk_new_lines == 14); | |
147 | cl_assert(exp.lines == 14); | |
148 | cl_assert(exp.line_adds == 14); | |
28ef7f9b | 149 | |
150 | opts.flags ^= GIT_DIFF_REVERSE; | |
151 | memset(&exp, 0, sizeof(exp)); | |
152 | ||
153 | cl_git_pass(git_diff_blobs( | |
154 | alien, NULL, &opts, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
155 | ||
156 | cl_assert(exp.at_least_one_of_them_is_binary == true); | |
157 | ||
158 | cl_assert(exp.files == 1); | |
159 | cl_assert(exp.file_dels == 1); | |
160 | cl_assert(exp.hunks == 0); | |
161 | cl_assert(exp.lines == 0); | |
162 | ||
163 | memset(&exp, 0, sizeof(exp)); | |
164 | ||
165 | cl_git_pass(git_diff_blobs( | |
166 | NULL, alien, &opts, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
167 | ||
168 | cl_assert(exp.at_least_one_of_them_is_binary == true); | |
169 | ||
170 | cl_assert(exp.files == 1); | |
171 | cl_assert(exp.file_adds == 1); | |
172 | cl_assert(exp.hunks == 0); | |
173 | cl_assert(exp.lines == 0); | |
174 | } | |
175 | ||
176 | void assert_binary_blobs_comparison(diff_expects exp) | |
177 | { | |
178 | cl_assert(exp.at_least_one_of_them_is_binary == true); | |
179 | ||
180 | cl_assert(exp.files == 1); | |
181 | cl_assert(exp.file_mods == 1); | |
182 | cl_assert(exp.hunks == 0); | |
183 | cl_assert(exp.lines == 0); | |
184 | } | |
185 | ||
186 | void test_diff_blob__can_compare_two_binary_blobs(void) | |
187 | { | |
188 | git_blob *heart; | |
189 | git_oid h_oid; | |
190 | ||
191 | /* heart.png */ | |
192 | cl_git_pass(git_oid_fromstrn(&h_oid, "de863bff", 8)); | |
193 | cl_git_pass(git_blob_lookup_prefix(&heart, g_repo, &h_oid, 4)); | |
194 | ||
195 | cl_git_pass(git_diff_blobs( | |
196 | alien, heart, &opts, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
197 | ||
198 | assert_binary_blobs_comparison(exp); | |
199 | ||
200 | memset(&exp, 0, sizeof(exp)); | |
201 | ||
202 | cl_git_pass(git_diff_blobs( | |
203 | heart, alien, &opts, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
204 | ||
205 | assert_binary_blobs_comparison(exp); | |
206 | ||
207 | git_blob_free(heart); | |
208 | } | |
209 | ||
210 | void test_diff_blob__can_compare_a_binary_blob_and_a_text_blob(void) | |
211 | { | |
212 | cl_git_pass(git_diff_blobs( | |
213 | alien, d, &opts, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
214 | ||
215 | assert_binary_blobs_comparison(exp); | |
216 | ||
217 | memset(&exp, 0, sizeof(exp)); | |
218 | ||
219 | cl_git_pass(git_diff_blobs( | |
220 | d, alien, &opts, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
221 | ||
222 | assert_binary_blobs_comparison(exp); | |
4f806761 | 223 | } |