Skip to content

Commit 062d21f

Browse files
committed
Add control keys to special key extension
1 parent 071a6ae commit 062d21f

File tree

2 files changed

+41
-34
lines changed

2 files changed

+41
-34
lines changed

test/ReadLine.Tests/ConsoleKeyInfoExtensions.cs

+16
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,21 @@ public static class ConsoleKeyInfoExtensions
1818

1919
public static readonly ConsoleKeyInfo Tab = new ConsoleKeyInfo('\0', ConsoleKey.Tab, false, false, false);
2020
public static readonly ConsoleKeyInfo ShiftTab = new ConsoleKeyInfo('\0', ConsoleKey.Tab, true, false, false);
21+
22+
public static readonly ConsoleKeyInfo ExclamationPoint = CharExtensions.ExclamationPoint.ToConsoleKeyInfo();
23+
public static readonly ConsoleKeyInfo Space = CharExtensions.Space.ToConsoleKeyInfo();
24+
public static readonly ConsoleKeyInfo CtrlA = CharExtensions.CtrlA.ToConsoleKeyInfo();
25+
public static readonly ConsoleKeyInfo CtrlB = CharExtensions.CtrlB.ToConsoleKeyInfo();
26+
public static readonly ConsoleKeyInfo CtrlD = CharExtensions.CtrlD.ToConsoleKeyInfo();
27+
public static readonly ConsoleKeyInfo CtrlE = CharExtensions.CtrlE.ToConsoleKeyInfo();
28+
public static readonly ConsoleKeyInfo CtrlF = CharExtensions.CtrlF.ToConsoleKeyInfo();
29+
public static readonly ConsoleKeyInfo CtrlH = CharExtensions.CtrlH.ToConsoleKeyInfo();
30+
public static readonly ConsoleKeyInfo CtrlK = CharExtensions.CtrlK.ToConsoleKeyInfo();
31+
public static readonly ConsoleKeyInfo CtrlL = CharExtensions.CtrlL.ToConsoleKeyInfo();
32+
public static readonly ConsoleKeyInfo CtrlN = CharExtensions.CtrlN.ToConsoleKeyInfo();
33+
public static readonly ConsoleKeyInfo CtrlP = CharExtensions.CtrlP.ToConsoleKeyInfo();
34+
public static readonly ConsoleKeyInfo CtrlT = CharExtensions.CtrlT.ToConsoleKeyInfo();
35+
public static readonly ConsoleKeyInfo CtrlU = CharExtensions.CtrlU.ToConsoleKeyInfo();
36+
public static readonly ConsoleKeyInfo CtrlW = CharExtensions.CtrlW.ToConsoleKeyInfo();
2137
}
2238
}

test/ReadLine.Tests/KeyHandlerTests.cs

+25-34
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using ReadLine.Tests.Abstractions;
88
using Internal.ReadLine;
99

10-
using static ReadLine.Tests.CharExtensions;
1110
using static ReadLine.Tests.ConsoleKeyInfoExtensions;
1211

1312
namespace ReadLine.Tests
@@ -72,15 +71,15 @@ public void TestDelete_EndOfLine()
7271
[Fact]
7372
public void TestControlH()
7473
{
75-
_keyHandler.Handle(CtrlH.ToConsoleKeyInfo());
74+
_keyHandler.Handle(CtrlH);
7675
Assert.Equal("Hell", _keyHandler.Text);
7776
}
7877

7978
[Fact]
8079
public void TestControlT()
8180
{
8281
var initialCursorCol = _console.CursorLeft;
83-
_keyHandler.Handle(CtrlT.ToConsoleKeyInfo());
82+
_keyHandler.Handle(CtrlT);
8483

8584
Assert.Equal("Helol", _keyHandler.Text);
8685
Assert.Equal(initialCursorCol, _console.CursorLeft);
@@ -91,7 +90,7 @@ public void TestControlT_LeftOnce_CursorMovesToEnd()
9190
{
9291
var initialCursorCol = _console.CursorLeft;
9392

94-
new List<ConsoleKeyInfo>() { LeftArrow, CtrlT.ToConsoleKeyInfo() }
93+
new List<ConsoleKeyInfo>() { LeftArrow, CtrlT }
9594
.ForEach(_keyHandler.Handle);
9695

9796
Assert.Equal("Helol", _keyHandler.Text);
@@ -108,7 +107,7 @@ public void TestControlT_CursorInMiddleOfLine()
108107

109108
var initialCursorCol = _console.CursorLeft;
110109

111-
_keyHandler.Handle(CtrlT.ToConsoleKeyInfo());
110+
_keyHandler.Handle(CtrlT);
112111

113112
Assert.Equal("Hlelo", _keyHandler.Text);
114113
Assert.Equal(initialCursorCol + 1, _console.CursorLeft);
@@ -117,11 +116,11 @@ public void TestControlT_CursorInMiddleOfLine()
117116
[Fact]
118117
public void TestControlT_CursorAtBeginningOfLine_HasNoEffect()
119118
{
120-
_keyHandler.Handle(CtrlA.ToConsoleKeyInfo());
119+
_keyHandler.Handle(CtrlA);
121120

122121
var initialCursorCol = _console.CursorLeft;
123122

124-
_keyHandler.Handle(CtrlT.ToConsoleKeyInfo());
123+
_keyHandler.Handle(CtrlT);
125124

126125
Assert.Equal("Hello", _keyHandler.Text);
127126
Assert.Equal(initialCursorCol, _console.CursorLeft);
@@ -139,7 +138,7 @@ public void TestHome()
139138
[Fact]
140139
public void TestControlA()
141140
{
142-
new List<ConsoleKeyInfo>() { CtrlA.ToConsoleKeyInfo(), 'S'.ToConsoleKeyInfo() }
141+
new List<ConsoleKeyInfo>() { CtrlA, 'S'.ToConsoleKeyInfo() }
143142
.ForEach(_keyHandler.Handle);
144143

145144
Assert.Equal("SHello", _keyHandler.Text);
@@ -148,7 +147,7 @@ public void TestControlA()
148147
[Fact]
149148
public void TestEnd()
150149
{
151-
new List<ConsoleKeyInfo>() { Home, End, ExclamationPoint.ToConsoleKeyInfo() }
150+
new List<ConsoleKeyInfo>() { Home, End, ExclamationPoint }
152151
.ForEach(_keyHandler.Handle);
153152

154153
Assert.Equal("Hello!", _keyHandler.Text);
@@ -157,12 +156,8 @@ public void TestEnd()
157156
[Fact]
158157
public void TestControlE()
159158
{
160-
new List<ConsoleKeyInfo>()
161-
{
162-
CtrlA.ToConsoleKeyInfo(),
163-
CtrlE.ToConsoleKeyInfo(),
164-
ExclamationPoint.ToConsoleKeyInfo()
165-
}.ForEach(_keyHandler.Handle);
159+
new List<ConsoleKeyInfo>() { CtrlA, CtrlE, ExclamationPoint }
160+
.ForEach(_keyHandler.Handle);
166161

167162
Assert.Equal("Hello!", _keyHandler.Text);
168163
}
@@ -182,7 +177,7 @@ public void TestLeftArrow()
182177
public void TestControlB()
183178
{
184179
" N".Select(c => c.ToConsoleKeyInfo())
185-
.Prepend(CtrlB.ToConsoleKeyInfo())
180+
.Prepend(CtrlB)
186181
.ToList()
187182
.ForEach(_keyHandler.Handle);
188183

@@ -192,7 +187,7 @@ public void TestControlB()
192187
[Fact]
193188
public void TestRightArrow()
194189
{
195-
new List<ConsoleKeyInfo>() { LeftArrow, RightArrow, ExclamationPoint.ToConsoleKeyInfo() }
190+
new List<ConsoleKeyInfo>() { LeftArrow, RightArrow, ExclamationPoint }
196191
.ForEach(_keyHandler.Handle);
197192

198193
Assert.Equal("Hello!", _keyHandler.Text);
@@ -202,7 +197,7 @@ public void TestRightArrow()
202197
public void TestControlD()
203198
{
204199
Enumerable.Repeat(LeftArrow, 4)
205-
.Append(CtrlD.ToConsoleKeyInfo())
200+
.Append(CtrlD)
206201
.ToList()
207202
.ForEach(_keyHandler.Handle);
208203

@@ -212,20 +207,16 @@ public void TestControlD()
212207
[Fact]
213208
public void TestControlF()
214209
{
215-
new List<ConsoleKeyInfo>()
216-
{
217-
LeftArrow,
218-
CtrlF.ToConsoleKeyInfo(),
219-
ExclamationPoint.ToConsoleKeyInfo()
220-
}.ForEach(_keyHandler.Handle);
210+
new List<ConsoleKeyInfo>() { LeftArrow, CtrlF, ExclamationPoint }
211+
.ForEach(_keyHandler.Handle);
221212

222213
Assert.Equal("Hello!", _keyHandler.Text);
223214
}
224215

225216
[Fact]
226217
public void TestControlL()
227218
{
228-
_keyHandler.Handle(CtrlL.ToConsoleKeyInfo());
219+
_keyHandler.Handle(CtrlL);
229220
Assert.Equal(string.Empty, _keyHandler.Text);
230221
}
231222

@@ -242,7 +233,7 @@ public void TestUpArrow()
242233
public void TestControlP()
243234
{
244235
_history.AsEnumerable().Reverse().ToList().ForEach((history) => {
245-
_keyHandler.Handle(CtrlP.ToConsoleKeyInfo());
236+
_keyHandler.Handle(CtrlP);
246237
Assert.Equal(history, _keyHandler.Text);
247238
});
248239
}
@@ -269,20 +260,20 @@ public void TestControlN()
269260

270261
_history.ForEach( history => {
271262
Assert.Equal(history, _keyHandler.Text);
272-
_keyHandler.Handle(CtrlN.ToConsoleKeyInfo());
263+
_keyHandler.Handle(CtrlN);
273264
});
274265
}
275266

276267
[Fact]
277268
public void TestControlU()
278269
{
279270
_keyHandler.Handle(LeftArrow);
280-
_keyHandler.Handle(CtrlU.ToConsoleKeyInfo());
271+
_keyHandler.Handle(CtrlU);
281272

282273
Assert.Equal("o", _keyHandler.Text);
283274

284275
_keyHandler.Handle(End);
285-
_keyHandler.Handle(CtrlU.ToConsoleKeyInfo());
276+
_keyHandler.Handle(CtrlU);
286277

287278
Assert.Equal(string.Empty, _keyHandler.Text);
288279
}
@@ -291,28 +282,28 @@ public void TestControlU()
291282
public void TestControlK()
292283
{
293284
_keyHandler.Handle(LeftArrow);
294-
_keyHandler.Handle(CtrlK.ToConsoleKeyInfo());
285+
_keyHandler.Handle(CtrlK);
295286

296287
Assert.Equal("Hell", _keyHandler.Text);
297288

298289
_keyHandler.Handle(Home);
299-
_keyHandler.Handle(CtrlK.ToConsoleKeyInfo());
290+
_keyHandler.Handle(CtrlK);
300291

301292
Assert.Equal(string.Empty, _keyHandler.Text);
302293
}
303294

304295
[Fact]
305296
public void TestControlW()
306297
{
307-
" World".Append(CtrlW)
308-
.Select(c => c.ToConsoleKeyInfo())
298+
" World".Select(c => c.ToConsoleKeyInfo())
299+
.Append(CtrlW)
309300
.ToList()
310301
.ForEach(_keyHandler.Handle);
311302

312303
Assert.Equal("Hello ", _keyHandler.Text);
313304

314305
_keyHandler.Handle(Backspace);
315-
_keyHandler.Handle(CtrlW.ToConsoleKeyInfo());
306+
_keyHandler.Handle(CtrlW);
316307

317308
Assert.Equal(string.Empty, _keyHandler.Text);
318309
}

0 commit comments

Comments
 (0)