1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.bdd;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23
24 import org.junit.jupiter.api.Test;
25
26
27
28
29 public class TestInputViolationTest {
30
31 @Test
32 public void testToRegexNullMessage() {
33 final TestInputViolation violation = new TestInputViolation(10, null);
34 assertWithMessage("Regex with null message should match line pattern only")
35 .that(violation.toRegex())
36 .isEqualTo("10:(?:\\d+:)?\\s.*");
37 }
38
39 @Test
40 public void testToRegexNoSpecialCharacters() {
41 final TestInputViolation violation = new TestInputViolation(5, "simple message");
42 assertWithMessage("Regex should match simple message")
43 .that(violation.toRegex())
44 .isEqualTo("5:(?:\\d+:)?\\s.*simple message.*");
45 }
46
47 @Test
48 public void testToRegexWithSpecialCharactersOutsideQuotes() {
49 final TestInputViolation violation = new TestInputViolation(12,
50 "message (with) {special} [brackets]");
51 assertWithMessage("Regex should escape special characters outside quotes")
52 .that(violation.toRegex())
53 .isEqualTo("12:(?:\\d+:)?\\s.*message \\(with\\) \\{special} \\[brackets\\].*");
54 }
55
56 @Test
57 public void testToRegexWithQuotedSpecialCharacters() {
58 final TestInputViolation violation = new TestInputViolation(1, "\\Q^[a-z][a-zA-Z0-9]*$\\E");
59 assertWithMessage("Regex should not escape special characters inside \\Q...\\E block")
60 .that(violation.toRegex())
61 .isEqualTo("1:(?:\\d+:)?\\s.*\\Q^[a-z][a-zA-Z0-9]*$\\E.*");
62 }
63
64 @Test
65 public void testToRegexWithMultipleQuotedBlocks() {
66 final TestInputViolation violation = new TestInputViolation(2,
67 "before \\Q^[a-z]\\E middle \\Q(test)\\E after (parenthesis)");
68 assertWithMessage(
69 "Regex should escape characters outside multiple \\Q...\\E blocks but not inside")
70 .that(violation.toRegex())
71 .isEqualTo("2:(?:\\d+:)?\\s.*before \\Q^[a-z]\\E middle \\Q(test)\\E after "
72 + "\\(parenthesis\\).*");
73 }
74
75 @Test
76 public void testToRegexWithUnmatchedQuotes() {
77 final TestInputViolation violation1 = new TestInputViolation(3, "unmatched \\Q^[a-z][A-Z]");
78 assertWithMessage("Regex should not escape characters after unmatched \\Q")
79 .that(violation1.toRegex())
80 .isEqualTo("3:(?:\\d+:)?\\s.*unmatched \\Q^[a-z][A-Z].*");
81
82 final TestInputViolation violation2 = new TestInputViolation(4,
83 "unmatched \\E(parenthesis)");
84 assertWithMessage("Regex should escape characters outside unmatched \\E")
85 .that(violation2.toRegex())
86 .isEqualTo("4:(?:\\d+:)?\\s.*unmatched \\E\\(parenthesis\\).*");
87 }
88
89 }