1
2
3
4
5
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
16 public JLineTestCase(String test) {
17 super(test);
18 }
19
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
27 public void assertBuffer(String expected, Buffer buffer) throws IOException {
28 assertBuffer(expected, buffer, true);
29 }
30
31 public void assertBuffer(String expected, Buffer buffer, boolean clear)
32 throws IOException {
33
34 if (clear) {
35 console.finishBuffer();
36 console.getHistory().clear();
37 }
38
39 console.setInput(new ByteArrayInputStream(buffer.getBytes()));
40
41
42 while (console.readLine((String) null) != null) {
43 ;
44 }
45
46 assertEquals(expected, console.getCursorBuffer().toString());
47 }
48
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
69 public Buffer() {
70 }
71
72 public Buffer(String str) {
73 append(str);
74 }
75
76 public byte[] getBytes() {
77 return bout.toByteArray();
78 }
79
80 public Buffer op(short operation) {
81 return append(getKeyForAction(operation));
82 }
83
84 public Buffer ctrlA() {
85 return append(getKeyForAction(ConsoleReader.MOVE_TO_BEG));
86 }
87
88 public Buffer ctrlU() {
89 return append(getKeyForAction(ConsoleReader.KILL_LINE_PREV));
90 }
91
92 public Buffer tab() {
93 return append(getKeyForAction(ConsoleReader.COMPLETE));
94 }
95
96 public Buffer back() {
97 return append(getKeyForAction(ConsoleReader.DELETE_PREV_CHAR));
98 }
99
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 }