001 /*
002 * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved.
003 *
004 * This software is distributable under the BSD license. See the terms of the
005 * BSD license in the documentation provided with this software.
006 */
007 package jline;
008
009 import junit.framework.*;
010
011 import java.io.*;
012
013 public abstract class JLineTestCase extends TestCase {
014 ConsoleReader console;
015
016 public JLineTestCase(String test) {
017 super(test);
018 }
019
020 public void setUp() throws Exception {
021 super.setUp();
022 console = new ConsoleReader(null, new PrintWriter(
023 new OutputStreamWriter(new ByteArrayOutputStream())), null,
024 new UnixTerminal());
025 }
026
027 public void assertBuffer(String expected, Buffer buffer) throws IOException {
028 assertBuffer(expected, buffer, true);
029 }
030
031 public void assertBuffer(String expected, Buffer buffer, boolean clear)
032 throws IOException {
033 // clear current buffer, if any
034 if (clear) {
035 console.finishBuffer();
036 console.getHistory().clear();
037 }
038
039 console.setInput(new ByteArrayInputStream(buffer.getBytes()));
040
041 // run it through the reader
042 while (console.readLine((String) null) != null) {
043 ;
044 }
045
046 assertEquals(expected, console.getCursorBuffer().toString());
047 }
048
049 private int getKeyForAction(short logicalAction) {
050 int action = console.getKeyForAction(logicalAction);
051
052 if (action == -1) {
053 fail("Keystroke for logical action " + logicalAction
054 + " was not bound in the console");
055 }
056
057 return action;
058 }
059
060 /**
061 * TODO: Fix this so tests don't break on windows machines.
062 *
063 * @author Ryan
064 *
065 */
066 class Buffer {
067 private final ByteArrayOutputStream bout = new ByteArrayOutputStream();
068
069 public Buffer() {
070 }
071
072 public Buffer(String str) {
073 append(str);
074 }
075
076 public byte[] getBytes() {
077 return bout.toByteArray();
078 }
079
080 public Buffer op(short operation) {
081 return append(getKeyForAction(operation));
082 }
083
084 public Buffer ctrlA() {
085 return append(getKeyForAction(ConsoleReader.MOVE_TO_BEG));
086 }
087
088 public Buffer ctrlU() {
089 return append(getKeyForAction(ConsoleReader.KILL_LINE_PREV));
090 }
091
092 public Buffer tab() {
093 return append(getKeyForAction(ConsoleReader.COMPLETE));
094 }
095
096 public Buffer back() {
097 return append(getKeyForAction(ConsoleReader.DELETE_PREV_CHAR));
098 }
099
100 public Buffer left() {
101 return append(UnixTerminal.ARROW_START).append(
102 UnixTerminal.ARROW_PREFIX).append(UnixTerminal.ARROW_LEFT);
103 }
104
105 public Buffer right() {
106 return append(UnixTerminal.ARROW_START).append(
107 UnixTerminal.ARROW_PREFIX).append(UnixTerminal.ARROW_RIGHT);
108 }
109
110 public Buffer up() {
111 return append(UnixTerminal.ARROW_START).append(
112 UnixTerminal.ARROW_PREFIX).append(UnixTerminal.ARROW_UP);
113 }
114
115 public Buffer down() {
116 return append(UnixTerminal.ARROW_START).append(
117 UnixTerminal.ARROW_PREFIX).append(UnixTerminal.ARROW_DOWN);
118 }
119
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
130 public Buffer append(int i) {
131 return append((byte) i);
132 }
133
134 public Buffer append(byte b) {
135 bout.write(b);
136
137 return this;
138 }
139 }
140 }