Skip to content

Commit

Permalink
Issue #5624: Google Style Should Enforce Spaces after Commas
Browse files Browse the repository at this point in the history
  • Loading branch information
shashwatj07 authored and romani committed Apr 21, 2020
1 parent f2e45f8 commit deea241
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/suppressions.xml
Expand Up @@ -6,7 +6,7 @@

<suppressions>
<!-- can't split long messages between lines -->
<suppress checks="RegexpSingleline" files="google_checks\.xml" lines="56,119"/>
<suppress checks="RegexpSingleline" files="google_checks\.xml" lines="56,124"/>
<!-- don't validate generated files -->
<suppress checks="RegexpSingleline" files="releasenotes\.xml"/>

Expand Down
@@ -0,0 +1,74 @@
////////////////////////////////////////////////////////////////////////////////
// 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 testWhitespaceAfterBad() throws Exception {
final Class<WhitespaceAfterCheck> clazz = WhitespaceAfterCheck.class;
final String message = "ws.notFollowed";

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

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

@Test
public void testWhitespaceAfterGood() throws Exception {
final String[] expected = {
};
final Configuration checkConfig = getModuleConfig("WhitespaceAfter");
final String filePath = getPath("InputWhitespaceAfterGood.java");

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

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

public class InputWhitespaceAfterBad {
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((float)a==0.0){ // warn
System.out.println("true");
}
else{ //warn
System.out.println("false");
}
}
}
@@ -0,0 +1,21 @@
package com.google.checkstyle.test.chapter4formatting.rule462horizontalwhitespace;

public class InputWhitespaceAfterGood {
public void check1(int x, int y){
for (int a=1, b=2; a<5; a++, b--);
while (x==0){
int a=0, b=1;
}
do {
System.out.println("Testing");
}while (x==0||y==2);
}
public void check2(final int a, final int b){
if ((float) a==0.0){
System.out.println("true");
}
else {
System.out.println("false");
}
}
}
5 changes: 5 additions & 0 deletions src/main/resources/google_checks.xml
Expand Up @@ -101,6 +101,11 @@
<property name="query" value="//RCURLY[parent::SLIST[count(./*)=1]
or preceding-sibling::*[last()][self::LCURLY]]"/>
</module>
<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="WhitespaceAround">
<property name="allowEmptyConstructors" value="true"/>
<property name="allowEmptyLambdas" value="true"/>
Expand Down
4 changes: 4 additions & 0 deletions src/xdocs/config_whitespace.xml
Expand Up @@ -2630,6 +2630,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
14 changes: 13 additions & 1 deletion src/xdocs/google_style.xml
Expand Up @@ -984,7 +984,13 @@
<img src="images/ok_green.png" alt="" />
</span>
<a href="config_whitespace.html#ParenPad">ParenPad</a>

<br />
<br />
<span class="wrapper inline">
<img src="images/ok_blue.png" alt="" />
</span>
<a href="config_whitespace.html#WhitespaceAfter">
WhitespaceAfter</a>
<br />
<br />
<span class="wrapper inline">
Expand Down Expand Up @@ -1025,6 +1031,12 @@
<a href="https://github.com/checkstyle/checkstyle/blob/master/src/it/java/com/google/checkstyle/test/chapter4formatting/rule462horizontalwhitespace/ParenPadTest.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+NoWhitespaceBefore">
config</a>
<br />
Expand Down

0 comments on commit deea241

Please sign in to comment.