package com.intellij.codeInsight.unwrap; public class UnwrapIfTest extends UnwrapTestCase { public void testDoesNothingOutsideOfStatements() throws Exception { assertUnwrapped("int i = 0;\n", "int i = 0;\n"); } public void testIfWithSingleStatement() throws Exception { assertUnwrapped("if(true) System.gc();\n", "System.gc();\n"); } public void testIfBlock() throws Exception { assertUnwrapped("if(true) {\n" + " int i;\n" + "}\n", "int i;\n"); } public void testIfBlockWithComment() throws Exception { assertUnwrapped("if(true) {\n" + " // a comment\n" + " int i;\n" + "}\n", "// a comment\n" + "int i;\n"); } public void testIfMultiStatementBlock() throws Exception { assertUnwrapped("if(true) {\n" + " int i;\n" + "\n" + " int j;\n" + "}\n", "int i;\n" + "\n" + "int j;\n"); } public void testIfEmpty()throws Exception { assertUnwrapped("{\n" + " if(true);\n" + "}\n", "{\n" + "}\n"); } public void testIfEmptyBlock() throws Exception { assertUnwrapped("{\n" + " if(true) {}\n" + "}\n", "{\n" + "}\n"); } public void testIfIncomplete() throws Exception { assertUnwrapped("{\n" + " if(true)\n" + "}\n", "{\n" + "}\n"); } public void testDoesNotAffectNeighbours() throws Exception { assertUnwrapped("if(true) {}\n" + "if(false) {}\n" + "if(true && false) {}\n", "if(true) {}\n" + "if(true && false) {}\n"); } public void testIfWithElse() throws Exception { assertUnwrapped("if(true) {\n" + " int i;\n" + "} else {\n" + " int j;\n" + "}\n", "int i;\n"); } public void testIfWithElses() throws Exception { assertUnwrapped("if(true) {\n" + " int i;\n" + "} else if (true) {\n" + " int j;\n" + "} else {\n" + " int k;\n" + "}\n", "int i;\n"); } public void testIfElseIncomplete() throws Exception { assertUnwrapped("if(true) {\n" + " int i;\n" + "} else \n", "int i;\n"); } public void testUnwrapElse() throws Exception { assertUnwrapped("{\n" + " if(true) {\n" + " int i;\n" + " } else System.gc();\n" + "}\n", "{\n" + " System.gc();\n" + "}\n"); } public void testUnwrapElseBlock() throws Exception { assertUnwrapped("{\n" + " if(true) {\n" + " int i;\n" + " } else {\n" + " int j;\n" + " }\n" + "}\n", "{\n" + " int j;\n" + "}\n"); } public void testUnwrapElseIf() throws Exception { assertUnwrapped("{\n" + " if(true) {\n" + " int i;\n" + " } else if (false) {\n" + " int j;\n" + " }\n" + "}\n", "{\n" + " int j;\n" + "}\n"); } public void testUnwrapIfElseIfElse() throws Exception { assertUnwrapped("{\n" + " if(true) {\n" + " int i;\n" + " } else if (false) {\n" + " int j;\n" + " } else {\n" + " int k;\n" + " }\n" + "}\n", "{\n" + " int k;\n" + "}\n"); } public void testUnwrapElseWhenCaretRightInTheElseKeyword() throws Exception { assertUnwrapped("{\n" + " if(true) {\n" + " int i;\n" + " } else {\n" + " int j;\n" + " }\n" + "}\n", "{\n" + " int j;\n" + "}\n"); } public void testRemovesWhileIfWhenElseIsIsIncomplete() throws Exception { assertUnwrapped("{\n" + " if(true) {\n" + " int i;\n" + " } else \n" + "}\n", "{\n" + " int i;\n" + "}\n"); } public void testRemoveElse() throws Exception { assertUnwrapped("{\n" + " if(true) {\n" + " int i;\n" + " } else {\n" + " int j;\n" + " }\n" + "}\n", "{\n" + " if(true) {\n" + " int i;\n" + " }\n" + "}\n", 1); } public void testRemoveElseWhenCaretRightInTheElseKeyword() throws Exception { assertUnwrapped("{\n" + " if(true) {\n" + " int i;\n" + " } else {\n" + " int j;\n" + " }\n" + "}\n", "{\n" + " if(true) {\n" + " int i;\n" + " }\n" + "}\n", 1); } public void testRemoveElseIf() throws Exception { assertUnwrapped("{\n" + " if(true) {\n" + " int i;\n" + " } else if(true) {\n" + " int j;\n" + " }\n" + "}\n", "{\n" + " if(true) {\n" + " int i;\n" + " }\n" + "}\n", 1); } public void testRemoveElseIfElse() throws Exception { assertUnwrapped("if(true) {\n" + " int i;\n" + "} else if (true) {\n" + " int j;\n" + "} else {\n" + " int k;\n" + "}\n", "if(true) {\n" + " int i;\n" + "} else {\n" + " int k;\n" + "}\n", 1); } public void testIfOption() throws Exception { assertOptions("if (true) {\n" + " \n" + "}\n", "Unwrap 'if...'"); } public void testIfElseOption() throws Exception { assertOptions("if (true) {\n" + "} else {\n" + " \n" + "}\n", "Unwrap 'else...'", "Remove 'else...'"); } public void testDoesNotIncludeDirectParentIfsWhenElseIsSelected() throws Exception { assertOptions("if (true) {\n" + "} else if (true) {\n" + "} else if (true) {\n" + "} else {\n" + " \n" + "}\n", "Unwrap 'else...'", "Remove 'else...'"); } public void testDoesNotIncludeIndirectParentIfsWhenElseIsSelected() throws Exception { assertOptions("if (true) {\n" + "} else if (true) {\n" + "} else {\n" + " if (true) {\n" + " } else {\n" + " \n" + " }\n" + "}\n", "Unwrap 'else...'", "Remove 'else...'", "Unwrap 'else...'", "Remove 'else...'"); } public void testIfElseIfOption() throws Exception { assertOptions("if (true) {\n" + "} else if (false) {\n" + " \n" + "}\n", "Unwrap 'else...'", "Remove 'else...'"); } public void testIfInsideElseOption() throws Exception { assertOptions("if (true) {\n" + "} else {\n" + " if (false) {\n" + " \n" + " }\n" + "}\n", "Unwrap 'if...'", "Unwrap 'else...'", "Remove 'else...'"); } public void testIfElseOptionWhenCaretIsRughtOnTheElseKeyword() throws Exception { assertOptions("if (true) {\n" + "} else {\n" + "}\n", "Unwrap 'else...'", "Remove 'else...'"); } }