/
VimRegexTest.cs
1120 lines (988 loc) · 33.8 KB
/
VimRegexTest.cs
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Linq;
using Microsoft.FSharp.Core;
using NUnit.Framework;
using Vim.Extensions;
namespace Vim.UnitTest
{
[TestFixture]
public sealed class VimRegexTest
{
private static readonly string[] LowerCaseLetters = TestConstants.LowerCaseLetters.Select(x => x.ToString()).ToArray();
private static readonly string[] UpperCaseLetters = TestConstants.UpperCaseLetters.Select(x => x.ToString()).ToArray();
private static readonly string[] Digits = TestConstants.Digits.Select(x => x.ToString()).ToArray();
private IVimGlobalSettings _globalSettings;
[SetUp]
public void Setup()
{
_globalSettings = new GlobalSettings();
_globalSettings.IgnoreCase = true;
_globalSettings.SmartCase = false;
}
private FSharpOption<VimRegex> Create(string pattern)
{
return VimRegexFactory.CreateForSettings(pattern, _globalSettings);
}
private void VerifyMatches(string pattern, params string[] inputArray)
{
VerifyMatches(VimRegexOptions.Default, pattern, inputArray);
}
private void VerifyMatches(VimRegexOptions options, string pattern, params string[] inputArray)
{
var opt = VimRegexFactory.Create(pattern, options);
Assert.IsTrue(opt.IsSome());
var regex = opt.Value;
foreach (var cur in inputArray)
{
Assert.IsTrue(regex.IsMatch(cur));
}
}
private void VerifyNotRegex(string pattern)
{
Assert.IsTrue(Create(pattern).IsNone());
}
private void VerifyNotMatches(string pattern, params string[] inputArray)
{
VerifyNotMatches(VimRegexOptions.Default, pattern, inputArray);
}
private void VerifyNotMatches(VimRegexOptions options, string pattern, params string[] inputArray)
{
var opt = VimRegexFactory.Create(pattern, options);
Assert.IsTrue(opt.IsSome());
var regex = opt.Value;
foreach (var cur in inputArray)
{
Assert.IsFalse(regex.IsMatch(cur));
}
}
private void VerifyMatchIs(string pattern, string input, string toMatch)
{
var regex = Create(pattern);
Assert.IsTrue(regex.IsSome());
var match = regex.Value.Regex.Match(input);
Assert.IsTrue(match.Success);
Assert.AreEqual(toMatch, match.Value);
}
private void VerifyReplace(string pattern, string input, string replace, string result)
{
VerifyReplace(VimRegexOptions.Default, pattern, input, replace, result);
}
private void VerifyReplace(VimRegexOptions options, string pattern, string input, string replace, string result)
{
var regex = VimRegexFactory.Create(pattern, options);
Assert.IsTrue(regex.IsSome());
var noMagic = VimRegexOptions.NoMagic == (options & VimRegexOptions.NoMagic);
var replaceData = new ReplaceData(Environment.NewLine, !noMagic, 1);
Assert.AreEqual(result, regex.Value.ReplaceAll(input, replace, replaceData));
}
[Test]
public void Case_Simple()
{
VerifyMatches(VimRegexOptions.IgnoreCase, "a", "a", "A");
VerifyMatches("b", "b", "b");
}
/// <summary>
/// Make sure the parsing is case sensitive by default
/// </summary>
[Test]
public void Case_RespectIgnoreCase()
{
VerifyMatches("a", "a");
VerifyNotMatches("a", "A");
VerifyMatches("b", "b");
VerifyNotMatches("b", "B");
}
[Test]
public void Case_SensitiveSpecifier()
{
VerifyMatches(@"\Ca", "a");
VerifyMatches(@"\Cb", "b");
VerifyNotMatches(@"\Ca", "A");
VerifyNotMatches(@"\Cb", "B");
}
/// <summary>
/// Make sure we support the \C specifier anywhere in the search string
/// </summary>
[Test]
public void Case_SensitiveSpecifierInMiddleOfString()
{
var regex = Create(@"d\Cog").Value;
Assert.IsTrue(regex.CaseSpecifier.IsOrdinalCase);
Assert.AreEqual(@"d\Cog", regex.VimPattern);
Assert.AreEqual("dog", regex.RegexPattern);
}
/// <summary>
/// The \C modifier takes precedence over ignorecase option
/// </summary>
[Test]
public void Case_SensitiveSpecifierBeatsIgonreCase()
{
VerifyMatches(VimRegexOptions.IgnoreCase, @"\Ca", "a");
VerifyMatches(VimRegexOptions.IgnoreCase, @"\Cb", "b");
VerifyNotMatches(VimRegexOptions.IgnoreCase, @"\Ca", "A");
VerifyNotMatches(VimRegexOptions.IgnoreCase, @"\Cb", "B");
}
[Test]
public void Case_InsensitiveSpecifier()
{
VerifyMatches(@"\ca", "a", "A");
VerifyMatches(@"\cb", "b", "B");
}
/// <summary>
/// Make sure we support the \c specifier anywhere in the search string
/// </summary>
[Test]
public void Case_InsensitiveSpecifierInMiddleOfString()
{
var regex = Create(@"D\cOG").Value;
Assert.IsTrue(regex.CaseSpecifier.IsIgnoreCase);
Assert.AreEqual(@"D\cOG", regex.VimPattern);
Assert.AreEqual("DOG", regex.RegexPattern);
}
/// <summary>
/// The \c modifier takes precedence over the ignore case option
/// </summary>
[Test]
public void Case_InsensitiveSpecifierBeatsDefault()
{
VerifyMatches(@"\ca", "a", "A");
VerifyMatches(@"\cb", "b", "B");
}
/// <summary>
/// SmartCase should match both if only lower
/// </summary>
[Test]
public void SmartCase_Simple()
{
VerifyMatches(VimRegexOptions.SmartCase | VimRegexOptions.IgnoreCase, "a", "A", "a");
VerifyMatches(VimRegexOptions.SmartCase | VimRegexOptions.IgnoreCase, "b", "b", "B");
}
/// <summary>
/// SmartCase is case sensitive if any are upper
/// </summary>
[Test]
public void SmartCase_WithUpper()
{
VerifyMatches(VimRegexOptions.SmartCase, "A", "A");
VerifyNotMatches(VimRegexOptions.SmartCase, "A", "a");
VerifyMatches(VimRegexOptions.SmartCase, "B", "B");
VerifyNotMatches(VimRegexOptions.SmartCase, "B", "b");
}
/// <summary>
/// The \c modifier beats smart case as well
/// </summary>
[Test]
public void SmartCase_InsensitiveSpecifierWins()
{
VerifyMatches(VimRegexOptions.SmartCase, @"\cFoo", "foo", "FOO", "fOO");
VerifyMatches(VimRegexOptions.SmartCase, @"\cBar", "BAR", "bar");
}
/// <summary>
/// The \C modifier beats smart case as well
/// </summary>
[Test]
public void SmartCase_SensitiveSpecifierWins()
{
var options = VimRegexOptions.SmartCase | VimRegexOptions.IgnoreCase;
VerifyMatches(options, @"\CFOO", "FOO");
VerifyNotMatches(options, @"\CFOO", "foo");
VerifyMatches(options, @"\CBAR", "BAR");
VerifyNotMatches(options, @"\CBAR", "bar");
}
[Test]
public void CreateRegexOptions_NoMagic()
{
_globalSettings.Magic = false;
var options = VimRegexFactory.CreateRegexOptions(_globalSettings);
Assert.AreEqual(VimRegexOptions.NoMagic, options & VimRegexOptions.NoMagic);
}
/// <summary>
/// Magic should the default setting
/// </summary>
[Test]
public void Magic_ShouldBeDefault()
{
VerifyMatches(".", "a", "b", "c");
}
/// <summary>
/// The \m should override the NoMagic option on the regex
/// </summary>
[Test]
public void Magic_MagicSpecifierHasPrecedence()
{
VerifyMatches(VimRegexOptions.NoMagic, @"\m.", "a", "b", "c");
}
/// <summary>
/// The \M should oveerride the default VimRegexOptions
/// </summary>
[Test]
public void Magic_NoMagicSpecifierHasPrecedence()
{
VerifyNotMatches(@"\M.", "a", "b", "c");
VerifyMatches(@"\M\.", "a", "b", "c");
}
/// <summary>
/// The \M should oveerride the default VimRegexOptions
/// </summary>
[Test]
public void Magic_MagicSpecifierInMiddle()
{
VerifyMatches(VimRegexOptions.NoMagic, @"a\m.", "ab", "ac");
}
[Test]
public void Magic_NoMagicSpecifierInMiddle()
{
VerifyNotMatches(@"a\M.", "ab", "ac");
VerifyMatches(@"a\M.", "a.");
}
[Test]
public void VeryMagic1()
{
VerifyMatches(VimRegexOptions.NoMagic, @"\v.", "a", "b");
}
[Test]
public void VeryMagic2()
{
VerifyNotMatches(@"\V.", "a", "b");
VerifyMatches(@"\V\.", "a", "b");
}
[Test]
public void VeryNoMagicDotIsNotSpecial()
{
VerifyNotMatches(@"\V.", "a");
VerifyMatches(@"\V.", ".");
}
[Test]
public void ItemStar1()
{
VerifyMatchIs(@"ab*", "abb", "abb");
VerifyMatchIs(@"ab*", "cab", "ab");
VerifyMatchIs(@"ab*", "cabb", "abb");
}
[Test]
public void ItemStar2()
{
VerifyMatchIs(@"\Mab*", "ab*", "ab*");
VerifyMatchIs(@"\Mab\*", "ab", "ab");
VerifyMatchIs(@"\Mab\*", "caabb", "a");
VerifyMatchIs(@"\Mab\*", "cabb", "abb");
}
[Test]
public void ItemStar3()
{
VerifyMatchIs(@"\mab*", "abb", "abb");
VerifyMatchIs(@"\mab*", "cab", "ab");
VerifyMatchIs(@"\mab*", "cabb", "abb");
}
[Test]
public void ItemQuestion1()
{
VerifyMatchIs(@"ab?", "ab?", "ab?");
VerifyMatchIs(@"ab\?", "ab", "ab");
VerifyMatchIs(@"ab\?", "abc", "ab");
VerifyMatchIs(@"ab\?", "adc", "a");
}
[Test]
public void ItemQuestion2()
{
VerifyMatchIs(@"\Mab?", "ab?", "ab?");
VerifyMatchIs(@"\Mab\?", "ab", "ab");
VerifyMatchIs(@"\Mab\?", "abc", "ab");
}
[Test]
public void ItemQuestion3()
{
VerifyMatchIs(@"\vab?", "ad", "a");
VerifyMatchIs(@"\vab?", "ab", "ab");
VerifyMatchIs(@"\vab?", "abc", "ab");
}
[Test]
public void ItemEqual1()
{
VerifyMatchIs(@"ab\=", "a", "a");
VerifyMatchIs(@"ab\=", "ab", "ab");
VerifyMatchIs(@"ab\=", "abc", "ab");
}
[Test]
public void ItemEqual2()
{
VerifyMatchIs(@"\Mab=", "ab=", "ab=");
VerifyMatchIs(@"\Mab\=", "ab", "ab");
VerifyMatchIs(@"\Mab\=", "abc", "ab");
VerifyMatchIs(@"\Mab\=", "adc", "a");
}
[Test]
public void ItemEqual3()
{
VerifyMatchIs(@"\vab=", "a", "a");
VerifyMatchIs(@"\vab=", "ab", "ab");
VerifyMatchIs(@"\vab=", "abc", "ab");
}
[Test]
public void AtomHat1()
{
VerifyMatches(@"^m", "m");
VerifyMatches(@"^", "aoeu");
}
[Test]
public void AtomHat2()
{
VerifyMatches(@"\M^m", "m");
VerifyMatches(@"\M^", "aoeu");
}
[Test]
public void AtomHat3()
{
VerifyMatches(@"\v^m", "m");
VerifyMatches(@"\v^", "aoeu");
}
[Test]
[Description("Only use ^ as magic at the start of a pattern")]
public void AtomHat4()
{
VerifyMatchIs(@"a^", "a^", "a^");
VerifyMatchIs(@"\Ma^", "a^", "a^");
}
[Test]
public void AtomHat5()
{
VerifyMatches(@"\V^m", "a^m");
VerifyMatches(@"\V^", "a^aoeu");
}
[Test]
public void AtomBackslashHat1()
{
VerifyMatchIs(@"\^", "^", "^");
VerifyMatchIs(@"\^a", "^a", "^a");
VerifyMatchIs(@"b\^a", "b^a", "b^a");
VerifyNotMatches(@"\^", "a");
}
[Test]
public void AtomBackslashHat2()
{
VerifyMatches(@"\V\^m", "m");
VerifyMatches(@"\V\^", "aoeu");
}
[Test]
public void AtomBackslashHat3()
{
VerifyMatchIs(@"\M\^", "^", "^");
VerifyMatchIs(@"\M\^a", "^a", "^a");
VerifyMatchIs(@"\Mb\^a", "b^a", "b^a");
VerifyNotMatches(@"\M\^", "a");
}
[Test]
public void AtomBackslashHat4()
{
VerifyMatchIs(@"\v\^", "^", "^");
VerifyMatchIs(@"\v\^a", "^a", "^a");
VerifyMatchIs(@"\vb\^a", "b^a", "b^a");
VerifyNotMatches(@"\v\^", "a");
}
[Test]
public void AtomBackslashUnderscoreHat1()
{
VerifyNotRegex(@"\_");
VerifyNotRegex(@"ab\_");
}
[Test]
public void AtomBackslashUnderscoreHat2()
{
VerifyNotRegex(@"\M\_");
VerifyNotRegex(@"\M\_");
}
[Test]
public void AtomBackslashUnderscoreHat3()
{
VerifyMatches(@"\_^", "abc");
VerifyMatches(@"\_^", "");
VerifyMatches(@"c\?\_^ab", "ab");
}
[Test]
public void AtomBackslashUnderscoreHat4()
{
VerifyMatches(@"\M\_^", "abc");
VerifyMatches(@"\M\_^", "");
VerifyMatches(@"\Mc\?\_^ab", "ab");
}
[Test]
public void AtomDollar1()
{
VerifyMatches(@"$", "");
VerifyMatches(@"$", "aoe");
VerifyMatches(@"\M$", "aoe");
}
[Test]
public void AtomDollar2()
{
VerifyMatchIs(@"a$", "baaa", "a");
VerifyMatchIs(@"a*$", "baaa", "aaa");
}
[Test]
public void AtomDollar3()
{
VerifyMatchIs(@"\Ma$", "baaa", "a");
VerifyMatchIs(@"\Ma\*$", "baaa", "aaa");
}
[Test]
public void AtomDollar4()
{
VerifyMatchIs(@"\Ma$b", "a$bz", "a$b");
VerifyMatchIs(@"\Ma\*$b", "aa$bz", "aa$b");
}
[Test]
public void AtomBackslashDollar1()
{
VerifyNotMatches(@"\$", "");
VerifyNotMatches(@"\$", "aoe");
VerifyNotMatches(@"\M\$", "aoe");
}
[Test]
public void AtomBackslashDollar2()
{
VerifyMatchIs(@"\$", "$", "$");
VerifyMatchIs(@"\$ab", "$ab", "$ab");
}
[Test]
public void AtomBackslashUnderscoreDollar1()
{
VerifyMatchIs(@"\$", "$", "$");
VerifyMatchIs(@"\$ab", "$ab", "$ab");
}
[Test]
public void AtomBackslashUnderscoreDollar2()
{
VerifyNotRegex(@"\_");
}
[Test]
public void AtomBackslashUnderscoreDollar3()
{
VerifyMatchIs(@"\Ma\_$", "baaa", "a");
VerifyMatchIs(@"\Ma\*\_$", "baaa", "aaa");
}
[Test]
public void WordBoundary1()
{
VerifyNotMatches(@"\<word", "aword");
VerifyNotMatches(@"\M\<word", "aword");
}
[Test]
public void WordBoundary2()
{
VerifyMatches(@"\<word", "a word");
VerifyMatches(@"\M\<word", "a word");
}
[Test]
public void WordBoundary3()
{
VerifyMatchIs(@"\<word", "a word", "word");
VerifyMatchIs(@"\M\<word", "a word", "word");
}
[Test]
public void WordBoundary4()
{
VerifyNotMatches(@"word\>", "words");
VerifyNotMatches(@"\Mword\>", "words");
}
[Test]
public void WordBoundary5()
{
VerifyMatches(@"word\>", "a word again");
VerifyMatches(@"\Mword\>", "a word again");
}
[Test]
[Description("Boundary at the end of a line")]
public void WordBoundary6()
{
VerifyMatches(@"word\>", "a word");
VerifyMatches(@"\Mword\>", "a word");
}
[Test]
public void Grouping1()
{
VerifyMatchIs(@"(a)", "foo(a)", "(a)");
VerifyMatchIs(@"(abc)", "foo(abc)", "(abc)");
}
[Test]
public void Grouping2()
{
VerifyMatchIs(@"\(ab\)", "foo(ab)", "ab");
VerifyMatchIs(@"\(ab\)", "abc", "ab");
}
[Test]
public void Grouping3()
{
VerifyMatchIs(@"\v(a)", "foo(a)", "a");
VerifyMatchIs(@"\v(abc)", "foo(abc)", "abc");
}
[Test]
public void Grouping4()
{
var regex = Create(@"\(");
Assert.IsTrue(regex.IsNone());
}
/// <summary>
/// Make sure that \1 can be used to match the previous group specified
/// </summary>
[Test]
public void Group_MatchPreviousGroup()
{
VerifyMatches(@"\(dog\)::\1", "dog::dog");
VerifyMatches(@"\(dog\)::cat::\1", "dog::cat::dog");
VerifyNotMatches(@"\(dog\)::\1", "dog::cat");
}
[Test]
public void Separator1()
{
VerifyMatchIs(@"a\|b", "foob", "b");
VerifyMatchIs(@"a\|b", "acat", "a");
}
[Test]
public void Separator2()
{
VerifyMatchIs(@"ab\|c", "abod", "ab");
VerifyMatchIs(@"ab\|c", "babod", "ab");
VerifyMatchIs(@"ab\|c", "bacod", "c");
}
[Test]
public void Separator3()
{
VerifyMatchIs(@"\vab|c", "abod", "ab");
VerifyMatchIs(@"\vab|c", "babod", "ab");
VerifyMatchIs(@"\vab|c", "bacod", "c");
}
[Test]
[Description("Simple no-magic replace")]
public void Replace1()
{
VerifyReplace(@"foo", "foo bar", "bar", "bar bar");
VerifyReplace(@"foo", "foo bar baz", "bar", "bar bar baz");
}
[Test]
[Description("Atom match and replace")]
public void Replace2()
{
VerifyReplace(@"a\|b", "cat", "o", "cot");
}
[Test]
[Description("Word boundary replacements")]
public void Replace3()
{
VerifyReplace(@"\<foo\>", "foo bar", "bar", "bar bar");
VerifyReplace(@"\<foo\>", "foobar", "bar", "foobar");
VerifyReplace(@"\<foo\>", "foo bar baz", "bar", "bar bar baz");
}
[Test]
public void Replace4()
{
VerifyReplace(@"(ab)", "foo(ab)", "()", "foo()");
VerifyReplace(@"foo(ab)", "foo(ab)", "()", "()");
VerifyReplace(@"foo()", "foo(ab)", "()", "foo(ab)");
}
[Test]
public void Replace5()
{
VerifyReplace(@"\(ab\)", "ab", "", "");
VerifyReplace(@"\(ab\)", "cab", "", "c");
VerifyReplace(@"\(ab\)", "c(ab)", "", "c()");
}
[Test]
[Description("Replacement using the group specifier")]
public void Replace6()
{
VerifyReplace(@"foo\(\.*\)", "foobar", @"\1", "bar");
VerifyReplace(@"jaz\(\.*\)", "jaz123", @"\1", "123");
}
[Test]
[Description("Replacement using the group specifier")]
public void Replace7()
{
VerifyReplace(@"\(\.*\)b\(\.*\)", "abc", @"\2", "ac");
VerifyReplace(@"\(\.*\)b\(\.*\)", "abc", @"\1\2", "ac");
VerifyReplace(@"\(\.*\)b\(\.*\)", "abc", @"\1blah\2", "ablahc");
}
/// <summary>
/// Escaped back slashes should appear as normal back slashes in the replacement string
/// </summary>
[Test]
public void Replace_EscapedBackSlashes()
{
VerifyReplace("b", "abc", @"\\\\", @"a\\c");
}
/// <summary>
/// Don't treat an escaped backslash in front of a 'n' character as a new line.
///
/// Issue #779
/// </summary>
[Test]
public void Replace_EscapedBackSlashNotNewLine()
{
VerifyReplace("b", "abc", @"\\n\\", @"a\n\c");
VerifyReplace("$", "dog", @"\\n\\", @"dog\n\");
}
/// <summary>
/// When the '&' character is used in the replacement string it should replace with
/// the entire matched pattern
/// </summary>
[Test]
public void Replace_Ampersand()
{
VerifyReplace("a", "cat", @"o&", "coat");
VerifyReplace(@"a\+", "caat", @"o&", "coaat");
}
/// <summary>
/// When there is no magic then the ampersand is not special and should replace
/// as normal
/// </summary>
[Test]
public void Replace_Ampersand_NoMagic()
{
VerifyReplace(VimRegexOptions.NoMagic, "a", "cat", @"o&", "co&t");
VerifyReplace(VimRegexOptions.NoMagic, @"a\+", "caat", @"o&", "co&t");
}
/// <summary>
/// When escaped with magic it should behave simply as an ampersand
/// </summary>
[Test]
public void Replace_EscapedAmpersand()
{
VerifyReplace("a", "cat", @"o\&", "co&t");
VerifyReplace(@"a\+", "caat", @"o\&", "co&t");
}
/// <summary>
/// The '\0' pattern is used to match the entire matched pattern. It acts exactly
/// as '&' does in the replacement string
/// </summary>
[Test]
public void Replace_EscapedZero()
{
VerifyReplace("a", "cat", @"o\0", "coat");
VerifyReplace(@"a\+", "caat", @"o\0", "coaat");
}
/// <summary>
/// The '\t' replacement string should insert a tab
/// </summary>
[Test]
public void Replace_Escaped_T()
{
VerifyReplace("a", "a", @"\t", "\t");
VerifyReplace(" ", " ", @"\t", "\t\t");
}
/// <summary>
/// The '\r' replacement should insert a carriage return
/// </summary>
[Test]
public void Replace_Escaped_R()
{
VerifyReplace("a", "a", @"\r", Environment.NewLine);
}
[Test]
public void CharacterSequence1()
{
VerifyMatches(@"[abc]", "a", "b", "c");
VerifyMatches(@"\M\[abc]", "a", "b", "c");
}
[Test]
[Description("Range support")]
public void CharacterSequence2()
{
VerifyMatches(@"[a-z]", "a", "b", "c", "z");
VerifyMatches(@"\M\[a-c]", "a", "b", "c");
}
[Test]
public void AtomDigits()
{
VerifyMatches(@"\d", "1", "2");
VerifyMatches(@"\M\d", "1", "2");
VerifyNotMatches(@"\d", "a");
}
[Test]
public void AtomNonDigits()
{
VerifyMatches(@"\D", "a", "b");
VerifyMatches(@"\M\D", "a", "b");
VerifyNotMatches(@"\M\D", "1", "2");
}
[Test]
public void AtomWordCharacter()
{
VerifyMatches(@"\w", "a", "A", "_", "1", "4");
VerifyMatches(@"\M\w", "a", "A", "_", "1", "4");
VerifyNotMatches(@"\w", "%");
VerifyNotMatches(@"\M\w", "%");
}
[Test]
public void AtomNonWordCharacter()
{
VerifyNotMatches(@"\W", "a", "A", "_", "1", "4");
VerifyNotMatches(@"\M\W", "a", "A", "_", "1", "4");
VerifyMatches(@"\W", "%");
VerifyMatches(@"\M\W", "%");
}
[Test]
public void AtomHexDigit()
{
VerifyMatches(@"\x", "0123456789abcdef".Select(x => x.ToString()).ToArray());
VerifyNotMatches(@"\x", "%", "^", "g", "h");
}
[Test]
public void AtomNonHexDigit()
{
VerifyNotMatches(@"\X", "0123456789abcdef".Select(x => x.ToString()).ToArray());
VerifyMatches(@"\X", "%", "^", "g", "h");
}
[Test]
public void AtomOctal()
{
VerifyMatches(@"\o", "01234567".Select(x => x.ToString()).ToArray());
VerifyNotMatches(@"\o", "%", "^", "g", "h", "8", "9");
}
[Test]
public void AtomNonOctal()
{
VerifyNotMatches(@"\O", "01234567".Select(x => x.ToString()).ToArray());
VerifyMatches(@"\O", "%", "^", "g", "h", "8", "9");
}
[Test]
public void AtomHeadOfWord()
{
VerifyMatches(@"\h", LowerCaseLetters);
VerifyMatches(@"\h", UpperCaseLetters);
VerifyMatches(@"\h", "_");
VerifyNotMatches(@"\h", Digits);
}
[Test]
public void AtomNonHeadOfWord()
{
VerifyNotMatches(@"\H", LowerCaseLetters);
VerifyNotMatches(@"\H", UpperCaseLetters);
VerifyNotMatches(@"\H", "_");
VerifyMatches(@"\H", Digits);
}
[Test]
public void AtomAlphabeticChar()
{
VerifyMatches(@"\a", LowerCaseLetters);
VerifyMatches(@"\a", UpperCaseLetters);
VerifyNotMatches(@"\a", Digits);
}
[Test]
public void AtomNonAlphabeticChar()
{
VerifyNotMatches(@"\A", LowerCaseLetters);
VerifyNotMatches(@"\A", UpperCaseLetters);
VerifyMatches(@"\A", "_");
VerifyMatches(@"\A", Digits);
}
[Test]
public void AtomLowerLetters()
{
_globalSettings.IgnoreCase = false;
VerifyMatches(@"\l", LowerCaseLetters);
VerifyNotMatches(@"\l", UpperCaseLetters);
VerifyNotMatches(@"\l", Digits);
}
[Test]
public void AtomNonLowerLetters()
{
_globalSettings.IgnoreCase = false;
VerifyNotMatches(@"\L", LowerCaseLetters);
VerifyMatches(@"\L", UpperCaseLetters);
VerifyMatches(@"\L", "_");
VerifyMatches(@"\L", Digits);
}
[Test]
public void AtomUpperLetters()
{
_globalSettings.IgnoreCase = false;
VerifyMatches(@"\u", UpperCaseLetters);
VerifyNotMatches(@"\u", LowerCaseLetters);
VerifyNotMatches(@"\u", Digits);
}
[Test]
public void AtomNonUpperLetters()
{
_globalSettings.IgnoreCase = false;
VerifyNotMatches(@"\U", UpperCaseLetters);
VerifyMatches(@"\U", LowerCaseLetters);
VerifyMatches(@"\U", "_");
VerifyMatches(@"\U", Digits);
}
[Test]
public void AtomPlus()
{
_globalSettings.Magic = true;
VerifyMatches(@"a\+", "a", "aa");
VerifyNotMatches(@"a\+", "b");
VerifyMatchIs(@"\va+", "aa", "aa");
}
[Test]
public void AtomCount()
{
_globalSettings.Magic = true;
VerifyMatchIs(@"a\{1}", "aaa", "a");
VerifyMatchIs(@"a\{1,2}", "aaa", "aa");
VerifyMatchIs(@"a\{1,3}", "aaa", "aaa");
VerifyMatchIs(@"a\{2,3}", "aaa", "aaa");
VerifyNotMatches(@"a\{3}", "a");
}
[Test]
public void AtomStar_Magic()
{
_globalSettings.Magic = true;
VerifyMatchIs(@"a*", "aa", "aa");
VerifyMatchIs(@"a\*", "a*", "a*");
}
[Test]
public void AtomStar_NoMagic()
{
_globalSettings.Magic = false;
VerifyMatchIs(@"a*", "a*", "a*");
VerifyMatchIs(@"a\*", "aaa", "aaa");
}
[Test]
public void AtomGroup_GroupAllBut()
{
_globalSettings.Magic = true;
VerifyMatchIs(@"[^""]*b", "acbd", "acb");
VerifyMatchIs(@"""[^""]*", @"b""cd", @"""cd");
}
[Test]
public void AtomWhitespace_NoMagic()
{
_globalSettings.Magic = false;
VerifyMatchIs(@"\s", " ", " ");
VerifyMatchIs(@"hello\sworld", "hello world", "hello world");
VerifyMatchIs(@"hello\s\*world", "hello world", "hello world");
}
[Test]
public void AtomWhitespace_Magic()
{
_globalSettings.Magic = true;
VerifyMatchIs(@"\s", " ", " ");
VerifyMatchIs(@"hello\sworld", "hello world", "hello world");
VerifyMatchIs(@"hello\s*world", "hello world", "hello world");
}
[Test]
public void AtomWhitespace_MultipleWithStarQualifier()
{
_globalSettings.Magic = true;
VerifyMatchIs(@"TCHAR\s\s*buff", "TCHAR buff", "TCHAR buff");
}
[Test]
public void AtomNonWhitespace_NoMagic()
{
_globalSettings.Magic = false;
VerifyMatchIs(@"\S", "a", "a");
VerifyMatchIs(@"hello\Sworld", "hello!world", "hello!world");
}
[Test]