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())),
024: null, new UnixTerminal());
025: }
026:
027: public void assertBuffer(String expected, Buffer buffer)
028: throws IOException {
029: assertBuffer(expected, buffer, true);
030: }
031:
032: public void assertBuffer(String expected, Buffer buffer,
033: boolean clear) throws IOException {
034: // clear current buffer, if any
035: if (clear) {
036: console.finishBuffer();
037: console.getHistory().clear();
038: }
039:
040: console.setInput(new ByteArrayInputStream(buffer.getBytes()));
041:
042: // run it through the reader
043: while (console.readLine((String) null) != null) {
044: ;
045: }
046:
047: assertEquals(expected, console.getCursorBuffer().toString());
048: }
049:
050: private int getKeyForAction(short logicalAction) {
051: int action = console.getKeyForAction(logicalAction);
052:
053: if (action == -1) {
054: fail("Keystroke for logical action " + logicalAction
055: + " was not bound in the console");
056: }
057:
058: return action;
059: }
060:
061: /**
062: * TODO: Fix this so tests don't break on windows machines.
063: *
064: * @author Ryan
065: *
066: */
067: class Buffer {
068: private final ByteArrayOutputStream bout = new ByteArrayOutputStream();
069:
070: public Buffer() {
071: }
072:
073: public Buffer(String str) {
074: append(str);
075: }
076:
077: public byte[] getBytes() {
078: return bout.toByteArray();
079: }
080:
081: public Buffer op(short operation) {
082: return append(getKeyForAction(operation));
083: }
084:
085: public Buffer ctrlA() {
086: return append(getKeyForAction(ConsoleReader.MOVE_TO_BEG));
087: }
088:
089: public Buffer ctrlU() {
090: return append(getKeyForAction(ConsoleReader.KILL_LINE_PREV));
091: }
092:
093: public Buffer tab() {
094: return append(getKeyForAction(ConsoleReader.COMPLETE));
095: }
096:
097: public Buffer back() {
098: return append(getKeyForAction(ConsoleReader.DELETE_PREV_CHAR));
099: }
100:
101: public Buffer left() {
102: return append(UnixTerminal.ARROW_START).append(
103: UnixTerminal.ARROW_PREFIX).append(
104: UnixTerminal.ARROW_LEFT);
105: }
106:
107: public Buffer right() {
108: return append(UnixTerminal.ARROW_START).append(
109: UnixTerminal.ARROW_PREFIX).append(
110: UnixTerminal.ARROW_RIGHT);
111: }
112:
113: public Buffer up() {
114: return append(UnixTerminal.ARROW_START).append(
115: UnixTerminal.ARROW_PREFIX).append(
116: UnixTerminal.ARROW_UP);
117: }
118:
119: public Buffer down() {
120: return append(UnixTerminal.ARROW_START).append(
121: UnixTerminal.ARROW_PREFIX).append(
122: UnixTerminal.ARROW_DOWN);
123: }
124:
125: public Buffer append(String str) {
126: byte[] bytes = str.getBytes();
127:
128: for (int i = 0; i < bytes.length; i++) {
129: append(bytes[i]);
130: }
131:
132: return this ;
133: }
134:
135: public Buffer append(int i) {
136: return append((byte) i);
137: }
138:
139: public Buffer append(byte b) {
140: bout.write(b);
141:
142: return this;
143: }
144: }
145: }
|