1 36 package jline; 37 38 import java.io.*; 39 40 import junit.framework.*; 41 42 43 public abstract class JLineTestCase 44 extends TestCase 45 { 46 ConsoleReader console; 47 48 49 public JLineTestCase (String test) 50 { 51 super (test); 52 } 53 54 55 public void setUp () 56 throws Exception 57 { 58 super.setUp (); 59 console = new ConsoleReader (null, new PrintWriter ( 60 new OutputStreamWriter (new ByteArrayOutputStream ()))); 61 } 62 63 64 public void assertBuffer (String expected, Buffer buffer) 65 throws IOException 66 { 67 assertBuffer (expected, buffer, true); 68 } 69 70 71 public void assertBuffer (String expected, Buffer buffer, boolean clear) 72 throws IOException 73 { 74 if (clear) 76 { 77 console.finishBuffer (); 78 console.getHistory ().clear (); 79 } 80 81 console.setInput (new ByteArrayInputStream (buffer.getBytes ())); 82 83 while (console.readLine ((String )null) != null); 85 86 assertEquals (expected, console.getCursorBuffer ().toString ()); 87 } 88 89 90 private int getKeyForAction (short logicalAction) 91 { 92 int action = console.getKeyForAction (logicalAction); 93 if (action == -1) 94 fail ("Keystroke for logical action " + logicalAction 95 + " was not bound in the console"); 96 return action; 97 } 98 99 100 class Buffer 101 { 102 private final ByteArrayOutputStream bout = new ByteArrayOutputStream (); 103 104 public Buffer () 105 { 106 } 107 108 109 public Buffer (String str) 110 { 111 append (str); 112 } 113 114 115 public byte [] getBytes () 116 { 117 return bout.toByteArray (); 118 } 119 120 121 public Buffer op (short operation) 122 { 123 return append (getKeyForAction (operation)); 124 } 125 126 127 public Buffer ctrlA () 128 { 129 return append (getKeyForAction (ConsoleReader.MOVE_TO_BEG)); 130 } 131 132 133 public Buffer ctrlU () 134 { 135 return append (getKeyForAction (ConsoleReader.KILL_LINE_PREV)); 136 } 137 138 139 public Buffer tab () 140 { 141 return append (getKeyForAction (ConsoleReader.COMPLETE)); 142 } 143 144 145 public Buffer back () 146 { 147 return append (getKeyForAction (ConsoleReader.DELETE_PREV_CHAR)); 148 } 149 150 151 public Buffer left () 152 { 153 return append (UnixTerminal.ARROW_START) 154 .append (UnixTerminal.ARROW_PREFIX) 155 .append (UnixTerminal.ARROW_LEFT); 156 } 157 158 159 public Buffer right () 160 { 161 return append (UnixTerminal.ARROW_START) 162 .append (UnixTerminal.ARROW_PREFIX) 163 .append (UnixTerminal.ARROW_RIGHT); 164 } 165 166 167 public Buffer up () 168 { 169 return append (UnixTerminal.ARROW_START) 170 .append (UnixTerminal.ARROW_PREFIX) 171 .append (UnixTerminal.ARROW_UP); 172 } 173 174 175 public Buffer down () 176 { 177 return append (UnixTerminal.ARROW_START) 178 .append (UnixTerminal.ARROW_PREFIX) 179 .append (UnixTerminal.ARROW_DOWN); 180 } 181 182 183 public Buffer append (String str) 184 { 185 byte [] bytes = str.getBytes (); 186 187 for (int i = 0; i < bytes.length; i++) 188 { 189 append (bytes [i]); 190 } 191 192 return this; 193 } 194 195 196 public Buffer append (int i) 197 { 198 return append ((byte)i); 199 } 200 201 202 public Buffer append (byte b) 203 { 204 bout.write (b); 205 206 return this; 207 } 208 } 209 } 210 | Popular Tags |