Skip to content

Commit

Permalink
Hint to add/remove parentheses #49 - testcase + support also operatio…
Browse files Browse the repository at this point in the history
…ns/returns
  • Loading branch information
markiewb committed Oct 24, 2016
1 parent 6893f69 commit 2013f80
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.ParenthesizedTree;
import com.sun.source.tree.Tree.Kind;
import static com.sun.source.tree.Tree.Kind.BOOLEAN_LITERAL;
import static com.sun.source.tree.Tree.Kind.CHAR_LITERAL;
import static com.sun.source.tree.Tree.Kind.CONDITIONAL_EXPRESSION;
import static com.sun.source.tree.Tree.Kind.DOUBLE_LITERAL;
import static com.sun.source.tree.Tree.Kind.FLOAT_LITERAL;
import static com.sun.source.tree.Tree.Kind.INT_LITERAL;
import static com.sun.source.tree.Tree.Kind.LOGICAL_COMPLEMENT;
import static com.sun.source.tree.Tree.Kind.LONG_LITERAL;
import static com.sun.source.tree.Tree.Kind.STRING_LITERAL;
import static com.sun.source.tree.Tree.Kind.*;
import com.sun.source.util.TreePath;
import org.netbeans.api.java.source.TreeMaker;
import org.netbeans.api.java.source.TreePathHandle;
Expand All @@ -35,7 +27,7 @@
"DESC_AddParentheses=Add parentheses<p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>",})
public class AddParentheses {

@TriggerTreeKind({INT_LITERAL, STRING_LITERAL, BOOLEAN_LITERAL, CHAR_LITERAL, DOUBLE_LITERAL, FLOAT_LITERAL, LOGICAL_COMPLEMENT, LONG_LITERAL, CONDITIONAL_EXPRESSION})
@TriggerTreeKind({INT_LITERAL, STRING_LITERAL, BOOLEAN_LITERAL, CHAR_LITERAL, DOUBLE_LITERAL, FLOAT_LITERAL, LOGICAL_COMPLEMENT, LONG_LITERAL, CONDITIONAL_EXPRESSION, METHOD_INVOCATION, DIVIDE, MINUS, PLUS, MULTIPLY, XOR})
@Hint(displayName = "#DN_AddParentheses", description = "#DESC_AddParentheses", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT)
public static ErrorDescription remove(HintContext ctx) {
// FIXME if errors, do not provide hints
Expand All @@ -47,7 +39,7 @@ public static ErrorDescription remove(HintContext ctx) {
ExpressionTree expressionTree = (ExpressionTree) ctx.getPath().getLeaf();
String expr = expressionTree.toString();

String label=Bundle.LABEL_AddParentheses(expr);
String label = Bundle.LABEL_AddParentheses(expr);
Fix fix = new FixImpl(TreePathHandle.create(ctx.getPath(), ctx.getInfo()), label).toEditorFix();
ErrorDescription forTree = ErrorDescriptionFactory.forTree(ctx, expressionTree, label, fix);
return forTree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void testAdd() throws Exception {
+ "}\n");

}

@Test
public void testAddComplement() throws Exception {
HintTest.create()
Expand All @@ -56,6 +57,121 @@ public void testAddComplement() throws Exception {

@Test
public void testAddToTernary() throws Exception {
HintTest.create()
.setCaretMarker('|')
.input("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int s = t|rue ? 1 : 0;\n"
+ " }\n"
+ "}\n")
.run(AddParentheses.class)
.findWarning("3:17-3:17:hint:" + Bundle.LABEL_AddParentheses("true"))
.applyFix()
.assertCompilable()
.assertOutput("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int s = (true) ? 1 : 0;\n"
+ " }\n"
+ "}\n");

}

@Test
public void testAddToReturnLiteral() throws Exception {
HintTest.create()
.setCaretMarker('|')
.input("package test;\n"
+ "public class Test {\n"
+ " public static int main(String[] args) {\n"
+ " return 4|2;\n"
+ " }\n"
+ "}\n")
.run(AddParentheses.class)
.findWarning("3:16-3:16:hint:" + Bundle.LABEL_AddParentheses("42"))
.applyFix()
.assertCompilable()
.assertOutput("package test;\n"
+ "public class Test {\n"
+ " public static int main(String[] args) {\n"
+ " return (42);\n"
+ " }\n"
+ "}\n");

}

@Test
public void testAddToReturnOperation() throws Exception {
HintTest.create()
.setCaretMarker('|')
.input("package test;\n"
+ "public class Test {\n"
+ " public static int main(String[] args) {\n"
+ " return 4|2 + 44;\n"
+ " }\n"
+ "}\n")
.run(AddParentheses.class)
.findWarning("3:16-3:16:hint:" + Bundle.LABEL_AddParentheses("42 + 44"))
.applyFix()
.assertCompilable()
.assertOutput("package test;\n"
+ "public class Test {\n"
+ " public static int main(String[] args) {\n"
+ " return (42 + 44);\n"
+ " }\n"
+ "}\n");

}

@Test
public void testAddToOperation_A() throws Exception {
HintTest.create()
.setCaretMarker('|')
.input("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int a = 4|2 + 44;\n"
+ " }\n"
+ "}\n")
.run(AddParentheses.class)
.findWarning("3:17-3:17:hint:" + Bundle.LABEL_AddParentheses("42"))
.applyFix()
.assertCompilable()
.assertOutput("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int a = (42) + 44;\n"
+ " }\n"
+ "}\n");

}

@Test
public void testAddToOperation_B() throws Exception {
HintTest.create()
.setCaretMarker('|')
.input("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int a = 4|2 + 44;\n"
+ " }\n"
+ "}\n")
.run(AddParentheses.class)
.findWarning("3:17-3:17:hint:" + Bundle.LABEL_AddParentheses("42 + 44"))
.applyFix()
.assertCompilable()
.assertOutput("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int a = (42 + 44);\n"
+ " }\n"
+ "}\n");

}

@Test
public void testAddToTernary_Surrounding() throws Exception {
HintTest.create()
.setCaretMarker('|')
.input("package test;\n"
Expand All @@ -76,6 +192,53 @@ public void testAddToTernary() throws Exception {
+ "}\n");

}

@Test
public void testAddToTernary_A() throws Exception {
HintTest.create()
.setCaretMarker('|')
.input("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int s = true ? 2|2 : 44;\n"
+ " }\n"
+ "}\n")
.run(AddParentheses.class)
.findWarning("3:24-3:24:hint:" + Bundle.LABEL_AddParentheses("22"))
.applyFix()
.assertCompilable()
.assertOutput("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int s = true ? (22) : 44;\n"
+ " }\n"
+ "}\n");

}

@Test
public void testAddToTernary_B() throws Exception {
HintTest.create()
.setCaretMarker('|')
.input("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int s = true ? 22 : 4|4;\n"
+ " }\n"
+ "}\n")
.run(AddParentheses.class)
.findWarning("3:29-3:29:hint:" + Bundle.LABEL_AddParentheses("44"))
.applyFix()
.assertCompilable()
.assertOutput("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int s = true ? 22 : (44);\n"
+ " }\n"
+ "}\n");

}

@Test
public void testAddToTernaryReturn() throws Exception {
HintTest.create()
Expand Down

0 comments on commit 2013f80

Please sign in to comment.