1 33 34 package edu.rice.cs.drjava.model.definitions; 35 36 import edu.rice.cs.drjava.DrJava; 37 import edu.rice.cs.drjava.DrJavaTestCase; 38 import edu.rice.cs.drjava.config.OptionConstants; 39 import edu.rice.cs.drjava.model.DJDocument; 40 import edu.rice.cs.drjava.model.GlobalEventNotifier; 41 42 import javax.swing.text.BadLocationException ; 43 44 48 public final class CommentTest extends DrJavaTestCase { 49 protected DefinitionsDocument doc; 50 private Integer _indentLevel = new Integer (2); 51 private GlobalEventNotifier _notifier; 53 54 58 public void setUp() throws Exception { 59 super.setUp(); 60 DrJava.getConfig().resetToDefaults(); 61 _notifier = new GlobalEventNotifier(); 62 doc = new DefinitionsDocument(_notifier); 63 DrJava.getConfig().setSetting(OptionConstants.INDENT_LEVEL,_indentLevel); 64 } 65 66 69 public void testCommentOutSingleLine() throws BadLocationException { 70 String text = 71 "Here is some abritrary text that should be commented.\n" + 72 "/* It is on multiple lines, and contains slashes // and other\n" + 73 "various */ obnoxious characters.\n"; 74 75 String commented = 76 "Here is some abritrary text that should be commented.\n" + 77 "///* It is on multiple lines, and contains slashes // and other\n" + 78 "various */ obnoxious characters.\n"; 79 80 doc.insertString(0, text, null); 81 _assertContents("Sample text is inserted improperly.", text, doc); 82 doc.commentLines(70, 75); 83 _assertContents("Only the second line should be wing-commented!", commented, doc); 84 } 85 86 89 public void testCommentOutMultipleLines() throws BadLocationException { 90 String text = 91 "Here is some abritrary text that should be commented.\n" + 92 "/* It is on multiple lines, and contains slashes // and other\n" + 93 "various */ obnoxious characters.\n"; 94 95 String commented = 96 "//Here is some abritrary text that should be commented.\n" + 97 "///* It is on multiple lines, and contains slashes // and other\n" + 98 "//various */ obnoxious characters.\n"; 99 100 doc.insertString(0, text, null); 101 _assertContents("Sample text is inserted improperly.", text, doc); 102 doc.commentLines(0, doc.getLength()); 103 _assertContents("These lines should be wing-commented!", commented, doc); 104 } 105 106 110 public void testUncommentIgnoreSingleLine() throws BadLocationException { 111 String text = 112 "Here is some abritrary text that should not be uncommented.\n" + 113 "/* It is on multiple lines, and contains slashes // and other\n" + 114 "* various */ obnoxious characters,\n" + 115 "sometimes // in block comments and sometimes not."; 116 117 doc.insertString(0, text, null); 118 _assertContents("Sample text is inserted improperly.", text, doc); 119 doc.uncommentLines(70, 75); 120 _assertContents("These lines should be unchanged by uncomment!", text, doc); 121 } 122 123 127 public void testUncommentIgnoreMultipleLines() throws BadLocationException { 128 String text = 129 "Here is some abritrary text that should not be uncommented.\n" + 130 "/* It is on multiple lines, and contains slashes // and other\n" + 131 "* various */ obnoxious characters,\n" + 132 "sometimes // in block comments and sometimes not."; 133 134 doc.insertString(0, text, null); 135 _assertContents("Sample text is inserted improperly.", text, doc); 136 doc.uncommentLines(0, doc.getLength()); 137 _assertContents("These lines should be unchanged by uncomment!", text, doc); 138 } 139 140 144 public void testUncommentSingleLine() throws BadLocationException { 145 String text = 146 "// // Here is some abritrary text that should be uncommented.\n" + 147 "// /* along with a little bit of code, just to spice\n" + 148 " //* things up.\n" + 149 "// */ \n" + 150 "// System.out.println(\"Aren't comments fun? // (yeah!)\")"; 151 152 String uncommented = 153 "// // Here is some abritrary text that should be uncommented.\n" + 154 "// /* along with a little bit of code, just to spice\n" + 155 " //* things up.\n" + 156 "// */ \n" + 157 " System.out.println(\"Aren't comments fun? // (yeah!)\")"; 158 159 doc.insertString(0, text, null); 160 _assertContents("Sample text is inserted improperly.", text, doc); 161 doc.uncommentLines(doc.getLength()-1, doc.getLength()); 162 _assertContents("The last line should have no commenting!", 163 uncommented, doc); 164 } 165 166 170 public void testUncommentMultipleLines() throws BadLocationException { 171 String text = 172 "//// Here is some abritrary text that should be uncommented.\n" + 173 "// /* along with a little bit of code, just to spice\n" + 174 "// * things up.\n" + 175 "// */ \n" + 176 "// System.out.println(\"Aren't comments fun? // (yeah!)\")"; 177 178 String uncommented = 179 "// Here is some abritrary text that should be uncommented.\n" + 180 " /* along with a little bit of code, just to spice\n" + 181 " * things up.\n" + 182 " */ \n" + 183 " System.out.println(\"Aren't comments fun? // (yeah!)\")"; 184 185 doc.insertString(0, text, null); 186 _assertContents("Sample text is inserted improperly.", text, doc); 187 doc.uncommentLines(0, doc.getLength()); 188 _assertContents("These lines should have at most one level of commenting!", uncommented, doc); 189 } 190 191 private static void _assertContents(String msg, String expected, DJDocument document) 192 throws BadLocationException { 193 assertEquals(msg, expected, document.getText()); 194 } 195 } 196 | Popular Tags |