-
Notifications
You must be signed in to change notification settings - Fork 2
/
cursor.test.ts
192 lines (159 loc) · 3.79 KB
/
cursor.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { assertEquals } from "./dev-deps.ts";
import { decode } from "./deps.ts";
import {
clearDown,
clearLeft,
clearLine,
clearRight,
clearScreen,
clearUp,
cursor,
goDown,
goHome,
goLeft,
goRight,
goTo,
goUp,
hideCursor,
nextLine,
position,
prevLine,
RESTORE,
restore,
SAVE,
save,
scrollDown,
scrollUp,
showCursor,
write,
} from "./cursor.ts";
function spy(obj: any, prop: any): any {
const savedTarget = obj[prop];
let calls: any[] = [];
const spy = (...args: any[]) => {
calls.push(args);
};
const restoreSpy = () => {
obj[prop] = savedTarget;
};
obj[prop] = spy;
return { calls, restoreSpy };
}
function test(
title: string,
testFn: (calls: any[]) => Promise<void>,
) {
Deno.test(title, async () => {
const { calls, restoreSpy } = spy(Deno.stdout, "write");
await testFn(calls);
restoreSpy();
});
}
test("write", async (calls) => {
await write("foo");
assertEquals(decode(calls[0][0]), "foo");
});
test("cursor", async (calls) => {
await cursor("1E");
assertEquals(decode(calls[0][0]), "\u001B[1E");
});
test("save", async (calls) => {
await save();
assertEquals(decode(calls[0][0]), SAVE);
});
test("restore", async (calls) => {
await restore();
assertEquals(decode(calls[0][0]), RESTORE);
});
test("position", async (calls) => {
await position();
assertEquals(decode(calls[0][0]), "\u001B[6n");
});
test("hideCursor", async (calls) => {
await hideCursor();
assertEquals(decode(calls[0][0]), "\u001B[?25l");
});
test("showCursor", async (calls) => {
await showCursor();
assertEquals(decode(calls[0][0]), "\u001B[?25h");
});
test("scrollUp", async (calls) => {
await scrollUp();
assertEquals(decode(calls[0][0]), "\u001B[T");
});
test("scrollDown", async (calls) => {
await scrollDown();
assertEquals(decode(calls[0][0]), "\u001B[S");
});
test("clearUp", async (calls) => {
await clearUp();
assertEquals(decode(calls[0][0]), "\u001B[1J");
});
test("clearDown", async (calls) => {
await clearDown();
assertEquals(decode(calls[0][0]), "\u001B[0J");
});
test("clearLeft", async (calls) => {
await clearLeft();
assertEquals(decode(calls[0][0]), "\u001B[1K");
});
test("clearRight", async (calls) => {
await clearRight();
assertEquals(decode(calls[0][0]), "\u001B[0K");
});
test("clearLine", async (calls) => {
await clearLine();
assertEquals(decode(calls[0][0]), "\u001B[2K");
});
test("clearScreen", async (calls) => {
await clearScreen();
assertEquals(decode(calls[0][0]), "\u001B[2J");
});
test("nextLine", async (calls) => {
await nextLine();
assertEquals(decode(calls[0][0]), "\u001B[1E");
});
test("prevLine", async (calls) => {
await prevLine();
assertEquals(decode(calls[0][0]), "\u001B[1F");
});
test("goHome", async (calls) => {
await goHome();
assertEquals(decode(calls[0][0]), "\u001B[H");
});
test("goUp", async (calls) => {
await goUp();
assertEquals(decode(calls[0][0]), "\u001B[1A");
});
test("goDown", async (calls) => {
await goDown();
assertEquals(decode(calls[0][0]), "\u001B[1B");
});
test("goLeft", async (calls) => {
await goLeft();
assertEquals(decode(calls[0][0]), "\u001B[1D");
});
test("goRight", async (calls) => {
await goRight();
assertEquals(decode(calls[0][0]), "\u001B[1C");
});
test("goUp(n)", async (calls) => {
await goUp(2);
assertEquals(decode(calls[0][0]), "\u001B[2A");
});
test("goDown(n)", async (calls) => {
await goDown(2);
assertEquals(decode(calls[0][0]), "\u001B[2B");
});
test("goLeft(n)", async (calls) => {
await goLeft(2);
assertEquals(decode(calls[0][0]), "\u001B[2D");
});
test("goRight(n)", async (calls) => {
await goRight(2);
assertEquals(decode(calls[0][0]), "\u001B[2C");
});
test("goTos", async (calls) => {
await goTo(10, 100);
assertEquals(decode(calls[0][0]), "\u001B[100;10H");
});