Skip to content

Commit c13ad54

Browse files
committed
Added unit tests and fixed a few issues found as a result.
1 parent c1baf12 commit c13ad54

File tree

5 files changed

+140
-19
lines changed

5 files changed

+140
-19
lines changed

PSReadLine/PSReadLine.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>PSReadLine</RootNamespace>
1111
<AssemblyName>Microsoft.PowerShell.PSReadLine</AssemblyName>
12-
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
14-
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
14+
<TargetFrameworkProfile></TargetFrameworkProfile>
1515
</PropertyGroup>
1616
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1717
<DebugSymbols>true</DebugSymbols>

PSReadLine/ReadLine.vi.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static void Search(char keyChar, object arg, bool backoff)
7676
Ding();
7777
}
7878

79-
public static void SearchDelete(char keyChar, object arg, bool backoff, Action<ConsoleKeyInfo?, object> instigator)
79+
public static bool SearchDelete(char keyChar, object arg, bool backoff, Action<ConsoleKeyInfo?, object> instigator)
8080
{
8181
int qty = (arg is int) ? (int) arg : 1;
8282

@@ -88,11 +88,12 @@ public static void SearchDelete(char keyChar, object arg, bool backoff, Action<C
8888
if (qty == 0)
8989
{
9090
DeleteToEndPoint(arg, backoff ? i : i + 1, instigator);
91-
return;
91+
return true;
9292
}
9393
}
9494
}
9595
Ding();
96+
return false;
9697
}
9798

9899
public static void SearchBackward(char keyChar, object arg, bool backoff)
@@ -116,7 +117,7 @@ public static void SearchBackward(char keyChar, object arg, bool backoff)
116117
Ding();
117118
}
118119

119-
public static void SearchBackwardDelete(char keyChar, object arg, bool backoff, Action<ConsoleKeyInfo?, object> instigator)
120+
public static bool SearchBackwardDelete(char keyChar, object arg, bool backoff, Action<ConsoleKeyInfo?, object> instigator)
120121
{
121122
Set(keyChar, isBackward: true, isBackoff: backoff);
122123
int qty = (arg is int) ? (int) arg : 1;
@@ -129,11 +130,12 @@ public static void SearchBackwardDelete(char keyChar, object arg, bool backoff,
129130
if (qty == 0)
130131
{
131132
DeleteBackwardToEndPoint(arg, backoff ? i + 1 : i, instigator);
132-
return;
133+
return true;
133134
}
134135
}
135136
}
136137
Ding();
138+
return false;
137139
}
138140
}
139141

@@ -303,14 +305,16 @@ private static void DeleteToEndPoint(object arg, int endPoint, Action<ConsoleKey
303305

304306
private static void DeleteBackwardToEndPoint(object arg, int endPoint, Action<ConsoleKeyInfo?, object> instigator)
305307
{
306-
_singleton.SaveToClipboard(endPoint, _singleton._current - endPoint);
308+
int deleteLength = _singleton._current - endPoint + 1;
309+
310+
_singleton.SaveToClipboard(endPoint, deleteLength);
307311
_singleton.SaveEditItem(EditItemDelete.Create(
308312
_singleton._clipboard,
309313
endPoint,
310314
instigator,
311315
arg
312316
));
313-
_singleton._buffer.Remove(endPoint, _singleton._current - endPoint);
317+
_singleton._buffer.Remove(endPoint, deleteLength);
314318
_singleton._current = endPoint;
315319
_singleton.Render();
316320
}
@@ -434,7 +438,6 @@ private static void ViDeleteToCharBackward(ConsoleKeyInfo? key = null, object ar
434438

435439
private static void ViDeleteToCharBack(char keyChar, ConsoleKeyInfo? key = null, object arg = null)
436440
{
437-
ViCharacterSearcher.Set(keyChar, isBackward: true, isBackoff: false);
438441
ViCharacterSearcher.SearchBackwardDelete(keyChar, arg, backoff: false, instigator: (_key, _arg) => ViDeleteToCharBack(keyChar, _key, _arg));
439442
}
440443

PSReadLine/Replace.vi.cs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,21 @@ private static void ViReplaceToChar(ConsoleKeyInfo? key = null, object arg = nul
260260

261261
private static void ViReplaceToChar(char keyChar, ConsoleKeyInfo? key = null, object arg = null)
262262
{
263+
bool shouldAppend = _singleton._current > 0;
264+
265+
_singleton._groupUndoHelper.StartGroup(ReplaceChar, arg);
263266
ViCharacterSearcher.Set(keyChar, isBackward: false, isBackoff: false);
264-
ViCharacterSearcher.SearchDelete(keyChar, arg, backoff: false, instigator: (_key, _arg) => ViReplaceToChar(keyChar, _key, _arg));
265-
ViInsertMode(key, arg);
267+
if (ViCharacterSearcher.SearchDelete(keyChar, arg, backoff: false, instigator: (_key, _arg) => ViReplaceToChar(keyChar, _key, _arg)))
268+
{
269+
if (shouldAppend)
270+
{
271+
ViInsertWithAppend(key, arg);
272+
}
273+
else
274+
{
275+
ViInsertMode(key, arg);
276+
}
277+
}
266278
}
267279

268280
/// <summary>
@@ -279,9 +291,11 @@ private static void ViReplaceToCharBackward(ConsoleKeyInfo? key = null, object a
279291

280292
private static void ViReplaceToCharBack(char keyChar, ConsoleKeyInfo? key = null, object arg = null)
281293
{
282-
ViCharacterSearcher.Set(keyChar, isBackward: true, isBackoff: false);
283-
ViCharacterSearcher.SearchBackwardDelete(keyChar, arg, backoff: false, instigator: (_key, _arg) => ViReplaceToCharBack(keyChar, _key, _arg));
284-
ViInsertMode(key, arg);
294+
_singleton._groupUndoHelper.StartGroup(ReplaceChar, arg);
295+
if (ViCharacterSearcher.SearchBackwardDelete(keyChar, arg, backoff: false, instigator: (_key, _arg) => ViReplaceToCharBack(keyChar, _key, _arg)))
296+
{
297+
ViInsertMode(key, arg);
298+
}
285299
}
286300

287301
/// <summary>
@@ -298,9 +312,12 @@ private static void ViReplaceToBeforeChar(ConsoleKeyInfo? key = null, object arg
298312

299313
private static void ViReplaceToBeforeChar(char keyChar, ConsoleKeyInfo? key = null, object arg = null)
300314
{
315+
_singleton._groupUndoHelper.StartGroup(ReplaceChar, arg);
301316
ViCharacterSearcher.Set(keyChar, isBackward: false, isBackoff: true);
302-
ViCharacterSearcher.SearchDelete(keyChar, arg, backoff: true, instigator: (_key, _arg) => ViReplaceToBeforeChar(keyChar, _key, _arg));
303-
ViInsertMode(key, arg);
317+
if (ViCharacterSearcher.SearchDelete(keyChar, arg, backoff: true, instigator: (_key, _arg) => ViReplaceToBeforeChar(keyChar, _key, _arg)))
318+
{
319+
ViInsertMode(key, arg);
320+
}
304321
}
305322

306323
/// <summary>
@@ -317,9 +334,11 @@ private static void ViReplaceToBeforeCharBackward(ConsoleKeyInfo? key = null, ob
317334

318335
private static void ViReplaceToBeforeCharBack(char keyChar, ConsoleKeyInfo? key = null, object arg = null)
319336
{
320-
ViCharacterSearcher.Set(keyChar, isBackward: true, isBackoff: true);
321-
ViCharacterSearcher.SearchBackwardDelete(keyChar, arg, backoff: true, instigator: (_key, _arg) => ViReplaceToBeforeCharBack(keyChar, _key, _arg));
322-
ViInsertMode(key, arg);
337+
_singleton._groupUndoHelper.StartGroup(ReplaceChar, arg);
338+
if (ViCharacterSearcher.SearchBackwardDelete(keyChar, arg, backoff: true, instigator: (_key, _arg) => ViReplaceToBeforeCharBack(keyChar, _key, _arg)))
339+
{
340+
ViInsertMode(key, arg);
341+
}
323342
}
324343

325344

UnitTestPSReadLine/BasicEditingTest.VI.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,8 @@ public void ViTestInsertLine()
725725
[TestMethod]
726726
public void ViTestJoinLines()
727727
{
728+
TestSetup(KeyMode.Vi);
729+
728730
Test("", Keys(
729731
"line1", _.Escape, "oline2", CheckThat(() => AssertLineIs("line1\nline2")),
730732
_.Escape, "kJ", CheckThat(() => AssertLineIs("line1 line2")),
@@ -742,5 +744,57 @@ public void ViTestJoinLines()
742744
"uuuu"
743745
));
744746
}
747+
748+
[TestMethod]
749+
public void ViTestChangeChar()
750+
{
751+
TestSetup(KeyMode.Vi);
752+
753+
Test("0123456", Keys(
754+
"0123456", _.Escape, CheckThat(() => AssertLineIs("0123456")),
755+
"0cf6abc", _.Escape, CheckThat(() => AssertLineIs("abc")),
756+
'u', CheckThat(() => AssertLineIs("0123456")), CheckThat(() => AssertCursorLeftIs(6)),
757+
"0cf5abc", _.Escape, CheckThat(() => AssertLineIs("abc6")),
758+
'u', CheckThat(() => AssertLineIs("0123456")), CheckThat(() => AssertCursorLeftIs(6)),
759+
"0lcf6abc", _.Escape, CheckThat(() => AssertLineIs("0abc")),
760+
'u', CheckThat(() => AssertLineIs("0123456")), CheckThat(() => AssertCursorLeftIs(6)),
761+
"cf6abc", _.Escape, CheckThat(() => AssertLineIs("0123456bc")),
762+
'u'
763+
));
764+
765+
Test("0123456", Keys(
766+
"0123456", _.Escape, CheckThat(() => AssertLineIs("0123456")),
767+
"cF0abc", _.Escape, CheckThat(() => AssertLineIs("abc")),
768+
'u', CheckThat(() => AssertLineIs("0123456")), CheckThat(() => AssertCursorLeftIs(6)),
769+
"cF1abc", _.Escape, CheckThat(() => AssertLineIs("0abc")),
770+
'u', CheckThat(() => AssertLineIs("0123456")), CheckThat(() => AssertCursorLeftIs(6)),
771+
"hcF0abc", _.Escape, CheckThat(() => AssertLineIs("abc6")),
772+
'u', CheckThat(() => AssertLineIs("0123456")), CheckThat(() => AssertCursorLeftIs(6)),
773+
"hcF1abc", _.Escape, CheckThat(() => AssertLineIs("0abc6")),
774+
'u', CheckThat(() => AssertLineIs("0123456")), CheckThat(() => AssertCursorLeftIs(6)),
775+
"0cF0abc", _.Escape, CheckThat(() => AssertLineIs("0bc123456")),
776+
'u'
777+
));
778+
779+
Test("0123456", Keys(
780+
"0123456", _.Escape, CheckThat(() => AssertLineIs("0123456")),
781+
"0ct6abc", _.Escape, CheckThat(() => AssertLineIs("abc6")),
782+
'u', CheckThat(() => AssertLineIs("0123456")), CheckThat(() => AssertCursorLeftIs(6)),
783+
"0lct6abc", _.Escape, CheckThat(() => AssertLineIs("0abc6")),
784+
'u', CheckThat(() => AssertLineIs("0123456")), CheckThat(() => AssertCursorLeftIs(6)),
785+
"ct6abc", _.Escape, CheckThat(() => AssertLineIs("0123456bc")),
786+
'u'
787+
));
788+
789+
Test("0123456", Keys(
790+
"0123456", _.Escape, CheckThat(() => AssertLineIs("0123456")),
791+
"cT1abc", _.Escape, CheckThat(() => AssertLineIs("01abc")),
792+
'u', CheckThat(() => AssertLineIs("0123456")), CheckThat(() => AssertCursorLeftIs(6)),
793+
"hcT1abc", _.Escape, CheckThat(() => AssertLineIs("01abc6")),
794+
'u', CheckThat(() => AssertLineIs("0123456")), CheckThat(() => AssertCursorLeftIs(6)),
795+
"0cT0abc", _.Escape, CheckThat(() => AssertLineIs("0bc123456")),
796+
'u'
797+
));
798+
}
745799
}
746800
}

UnitTestPSReadLine/MovementTest.VI.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,5 +572,50 @@ public void ViTestBOLErrorCase()
572572
_.Escape, 'h', _.Space, "ih"
573573
));
574574
}
575+
576+
[TestMethod]
577+
public void ViTestCharDelete()
578+
{
579+
TestSetup(KeyMode.Vi);
580+
581+
Test("", Keys(
582+
"abcdefg", _.Escape, CheckThat(()=> AssertLineIs("abcdefg")),
583+
"0dfg", CheckThat(() => AssertLineIs("")),
584+
'u', CheckThat(() => AssertLineIs("abcdefg")), CheckThat(() => AssertCursorLeftIs(6)),
585+
"0dff", CheckThat(() => AssertLineIs("g")),
586+
'u', CheckThat(() => AssertLineIs("abcdefg")), CheckThat(() => AssertCursorLeftIs(6)),
587+
"0dfg"
588+
));
589+
590+
Test("", Keys(
591+
"abcdefg", _.Escape, CheckThat(() => AssertLineIs("abcdefg")),
592+
"dFa", _.Escape, CheckThat(() => AssertLineIs("")),
593+
'u', CheckThat(() => AssertCursorLeftIs(6)),
594+
"dFb", CheckThat(() => AssertLineIs("a")),
595+
'u', CheckThat(() => AssertCursorLeftIs(6)),
596+
"dFa"
597+
));
598+
599+
Test("0123456", Keys(
600+
"0123456", _.Escape, CheckThat(() => AssertLineIs("0123456")),
601+
"0dt6", CheckThat(() => AssertLineIs("6")),
602+
'u', CheckThat(() => AssertLineIs("0123456")), CheckThat(() => AssertCursorLeftIs(6)),
603+
"0dt5", CheckThat(() => AssertLineIs("56")),
604+
'u', CheckThat(() => AssertLineIs("0123456")),
605+
"0ldt6", CheckThat(() => AssertLineIs("06")),
606+
'u', CheckThat(() => AssertLineIs("0123456")),
607+
"0ldt5", CheckThat(() => AssertLineIs("056")),
608+
'u'
609+
));
610+
611+
Test("0123456", Keys(
612+
"0123456", _.Escape, CheckThat(() => AssertLineIs("0123456")),
613+
"dT0", CheckThat(() => AssertLineIs("0")),
614+
'u', CheckThat(() => AssertLineIs("0123456")), CheckThat(() => AssertCursorLeftIs(6)),
615+
"hdT0", CheckThat(() => AssertLineIs("06")),
616+
'u', CheckThat(() => AssertLineIs("0123456")), CheckThat(() => AssertCursorLeftIs(6)),
617+
"0dT0"
618+
));
619+
}
575620
}
576621
}

0 commit comments

Comments
 (0)