1 33 34 package edu.rice.cs.drjava.model.definitions.indent; 35 36 import javax.swing.text.BadLocationException ; 37 38 43 public final class QuestionCurrLineStartsWithTest extends IndentRulesTestCase { 44 45 46 public void testNoPrefix() throws BadLocationException { 47 IndentRuleQuestion rule = new QuestionCurrLineStartsWith("{", null, null); 48 49 _setDocText("foo();\n}\n"); 51 assertTrue("no open brace", !rule.applyRule(_doc, 0, Indenter.OTHER)); 52 assertTrue("line of close brace (no open brace)", !rule.applyRule(_doc, 7, Indenter.OTHER)); 53 assertTrue("line after close brace (no open brace)", !rule.applyRule(_doc, 8, Indenter.OTHER)); 54 55 rule = new QuestionCurrLineStartsWith("}", null, null); 57 _setDocText("{\nfoo();"); 58 assertTrue("no close brace", !rule.applyRule(_doc, 0, Indenter.OTHER)); 59 } 60 61 64 public void testStartsWithPrefixWithText() throws BadLocationException { 65 IndentRuleQuestion rule = new QuestionCurrLineStartsWith("}", null, null); 66 67 _setDocText("foo();\n}bar();\n"); 69 assertTrue("line before brace (no space)", !rule.applyRule(_doc, 0, Indenter.OTHER)); 70 assertTrue("just before brace (no space)", rule.applyRule(_doc, 7, Indenter.OTHER)); 71 assertTrue("just after brace (no space)", rule.applyRule(_doc, 9, Indenter.OTHER)); 72 assertTrue("line after brace (no space)", !rule.applyRule(_doc, 15, Indenter.OTHER)); 73 74 rule = new QuestionCurrLineStartsWith("*", null, null); 76 _setDocText("foo\n * comment\nbar"); 77 assertTrue("line before star (with space)", !rule.applyRule(_doc, 0, Indenter.OTHER)); 78 assertTrue("just before star (with space)", rule.applyRule(_doc, 4, Indenter.OTHER)); 79 assertTrue("just after star (with space)", rule.applyRule(_doc, 6, Indenter.OTHER)); 80 assertTrue("line after star (with space)", !rule.applyRule(_doc, 15, Indenter.OTHER)); 81 } 82 83 86 public void testStartsWithPrefixNoText() throws BadLocationException { 87 IndentRuleQuestion rule = new QuestionCurrLineStartsWith("{", null, null); 88 89 _setDocText("foo();\n{\nbar();\n"); 91 assertTrue("line before brace (no space)", !rule.applyRule(_doc, 0, Indenter.OTHER)); 92 assertTrue("just before brace (no space)", rule.applyRule(_doc, 7, Indenter.OTHER)); 93 assertTrue("just after brace (no space)", rule.applyRule(_doc, 8, Indenter.OTHER)); 94 assertTrue("line after brace (no space)", !rule.applyRule(_doc, 10, Indenter.OTHER)); 95 96 _setDocText("foo();\n {\nbar();\n"); 98 assertTrue("line before brace (with space)", !rule.applyRule(_doc, 0, Indenter.OTHER)); 99 assertTrue("just before brace (with space)", rule.applyRule(_doc, 7, Indenter.OTHER)); 100 assertTrue("just after brace (with space)", rule.applyRule(_doc, 11, Indenter.OTHER)); 101 assertTrue("line after brace (with space)", !rule.applyRule(_doc, 14, Indenter.OTHER)); 102 } 103 104 107 public void testMultipleCharPrefix() throws BadLocationException { 108 IndentRuleQuestion rule = new QuestionCurrLineStartsWith(".*.", null, null); 109 110 _setDocText("*\n.*\n.*.\n.*.foo"); 112 assertTrue("star", !rule.applyRule(_doc, 0, Indenter.OTHER)); 113 assertTrue("dot star", !rule.applyRule(_doc, 2, Indenter.OTHER)); 114 assertTrue("dot star dot", rule.applyRule(_doc, 7, Indenter.OTHER)); 115 assertTrue("dot star dot text", rule.applyRule(_doc, 9, Indenter.OTHER)); 116 } 117 118 121 public void testCommentedPrefixDontSearchComment() throws BadLocationException { 122 IndentRuleQuestion rule = new QuestionCurrLineStartsWith("{", null, null); 123 124 _setDocText("foo();\n// {\nbar();\n"); 126 assertTrue("just before brace", !rule.applyRule(_doc, 7, Indenter.OTHER)); 127 assertTrue("just after brace", !rule.applyRule(_doc, 11, Indenter.OTHER)); 128 assertTrue("line after brace", !rule.applyRule(_doc, 12, Indenter.OTHER)); 129 } 130 131 134 public void testCommentedPrefixSearchComment() throws BadLocationException { 135 IndentRuleQuestion rule = new QuestionCurrLineStartsWith("*", null, null); 136 137 _setDocText("/**\n* \ncomment\n"); 139 assertTrue("line before star", !rule.applyRule(_doc, 0, Indenter.OTHER)); 140 assertTrue("just before star", rule.applyRule(_doc, 4, Indenter.OTHER)); 141 assertTrue("just after star", rule.applyRule(_doc, 6, Indenter.OTHER)); 142 assertTrue("line after star", !rule.applyRule(_doc, 7, Indenter.OTHER)); 143 } 144 145 148 public void testDoesNotStartWithPrefix() throws BadLocationException { 149 IndentRuleQuestion rule = new QuestionCurrLineStartsWith("}", null, null); 150 151 _setDocText("foo(); }\nbar();\n"); 153 assertTrue("before brace", !rule.applyRule(_doc, 0, Indenter.OTHER)); 154 assertTrue("just before brace", !rule.applyRule(_doc, 7, Indenter.OTHER)); 155 assertTrue("just after brace", !rule.applyRule(_doc, 8, Indenter.OTHER)); 156 assertTrue("line after brace", !rule.applyRule(_doc, 10, Indenter.OTHER)); 157 } 158 159 162 public void testPrefixAtEnd() throws BadLocationException { 163 IndentRuleQuestion rule = new QuestionCurrLineStartsWith("}", null, null); 164 165 _setDocText("void foo() {\n}"); 166 assertTrue("first line", !rule.applyRule(_doc, 3, Indenter.OTHER)); 167 assertTrue("end of first line", !rule.applyRule(_doc, 12, Indenter.OTHER)); 168 assertTrue("beginning of second line", rule.applyRule(_doc, 13, Indenter.OTHER)); 169 assertTrue("end of second line", rule.applyRule(_doc, 14, Indenter.OTHER)); 170 } 171 172 175 public void testMultCharPrefix() throws BadLocationException { 176 IndentRuleQuestion rule = new QuestionCurrLineStartsWith("abcdefg", null, null); 177 178 _setDocText(" abcdefghij\n abcde"); 179 assertTrue("first line, beginning", rule.applyRule(_doc, 0, Indenter.OTHER)); 180 assertTrue("first line, mid", rule.applyRule(_doc, 6, Indenter.OTHER)); 181 assertTrue("first line, end", rule.applyRule(_doc, 13, Indenter.OTHER)); 182 assertTrue("second line, beginning", !rule.applyRule(_doc, 14, Indenter.OTHER)); 183 assertTrue("second line, mid", !rule.applyRule(_doc, 18, Indenter.OTHER)); 184 assertTrue("second line, end", !rule.applyRule(_doc, 21, Indenter.OTHER)); 185 } 186 } | Popular Tags |