Improve hard-wrapping in commit messages
[egit/eclipse.git] / org.eclipse.egit.ui.test / src / org / eclipse / egit / ui / internal / dialogs / SpellcheckableMessageAreaTest.java
blob4037a81f16fe446fb7f7556364118b00ef8a5d41
1 /*******************************************************************************
2 * Copyright (C) 2010, Robin Stocker
3 * Copyright (C) 2011, Matthias Sohn <matthias.sohn@sap.com>
4 * Copyright (C) 2012, IBM Corporation (Markus Keller <markus_keller@ch.ibm.com>)
5 * Copyright (C) 2015, Thomas Wolf <thomas.wolf@paranor.ch>
7 * All rights reserved. This program and the accompanying materials
8 * are made available under the terms of the Eclipse Public License v1.0
9 * which accompanies this distribution, and is available at
10 * http://www.eclipse.org/legal/epl-v10.html
11 *******************************************************************************/
12 package org.eclipse.egit.ui.internal.dialogs;
14 import static org.junit.Assert.assertEquals;
16 import org.eclipse.egit.core.internal.Utils;
17 import org.junit.Test;
19 public class SpellcheckableMessageAreaTest {
21 @Test
22 public void dontWrapShortText() {
23 String input = "short message";
24 assertWrappedEquals(input, input);
27 @Test
28 public void dontWrapAlreadyWrappedText() {
29 String input = "This is a test of wrapping\n\nDid it work?\n\nHm?";
30 assertWrappedEquals(input, input);
33 @Test
34 public void dontWrapMaximumLengthText() {
35 String input = "123456789 123456789 123456789 123456789 123456789 123456789 123456789 12";
36 assertWrappedEquals(input, input);
39 @Test
40 public void wrapOverlengthText() {
41 String input = "123456789 123456789 123456789 123456789 123456789 123456789 123456789 12 3";
42 String expected = "123456789 123456789 123456789 123456789 123456789 123456789 123456789 12\n3";
43 assertWrappedEquals(expected, input);
46 @Test
47 public void wrapOverlengthTextByOne() {
48 String input = "123456789 123456789 123456789 123456789 123456789 123456789 123456789.abc";
49 String expected = "123456789 123456789 123456789 123456789 123456789 123456789\n123456789.abc";
50 assertWrappedEquals(expected, input);
53 @Test
54 public void wrapOverlengthText2() {
55 String input = "123456789 123456789 123456789 123456789 123456789 123456789. 1234567890123456";
56 String expected = "123456789 123456789 123456789 123456789 123456789 123456789.\n1234567890123456";
57 assertWrappedEquals(expected, input);
60 public void wrapOverlengthTextTwice() {
61 String input = "123456789 123456789 123456789 123456789 123456789 123456789 123456789.12 "
62 + "123456789 123456789 123456789 123456789 123456789 123456789 123456789.12 "
63 + "123456789 123456789 123456789 123456789 123456789 123456789 123456789.12";
64 String expected = "123456789 123456789 123456789 123456789 123456789 123456789 123456789.12\n"
65 + "123456789 123456789 123456789 123456789 123456789 123456789 123456789.12\n"
66 + "123456789 123456789 123456789 123456789 123456789 123456789 123456789.12";
67 assertWrappedEquals(expected, input);
70 @Test
71 public void dontWrapWordLongerThanOneLineAtStart() {
72 String input = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012 the previous was longer than a line";
73 String expected = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012\nthe previous was longer than a line";
74 assertWrappedEquals(expected, input);
77 @Test
78 public void dontWrapWordLongerThanOneLine() {
79 String input = "This has to be on its own line: 1234567890123456789012345678901234567890123456789012345678901234567890123456789012 this not";
80 String expected = "This has to be on its own line:\n1234567890123456789012345678901234567890123456789012345678901234567890123456789012\nthis not";
81 assertWrappedEquals(expected, input);
84 @Test
85 public void dontWrapWordLongerThanOneLineAndKeepSpaceAtFront() {
86 String input = " 1234567890123456789012345678901234567890123456789012345678901234567890123456789012";
87 assertWrappedEquals(input, input);
90 @Test
91 public void wrapSecondLongLine() {
92 String input = "First line\n123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789.12";
93 String expected = "First line\n123456789 123456789 123456789 123456789 123456789 123456789 123456789\n123456789.12";
94 assertWrappedEquals(expected, input);
97 @Test
98 public void keepExistingNewlines() {
99 String input = "This\n\nis\nall\nok\n123456789 123456789 123456789 123456789 123456789 123456789 123456789.12";
100 assertWrappedEquals(input, input);
103 @Test
104 public void keepNewlineAtEnd() {
105 String input = "Newline\nat\nend\n";
106 assertWrappedEquals(input, input);
109 @Test
110 public void keepWhitespace() {
111 String input = " this is deliberate whitespace";
112 assertWrappedEquals(input, input);
115 @Test
116 public void keepTrailingSpace() {
117 String input = "space at end ";
118 assertWrappedEquals(input, input);
121 @Test
122 public void lineAfterWrappedWordShouldNotBeJoined() {
123 String input = "000000001 000000002 000000003 000000004 000000005 000000006 000000007 000000008\n000000009";
124 String expected = "000000001 000000002 000000003 000000004 000000005 000000006 000000007\n000000008\n000000009";
125 assertWrappedEquals(expected, input);
128 @Test
129 public void lineAfterWrappedWordShouldNotBeJoined2() {
130 String input = "000000001 000000002 000000003 000000004 000000005 000000006 000000007 000000008\n"
131 + "000000009 000000010 000000011 000000012 000000013 000000014 000000015 000000016";
132 String expected = "000000001 000000002 000000003 000000004 000000005 000000006 000000007\n"
133 + "000000008\n"
134 + "000000009 000000010 000000011 000000012 000000013 000000014 000000015\n"
135 + "000000016";
136 assertWrappedEquals(expected, input);
139 @Test
140 public void lineAfterWrappedLineShouldBeJoinedAndFollowingLineWrappedCorrectly() {
141 String input = "000000001 000000002 000000003 000000004 000000005 000000006 000000007 "
142 + "000000008 000000009 000000010 000000011 000000012 000000013 000000014\n"
143 + "000000015 000000016 000000017 000000018 000000019 000000020 000000021";
144 String expected = "000000001 000000002 000000003 000000004 000000005 000000006 000000007\n"
145 + "000000008 000000009 000000010 000000011 000000012 000000013 000000014\n"
146 + "000000015 000000016 000000017 000000018 000000019 000000020 000000021";
147 assertWrappedEquals(expected, input);
150 @Test
151 public void lineAfterWrappedWordShouldNotBeJoinedIfItsEmpty() {
152 String input = "000000001 000000002 000000003 000000004 000000005 000000006 000000007 000000008\n\nNew paragraph";
153 String expected = "000000001 000000002 000000003 000000004 000000005 000000006 000000007\n000000008\n\nNew paragraph";
154 assertWrappedEquals(expected, input);
157 @Test
158 public void lineAfterWrappedWordShouldNotBeJoinedIfItStartsWithASymbol() {
159 String input = "* 000000001 000000002 000000003 000000004 000000005 000000006 000000007 000000008\n* Bullet 2";
160 String expected = "* 000000001 000000002 000000003 000000004 000000005 000000006 000000007\n000000008\n* Bullet 2";
161 assertWrappedEquals(expected, input);
164 @Test
165 public void lineAfterWrappedWordShouldNotBeJoined3() {
166 String input = "* 000000001 000000002 000000003 000000004 000000005 000000006 000000007 000000008\n(paren)";
167 String expected = "* 000000001 000000002 000000003 000000004 000000005 000000006 000000007\n000000008\n(paren)";
168 assertWrappedEquals(expected, input);
172 private static void assertWrappedEquals(String expected, String input) {
173 assertWrappedEqualsOnUnix(expected, input);
174 assertWrappedEqualsOnWindows(expected, input);
177 private static void assertWrappedEqualsOnUnix(String expected, String input) {
178 String wrapped = wrap(input, "\n");
179 assertEquals(expected, wrapped);
182 private static void assertWrappedEqualsOnWindows(String expected,
183 String input) {
184 String wrapped = wrap(input.replaceAll("\n", "\r\n"), "\r\n");
185 assertEquals(expected.replaceAll("\n", "\r\n"), wrapped);
188 private static String wrap(String text, String lineDelimiter) {
189 String wrapped = SpellcheckableMessageArea.hardWrap(Utils.normalizeLineEndings(text));
190 return wrapped.replaceAll("\n", lineDelimiter);
193 @Test
194 public void dontWrapShortMessage() {
195 String input = "short";
196 assertEquals(input, SpellcheckableMessageArea.wrapCommitMessage(input));
199 @Test
200 public void dontWrapLongCommitMessageFooter() {
201 String input = "short\n\nfoo\n\n"
202 + "Change-Id: I0000000000000000000000000000000000000000\n"
203 + "Signed-off-by: Some-Arguablylong Name <jsomearguablylong.name@somecompany.com>";
204 assertEquals(input, SpellcheckableMessageArea.wrapCommitMessage(input));
207 @Test
208 public void wrapOnlyCommitMessageBody() {
209 String input = "short\n\n"
210 + "123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789\n\n"
211 + "Change-Id: I0000000000000000000000000000000000000000\n"
212 + "Signed-off-by: Some-Arguablylong Name <somearguablylong.name@somecompany.com>";
213 String expected = "short\n\n"
214 + "123456789 123456789 123456789 123456789 123456789 123456789 123456789\n"
215 + "123456789\n\n"
216 + "Change-Id: I0000000000000000000000000000000000000000\n"
217 + "Signed-off-by: Some-Arguablylong Name <somearguablylong.name@somecompany.com>";
218 assertEquals(expected,
219 SpellcheckableMessageArea.wrapCommitMessage(input));
222 @Test
223 public void wrapAlternatingBlankNonBlank() {
224 String input = " x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x";
225 String expected = " x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x\n"
226 + "x x x x x x";
227 assertEquals(expected,
228 SpellcheckableMessageArea.wrapCommitMessage(input));
231 @Test
232 public void testWrappingLongWords() {
233 String input = "A commit message with long words\n\n"
234 + "123456789 123456789 123456789 123456789 123456789 123456789 A_single_rather_long_word_that_exceeds_the_margin\n"
235 + "More text with normal short words on a single line fitting the margin.\n"
236 + "Even more text with normal short words on a single line exceeding the margin.\n\n"
237 + "[1] https://foo.example.org/with/a/very/long/url/that/should/remain/on/this/line/\n"
238 + " * https://foo.example.org/with/a/very/long/url/that/should/also/remain/on/this/line/\n"
239 + " * ** https://foo.example.org/with/a/very/long/url/that/should/also/remain/on/this/line/\n"
240 + " this_long_word_also_should_remain_here, but this should be a new line\n"
241 + " * but_this_long_word_should_be_wrapped, and this should follow\n\n"
242 + "Change-Id: I0000000000000000000000000000000000000000\n"
243 + "Signed-off-by: Some-Arguablylong Name <somearguablylong.name@somecompany.com>";
244 String expected = "A commit message with long words\n\n"
245 + "123456789 123456789 123456789 123456789 123456789 123456789\n"
246 + "A_single_rather_long_word_that_exceeds_the_margin\n"
247 + "More text with normal short words on a single line fitting the margin.\n"
248 + "Even more text with normal short words on a single line exceeding the\n"
249 + "margin.\n\n"
250 + "[1] https://foo.example.org/with/a/very/long/url/that/should/remain/on/this/line/\n"
251 + " * https://foo.example.org/with/a/very/long/url/that/should/also/remain/on/this/line/\n"
252 + " * ** https://foo.example.org/with/a/very/long/url/that/should/also/remain/on/this/line/\n"
253 + " this_long_word_also_should_remain_here,\n"
254 + "but this should be a new line\n"
255 + " *\n" // Trailing blanks stripped!
256 + "but_this_long_word_should_be_wrapped, and this should follow\n\n"
257 + "Change-Id: I0000000000000000000000000000000000000000\n"
258 + "Signed-off-by: Some-Arguablylong Name <somearguablylong.name@somecompany.com>";
259 assertEquals(expected,
260 SpellcheckableMessageArea.wrapCommitMessage(input));