1 33 34 package edu.rice.cs.drjava.model.definitions.indent; 35 36 import javax.swing.text.BadLocationException ; 37 38 49 public final class QuestionExistsCharInStmtTest extends IndentRulesTestCase { 50 51 55 public void testColonInTernaryOpOneLineStmts() throws BadLocationException { 56 IndentRuleQuestion rule = new QuestionExistsCharInStmt('?', ':', null, null); 57 58 _setDocText("case 1: foo()\ncase default: break;\n"); 60 _doc.setCurrentLocation(0); 61 assertTrue("colon not in ternary op, one line stmt, no '?'", 62 !rule.applyRule(_doc, Indenter.OTHER)); 63 _doc.setCurrentLocation(16); 64 assertTrue("after newline (colon not in ternary op, one line stmt, no '?')", 65 !rule.applyRule(_doc, Indenter.OTHER)); 66 67 _setDocText("foo();\nreturn (test ? x : y;)\n"); 69 _doc.setCurrentLocation(10); 70 assertTrue("colon in ternary op, same line", 71 rule.applyRule(_doc, Indenter.OTHER)); 72 } 73 74 80 public void testColonInTernaryOpTwoStmtsOnOneLine() throws BadLocationException { 81 IndentRuleQuestion rule = new QuestionExistsCharInStmt('?', ':', null, null); 82 83 _setDocText("foo();\nreturn (test ? x : y); case default: break;\n"); 85 _doc.setCurrentLocation(7); 86 assertTrue("colon in ternary op, two stmts on one line", 87 rule.applyRule(_doc, Indenter.OTHER)); 88 89 _setDocText("foo();\ncase default: break; return test ? x : y;\n"); 90 _doc.setCurrentLocation(7); 92 assertTrue("colon not in ternary op, two stmts on one line", 93 !rule.applyRule(_doc, Indenter.OTHER)); 94 } 95 96 100 public void testColonInTernaryOpMultiLineStmts() throws BadLocationException { 101 IndentRuleQuestion rule = new QuestionExistsCharInStmt('?', ':', null, null); 102 103 _setDocText("foo();\nreturn test ?\nx : y;\n"); 105 _doc.setCurrentLocation(22); 106 assertTrue("colon in ternary op, multi-line stmt", 107 rule.applyRule(_doc, Indenter.OTHER)); 108 } 109 110 115 public void testColonInTernaryOpIgnoreWhitespaceAndComments() throws BadLocationException { 116 IndentRuleQuestion rule = new QuestionExistsCharInStmt('?', ':', null, null); 117 118 _setDocText("foo;\nreturn test ?\n \n \t \nx : y;\n"); 120 _doc.setCurrentLocation(28); 121 assertTrue("colon in ternary op, multi-line stmt, ignores whitespace", 122 rule.applyRule(_doc, Indenter.OTHER)); 123 124 _setDocText("foo();\nreturn test ? //{\n//case 1: bar();\nx() : y();\n"); 126 _doc.setCurrentLocation(42); 127 assertTrue("colon in ternary op, ignores single line comments", 128 rule.applyRule(_doc, Indenter.OTHER)); 129 130 _setDocText("foo();\nreturn test ? /* {\ncase 1 : bar();*/\nx() : y();\n"); 132 _doc.setCurrentLocation(44); 133 assertTrue("colon in ternary op, ignores multi-line comments", 134 rule.applyRule(_doc, Indenter.OTHER)); 135 } 136 137 143 public void testColonNotInTernaryOpDueToQuestionMarkInCommentsOrQuotes() throws BadLocationException { 144 IndentRuleQuestion rule = new QuestionExistsCharInStmt('?', ':', null, null); 145 146 _setDocText("foo();\nreturn test; //?\ncase default: break;\n"); 148 _doc.setCurrentLocation(38); 149 assertTrue("colon not in ternary op, ignores '?' in single-line comments", 150 !rule.applyRule(_doc, Indenter.OTHER)); 151 152 _setDocText("foo();\nreturn test; /* huh? okay */\ncase default: break;\n"); 154 _doc.setCurrentLocation(36); 155 assertTrue("colon not in ternary op, ignores '?' in multi-line comments", 156 !rule.applyRule(_doc, Indenter.OTHER)); 157 158 _setDocText("foo();\nreturn str + \"?\";\ncase default: break;\n"); 160 _doc.setCurrentLocation(25); 161 assertTrue("colon not in ternary op, ignores '?' in quotes", 162 !rule.applyRule(_doc, Indenter.OTHER)); 163 164 } 165 166 171 public void testColonNotInTernaryOpMultiLineStmts() throws BadLocationException { 172 IndentRuleQuestion rule = new QuestionExistsCharInStmt('?', ':', null, null); 173 174 _setDocText("return test ? x : y;\ncase 1\n: foo();\n"); 176 _doc.setCurrentLocation(28); 177 assertTrue("colon not in ternary op, multi-line stmt", 178 !rule.applyRule(_doc, Indenter.OTHER)); 179 180 _setDocText("foo()\nreturn test ? x :\ny; case default: break;\n"); 183 _doc.setCurrentLocation(24); 184 assertTrue("colon not in ternary op, multi-line stmt, same line as end of ternary op", 185 !rule.applyRule(_doc, Indenter.OTHER)); 186 } 187 } 188 189 190 | Popular Tags |