1 /* 2 * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. 3 * 4 * This software is distributable under the BSD license. See the terms of the 5 * BSD license in the documentation provided with this software. 6 */ 7 package jline; 8 9 import junit.framework.*; 10 11 import java.io.*; 12 13 public abstract class JLineTestCase extends TestCase { 14 ConsoleReader console; 15 JLineTestCase(String test)16 public JLineTestCase(String test) { 17 super(test); 18 } 19 setUp()20 public void setUp() throws Exception { 21 super.setUp(); 22 console = new ConsoleReader(null, new PrintWriter( 23 new OutputStreamWriter(new ByteArrayOutputStream())), null, 24 new UnixTerminal()); 25 } 26 assertBuffer(String expected, Buffer buffer)27 public void assertBuffer(String expected, Buffer buffer) throws IOException { 28 assertBuffer(expected, buffer, true); 29 } 30 assertBuffer(String expected, Buffer buffer, boolean clear)31 public void assertBuffer(String expected, Buffer buffer, boolean clear) 32 throws IOException { 33 // clear current buffer, if any 34 if (clear) { 35 console.finishBuffer(); 36 console.getHistory().clear(); 37 } 38 39 console.setInput(new ByteArrayInputStream(buffer.getBytes())); 40 41 // run it through the reader 42 while (console.readLine((String) null) != null) { 43 ; 44 } 45 46 assertEquals(expected, console.getCursorBuffer().toString()); 47 } 48 getKeyForAction(short logicalAction)49 private int getKeyForAction(short logicalAction) { 50 int action = console.getKeyForAction(logicalAction); 51 52 if (action == -1) { 53 fail("Keystroke for logical action " + logicalAction 54 + " was not bound in the console"); 55 } 56 57 return action; 58 } 59 60 /** 61 * TODO: Fix this so tests don't break on windows machines. 62 * 63 * @author Ryan 64 * 65 */ 66 class Buffer { 67 private final ByteArrayOutputStream bout = new ByteArrayOutputStream(); 68 Buffer()69 public Buffer() { 70 } 71 Buffer(String str)72 public Buffer(String str) { 73 append(str); 74 } 75 getBytes()76 public byte[] getBytes() { 77 return bout.toByteArray(); 78 } 79 op(short operation)80 public Buffer op(short operation) { 81 return append(getKeyForAction(operation)); 82 } 83 ctrlA()84 public Buffer ctrlA() { 85 return append(getKeyForAction(ConsoleReader.MOVE_TO_BEG)); 86 } 87 ctrlU()88 public Buffer ctrlU() { 89 return append(getKeyForAction(ConsoleReader.KILL_LINE_PREV)); 90 } 91 tab()92 public Buffer tab() { 93 return append(getKeyForAction(ConsoleReader.COMPLETE)); 94 } 95 back()96 public Buffer back() { 97 return append(getKeyForAction(ConsoleReader.DELETE_PREV_CHAR)); 98 } 99 left()100 public Buffer left() { 101 return append(UnixTerminal.ARROW_START).append( 102 UnixTerminal.ARROW_PREFIX).append(UnixTerminal.ARROW_LEFT); 103 } 104 right()105 public Buffer right() { 106 return append(UnixTerminal.ARROW_START).append( 107 UnixTerminal.ARROW_PREFIX).append(UnixTerminal.ARROW_RIGHT); 108 } 109 up()110 public Buffer up() { 111 return append(UnixTerminal.ARROW_START).append( 112 UnixTerminal.ARROW_PREFIX).append(UnixTerminal.ARROW_UP); 113 } 114 down()115 public Buffer down() { 116 return append(UnixTerminal.ARROW_START).append( 117 UnixTerminal.ARROW_PREFIX).append(UnixTerminal.ARROW_DOWN); 118 } 119 append(String str)120 public Buffer append(String str) { 121 byte[] bytes = str.getBytes(); 122 123 for (int i = 0; i < bytes.length; i++) { 124 append(bytes[i]); 125 } 126 127 return this; 128 } 129 append(int i)130 public Buffer append(int i) { 131 return append((byte) i); 132 } 133 append(byte b)134 public Buffer append(byte b) { 135 bout.write(b); 136 137 return this; 138 } 139 } 140 } 141