message.c 9 KB
Newer Older
1 2 3 4 5 6 7 8
#include "clar_libgit2.h"
#include "buffer.h"
#include "message.h"

static void assert_message_prettifying(char *expected_output, char *input, int strip_comments)
{
	git_buf prettified_message = GIT_BUF_INIT;

9
	git_message_prettify(&prettified_message, input, strip_comments, '#');
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
	cl_assert_equal_s(expected_output, git_buf_cstr(&prettified_message));

	git_buf_free(&prettified_message);
}

#define t40 "A quick brown fox jumps over the lazy do"
#define s40 "                                        "
#define sss s40 s40 s40 s40 s40 s40 s40 s40 s40 s40 // # 400
#define ttt t40 t40 t40 t40 t40 t40 t40 t40 t40 t40 // # 400

/* Ported from git.git */
/* see https://github.com/git/git/blob/master/t/t0030-stripspace.sh */
void test_object_message__long_lines_without_spaces_should_be_unchanged(void)
{
	assert_message_prettifying(ttt "\n", ttt, 0);
	assert_message_prettifying(ttt ttt "\n", ttt ttt, 0);
	assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt, 0);
	assert_message_prettifying(ttt ttt ttt ttt "\n", ttt ttt ttt ttt, 0);
}

void test_object_message__lines_with_spaces_at_the_beginning_should_be_unchanged(void)
{
	assert_message_prettifying(sss ttt "\n", sss ttt, 0);
	assert_message_prettifying(sss sss ttt "\n", sss sss ttt, 0);
	assert_message_prettifying(sss sss sss ttt "\n", sss sss sss ttt, 0);
}

void test_object_message__lines_with_intermediate_spaces_should_be_unchanged(void)
{
	assert_message_prettifying(ttt sss ttt "\n", ttt sss ttt, 0);
	assert_message_prettifying(ttt sss sss ttt "\n", ttt sss sss ttt, 0);
}

void test_object_message__consecutive_blank_lines_should_be_unified(void)
{
	assert_message_prettifying(ttt "\n\n" ttt "\n", ttt "\n\n\n\n\n" ttt "\n", 0);
	assert_message_prettifying(ttt ttt "\n\n" ttt "\n", ttt ttt "\n\n\n\n\n" ttt "\n", 0);
	assert_message_prettifying(ttt ttt ttt "\n\n" ttt "\n", ttt ttt ttt "\n\n\n\n\n" ttt "\n", 0);

	assert_message_prettifying(ttt "\n\n" ttt ttt "\n", ttt "\n\n\n\n\n" ttt ttt "\n", 0);
	assert_message_prettifying(ttt "\n\n" ttt ttt ttt "\n", ttt "\n\n\n\n\n" ttt ttt ttt "\n", 0);

	assert_message_prettifying(ttt "\n\n" ttt "\n", ttt "\n\t\n \n\n  \t\t\n" ttt "\n", 0);
	assert_message_prettifying(ttt ttt "\n\n" ttt "\n", ttt ttt "\n\t\n \n\n  \t\t\n" ttt "\n", 0);
	assert_message_prettifying(ttt ttt ttt "\n\n" ttt "\n", ttt ttt ttt "\n\t\n \n\n  \t\t\n" ttt "\n", 0);

	assert_message_prettifying(ttt "\n\n" ttt ttt "\n", ttt "\n\t\n \n\n  \t\t\n" ttt ttt "\n", 0);
	assert_message_prettifying(ttt "\n\n" ttt ttt ttt "\n", ttt "\n\t\n \n\n  \t\t\n" ttt ttt ttt "\n", 0);
}

void test_object_message__only_consecutive_blank_lines_should_be_completely_removed(void)
{
	assert_message_prettifying("", "\n", 0);
	assert_message_prettifying("", "\n\n\n", 0);
	assert_message_prettifying("", sss "\n" sss "\n" sss "\n", 0);
	assert_message_prettifying("", sss sss "\n" sss "\n\n", 0);
}

void test_object_message__consecutive_blank_lines_at_the_beginning_should_be_removed(void)
{
	assert_message_prettifying(ttt "\n", "\n" ttt "\n", 0);
	assert_message_prettifying(ttt "\n", "\n\n\n" ttt "\n", 0);
	assert_message_prettifying(ttt ttt "\n", "\n\n\n" ttt ttt "\n", 0);
	assert_message_prettifying(ttt ttt ttt "\n", "\n\n\n" ttt ttt ttt "\n", 0);
	assert_message_prettifying(ttt ttt ttt ttt "\n", "\n\n\n" ttt ttt ttt ttt "\n", 0);
	assert_message_prettifying(ttt "\n", sss "\n" sss "\n" sss "\n" ttt "\n", 0);
	assert_message_prettifying(ttt "\n", "\n" sss "\n" sss sss "\n" ttt "\n", 0);
	assert_message_prettifying(ttt "\n", sss sss "\n" sss "\n\n" ttt "\n", 0);
	assert_message_prettifying(ttt "\n", sss sss sss "\n\n\n" ttt "\n", 0);
	assert_message_prettifying(ttt "\n", "\n" sss sss sss "\n\n" ttt "\n", 0);
	assert_message_prettifying(ttt "\n", "\n\n" sss sss sss "\n" ttt "\n", 0);
}

void test_object_message__consecutive_blank_lines_at_the_end_should_be_removed(void)
{
	assert_message_prettifying(ttt "\n", ttt "\n\n", 0);
	assert_message_prettifying(ttt "\n", ttt "\n\n\n\n", 0);
	assert_message_prettifying(ttt ttt "\n", ttt ttt "\n\n\n\n", 0);
	assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt "\n\n\n\n", 0);
	assert_message_prettifying(ttt ttt ttt ttt "\n", ttt ttt ttt ttt "\n\n\n\n", 0);
	assert_message_prettifying(ttt "\n", ttt "\n" sss "\n" sss "\n" sss "\n", 0);
	assert_message_prettifying(ttt "\n", ttt "\n\n" sss "\n" sss sss "\n", 0);
	assert_message_prettifying(ttt "\n", ttt "\n" sss sss "\n" sss "\n\n", 0);
	assert_message_prettifying(ttt "\n", ttt "\n" sss sss sss "\n\n\n", 0);
	assert_message_prettifying(ttt "\n", ttt "\n\n" sss sss sss "\n\n", 0);
	assert_message_prettifying(ttt "\n", ttt "\n\n\n" sss sss sss "\n\n", 0);
}

void test_object_message__text_without_newline_at_end_should_end_with_newline(void)
{
	assert_message_prettifying(ttt "\n", ttt, 0);
	assert_message_prettifying(ttt ttt "\n", ttt ttt, 0);
	assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt, 0);
	assert_message_prettifying(ttt ttt ttt ttt "\n", ttt ttt ttt ttt, 0);
}

void test_object_message__text_plus_spaces_without_newline_should_not_show_spaces_and_end_with_newline(void)
{
	assert_message_prettifying(ttt "\n", ttt sss, 0);
	assert_message_prettifying(ttt ttt "\n", ttt ttt sss, 0);
	assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt sss, 0);
	assert_message_prettifying(ttt "\n", ttt sss sss, 0);
	assert_message_prettifying(ttt ttt "\n", ttt ttt sss sss, 0);
	assert_message_prettifying(ttt "\n", ttt sss sss sss, 0);
}

void test_object_message__text_plus_spaces_ending_with_newline_should_be_cleaned_and_newline_must_remain(void){
	assert_message_prettifying(ttt "\n", ttt sss "\n", 0);
	assert_message_prettifying(ttt "\n", ttt sss sss "\n", 0);
	assert_message_prettifying(ttt "\n", ttt sss sss sss "\n", 0);
	assert_message_prettifying(ttt ttt "\n", ttt ttt sss "\n", 0);
	assert_message_prettifying(ttt ttt "\n", ttt ttt sss sss "\n", 0);
	assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt sss "\n", 0);
}

void test_object_message__spaces_with_newline_at_end_should_be_replaced_with_empty_string(void)
{
	assert_message_prettifying("", sss "\n", 0);
	assert_message_prettifying("", sss sss "\n", 0);
	assert_message_prettifying("", sss sss sss "\n", 0);
	assert_message_prettifying("", sss sss sss sss "\n", 0);
}

void test_object_message__spaces_without_newline_at_end_should_be_replaced_with_empty_string(void)
{
	assert_message_prettifying("", "", 0);
	assert_message_prettifying("", sss sss, 0);
	assert_message_prettifying("", sss sss sss, 0);
	assert_message_prettifying("", sss sss sss sss, 0);
}

void test_object_message__consecutive_text_lines_should_be_unchanged(void)
{
	assert_message_prettifying(ttt ttt "\n" ttt "\n", ttt ttt "\n" ttt "\n", 0);
	assert_message_prettifying(ttt "\n" ttt ttt "\n" ttt "\n", ttt "\n" ttt ttt "\n" ttt "\n", 0);
	assert_message_prettifying(ttt "\n" ttt "\n" ttt "\n" ttt ttt "\n", ttt "\n" ttt "\n" ttt "\n" ttt ttt "\n", 0);
	assert_message_prettifying(ttt "\n" ttt "\n\n" ttt ttt "\n" ttt "\n", ttt "\n" ttt "\n\n" ttt ttt "\n" ttt "\n", 0);
	assert_message_prettifying(ttt ttt "\n\n" ttt "\n" ttt ttt "\n", ttt ttt "\n\n" ttt "\n" ttt ttt "\n", 0);
	assert_message_prettifying(ttt "\n" ttt ttt "\n\n" ttt "\n", ttt "\n" ttt ttt "\n\n" ttt "\n", 0);
}

void test_object_message__strip_comments(void)
{
	assert_message_prettifying("", "# comment", 1);
	assert_message_prettifying("", "# comment\n", 1);
	assert_message_prettifying("", "# comment    \n", 1);

	assert_message_prettifying(ttt "\n", ttt "\n" "# comment\n", 1);
	assert_message_prettifying(ttt "\n", "# comment\n" ttt "\n", 1);
	assert_message_prettifying(ttt "\n" ttt "\n", ttt "\n" "# comment\n" ttt "\n", 1);
}

void test_object_message__keep_comments(void)
{
	assert_message_prettifying("# comment\n", "# comment", 0);
	assert_message_prettifying("# comment\n", "# comment\n", 0);
	assert_message_prettifying("# comment\n", "# comment    \n", 0);

	assert_message_prettifying(ttt "\n" "# comment\n", ttt "\n" "# comment\n", 0);
	assert_message_prettifying("# comment\n" ttt "\n", "# comment\n" ttt "\n", 0);
	assert_message_prettifying(ttt "\n" "# comment\n" ttt "\n", ttt "\n" "# comment\n" ttt "\n", 0);
}
nulltoken committed
172 173 174

void test_object_message__message_prettify(void)
{
175 176 177
	git_buf buffer;

	memset(&buffer, 0, sizeof(buffer));
178
	cl_git_pass(git_message_prettify(&buffer, "", 0, '#'));
179 180
	cl_assert_equal_s(buffer.ptr, "");
	git_buf_free(&buffer);
181
	cl_git_pass(git_message_prettify(&buffer, "", 1, '#'));
182 183 184
	cl_assert_equal_s(buffer.ptr, "");
	git_buf_free(&buffer);

185
	cl_git_pass(git_message_prettify(&buffer, "Short", 0, '#'));
186 187
	cl_assert_equal_s("Short\n", buffer.ptr);
	git_buf_free(&buffer);
188
	cl_git_pass(git_message_prettify(&buffer, "Short", 1, '#'));
189 190 191
	cl_assert_equal_s("Short\n", buffer.ptr);
	git_buf_free(&buffer);

192
	cl_git_pass(git_message_prettify(&buffer, "This is longer\nAnd multiline\n# with some comments still in\n", 0, '#'));
193 194 195
	cl_assert_equal_s(buffer.ptr, "This is longer\nAnd multiline\n# with some comments still in\n");
	git_buf_free(&buffer);

196
	cl_git_pass(git_message_prettify(&buffer, "This is longer\nAnd multiline\n# with some comments still in\n", 1, '#'));
197 198
	cl_assert_equal_s(buffer.ptr, "This is longer\nAnd multiline\n");
	git_buf_free(&buffer);
nulltoken committed
199
}