Fix RightCurlyCheck with same option not to rise expression in single-line blocks - issue #1416

This commit is contained in:
liscju 2015-07-20 14:27:44 +02:00 committed by Roman Ivanov
parent 322b8da20f
commit 1e6dc8f4ea
5 changed files with 51 additions and 7 deletions

View File

@ -112,4 +112,17 @@ public class LeftCurlyRightCurlyTest extends BaseCheckTestSupport {
Integer[] warnList = builder.getLinesWithWarn(filePath);
verify(newCheckConfig, filePath, expected, warnList);
}
@Test
public void rightCurlyTestSame() throws Exception {
DefaultConfiguration newCheckConfig = createCheckConfig(RightCurlyCheck.class);
newCheckConfig.addAttribute("option", RightCurlyOption.SAME.toString());
final String[] expected = {
};
String filePath = builder.getFilePath("RightCurlyInputSame");
Integer[] warnList = builder.getLinesWithWarn(filePath);
verify(newCheckConfig, filePath, expected, warnList);
}
}

View File

@ -0,0 +1,9 @@
package com.google.checkstyle.test.chapter4formatting.rule412nonemptyblocks;
public class RightCurlyInputSame {
public static void main(String[] args) {
boolean after = false;
try {
} finally { after = true; }
}
}

View File

@ -78,6 +78,7 @@ import com.puppycrawl.tools.checkstyle.checks.CheckUtils;
* @author o_sukhodolsky
* @author maxvetrenko
* @author Andrei Selkin
* @author <a href="mailto:piotr.listkiewicz@gmail.com">liscju</a>
*/
public class RightCurlyCheck extends AbstractOptionCheck<RightCurlyOption> {
/**
@ -199,7 +200,8 @@ public class RightCurlyCheck extends AbstractOptionCheck<RightCurlyOption> {
String violation = "";
if (bracePolicy == RightCurlyOption.SAME
&& !hasLineBreakBefore(rcurly)) {
&& !hasLineBreakBefore(rcurly)
&& lcurly.getLineNo() != rcurly.getLineNo()) {
violation = MSG_KEY_LINE_BREAK_BEFORE;
}
else if (shouldCheckLastRcurly) {

View File

@ -46,7 +46,6 @@ public class RightCurlyCheckTest extends BaseCheckTestSupport {
"40:13: " + getCheckMessage(MSG_KEY_LINE_SAME, "}", 13),
"44:13: " + getCheckMessage(MSG_KEY_LINE_SAME, "}", 13),
"93:27: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 27),
"97:54: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 54),
};
verify(checkConfig, getPath("InputLeftCurlyOther.java"), expected);
}
@ -60,11 +59,18 @@ public class RightCurlyCheckTest extends BaseCheckTestSupport {
"40:13: " + getCheckMessage(MSG_KEY_LINE_SAME, "}", 13),
"44:13: " + getCheckMessage(MSG_KEY_LINE_SAME, "}", 13),
"93:27: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 27),
"97:54: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 54),
};
verify(checkConfig, getPath("InputLeftCurlyOther.java"), expected);
}
@Test
public void testSameOmitOneLiners() throws Exception {
checkConfig.addAttribute("option", RightCurlyOption.SAME.toString());
final String[] expected = {
};
verify(checkConfig, getPath("InputRightCurlySameForOneLiners.java"), expected);
}
@Test
public void testAlone() throws Exception {
checkConfig.addAttribute("option", RightCurlyOption.ALONE.toString());
@ -125,10 +131,6 @@ public class RightCurlyCheckTest extends BaseCheckTestSupport {
@Test
public void testForceLineBreakBefore2() throws Exception {
final String[] expected = {
"24:33: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 33),
"32:44: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 44),
"32:63: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 63),
"52:48: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 48),
};
verify(checkConfig, getPath("InputRightCurlyLineBreakBefore.java"), expected);
}

View File

@ -0,0 +1,18 @@
////////////////////////////////////////////////////////////////////////////////
// Test case file for checkstyle.
// Created: 2015
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
/**
* Test case for RightCurly with option SAME to omit oneliners
* @see https://github.com/checkstyle/checkstyle/issues/1416
* @author <a href="mailto:piotr.listkiewicz@gmail.com">liscju</a>
*/
public class InputRightCurlySameForOneLiners {
public static void main(String[] args) {
boolean after = false;
try {
} finally { after = true; }
}
}