summaryrefslogtreecommitdiffstats
path: root/javatests/com/google/gerrit/server/mail/send/CommentFormatterTest.java
blob: f4fbc7846e2696f517a7b18e3b53338eb9ec7f00 (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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.mail.send;

import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.server.mail.send.CommentFormatter.BlockType.LIST;
import static com.google.gerrit.server.mail.send.CommentFormatter.BlockType.PARAGRAPH;
import static com.google.gerrit.server.mail.send.CommentFormatter.BlockType.PRE_FORMATTED;
import static com.google.gerrit.server.mail.send.CommentFormatter.BlockType.QUOTE;

import java.util.List;
import org.junit.Test;

public class CommentFormatterTest {
  private void assertBlock(
      List<CommentFormatter.Block> list, int index, CommentFormatter.BlockType type, String text) {
    CommentFormatter.Block block = list.get(index);
    assertThat(block.type).isEqualTo(type);
    assertThat(block.text).isEqualTo(text);
    assertThat(block.items).isNull();
    assertThat(block.quotedBlocks).isNull();
  }

  private void assertListBlock(
      List<CommentFormatter.Block> list, int index, int itemIndex, String text) {
    CommentFormatter.Block block = list.get(index);
    assertThat(block.type).isEqualTo(LIST);
    assertThat(block.items.get(itemIndex)).isEqualTo(text);
    assertThat(block.text).isNull();
    assertThat(block.quotedBlocks).isNull();
  }

  private void assertQuoteBlock(List<CommentFormatter.Block> list, int index, int size) {
    CommentFormatter.Block block = list.get(index);
    assertThat(block.type).isEqualTo(QUOTE);
    assertThat(block.items).isNull();
    assertThat(block.text).isNull();
    assertThat(block.quotedBlocks).hasSize(size);
  }

  @Test
  public void parseNullAsEmpty() {
    assertThat(CommentFormatter.parse(null)).isEmpty();
  }

  @Test
  public void parseEmpty() {
    assertThat(CommentFormatter.parse("")).isEmpty();
  }

  @Test
  public void parseSimple() {
    String comment = "Para1";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(1);
    assertBlock(result, 0, PARAGRAPH, comment);
  }

  @Test
  public void parseMultilinePara() {
    String comment = "Para 1\nStill para 1";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(1);
    assertBlock(result, 0, PARAGRAPH, comment);
  }

  @Test
  public void parseParaBreak() {
    String comment = "Para 1\n\nPara 2\n\nPara 3";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(3);
    assertBlock(result, 0, PARAGRAPH, "Para 1");
    assertBlock(result, 1, PARAGRAPH, "Para 2");
    assertBlock(result, 2, PARAGRAPH, "Para 3");
  }

  @Test
  public void parseQuote() {
    String comment = "> Quote text";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(1);
    assertQuoteBlock(result, 0, 1);
    assertBlock(result.get(0).quotedBlocks, 0, PARAGRAPH, "Quote text");
  }

  @Test
  public void parseExcludesEmpty() {
    String comment = "Para 1\n\n\n\nPara 2";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(2);
    assertBlock(result, 0, PARAGRAPH, "Para 1");
    assertBlock(result, 1, PARAGRAPH, "Para 2");
  }

  @Test
  public void parseQuoteLeadSpace() {
    String comment = " > Quote text";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(1);
    assertQuoteBlock(result, 0, 1);
    assertBlock(result.get(0).quotedBlocks, 0, PARAGRAPH, "Quote text");
  }

  @Test
  public void parseMultiLineQuote() {
    String comment = "> Quote line 1\n> Quote line 2\n > Quote line 3\n";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(1);
    assertQuoteBlock(result, 0, 1);
    assertBlock(
        result.get(0).quotedBlocks, 0, PARAGRAPH, "Quote line 1\nQuote line 2\nQuote line 3\n");
  }

  @Test
  public void parsePre() {
    String comment = "    Four space indent.";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(1);
    assertBlock(result, 0, PRE_FORMATTED, comment);
  }

  @Test
  public void parseOneSpacePre() {
    String comment = " One space indent.\n Another line.";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(1);
    assertBlock(result, 0, PRE_FORMATTED, comment);
  }

  @Test
  public void parseTabPre() {
    String comment = "\tOne tab indent.\n\tAnother line.\n  Yet another!";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(1);
    assertBlock(result, 0, PRE_FORMATTED, comment);
  }

  @Test
  public void parseIntermediateLeadingWhitespacePre() {
    String comment = "No indent.\n\tNonzero indent.\nNo indent again.";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(1);
    assertBlock(result, 0, PRE_FORMATTED, comment);
  }

  @Test
  public void parseStarList() {
    String comment = "* Item 1\n* Item 2\n* Item 3";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(1);
    assertListBlock(result, 0, 0, "Item 1");
    assertListBlock(result, 0, 1, "Item 2");
    assertListBlock(result, 0, 2, "Item 3");
  }

  @Test
  public void parseDashList() {
    String comment = "- Item 1\n- Item 2\n- Item 3";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(1);
    assertListBlock(result, 0, 0, "Item 1");
    assertListBlock(result, 0, 1, "Item 2");
    assertListBlock(result, 0, 2, "Item 3");
  }

  @Test
  public void parseMixedList() {
    String comment = "- Item 1\n* Item 2\n- Item 3\n* Item 4";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(1);
    assertListBlock(result, 0, 0, "Item 1");
    assertListBlock(result, 0, 1, "Item 2");
    assertListBlock(result, 0, 2, "Item 3");
    assertListBlock(result, 0, 3, "Item 4");
  }

  @Test
  public void parseMixedBlockTypes() {
    String comment =
        "Paragraph\nacross\na\nfew\nlines."
            + "\n\n"
            + "> Quote\n> across\n> not many lines."
            + "\n\n"
            + "Another paragraph"
            + "\n\n"
            + "* Series\n* of\n* list\n* items"
            + "\n\n"
            + "Yet another paragraph"
            + "\n\n"
            + "\tPreformatted text."
            + "\n\n"
            + "Parting words.";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(7);
    assertBlock(result, 0, PARAGRAPH, "Paragraph\nacross\na\nfew\nlines.");
    assertQuoteBlock(result, 1, 1);
    assertBlock(result.get(1).quotedBlocks, 0, PARAGRAPH, "Quote\nacross\nnot many lines.");
    assertBlock(result, 2, PARAGRAPH, "Another paragraph");
    assertListBlock(result, 3, 0, "Series");
    assertListBlock(result, 3, 1, "of");
    assertListBlock(result, 3, 2, "list");
    assertListBlock(result, 3, 3, "items");
    assertBlock(result, 4, PARAGRAPH, "Yet another paragraph");
    assertBlock(result, 5, PRE_FORMATTED, "\tPreformatted text.");
    assertBlock(result, 6, PARAGRAPH, "Parting words.");
  }

  @Test
  public void bulletList1() {
    String comment = "A\n\n* line 1\n* 2nd line";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(2);
    assertBlock(result, 0, PARAGRAPH, "A");
    assertListBlock(result, 1, 0, "line 1");
    assertListBlock(result, 1, 1, "2nd line");
  }

  @Test
  public void bulletList2() {
    String comment = "A\n\n* line 1\n* 2nd line\n\nB";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(3);
    assertBlock(result, 0, PARAGRAPH, "A");
    assertListBlock(result, 1, 0, "line 1");
    assertListBlock(result, 1, 1, "2nd line");
    assertBlock(result, 2, PARAGRAPH, "B");
  }

  @Test
  public void bulletList3() {
    String comment = "* line 1\n* 2nd line\n\nB";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(2);
    assertListBlock(result, 0, 0, "line 1");
    assertListBlock(result, 0, 1, "2nd line");
    assertBlock(result, 1, PARAGRAPH, "B");
  }

  @Test
  public void bulletList4() {
    String comment =
        "To see this bug, you have to:\n" //
            + "* Be on IMAP or EAS (not on POP)\n" //
            + "* Be very unlucky\n";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(2);
    assertBlock(result, 0, PARAGRAPH, "To see this bug, you have to:");
    assertListBlock(result, 1, 0, "Be on IMAP or EAS (not on POP)");
    assertListBlock(result, 1, 1, "Be very unlucky");
  }

  @Test
  public void bulletList5() {
    String comment =
        "To see this bug,\n" //
            + "you have to:\n" //
            + "* Be on IMAP or EAS (not on POP)\n" //
            + "* Be very unlucky\n";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(2);
    assertBlock(result, 0, PARAGRAPH, "To see this bug, you have to:");
    assertListBlock(result, 1, 0, "Be on IMAP or EAS (not on POP)");
    assertListBlock(result, 1, 1, "Be very unlucky");
  }

  @Test
  public void dashList1() {
    String comment = "A\n\n- line 1\n- 2nd line";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(2);
    assertBlock(result, 0, PARAGRAPH, "A");
    assertListBlock(result, 1, 0, "line 1");
    assertListBlock(result, 1, 1, "2nd line");
  }

  @Test
  public void dashList2() {
    String comment = "A\n\n- line 1\n- 2nd line\n\nB";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(3);
    assertBlock(result, 0, PARAGRAPH, "A");
    assertListBlock(result, 1, 0, "line 1");
    assertListBlock(result, 1, 1, "2nd line");
    assertBlock(result, 2, PARAGRAPH, "B");
  }

  @Test
  public void dashList3() {
    String comment = "- line 1\n- 2nd line\n\nB";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(2);
    assertListBlock(result, 0, 0, "line 1");
    assertListBlock(result, 0, 1, "2nd line");
    assertBlock(result, 1, PARAGRAPH, "B");
  }

  @Test
  public void preformat1() {
    String comment = "A\n\n  This is pre\n  formatted";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(2);
    assertBlock(result, 0, PARAGRAPH, "A");
    assertBlock(result, 1, PRE_FORMATTED, "  This is pre\n  formatted");
  }

  @Test
  public void preformat2() {
    String comment = "A\n\n  This is pre\n  formatted\n\nbut this is not";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(3);
    assertBlock(result, 0, PARAGRAPH, "A");
    assertBlock(result, 1, PRE_FORMATTED, "  This is pre\n  formatted");
    assertBlock(result, 2, PARAGRAPH, "but this is not");
  }

  @Test
  public void preformat3() {
    String comment = "A\n\n  Q\n    <R>\n  S\n\nB";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(3);
    assertBlock(result, 0, PARAGRAPH, "A");
    assertBlock(result, 1, PRE_FORMATTED, "  Q\n    <R>\n  S");
    assertBlock(result, 2, PARAGRAPH, "B");
  }

  @Test
  public void preformat4() {
    String comment = "  Q\n    <R>\n  S\n\nB";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(2);
    assertBlock(result, 0, PRE_FORMATTED, "  Q\n    <R>\n  S");
    assertBlock(result, 1, PARAGRAPH, "B");
  }

  @Test
  public void quote1() {
    String comment = "> I'm happy\n > with quotes!\n\nSee above.";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(2);
    assertQuoteBlock(result, 0, 1);
    assertBlock(result.get(0).quotedBlocks, 0, PARAGRAPH, "I'm happy\nwith quotes!");
    assertBlock(result, 1, PARAGRAPH, "See above.");
  }

  @Test
  public void quote2() {
    String comment = "See this said:\n\n > a quoted\n > string block\n\nOK?";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(3);
    assertBlock(result, 0, PARAGRAPH, "See this said:");
    assertQuoteBlock(result, 1, 1);
    assertBlock(result.get(1).quotedBlocks, 0, PARAGRAPH, "a quoted\nstring block");
    assertBlock(result, 2, PARAGRAPH, "OK?");
  }

  @Test
  public void nestedQuotes1() {
    String comment = " > > prior\n > \n > next\n";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(1);
    assertQuoteBlock(result, 0, 2);
    assertQuoteBlock(result.get(0).quotedBlocks, 0, 1);
    assertBlock(result.get(0).quotedBlocks.get(0).quotedBlocks, 0, PARAGRAPH, "prior");
    assertBlock(result.get(0).quotedBlocks, 1, PARAGRAPH, "next\n");
  }

  @Test
  public void largeMixedQuote() {
    String comment =
        "> > Paragraph 1.\n"
            + "> > \n"
            + "> > > Paragraph 2.\n"
            + "> > \n"
            + "> > Paragraph 3.\n"
            + "> > \n"
            + "> >    pre line 1;\n"
            + "> >    pre line 2;\n"
            + "> > \n"
            + "> > Paragraph 4.\n"
            + "> > \n"
            + "> > * List item 1.\n"
            + "> > * List item 2.\n"
            + "> > \n"
            + "> > Paragraph 5.\n"
            + "> \n"
            + "> Paragraph 6.\n"
            + "\n"
            + "Paragraph 7.\n";
    List<CommentFormatter.Block> result = CommentFormatter.parse(comment);

    assertThat(result).hasSize(2);
    assertQuoteBlock(result, 0, 2);

    assertQuoteBlock(result.get(0).quotedBlocks, 0, 7);
    List<CommentFormatter.Block> bigQuote = result.get(0).quotedBlocks.get(0).quotedBlocks;
    assertBlock(bigQuote, 0, PARAGRAPH, "Paragraph 1.");
    assertQuoteBlock(bigQuote, 1, 1);
    assertBlock(bigQuote.get(1).quotedBlocks, 0, PARAGRAPH, "Paragraph 2.");
    assertBlock(bigQuote, 2, PARAGRAPH, "Paragraph 3.");
    assertBlock(bigQuote, 3, PRE_FORMATTED, "   pre line 1;\n   pre line 2;");
    assertBlock(bigQuote, 4, PARAGRAPH, "Paragraph 4.");
    assertListBlock(bigQuote, 5, 0, "List item 1.");
    assertListBlock(bigQuote, 5, 1, "List item 2.");
    assertBlock(bigQuote, 6, PARAGRAPH, "Paragraph 5.");
    assertBlock(result.get(0).quotedBlocks, 1, PARAGRAPH, "Paragraph 6.");
    assertBlock(result, 1, PARAGRAPH, "Paragraph 7.\n");
  }
}