1 45 46 package edu.rice.cs.drjava.model; 47 48 import javax.swing.text.BadLocationException ; 49 import java.util.List ; 50 51 import edu.rice.cs.drjava.model.definitions.indent.Indenter; 52 import edu.rice.cs.util.OperationCanceledException; 53 54 60 public final class GlobalIndentTest extends GlobalModelTestCase { 61 private static final String FOO_EX_1 = "public class Foo {\n"; 62 private static final String FOO_EX_2 = "int foo;\n"; 63 private static final String BAR_CALL_1 = "bar(monkey,\n"; 64 private static final String BAR_CALL_2 = "banana)\n"; 65 68 73 public void testIndentGrowTabAtStart() throws BadLocationException , OperationCanceledException { 74 OpenDefinitionsDocument openDoc = _getOpenDoc(); 75 76 openDoc.insertString(0, FOO_EX_1, null); 77 openDoc.insertString(FOO_EX_1.length(), " " + FOO_EX_2, null); 78 openDoc.setCurrentLocation(FOO_EX_1.length()); 79 int loc = openDoc.getCurrentLocation(); 80 openDoc.indentLines(loc, loc, Indenter.OTHER, null); 81 _assertContents(FOO_EX_1 + " " + FOO_EX_2, openDoc); 82 _assertLocation(FOO_EX_1.length() + 2, openDoc); 83 } 84 85 89 public void testIndentGrowTabAtMiddle() throws BadLocationException , OperationCanceledException { 90 OpenDefinitionsDocument openDoc = _getOpenDoc(); 91 92 openDoc.insertString(0, FOO_EX_1, null); 93 openDoc.insertString(FOO_EX_1.length(), " " + FOO_EX_2, null); 94 openDoc.setCurrentLocation(FOO_EX_1.length() + 5); 95 int loc = openDoc.getCurrentLocation(); 96 openDoc.indentLines(loc, loc, Indenter.OTHER, null); 97 _assertContents(FOO_EX_1 + " " + FOO_EX_2, openDoc); 98 _assertLocation(FOO_EX_1.length() + 6, openDoc); 99 } 100 101 105 public void testIndentGrowTabAtEnd() throws BadLocationException , OperationCanceledException { 106 OpenDefinitionsDocument openDoc = _getOpenDoc(); 107 108 openDoc.insertString(0, FOO_EX_1, null); 109 openDoc.insertString(FOO_EX_1.length(), " " + FOO_EX_2, null); 110 openDoc.setCurrentLocation(openDoc.getLength() - 1); 111 int loc = openDoc.getCurrentLocation(); 112 openDoc.indentLines(loc, loc, Indenter.OTHER, null); 113 _assertContents(FOO_EX_1 + " " + FOO_EX_2, openDoc); 114 _assertLocation(openDoc.getLength() - 1, openDoc); 115 } 116 117 122 public void testIndentShrinkTabAtStart() throws BadLocationException , OperationCanceledException{ 123 OpenDefinitionsDocument openDoc = _getOpenDoc(); 124 125 openDoc.insertString(0, FOO_EX_1, null); 126 openDoc.insertString(FOO_EX_1.length(), " " + FOO_EX_2, null); 127 openDoc.setCurrentLocation(FOO_EX_1.length()); 128 int loc = openDoc.getCurrentLocation(); 129 openDoc.indentLines(loc, loc, Indenter.OTHER, null); 130 _assertContents(FOO_EX_1 + " " + FOO_EX_2, openDoc); 131 _assertLocation(FOO_EX_1.length() + 2, openDoc); 132 } 133 134 138 public void testIndentShrinkTabAtMiddle() throws BadLocationException , OperationCanceledException { 139 OpenDefinitionsDocument openDoc = _getOpenDoc(); 140 141 openDoc.insertString(0, FOO_EX_1, null); 142 openDoc.insertString(FOO_EX_1.length(), " " + FOO_EX_2, null); 143 openDoc.setCurrentLocation(FOO_EX_1.length() + 5); 144 int loc = openDoc.getCurrentLocation(); 145 openDoc.indentLines(loc, loc, Indenter.OTHER, null); 146 _assertContents(FOO_EX_1 + " " + FOO_EX_2, openDoc); 147 _assertLocation(FOO_EX_1.length() + 4, openDoc); 148 } 149 150 154 public void testIndentShrinkTabAtEnd() 155 throws BadLocationException , OperationCanceledException { 156 OpenDefinitionsDocument openDoc = _getOpenDoc(); 157 158 openDoc.insertString(0, FOO_EX_1, null); 159 openDoc.insertString(FOO_EX_1.length(), " " + FOO_EX_2, null); 160 openDoc.setCurrentLocation(openDoc.getLength() - 1); 161 int loc = openDoc.getCurrentLocation(); 162 openDoc.indentLines(loc, loc, Indenter.OTHER, null); 163 _assertContents(FOO_EX_1 + " " + FOO_EX_2, openDoc); 164 _assertLocation(openDoc.getLength() - 1, openDoc); 165 } 166 167 170 public void testIndentSameAsLineAboveAtStart() throws BadLocationException , OperationCanceledException { 171 OpenDefinitionsDocument openDoc = _getOpenDoc(); 172 173 openDoc.insertString(0, FOO_EX_2, null); 174 openDoc.insertString(FOO_EX_2.length(), " " + FOO_EX_2, null); 175 openDoc.setCurrentLocation(FOO_EX_2.length()); 176 int loc = openDoc.getCurrentLocation(); 177 openDoc.indentLines(loc, loc, Indenter.OTHER, null); 178 _assertContents(FOO_EX_2 + FOO_EX_2, openDoc); 179 _assertLocation(FOO_EX_2.length(), openDoc); 180 } 181 182 185 public void testIndentSameAsLineAboveAtEnd() throws BadLocationException , OperationCanceledException { 186 OpenDefinitionsDocument openDoc = _getOpenDoc(); 187 188 openDoc.insertString(0, FOO_EX_2, null); 189 openDoc.insertString(FOO_EX_2.length(), " " + FOO_EX_2, null); 190 openDoc.setCurrentLocation(openDoc.getLength() - 1); 191 int loc = openDoc.getCurrentLocation(); 192 openDoc.indentLines(loc, loc, Indenter.OTHER, null); 193 _assertContents(FOO_EX_2 + FOO_EX_2, openDoc); 194 _assertLocation(openDoc.getLength() - 1, openDoc); 195 } 196 197 202 public void testIndentInsideParenAtStart() throws BadLocationException , OperationCanceledException { 203 OpenDefinitionsDocument openDoc = _getOpenDoc(); 204 205 openDoc.insertString(0, BAR_CALL_1, null); 206 openDoc.insertString(BAR_CALL_1.length(), BAR_CALL_2, null); 207 openDoc.setCurrentLocation(BAR_CALL_1.length()); 208 int loc = openDoc.getCurrentLocation(); 209 openDoc.indentLines(loc, loc, Indenter.OTHER, null); 210 _assertContents(BAR_CALL_1 + " " + BAR_CALL_2, openDoc); 211 _assertLocation(BAR_CALL_1.length() + 4, openDoc); 212 } 213 214 217 public void testIndentInsideParenAtEnd() throws BadLocationException , OperationCanceledException { 218 OpenDefinitionsDocument openDoc = _getOpenDoc(); 219 220 openDoc.insertString(0, BAR_CALL_1, null); 221 openDoc.insertString(BAR_CALL_1.length(), BAR_CALL_2, null); 222 openDoc.setCurrentLocation(openDoc.getLength() - 1); 223 int loc = openDoc.getCurrentLocation(); 224 openDoc.indentLines(loc, loc, Indenter.OTHER, null); 225 _assertContents(BAR_CALL_1 + " " + BAR_CALL_2, openDoc); 226 _assertLocation(openDoc.getLength() - 1, openDoc); 227 } 228 229 230 public void testIndentDoesNothing() throws BadLocationException , OperationCanceledException { 231 OpenDefinitionsDocument openDoc = _getOpenDoc(); 232 233 openDoc.insertString(0, FOO_EX_2 + FOO_EX_2, null); 234 openDoc.setCurrentLocation(openDoc.getLength() - 1); 235 int loc = openDoc.getCurrentLocation(); 236 openDoc.indentLines(loc, loc, Indenter.OTHER, null); 237 _assertContents(FOO_EX_2 + FOO_EX_2, openDoc); 238 _assertLocation(openDoc.getLength() - 1, openDoc); 239 } 240 241 242 261 262 266 276 277 private OpenDefinitionsDocument _getOpenDoc() { 278 _assertNumOpenDocs(1); 279 OpenDefinitionsDocument doc = _model.newFile(); 280 doc.setIndent(2); 281 List <OpenDefinitionsDocument> docs = _model.getOpenDefinitionsDocuments(); 282 _assertNumOpenDocs(2); 283 return docs.get(0); 284 } 285 286 private void _assertNumOpenDocs(int num) { 287 assertEquals("number of open documents", num, _model.getOpenDefinitionsDocuments().size()); 288 } 289 290 private void _assertContents(String expected, OpenDefinitionsDocument document) throws BadLocationException { 291 assertEquals("document contents", expected, document.getText()); 292 } 293 294 private void _assertLocation(int loc, OpenDefinitionsDocument openDoc) { 295 assertEquals("current def'n loc", loc, openDoc.getCurrentLocation()); 296 } 297 } 298 | Popular Tags |