Skip to content

Commit

Permalink
Issue checkstyle#13345: Enable RightCurlyCheckExampleTest
Browse files Browse the repository at this point in the history
  • Loading branch information
ckcherry23 authored and romani committed Jan 4, 2024
1 parent a820a89 commit a379ad8
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@

package com.puppycrawl.tools.checkstyle.checks.blocks;

import org.junit.jupiter.api.Disabled;
import static com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck.MSG_KEY_LINE_ALONE;
import static com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck.MSG_KEY_LINE_SAME;

import org.junit.jupiter.api.Test;

import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;

@Disabled("until https://github.com/checkstyle/checkstyle/issues/13345")
public class RightCurlyCheckExamplesTest extends AbstractExamplesModuleTestSupport {
@Override
protected String getPackageLocation() {
Expand All @@ -34,45 +35,48 @@ protected String getPackageLocation() {
@Test
public void testExample1() throws Exception {
final String[] expected = {

"19:5: " + getCheckMessage(MSG_KEY_LINE_SAME, "}", "5"),
"32:23: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", "23"),
"40:5: " + getCheckMessage(MSG_KEY_LINE_SAME, "}", "5"),
};

verifyWithInlineConfigParser(getPath("Example1.txt"), expected);
verifyWithInlineConfigParser(getPath("Example1.java"), expected);
}

@Test
public void testExample2() throws Exception {
final String[] expected = {

"22:21: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", "21"),
"43:47: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", "47"),
};

verifyWithInlineConfigParser(getPath("Example2.txt"), expected);
verifyWithInlineConfigParser(getPath("Example2.java"), expected);
}

@Test
public void testExample3() throws Exception {
final String[] expected = {

"35:16: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", "16"),
};

verifyWithInlineConfigParser(getPath("Example3.txt"), expected);
verifyWithInlineConfigParser(getPath("Example3.java"), expected);
}

@Test
public void testExample4() throws Exception {
final String[] expected = {

"22:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", "5"),
};

verifyWithInlineConfigParser(getPath("Example4.txt"), expected);
verifyWithInlineConfigParser(getPath("Example4.java"), expected);
}

@Test
public void testExample5() throws Exception {
final String[] expected = {

"41:16: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", "16"),
};

verifyWithInlineConfigParser(getPath("Example5.txt"), expected);
verifyWithInlineConfigParser(getPath("Example5.java"), expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*xml
<module name="Checker">
<module name="TreeWalker">
<module name="RightCurly"/>
</module>
</module>
*/

package com.puppycrawl.tools.checkstyle.checks.blocks.rightcurly;

// xdoc section -- start
public class Example1 {

public void test() {

boolean foo = false;
if (foo) {
bar();
} // violation, 'should be on the same line'
// as the next part of a multi-block statement (one that directly
// contains multiple blocks: if/else-if/else, do/while or try/catch/finally).
else {
bar();
}

if (foo) {
bar();
} else {
bar();
}

if (foo) { bar(); } int i = 0;
// violation above, 'should be alone on a line.'

if (foo) { bar(); }
i = 0;

try {
bar();
} // violation, 'should be on the same line'
// as the next part of a multi-block statement (one that directly
// contains multiple blocks: if/else-if/else, do/while or try/catch/finally).
catch (Exception e) {
bar();
}

try {
bar();
} catch (Exception e) {
bar();
}

}

private void bar() {
}

public void testSingleLine() { bar(); } // OK, because singleline is allowed
}
// xdoc section -- end

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,42 @@
</module>
*/

package com.puppycrawl.tools.checkstyle.checks.blocks.rightcurly;

// xdoc section -- start
public class Test {
public class Example2 {

public void test() {

boolean foo = false;
if (foo) {
bar();
} else { bar(); } // violation, right curly must be alone on line
} else { bar(); }
// violation above, 'should be alone on a line.'

if (foo) {
bar();
} else {
bar();
} // OK
}

try {
bar();
} catch (Exception e) { // OK because config is set to token METHOD_DEF and LITERAL_ELSE
} catch (Exception e) {
// OK above because config is set to token METHOD_DEF and LITERAL_ELSE
bar();
}

} // OK
}

private void bar() {
}

public void violate() { bar; } // violation, singleline is not allowed here
public void violate() { Object bar = "bar"; }
// violation above, 'should be alone on a line.'

public void ok() {
bar();
} // OK
}
}
// xdoc section -- end
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*xml
<module name="Checker">
<module name="TreeWalker">
<module name="RightCurly">
<property name="option" value="alone"/>
<property name="tokens" value="LITERAL_SWITCH"/>
</module>
</module>
</module>
*/

package com.puppycrawl.tools.checkstyle.checks.blocks.rightcurly;

// xdoc section -- start
class Example3 {

public void method0() {
int mode = 0;
switch (mode) {
case 1:
int x = 1;
break;
default:
x = 0;
} // ok, RightCurly is alone
}

public void method01() {
int mode = 0;
switch (mode) {
case 1:
int x = 1;
break;
default:
x = 0; }
// violation above, 'should be alone on a line.'
}

}
// xdoc section -- end

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,37 @@
</module>
*/

package com.puppycrawl.tools.checkstyle.checks.blocks.rightcurly;

// xdoc section -- start
public class Test {
public class Example4 {

public void test() {

boolean foo = false;
if (foo) {
bar();
} else { // violation, right curly must be alone on line
} else { // violation, 'should be alone on a line.'
bar();
}

if (foo) {
bar();
} // OK
}
else {
bar();
}

try {
bar();
} catch (Exception e) { // OK because config did not set token LITERAL_TRY
} catch (Exception e) { // OK because config did not set token LITERAL_TRY
bar();
}

} // OK
}

private void bar() {
}

public void violate() { bar(); } // OK , because singleline
}
Expand Down
Loading

0 comments on commit a379ad8

Please sign in to comment.