Skip to content

Commit

Permalink
Issue checkstyle#5624: Google Style Should Enforce Spaces after Commas
Browse files Browse the repository at this point in the history
  • Loading branch information
shashwatj07 committed Mar 29, 2020
1 parent a8a90cf commit 5a11490
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
@@ -0,0 +1,62 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2020 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////

package com.google.checkstyle.test.chapter4formatting.rule462horizontalwhitespace;

import org.junit.jupiter.api.Test;

import com.google.checkstyle.test.base.AbstractGoogleModuleTestSupport;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck;

public class WhitespaceAfterTest extends AbstractGoogleModuleTestSupport {

@Override
protected String getPackageLocation() {
return "com/google/checkstyle/test/chapter4formatting/rule462horizontalwhitespace";
}

@Test
public void testWhitespaceAfterBasic() throws Exception {
final Class<WhitespaceAfterCheck> clazz = WhitespaceAfterCheck.class;
final String msgFollowed = "ws.notFollowed";

final String[] expected = {
"4:29: " + getCheckMessage(clazz, msgFollowed, ","),
"5:9: " + getCheckMessage(clazz, msgFollowed, "for"),
"5:20: " + getCheckMessage(clazz, msgFollowed, ","),
"5:24: " + getCheckMessage(clazz, msgFollowed, ";"),
"5:28: " + getCheckMessage(clazz, msgFollowed, ";"),
"5:32: " + getCheckMessage(clazz, msgFollowed, ","),
"6:9: " + getCheckMessage(clazz, msgFollowed, "while"),
"7:20: " + getCheckMessage(clazz, msgFollowed, ","),
"9:9: " + getCheckMessage(clazz, msgFollowed, "do"),
"11:10: " + getCheckMessage(clazz, msgFollowed, "while"),
"13:35: " + getCheckMessage(clazz, msgFollowed, ","),
"14:9: " + getCheckMessage(clazz, msgFollowed, "if"),
"17:9: " + getCheckMessage(clazz, msgFollowed, "else"),
};
final Configuration checkConfig = getModuleConfig("WhitespaceAfter");
final String filePath = getPath("InputWhitespaceAfter.java");

final Integer[] warnList = getLinesWithWarn(filePath);
verify(checkConfig, filePath, expected, warnList);
}

}
@@ -0,0 +1,21 @@
package com.google.checkstyle.test.chapter4formatting.rule462horizontalwhitespace;

public class InputWhitespaceAfter {
public void check1(int x,int y){ //warn
for(int a=1,b=2;a<5;a++,b--); // warn
while(x==0){ // warn
int a=0,b=1; // warn
}
do{ // warn
System.out.println("Testing");
}while(x==0||y==2); // warn
}
public void check2(final int a,final int b){ // warn
if(a==0){ // warn
System.out.println("true");
}
else{ //warn
System.out.println("false");
}
}
}
11 changes: 9 additions & 2 deletions src/main/resources/google_checks.xml
Expand Up @@ -47,13 +47,19 @@
</module>

<module name="TreeWalker">
<module name="WhitespaceAfter">
<property name="tokens"
value="COMMA, SEMI, TYPECAST, LITERAL_IF, LITERAL_ELSE,
LITERAL_WHILE, LITERAL_DO, LITERAL_FOR, DO_WHILE"/>
</module>
<module name="OuterTypeFilename"/>
<module name="IllegalTokenText">
<property name="tokens" value="STRING_LITERAL, CHAR_LITERAL"/>
<property name="format"
value="\\u00(09|0(a|A)|0(c|C)|0(d|D)|22|27|5(C|c))|\\(0(10|11|12|14|15|42|47)|134)"/>
<property name="message"
value="Consider using special escape sequence instead of octal value or Unicode escaped value."/>
value="Consider using special escape sequence instead of
octal value or Unicode escaped value."/>
</module>
<module name="AvoidEscapedUnicodeCharacters">
<property name="allowEscapesForControlCharacters" value="true"/>
Expand Down Expand Up @@ -116,7 +122,8 @@
NOT_EQUAL, PLUS, PLUS_ASSIGN, QUESTION, RCURLY, SL, SLIST, SL_ASSIGN, SR,
SR_ASSIGN, STAR, STAR_ASSIGN, LITERAL_ASSERT, TYPE_EXTENSION_AND"/>
<message key="ws.notFollowed"
value="WhitespaceAround: ''{0}'' is not followed by whitespace. Empty blocks may only be represented as '{}' when not part of a multi-block statement (4.1.3)"/>
value="WhitespaceAround: ''{0}'' is not followed by whitespace. Empty blocks
may only be represented as '{}' when not part of a multi-block statement (4.1.3)"/>
<message key="ws.notPreceded"
value="WhitespaceAround: ''{0}'' is not preceded with whitespace."/>
</module>
Expand Down
4 changes: 4 additions & 0 deletions src/xdocs/config_whitespace.xml
Expand Up @@ -2411,6 +2411,10 @@ public void myTest() {

<subsection name="Example of Usage" id="WhitespaceAfter_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Agoogle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+WhitespaceAfter">
Google Style</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Asun_checks.xml+repo%3Acheckstyle%2Fcheckstyle+WhitespaceAfter">
Sun Style</a>
Expand Down
13 changes: 13 additions & 0 deletions src/xdocs/google_style.xml
Expand Up @@ -970,6 +970,13 @@
<span class="wrapper inline">
<img src="images/ok_green.png" alt="" />
</span>
<a href="config_whitespace.html#WhitespaceAfter">
WhitespaceAfter</a>
<br />
<br />
<span class="wrapper inline">
<img src="images/ok_green.png" alt="" />
</span>
<a href="config_whitespace.html#GenericWhitespace">
GenericWhitespace</a>
<br />
Expand Down Expand Up @@ -1007,6 +1014,12 @@
<a href="https://github.com/checkstyle/checkstyle/blob/master/src/it/java/com/google/checkstyle/test/chapter4formatting/rule462horizontalwhitespace/WhitespaceAroundTest.java">
test</a>
<br />
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Agoogle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+WhitespaceAfter">
config</a>
<br />
<a href="https://github.com/checkstyle/checkstyle/blob/master/src/it/java/com/google/checkstyle/test/chapter4formatting/rule462horizontalwhitespace/WhitespaceAfterTest.java">
test</a>
<br />
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Agoogle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+GenericWhitespace">
config</a>
<br />
Expand Down

0 comments on commit 5a11490

Please sign in to comment.