]> git.proxmox.com Git - libgit2.git/blame - tests/object/message.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / object / message.c
CommitLineData
458b9450 1#include "clar_libgit2.h"
458b9450 2
3static void assert_message_prettifying(char *expected_output, char *input, int strip_comments)
4{
5 git_buf prettified_message = GIT_BUF_INIT;
6
49e369b2 7 git_message_prettify(&prettified_message, input, strip_comments, '#');
e579e0f7 8 cl_assert_equal_s(expected_output, prettified_message.ptr);
458b9450 9
ac3d33df 10 git_buf_dispose(&prettified_message);
458b9450 11}
12
13#define t40 "A quick brown fox jumps over the lazy do"
14#define s40 " "
ac3d33df
JK
15#define sss s40 s40 s40 s40 s40 s40 s40 s40 s40 s40 /* # 400 */
16#define ttt t40 t40 t40 t40 t40 t40 t40 t40 t40 t40 /* # 400 */
458b9450 17
18/* Ported from git.git */
19/* see https://github.com/git/git/blob/master/t/t0030-stripspace.sh */
20void test_object_message__long_lines_without_spaces_should_be_unchanged(void)
21{
22 assert_message_prettifying(ttt "\n", ttt, 0);
23 assert_message_prettifying(ttt ttt "\n", ttt ttt, 0);
24 assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt, 0);
25 assert_message_prettifying(ttt ttt ttt ttt "\n", ttt ttt ttt ttt, 0);
26}
27
28void test_object_message__lines_with_spaces_at_the_beginning_should_be_unchanged(void)
29{
30 assert_message_prettifying(sss ttt "\n", sss ttt, 0);
31 assert_message_prettifying(sss sss ttt "\n", sss sss ttt, 0);
32 assert_message_prettifying(sss sss sss ttt "\n", sss sss sss ttt, 0);
33}
34
35void test_object_message__lines_with_intermediate_spaces_should_be_unchanged(void)
36{
37 assert_message_prettifying(ttt sss ttt "\n", ttt sss ttt, 0);
38 assert_message_prettifying(ttt sss sss ttt "\n", ttt sss sss ttt, 0);
39}
40
41void test_object_message__consecutive_blank_lines_should_be_unified(void)
42{
43 assert_message_prettifying(ttt "\n\n" ttt "\n", ttt "\n\n\n\n\n" ttt "\n", 0);
44 assert_message_prettifying(ttt ttt "\n\n" ttt "\n", ttt ttt "\n\n\n\n\n" ttt "\n", 0);
45 assert_message_prettifying(ttt ttt ttt "\n\n" ttt "\n", ttt ttt ttt "\n\n\n\n\n" ttt "\n", 0);
46
47 assert_message_prettifying(ttt "\n\n" ttt ttt "\n", ttt "\n\n\n\n\n" ttt ttt "\n", 0);
48 assert_message_prettifying(ttt "\n\n" ttt ttt ttt "\n", ttt "\n\n\n\n\n" ttt ttt ttt "\n", 0);
49
50 assert_message_prettifying(ttt "\n\n" ttt "\n", ttt "\n\t\n \n\n \t\t\n" ttt "\n", 0);
51 assert_message_prettifying(ttt ttt "\n\n" ttt "\n", ttt ttt "\n\t\n \n\n \t\t\n" ttt "\n", 0);
52 assert_message_prettifying(ttt ttt ttt "\n\n" ttt "\n", ttt ttt ttt "\n\t\n \n\n \t\t\n" ttt "\n", 0);
53
54 assert_message_prettifying(ttt "\n\n" ttt ttt "\n", ttt "\n\t\n \n\n \t\t\n" ttt ttt "\n", 0);
55 assert_message_prettifying(ttt "\n\n" ttt ttt ttt "\n", ttt "\n\t\n \n\n \t\t\n" ttt ttt ttt "\n", 0);
56}
57
58void test_object_message__only_consecutive_blank_lines_should_be_completely_removed(void)
59{
60 assert_message_prettifying("", "\n", 0);
61 assert_message_prettifying("", "\n\n\n", 0);
62 assert_message_prettifying("", sss "\n" sss "\n" sss "\n", 0);
63 assert_message_prettifying("", sss sss "\n" sss "\n\n", 0);
64}
65
66void test_object_message__consecutive_blank_lines_at_the_beginning_should_be_removed(void)
67{
68 assert_message_prettifying(ttt "\n", "\n" ttt "\n", 0);
69 assert_message_prettifying(ttt "\n", "\n\n\n" ttt "\n", 0);
70 assert_message_prettifying(ttt ttt "\n", "\n\n\n" ttt ttt "\n", 0);
71 assert_message_prettifying(ttt ttt ttt "\n", "\n\n\n" ttt ttt ttt "\n", 0);
72 assert_message_prettifying(ttt ttt ttt ttt "\n", "\n\n\n" ttt ttt ttt ttt "\n", 0);
73 assert_message_prettifying(ttt "\n", sss "\n" sss "\n" sss "\n" ttt "\n", 0);
74 assert_message_prettifying(ttt "\n", "\n" sss "\n" sss sss "\n" ttt "\n", 0);
75 assert_message_prettifying(ttt "\n", sss sss "\n" sss "\n\n" ttt "\n", 0);
76 assert_message_prettifying(ttt "\n", sss sss sss "\n\n\n" ttt "\n", 0);
77 assert_message_prettifying(ttt "\n", "\n" sss sss sss "\n\n" ttt "\n", 0);
78 assert_message_prettifying(ttt "\n", "\n\n" sss sss sss "\n" ttt "\n", 0);
79}
80
81void test_object_message__consecutive_blank_lines_at_the_end_should_be_removed(void)
82{
83 assert_message_prettifying(ttt "\n", ttt "\n\n", 0);
84 assert_message_prettifying(ttt "\n", ttt "\n\n\n\n", 0);
85 assert_message_prettifying(ttt ttt "\n", ttt ttt "\n\n\n\n", 0);
86 assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt "\n\n\n\n", 0);
87 assert_message_prettifying(ttt ttt ttt ttt "\n", ttt ttt ttt ttt "\n\n\n\n", 0);
88 assert_message_prettifying(ttt "\n", ttt "\n" sss "\n" sss "\n" sss "\n", 0);
89 assert_message_prettifying(ttt "\n", ttt "\n\n" sss "\n" sss sss "\n", 0);
90 assert_message_prettifying(ttt "\n", ttt "\n" sss sss "\n" sss "\n\n", 0);
91 assert_message_prettifying(ttt "\n", ttt "\n" sss sss sss "\n\n\n", 0);
92 assert_message_prettifying(ttt "\n", ttt "\n\n" sss sss sss "\n\n", 0);
93 assert_message_prettifying(ttt "\n", ttt "\n\n\n" sss sss sss "\n\n", 0);
94}
95
96void test_object_message__text_without_newline_at_end_should_end_with_newline(void)
97{
98 assert_message_prettifying(ttt "\n", ttt, 0);
99 assert_message_prettifying(ttt ttt "\n", ttt ttt, 0);
100 assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt, 0);
101 assert_message_prettifying(ttt ttt ttt ttt "\n", ttt ttt ttt ttt, 0);
102}
103
104void test_object_message__text_plus_spaces_without_newline_should_not_show_spaces_and_end_with_newline(void)
105{
106 assert_message_prettifying(ttt "\n", ttt sss, 0);
107 assert_message_prettifying(ttt ttt "\n", ttt ttt sss, 0);
108 assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt sss, 0);
109 assert_message_prettifying(ttt "\n", ttt sss sss, 0);
110 assert_message_prettifying(ttt ttt "\n", ttt ttt sss sss, 0);
111 assert_message_prettifying(ttt "\n", ttt sss sss sss, 0);
112}
113
114void test_object_message__text_plus_spaces_ending_with_newline_should_be_cleaned_and_newline_must_remain(void){
115 assert_message_prettifying(ttt "\n", ttt sss "\n", 0);
116 assert_message_prettifying(ttt "\n", ttt sss sss "\n", 0);
117 assert_message_prettifying(ttt "\n", ttt sss sss sss "\n", 0);
118 assert_message_prettifying(ttt ttt "\n", ttt ttt sss "\n", 0);
119 assert_message_prettifying(ttt ttt "\n", ttt ttt sss sss "\n", 0);
120 assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt sss "\n", 0);
121}
122
123void test_object_message__spaces_with_newline_at_end_should_be_replaced_with_empty_string(void)
124{
125 assert_message_prettifying("", sss "\n", 0);
126 assert_message_prettifying("", sss sss "\n", 0);
127 assert_message_prettifying("", sss sss sss "\n", 0);
128 assert_message_prettifying("", sss sss sss sss "\n", 0);
129}
130
131void test_object_message__spaces_without_newline_at_end_should_be_replaced_with_empty_string(void)
132{
133 assert_message_prettifying("", "", 0);
134 assert_message_prettifying("", sss sss, 0);
135 assert_message_prettifying("", sss sss sss, 0);
136 assert_message_prettifying("", sss sss sss sss, 0);
137}
138
139void test_object_message__consecutive_text_lines_should_be_unchanged(void)
140{
141 assert_message_prettifying(ttt ttt "\n" ttt "\n", ttt ttt "\n" ttt "\n", 0);
142 assert_message_prettifying(ttt "\n" ttt ttt "\n" ttt "\n", ttt "\n" ttt ttt "\n" ttt "\n", 0);
143 assert_message_prettifying(ttt "\n" ttt "\n" ttt "\n" ttt ttt "\n", ttt "\n" ttt "\n" ttt "\n" ttt ttt "\n", 0);
144 assert_message_prettifying(ttt "\n" ttt "\n\n" ttt ttt "\n" ttt "\n", ttt "\n" ttt "\n\n" ttt ttt "\n" ttt "\n", 0);
145 assert_message_prettifying(ttt ttt "\n\n" ttt "\n" ttt ttt "\n", ttt ttt "\n\n" ttt "\n" ttt ttt "\n", 0);
146 assert_message_prettifying(ttt "\n" ttt ttt "\n\n" ttt "\n", ttt "\n" ttt ttt "\n\n" ttt "\n", 0);
147}
148
149void test_object_message__strip_comments(void)
150{
151 assert_message_prettifying("", "# comment", 1);
152 assert_message_prettifying("", "# comment\n", 1);
153 assert_message_prettifying("", "# comment \n", 1);
154
155 assert_message_prettifying(ttt "\n", ttt "\n" "# comment\n", 1);
156 assert_message_prettifying(ttt "\n", "# comment\n" ttt "\n", 1);
157 assert_message_prettifying(ttt "\n" ttt "\n", ttt "\n" "# comment\n" ttt "\n", 1);
158}
159
160void test_object_message__keep_comments(void)
161{
162 assert_message_prettifying("# comment\n", "# comment", 0);
163 assert_message_prettifying("# comment\n", "# comment\n", 0);
164 assert_message_prettifying("# comment\n", "# comment \n", 0);
165
166 assert_message_prettifying(ttt "\n" "# comment\n", ttt "\n" "# comment\n", 0);
167 assert_message_prettifying("# comment\n" ttt "\n", "# comment\n" ttt "\n", 0);
168 assert_message_prettifying(ttt "\n" "# comment\n" ttt "\n", ttt "\n" "# comment\n" ttt "\n", 0);
169}
65d12df5 170
171void test_object_message__message_prettify(void)
172{
e1d7f003
CMN
173 git_buf buffer;
174
175 memset(&buffer, 0, sizeof(buffer));
49e369b2 176 cl_git_pass(git_message_prettify(&buffer, "", 0, '#'));
e1d7f003 177 cl_assert_equal_s(buffer.ptr, "");
ac3d33df 178 git_buf_dispose(&buffer);
49e369b2 179 cl_git_pass(git_message_prettify(&buffer, "", 1, '#'));
e1d7f003 180 cl_assert_equal_s(buffer.ptr, "");
ac3d33df 181 git_buf_dispose(&buffer);
e1d7f003 182
49e369b2 183 cl_git_pass(git_message_prettify(&buffer, "Short", 0, '#'));
e1d7f003 184 cl_assert_equal_s("Short\n", buffer.ptr);
ac3d33df 185 git_buf_dispose(&buffer);
49e369b2 186 cl_git_pass(git_message_prettify(&buffer, "Short", 1, '#'));
e1d7f003 187 cl_assert_equal_s("Short\n", buffer.ptr);
ac3d33df 188 git_buf_dispose(&buffer);
e1d7f003 189
49e369b2 190 cl_git_pass(git_message_prettify(&buffer, "This is longer\nAnd multiline\n# with some comments still in\n", 0, '#'));
e1d7f003 191 cl_assert_equal_s(buffer.ptr, "This is longer\nAnd multiline\n# with some comments still in\n");
ac3d33df 192 git_buf_dispose(&buffer);
e1d7f003 193
49e369b2 194 cl_git_pass(git_message_prettify(&buffer, "This is longer\nAnd multiline\n# with some comments still in\n", 1, '#'));
e1d7f003 195 cl_assert_equal_s(buffer.ptr, "This is longer\nAnd multiline\n");
ac3d33df 196 git_buf_dispose(&buffer);
65d12df5 197}