1 33 34 package edu.rice.cs.drjava.model.definitions; 35 36 import javax.swing.text.BadLocationException ; 37 import edu.rice.cs.drjava.model.definitions.indent.IndentRulesTestCase; 38 39 43 public final class IndentHelperTest extends IndentRulesTestCase { 44 45 46 public void testFindPrevDelimiter() throws BadLocationException { 47 char[] delimiters1 = {';', ':', '?'}; 48 49 char[] delimiters2 = {'%'}; 51 52 char[] delimiters3 = {'f'}; 54 55 char[] delimiters4 = {'/', '*'}; 57 58 _setDocText("/*bar;\nfoo();\nx;*/\nreturn foo;\n"); 59 assertEquals("Check that delimiters in multi-line " + 60 "comments are ignored", 61 DefinitionsDocument.ERROR_INDEX, 62 _doc.findPrevDelimiter(23, delimiters1)); 63 64 _setDocText("foo();\n//bar();\nbiz();\n"); 65 assertEquals("Check that delimiters in single-line " + 66 "comments are ignored", 67 5, 68 _doc.findPrevDelimiter(16, delimiters1)); 69 70 _setDocText("x=';'\n"); 71 assertEquals("Check that delimiters in single-quotes " + 72 "are ignored", 73 DefinitionsDocument.ERROR_INDEX, 74 _doc.findPrevDelimiter(5, delimiters1)); 75 76 _setDocText("x=\";\"\n"); 77 assertEquals("Check that delimiters in double-quotes " + 78 "are ignored", 79 DefinitionsDocument.ERROR_INDEX, 80 _doc.findPrevDelimiter(5, delimiters1)); 81 82 _setDocText("foo();\nfor(;;)\n"); 83 assertEquals("Check that delimiters in paren phrases " + 84 "are usually ignored", 85 5, 86 _doc.findPrevDelimiter(14, delimiters1)); 87 88 _setDocText("foo();\nfor(;;)\n"); 89 assertEquals("Check that delimiters in paren phrases " + 90 "can be detected", 91 12, 92 _doc.findPrevDelimiter(14, delimiters1, false)); 93 94 _setDocText("foo();\n test ? x : y;\n\t return blah();\n"); 95 assertEquals("Check that ERROR_INDEX is returned if no matching character is found", 96 DefinitionsDocument.ERROR_INDEX, 97 _doc.findPrevDelimiter(20, delimiters2)); 98 assertEquals("Check that delimiter is found if it is right after DOCSTART", 99 0, 100 _doc.findPrevDelimiter(20, delimiters3)); 101 assertEquals("Check that delimiter is not found if " + 102 "it is at cursor's position", 103 DefinitionsDocument.ERROR_INDEX, 104 _doc.findPrevDelimiter(5, delimiters1)); 105 assertEquals("Check that the first delimiter in the list is found", 106 17, 107 _doc.findPrevDelimiter(19, delimiters1)); 108 assertEquals("Check that the second delimiter in the list is found", 109 13, 110 _doc.findPrevDelimiter(17, delimiters1)); 111 assertEquals("Check that the last delimiter in the list is found", 112 5, 113 _doc.findPrevDelimiter(13, delimiters1)); 114 115 _setDocText("foo *\n" + 116 "// comment\n" + 117 "bar\n"); 118 assertEquals("Check that findprevDelimiter ignores comments even" + 119 "when delimiters include * and / (1)", 120 4, 121 _doc.findPrevDelimiter(17, delimiters4)); 122 _setDocText("foo /\n" + 123 "/* comment */\n" + 124 "bar\n"); 125 assertEquals("Check that findprevDelimiter ignores comments even" + 126 "when delimiters include * and / (2)", 127 4, 128 _doc.findPrevDelimiter(17, delimiters4)); 129 130 _setDocText("abcdefghijk"); 131 _doc.setCurrentLocation(3); 132 int reducedModelPos = _doc.getReduced().absOffset(); 133 _doc.findPrevDelimiter(8, delimiters2); 134 assertEquals("Check that position in reduced model is unaffected " + 135 "after call to findPrevDelimiter", 136 reducedModelPos, 137 _doc.getReduced().absOffset()); 138 139 140 } 141 142 143 public void testPosInParenPhrase() 144 throws BadLocationException { 145 146 _setDocText("(;)"); 147 assertEquals("';' in parent phrase", 148 true, 149 _doc.posInParenPhrase(1)); 150 151 _setDocText("abcdefghijk"); 152 _doc.setCurrentLocation(3); 153 int reducedModelPos = _doc.getReduced().absOffset(); 154 _doc.posInParenPhrase(8); 155 assertEquals("Check that position in reduced model is unaffected " + 156 "after call to posInParenPhrase", 157 reducedModelPos, 158 _doc.getReduced().absOffset()); 159 } 160 161 162 public void testGetIndentOfCurrStmtDelimiters() throws BadLocationException { 163 164 _setDocText("foo();\n"); 165 assertEquals("prev delimiter DOCSTART, no indent", 166 "", 167 _doc.getIndentOfCurrStmt(3)); 168 _setDocText(" foo();\n"); 169 assertEquals("prev delimiter DOCSTART, indent two spaces", 170 " ", 171 _doc.getIndentOfCurrStmt(7)); 172 173 _setDocText("bar();\nfoo();\n"); 174 assertEquals("prev delimiter ';', no indent", 175 "", 176 _doc.getIndentOfCurrStmt(7)); 177 _setDocText(" bar();\n foo();\n"); 178 assertEquals("prev delimiter ';', indent two spaces", 179 " ", 180 _doc.getIndentOfCurrStmt(9)); 181 182 _setDocText("void bar()\n{\nfoo();\n"); 183 assertEquals("prev delimiter '{', no indent", 184 "", 185 _doc.getIndentOfCurrStmt(13)); 186 _setDocText("void bar()\n{\n foo();\n"); 187 assertEquals("prev delimiter '{', indent two spaces", 188 " ", 189 _doc.getIndentOfCurrStmt(13)); 190 191 _setDocText("}\nfoo();\n"); 192 assertEquals("prev delimiter '}', no indent", 193 "", 194 _doc.getIndentOfCurrStmt(2)); 195 _setDocText("}\n foo();\n"); 196 assertEquals("prev delimiter '}', indent two spaces", 197 " ", 198 _doc.getIndentOfCurrStmt(2)); 199 } 200 201 public void testGetIndentOfCurrStmtDelimiterSameLine() 202 throws BadLocationException { 203 204 _setDocText("bar(); foo();\n"); 205 assertEquals("prev delimiter on same line, no indent", 206 "", 207 _doc.getIndentOfCurrStmt(6)); 208 209 _setDocText(" bar(); foo();\n"); 210 assertEquals("prev delimiter on same line, indent two spaces", 211 " ", 212 _doc.getIndentOfCurrStmt(8)); 213 } 214 215 public void testGetIndentOfCurrStmtMultipleLines() 216 throws BadLocationException { 217 218 String text = 219 " oogabooga();\n" + 220 " bar().\n" + 221 " bump().\n" + 222 " //comment\n" + 223 " /*commment\n" + 224 " *again;{}\n" + 225 " */\n" + 226 " foo();\n"; 227 228 _setDocText(text); 229 assertEquals("start stmt on previous line, indent two spaces", 230 " ", 231 _doc.getIndentOfCurrStmt(24)); 232 assertEquals("start stmt before previous line, " + 233 "cursor inside single-line comment " + 234 "indent two spaces", 235 " ", 236 _doc.getIndentOfCurrStmt(42)); 237 assertEquals("start stmt before single-line comment, " + 238 "cursor inside multi-line comment " + 239 "indent two spaces", 240 " ", 241 _doc.getIndentOfCurrStmt(56)); 242 assertEquals("start stmt before multi-line comment, " + 243 "indent two spaces", 244 " ", 245 _doc.getIndentOfCurrStmt(88)); 246 } 247 248 public void testGetIndentOfCurrStmtIgnoreDelimsInParenPhrase() 249 throws BadLocationException 250 { 251 252 String text = 253 " bar.\n (;)\nfoo();"; 254 255 _setDocText(text); 256 assertEquals("ignores delimiter in paren phrase", 257 " ", 258 _doc.getIndentOfCurrStmt(12)); 259 } 260 261 public void testGetIndentOfCurrStmtEndOfDoc() 262 throws BadLocationException 263 { 264 265 _setDocText("foo.\n"); 266 assertEquals("cursor at end of document, no indent", 267 "", 268 _doc.getIndentOfCurrStmt(5)); 269 270 } 271 272 public void testGetLineStartPos() throws BadLocationException { 273 _setDocText("foo\nbar\nblah"); 274 assertEquals("Returns position after the previous newline", 275 4, 276 _doc.getLineStartPos(6)); 277 assertEquals("Returns position after previous newline when cursor " + 278 "is at the position after the previous newline", 279 4, 280 _doc.getLineStartPos(4)); 281 assertEquals("Returns DOCSTART when there's no previous newline", 282 0, 283 _doc.getLineStartPos(2)); 284 assertEquals("Returns DOCSTART when the cursor is at DOCSTART", 285 0, 286 _doc.getLineStartPos(0)); 287 288 _setDocText("abcdefghijk"); 289 _doc.setCurrentLocation(3); 290 int reducedModelPos = _doc.getReduced().absOffset(); 291 _doc.getLineStartPos(5); 292 assertEquals("Check that position in reduced model is unaffected " + 293 "after call to getLineStartPos", 294 reducedModelPos, 295 _doc.getReduced().absOffset()); 296 } 297 298 public void testGetLineEndPos() throws BadLocationException { 299 _setDocText("foo\nbar\nblah"); 300 assertEquals("Returns position before the next newline", 301 7, 302 _doc.getLineEndPos(5)); 303 assertEquals("Returns position before the next newline when cursor " + 304 "is at the position before the next newline", 305 7, 306 _doc.getLineEndPos(7)); 307 assertEquals("Returns the end of the document when there's no next newline", 308 12, 309 _doc.getLineEndPos(9)); 310 311 _setDocText("abcdefghijk"); 312 _doc.setCurrentLocation(3); 313 int reducedModelPos = _doc.getReduced().absOffset(); 314 _doc.getLineEndPos(5); 315 assertEquals("Check that position in reduced model is unaffected " + 316 "after call to getLineEndPos", 317 reducedModelPos, 318 _doc.getReduced().absOffset()); 319 } 320 321 public void testGetLineFirstCharPos() throws BadLocationException { 322 _setDocText(" "); 323 assertEquals("Returns the end of the document if the line " + 324 "is the last line in the document and has no " + 325 "non-whitespace characters", 326 3, 327 _doc.getLineFirstCharPos(1)); 328 329 _setDocText(" \nfoo();\n"); 330 assertEquals("Returns the next newline if there are " + 331 "no non-whitespace characters on the line", 332 3, 333 _doc.getLineFirstCharPos(1)); 334 335 _setDocText("foo();\n \t bar();\nbiz()\n"); 336 assertEquals("Returns first non-whitespace character on the line " + 337 "when position is at the start of the line", 338 13, 339 _doc.getLineFirstCharPos(7)); 340 assertEquals("Returns first non-whitespace character on the line " + 341 "when the position is after the first non-ws character " + 342 "on the line", 343 13, 344 _doc.getLineFirstCharPos(16)); 345 assertEquals("Returns first non-whitespace character on the line " + 346 "when the position is at the newline", 347 13, 348 _doc.getLineFirstCharPos(19)); 349 350 351 _setDocText("abcdefghijk"); 352 _doc.setCurrentLocation(3); 353 int reducedModelPos = _doc.getReduced().absOffset(); 354 _doc.getLineFirstCharPos(5); 355 assertEquals("Check that position in reduced model is unaffected " + 356 "after call to getLineFirstCharPos", 357 reducedModelPos, 358 _doc.getReduced().absOffset()); 359 } 360 361 362 public void testGetFirstNonWSCharPos() throws BadLocationException { 363 364 _setDocText("foo();\nbar()\tx(); y();\n \t \n\nz();\n "); 365 assertEquals("Current position is non-whitespace", 0, _doc.getFirstNonWSCharPos(0)); 366 assertEquals("Current position is non-whitespace, end of line", 5, _doc.getFirstNonWSCharPos(5)); 367 assertEquals("Next non-whitespace is 1 '\\n' ahead.", 7, _doc.getFirstNonWSCharPos(6)); 368 assertEquals("Next non-whitespace is 2 '\\t' ahead.", 13, _doc.getFirstNonWSCharPos(12)); 369 assertEquals("Next non-whitespace is 3 spaces ahead.", 22, _doc.getFirstNonWSCharPos(20)); 370 assertEquals("Next non-whitespace is multiple whitespaces ('\\n', '\\t', ' ') ahead.", 371 34, 372 _doc.getFirstNonWSCharPos(27)); 373 assertEquals("Next non-whitespace is end of document", 374 -1, 375 _doc.getFirstNonWSCharPos(39)); 376 377 _setDocText("foo();\n// comment\nbar();\n"); 378 assertEquals("Ignore single-line comments", 379 18, 380 _doc.getFirstNonWSCharPos(6)); 381 382 _setDocText("foo();\n /* bar\nblah */ boo\n"); 383 assertEquals("Ignore multiline comments", 384 23, 385 _doc.getFirstNonWSCharPos(6)); 386 _setDocText("foo /"); 387 assertEquals("Slash at end of document", 388 6, 389 _doc.getFirstNonWSCharPos(4)); 390 _setDocText("foo //"); 391 assertEquals("// at end", 392 -1, 393 _doc.getFirstNonWSCharPos(4)); 394 _setDocText("foo /*"); 395 assertEquals("/* at end", 396 -1, 397 _doc.getFirstNonWSCharPos(4)); 398 399 _setDocText("abcdefghijk"); 400 _doc.setCurrentLocation(3); 401 int reducedModelPos = _doc.getReduced().absOffset(); 402 _doc.getLineFirstCharPos(5); 403 assertEquals("Check that position in reduced model is unaffected " + 404 "after call to getLineFirstCharPos", 405 reducedModelPos, 406 _doc.getReduced().absOffset()); 407 } 408 409 415 public void testGetIntelligentBeginLinePos() throws BadLocationException { 416 _setDocText(" foo();"); 417 assertEquals("simple text, in WS", 418 0, _doc.getIntelligentBeginLinePos(1)); 419 assertEquals("simple text, end of WS", 420 0, _doc.getIntelligentBeginLinePos(3)); 421 assertEquals("simple text, in text", 422 3, _doc.getIntelligentBeginLinePos(4)); 423 assertEquals("simple text, at end", 424 3, _doc.getIntelligentBeginLinePos(9)); 425 426 _setDocText(" // foo"); 427 assertEquals("comment, in WS", 428 0, _doc.getIntelligentBeginLinePos(0)); 429 assertEquals("comment, end of WS", 430 0, _doc.getIntelligentBeginLinePos(3)); 431 assertEquals("comment, in text", 432 3, _doc.getIntelligentBeginLinePos(4)); 433 assertEquals("comment, at end", 434 3, _doc.getIntelligentBeginLinePos(9)); 435 436 _setDocText(" foo();\n bar();\n"); 437 assertEquals("multiple lines, at start", 438 10, _doc.getIntelligentBeginLinePos(10)); 439 assertEquals("multiple lines, end of WS", 440 10, _doc.getIntelligentBeginLinePos(11)); 441 assertEquals("multiple lines, in text", 442 11, _doc.getIntelligentBeginLinePos(13)); 443 assertEquals("multiple lines, at end", 444 11, _doc.getIntelligentBeginLinePos(17)); 445 446 _setDocText("abc def"); 447 assertEquals("no leading WS, in middle", 448 0, _doc.getIntelligentBeginLinePos(5)); 449 } 450 } 451 452 453 | Popular Tags |