View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.checks.whitespace;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheck.MSG_KEY;
23  
24  import org.junit.jupiter.api.Test;
25  
26  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
27  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
28  
29  public class NoWhitespaceBeforeCheckTest
30      extends AbstractModuleTestSupport {
31  
32      @Override
33      protected String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/whitespace/nowhitespacebefore";
35      }
36  
37      @Test
38      public void testDefault() throws Exception {
39          final String[] expected = {
40              "34:15: " + getCheckMessage(MSG_KEY, "++"),
41              "34:22: " + getCheckMessage(MSG_KEY, "--"),
42              "180:19: " + getCheckMessage(MSG_KEY, ";"),
43              "182:24: " + getCheckMessage(MSG_KEY, ";"),
44              "189:19: " + getCheckMessage(MSG_KEY, ";"),
45              "191:28: " + getCheckMessage(MSG_KEY, ";"),
46              "199:27: " + getCheckMessage(MSG_KEY, ";"),
47              "215:16: " + getCheckMessage(MSG_KEY, ";"),
48              "270:1: " + getCheckMessage(MSG_KEY, ";"),
49              "274:16: " + getCheckMessage(MSG_KEY, ";"),
50              "288:1: " + getCheckMessage(MSG_KEY, ";"),
51              "291:62: " + getCheckMessage(MSG_KEY, "..."),
52              "295:16: " + getCheckMessage(MSG_KEY, ":"),
53          };
54          verifyWithInlineConfigParser(
55                  getPath("InputNoWhitespaceBeforeDefault.java"), expected);
56      }
57  
58      @Test
59      public void testDot() throws Exception {
60          final String[] expected = {
61              "9:13: " + getCheckMessage(MSG_KEY, "."),
62              "10:5: " + getCheckMessage(MSG_KEY, "."),
63              "133:18: " + getCheckMessage(MSG_KEY, "."),
64              "139:13: " + getCheckMessage(MSG_KEY, "."),
65              "140:11: " + getCheckMessage(MSG_KEY, "."),
66              "268:1: " + getCheckMessage(MSG_KEY, "."),
67          };
68          verifyWithInlineConfigParser(
69                  getPath("InputNoWhitespaceBeforeDot.java"), expected);
70      }
71  
72      @Test
73      public void testDotAllowLineBreaks() throws Exception {
74          final String[] expected = {
75              "9:13: " + getCheckMessage(MSG_KEY, "."),
76              "133:18: " + getCheckMessage(MSG_KEY, "."),
77              "140:11: " + getCheckMessage(MSG_KEY, "."),
78          };
79          verifyWithInlineConfigParser(
80                  getPath("InputNoWhitespaceBeforeDotAllowLineBreaks.java"), expected);
81      }
82  
83      @Test
84      public void testMethodReference() throws Exception {
85          final String[] expected = {
86              "25:32: " + getCheckMessage(MSG_KEY, "::"),
87              "26:61: " + getCheckMessage(MSG_KEY, "::"),
88          };
89          verifyWithInlineConfigParser(
90                  getPath("InputNoWhitespaceBeforeMethodRef.java"), expected);
91      }
92  
93      @Test
94      public void testDotAtTheStartOfTheLine() throws Exception {
95          final String[] expected = {
96              "10:1: " + getCheckMessage(MSG_KEY, "."),
97          };
98          verifyWithInlineConfigParser(
99                  getPath("InputNoWhitespaceBeforeAtStartOfTheLine.java"), expected);
100     }
101 
102     @Test
103     public void testMethodRefAtTheStartOfTheLine() throws Exception {
104         final String[] expected = {
105             "22:3: " + getCheckMessage(MSG_KEY, "::"),
106         };
107         verifyWithInlineConfigParser(
108                 getPath("InputNoWhitespaceBeforeAtStartOfTheLine2.java"), expected);
109     }
110 
111     @Test
112     public void testEmptyForLoop() throws Exception {
113         final String[] expected = {
114             "20:24: " + getCheckMessage(MSG_KEY, ";"),
115             "26:32: " + getCheckMessage(MSG_KEY, ";"),
116         };
117         verifyWithInlineConfigParser(
118                 getPath("InputNoWhitespaceBeforeEmptyForLoop.java"), expected);
119     }
120 
121     @Test
122     public void testNoWhitespaceBeforeTextBlocksWithTabIndent() throws Exception {
123 
124         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
125 
126         verifyWithInlineConfigParser(
127                 getNonCompilablePath("InputNoWhitespaceBeforeTextBlocksTabIndent.java"), expected);
128     }
129 
130     @Test
131     public void testNoWhitespaceBeforeWithEmoji() throws Exception {
132         final String[] expected = {
133             "13:15: " + getCheckMessage(MSG_KEY, ","),
134             "14:17: " + getCheckMessage(MSG_KEY, ","),
135             "20:37: " + getCheckMessage(MSG_KEY, ";"),
136             "21:37: " + getCheckMessage(MSG_KEY, ";"),
137         };
138 
139         verifyWithInlineConfigParser(
140                 getPath("InputNoWhitespaceBeforeWithEmoji.java"), expected);
141     }
142 
143 }