View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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   * Unit tests for {@link TestInputViolation}.
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  }