Add test coverage to MethodCallHandler in Indentation check. #1270

This commit is contained in:
Michal Kordas 2015-07-17 23:22:30 +02:00 committed by Roman Ivanov
parent 4f16cb85a9
commit 3d4290baf0
5 changed files with 76 additions and 52 deletions

View File

@ -1162,7 +1162,6 @@
<regex><pattern>.*.checks.indentation.ImportHandler</pattern><branchRate>50</branchRate><lineRate>87</lineRate></regex>
<regex><pattern>.*.checks.indentation.IndentationCheck</pattern><branchRate>100</branchRate><lineRate>93</lineRate></regex>
<regex><pattern>.*.checks.indentation.LineWrappingHandler</pattern><branchRate>87</branchRate><lineRate>95</lineRate></regex>
<regex><pattern>.*.checks.indentation.MethodCallHandler</pattern><branchRate>63</branchRate><lineRate>87</lineRate></regex>
<regex><pattern>.*.checks.indentation.MethodCallLineWrapHandler</pattern><branchRate>0</branchRate><lineRate>0</lineRate></regex>
<regex><pattern>.*.checks.indentation.MethodDefHandler</pattern><branchRate>87</branchRate><lineRate>100</lineRate></regex>
<regex><pattern>.*.checks.indentation.NewHandler</pattern><branchRate>83</branchRate><lineRate>77</lineRate></regex>

View File

@ -38,11 +38,7 @@ public class MethodCallHandler extends AbstractExpressionHandler {
*/
public MethodCallHandler(IndentationCheck indentCheck,
DetailAST ast, AbstractExpressionHandler parent) {
super(indentCheck,
ast.getType() == TokenTypes.METHOD_CALL
? "method call" : "ctor call",
ast,
parent);
super(indentCheck, "method call", ast, parent);
}
@Override
@ -52,30 +48,17 @@ public class MethodCallHandler extends AbstractExpressionHandler {
if (getParent() instanceof MethodCallHandler) {
final MethodCallHandler container =
(MethodCallHandler) getParent();
if (container != null) {
if (areOnSameLine(container.getMainAst(), getMainAst())) {
return container.getLevel();
}
// we should increase indentation only if this is the first
// chained method call which was moved to the next line
if (isChainedMethodCallWrapped()) {
return container.getLevel();
}
else {
return new IndentLevel(container.getLevel(), getBasicOffset());
}
if (areOnSameLine(container.getMainAst(), getMainAst())) {
return container.getLevel();
}
// if we get here, we are the child of the left hand side (name
// side) of a method call with no "containing" call, use
// the first non-method call parent
AbstractExpressionHandler p = getParent();
while (p instanceof MethodCallHandler) {
p = p.getParent();
// we should increase indentation only if this is the first
// chained method call which was moved to the next line
if (isChainedMethodCallWrapped()) {
return container.getLevel();
}
else {
return new IndentLevel(container.getLevel(), getBasicOffset());
}
return p.suggestedChildLevel(this);
}
// if our expression isn't first on the line, just use the start
@ -100,15 +83,12 @@ public class MethodCallHandler extends AbstractExpressionHandler {
final DetailAST dot = main.getFirstChild();
final DetailAST target = dot.getFirstChild();
if (dot.getType() == TokenTypes.DOT
&& target.getType() == TokenTypes.METHOD_CALL) {
final DetailAST dot1 = target.getFirstChild();
final DetailAST target1 = dot1.getFirstChild();
final DetailAST dot1 = target.getFirstChild();
final DetailAST target1 = dot1.getFirstChild();
if (dot1.getType() == TokenTypes.DOT
&& target1.getType() == TokenTypes.METHOD_CALL) {
result = true;
}
if (dot1.getType() == TokenTypes.DOT
&& target1.getType() == TokenTypes.METHOD_CALL) {
result = true;
}
return result;
}
@ -126,14 +106,9 @@ public class MethodCallHandler extends AbstractExpressionHandler {
// call name
DetailAST astNode = ast.getFirstChild();
while (astNode != null && astNode.getType() == TokenTypes.DOT) {
while (astNode.getType() == TokenTypes.DOT) {
astNode = astNode.getFirstChild();
}
if (astNode == null) {
astNode = ast;
}
return astNode;
}
@ -157,8 +132,7 @@ public class MethodCallHandler extends AbstractExpressionHandler {
@Override
public void checkIndentation() {
final DetailAST exprNode = getMainAst().getParent();
if (exprNode.getParent().getType() != TokenTypes.LCURLY
&& exprNode.getParent().getType() != TokenTypes.SLIST) {
if (exprNode.getParent().getType() != TokenTypes.SLIST) {
return;
}
final DetailAST methodName = getMainAst().getFirstChild();
@ -197,13 +171,6 @@ public class MethodCallHandler extends AbstractExpressionHandler {
* method calls are chained returns right paren for last call.
*/
private static DetailAST getMethodCallLastNode(DetailAST firstNode) {
DetailAST lastNode;
if (firstNode.getNextSibling() == null) {
lastNode = firstNode.getLastChild();
}
else {
lastNode = firstNode.getNextSibling();
}
return lastNode;
return firstNode.getLastChild();
}
}

View File

@ -1470,6 +1470,27 @@ public class IndentationCheckTest extends BaseCheckTestSupport {
verifyWarns(checkConfig, getPath("indentation/InputSynchronizedMethod.java"), expected);
}
@Test
public void testAnonymousClassInMethod() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class);
checkConfig.addAttribute("tabWidth", "8");
checkConfig.addAttribute("basicOffset", "2");
checkConfig.addAttribute("braceAdjustment", "0");
checkConfig.addAttribute("caseIndent", "2");
checkConfig.addAttribute("lineWrappingIndentation", "4");
checkConfig.addAttribute("throwsIndent", "4");
checkConfig.addAttribute("arrayInitIndent", "2");
final String[] expected = {
"19: " + getCheckMessage(MSG_ERROR, "method def modifier", 8, 2),
"20: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 16, 4),
"21: " + getCheckMessage(MSG_ERROR_MULTI, "method def modifier", 24, "18, 20, 22"),
"23: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "method def", 32, "20, 22, 24"),
"24: " + getCheckMessage(MSG_ERROR_MULTI, "method def rcurly", 24, "18, 20, 22"),
"26: " + getCheckMessage(MSG_ERROR, "method def rcurly", 8, 2),
};
verifyWarns(checkConfig, getPath("indentation/InputAnonymousClassInMethod.java"), expected);
}
@Test
public void testAnnotationDefinition() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class);

View File

@ -0,0 +1,27 @@
package com.puppycrawl.tools.checkstyle.indentation; //indent:0 exp:0
import java.io.File; //indent:0 exp:0
import java.io.FileFilter; //indent:0 exp:0
/** //indent:0 exp:0
* This test-input is intended to be checked using following configuration: //indent:1 exp:1
* //indent:1 exp:1
* arrayInitIndent = 2 //indent:1 exp:1
* basicOffset = 2 //indent:1 exp:1
* braceAdjustment = 0 //indent:1 exp:1
* caseIndent = 2 //indent:1 exp:1
* forceStrictCondition = false //indent:1 exp:1
* lineWrappingIndentation = 4 //indent:1 exp:1
* tabWidth = 8 //indent:1 exp:1
* throwsIndent = 4 //indent:1 exp:1
*/ //indent:1 exp:1
public class InputAnonymousClassInMethod { //indent:0 exp:0
private void walkDir(File dir, FileFilter fileFilter) { //indent:8 exp:2 warn
walkDir( dir, new FileFilter() { //indent:16 exp:4 warn
@Override //indent:24 exp:8 warn
public boolean accept(File path) { //indent:24 exp:24
return ( path.isDirectory() ); //indent:32 exp:12 warn
} //indent:24 exp:8 warn
} ); //indent:16 exp:16
} //indent:8 exp:2 warn
} //indent:0 exp:0

View File

@ -52,4 +52,14 @@ public class InputMethodCallLineWrap { //indent:0 exp:0
); //indent:14 exp:16 warn
} //indent:8 exp:8
}; //indent:4 exp:4
void chaining() { //indent:4 exp:4
toString() //indent:8 exp:8
.getClass(); //indent:16 exp:16
toString().contains(//indent:8 exp:8
new String(//indent:12 exp:12
"a" //indent:20 exp:20
)//indent:12 exp:12
); //indent:8 exp:8
} //indent:4 exp:4
} //indent:0 exp:0