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.WhitespaceAroundCheck.MSG_WS_NOT_FOLLOWED;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck.MSG_WS_NOT_PRECEDED;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
28  
29  public class WhitespaceAroundExamplesTest extends AbstractExamplesModuleTestSupport {
30      @Override
31      protected String getPackageLocation() {
32          return "com/puppycrawl/tools/checkstyle/checks/whitespace/whitespacearound";
33      }
34  
35      @Test
36      public void testExample1() throws Exception {
37          final String[] expected = {
38              "15:20: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
39              "15:20: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
40              "15:21: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
41              "19:5: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "else"),
42              "19:9: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
43              "23:33: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
44              "23:34: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
45              "26:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "->"),
46              "26:26: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
47              "26:26: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
48              "26:27: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
49              "29:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
50              "29:24: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
51              "29:25: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
52          };
53  
54          verifyWithInlineConfigParser(getPath("Example1.java"), expected);
55      }
56  
57      @Test
58      public void testExample2() throws Exception {
59          final String[] expected = {
60              "21:10: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "="),
61              "21:10: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "="),
62              "24:6: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "+="),
63              "24:6: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "+="),
64              "27:6: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "*="),
65              "27:6: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "*="),
66              "30:6: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "-="),
67              "30:6: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "-="),
68              "33:6: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "/="),
69              "33:6: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "/="),
70              "36:6: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "%="),
71              "36:6: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "%="),
72              "39:6: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ">>="),
73              "39:6: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ">>="),
74              "42:6: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ">>>="),
75              "42:6: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ">>>="),
76          };
77  
78          verifyWithInlineConfigParser(getPath("Example2.java"), expected);
79      }
80  
81      @Test
82      public void testExample3() throws Exception {
83          final String[] expected = {
84              "17:22: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
85          };
86  
87          verifyWithInlineConfigParser(getPath("Example3.java"), expected);
88      }
89  
90      @Test
91      public void testExample4() throws Exception {
92          final String[] expected = {
93              "18:8: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "="),
94              "18:8: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "="),
95          };
96  
97          verifyWithInlineConfigParser(getPath("Example4.java"), expected);
98      }
99  
100     @Test
101     public void testExample5() throws Exception {
102         final String[] expected = {
103             "18:28: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
104             "18:29: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
105         };
106 
107         verifyWithInlineConfigParser(getPath("Example5.java"), expected);
108     }
109 
110     @Test
111     public void testExample6() throws Exception {
112         final String[] expected = {
113             "20:10: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "="),
114             "20:10: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "="),
115         };
116 
117         verifyWithInlineConfigParser(getPath("Example6.java"), expected);
118     }
119 
120     @Test
121     public void testExample7() throws Exception {
122         final String[] expected = {
123             "21:10: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "="),
124             "21:10: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "="),
125         };
126 
127         verifyWithInlineConfigParser(getPath("Example7.java"), expected);
128     }
129 
130     @Test
131     public void testExample8() throws Exception {
132         final String[] expected = {
133             "19:10: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "="),
134             "19:10: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "="),
135         };
136 
137         verifyWithInlineConfigParser(getPath("Example8.java"), expected);
138     }
139 
140     @Test
141     public void testExample9() throws Exception {
142         final String[] expected = {
143             "18:10: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "="),
144             "18:10: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "="),
145         };
146 
147         verifyWithInlineConfigParser(getPath("Example9.java"), expected);
148     }
149 
150     @Test
151     public void testExample10() throws Exception {
152         final String[] expected = {
153             "18:10: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "="),
154             "18:10: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "="),
155             "21:19: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ":"),
156         };
157 
158         verifyWithInlineConfigParser(getPath("Example10.java"), expected);
159     }
160 }