forked from dotnet/roslyn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAnalyzerConfigTests.cs
2431 lines (2034 loc) · 79.9 KB
/
AnalyzerConfigTests.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable disable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Test.Utilities;
using Xunit;
using static Microsoft.CodeAnalysis.AnalyzerConfig;
using static Roslyn.Test.Utilities.TestHelpers;
using KeyValuePair = Roslyn.Utilities.KeyValuePairUtil;
namespace Microsoft.CodeAnalysis.UnitTests
{
public class EditorConfigTests
{
#region Parsing Tests
private static AnalyzerConfig ParseConfigFile(string text) => Parse(text, "/.editorconfig");
[Fact]
public void SimpleCase()
{
var config = Parse(@"
root = true
# Comment1
# Comment2
##################################
my_global_prop = my_global_val
[*.cs]
my_prop = my_val
", "/bogus/.editorconfig");
Assert.Equal("", config.GlobalSection.Name);
var properties = config.GlobalSection.Properties;
AssertEx.SetEqual(
new[] { KeyValuePair.Create("my_global_prop", "my_global_val"),
KeyValuePair.Create("root", "true") },
properties);
var namedSections = config.NamedSections;
Assert.Equal("*.cs", namedSections[0].Name);
AssertEx.SetEqual(
new[] { KeyValuePair.Create("my_prop", "my_val") },
namedSections[0].Properties);
Assert.True(config.IsRoot);
Assert.Equal("/bogus", config.NormalizedDirectory);
}
[ConditionalFact(typeof(WindowsOnly))]
public void WindowsPath()
{
const string path = "Z:\\bogus\\.editorconfig";
var config = Parse("", path);
Assert.Equal("Z:/bogus", config.NormalizedDirectory);
Assert.Equal(path, config.PathToFile);
}
[Fact]
public void MissingClosingBracket()
{
var config = ParseConfigFile(@"
[*.cs
my_prop = my_val");
var properties = config.GlobalSection.Properties;
AssertEx.SetEqual(
new[] { KeyValuePair.Create("my_prop", "my_val") },
properties);
Assert.Equal(0, config.NamedSections.Length);
}
[Fact]
public void EmptySection()
{
var config = ParseConfigFile(@"
[]
my_prop = my_val");
var properties = config.GlobalSection.Properties;
Assert.Equal(new[] { KeyValuePair.Create("my_prop", "my_val") }, properties);
Assert.Equal(0, config.NamedSections.Length);
}
[Fact]
public void CaseInsensitivePropKey()
{
var config = ParseConfigFile(@"
my_PROP = my_VAL");
var properties = config.GlobalSection.Properties;
Assert.True(properties.TryGetValue("my_PrOp", out var val));
Assert.Equal("my_VAL", val);
Assert.Equal("my_prop", properties.Keys.Single());
}
[Fact]
public void NonReservedKeyPreservedCaseVal()
{
var config = ParseConfigFile(string.Join(Environment.NewLine,
AnalyzerConfig.ReservedKeys.Select(k => "MY_" + k + " = MY_VAL")));
AssertEx.SetEqual(
AnalyzerConfig.ReservedKeys.Select(k => KeyValuePair.Create("my_" + k, "MY_VAL")).ToList(),
config.GlobalSection.Properties);
}
[Fact]
public void DuplicateKeys()
{
var config = ParseConfigFile(@"
my_prop = my_val
my_prop = my_other_val");
var properties = config.GlobalSection.Properties;
Assert.Equal(new[] { KeyValuePair.Create("my_prop", "my_other_val") }, properties);
}
[Fact]
public void DuplicateKeysCasing()
{
var config = ParseConfigFile(@"
my_prop = my_val
my_PROP = my_other_val");
var properties = config.GlobalSection.Properties;
Assert.Equal(new[] { KeyValuePair.Create("my_prop", "my_other_val") }, properties);
}
[Fact]
public void MissingKey()
{
var config = ParseConfigFile(@"
= my_val1
my_prop = my_val2");
var properties = config.GlobalSection.Properties;
AssertEx.SetEqual(
new[] { KeyValuePair.Create("my_prop", "my_val2") },
properties);
}
[Fact]
public void MissingVal()
{
var config = ParseConfigFile(@"
my_prop1 =
my_prop2 = my_val");
var properties = config.GlobalSection.Properties;
AssertEx.SetEqual(
new[] { KeyValuePair.Create("my_prop1", ""),
KeyValuePair.Create("my_prop2", "my_val") },
properties);
}
[Fact]
public void SpacesInProperties()
{
var config = ParseConfigFile(@"
my prop1 = my_val1
my_prop2 = my val2");
var properties = config.GlobalSection.Properties;
AssertEx.SetEqual(
new[] { KeyValuePair.Create("my_prop2", "my val2") },
properties);
}
[Fact]
public void EndOfLineComments()
{
var config = ParseConfigFile(@"
my_prop2 = my val2 # Comment");
var properties = config.GlobalSection.Properties;
AssertEx.SetEqual(
new[] { KeyValuePair.Create("my_prop2", "my val2") },
properties);
}
[Fact]
public void SymbolsStartKeys()
{
var config = ParseConfigFile(@"
@!$abc = my_val1
@!$\# = my_val2");
var properties = config.GlobalSection.Properties;
Assert.Equal(0, properties.Count);
}
[Fact]
public void EqualsAndColon()
{
var config = ParseConfigFile(@"
my:key1 = my_val
my_key2 = my:val");
var properties = config.GlobalSection.Properties;
AssertEx.SetEqual(
new[] { KeyValuePair.Create("my", "key1 = my_val"),
KeyValuePair.Create("my_key2", "my:val")},
properties);
}
[Fact]
public void SymbolsInProperties()
{
var config = ParseConfigFile(@"
my@key1 = my_val
my_key2 = my@val");
var properties = config.GlobalSection.Properties;
AssertEx.SetEqual(
new[] { KeyValuePair.Create("my_key2", "my@val") },
properties);
}
[Fact]
public void LongLines()
{
// This example is described in the Python ConfigParser as allowing
// line continuation via the RFC 822 specification, section 3.1.1
// LONG HEADER FIELDS. The VS parser does not accept this as a
// valid parse for an editorconfig file. We follow similarly.
var config = ParseConfigFile(@"
long: this value continues
in the next line");
var properties = config.GlobalSection.Properties;
AssertEx.SetEqual(
new[] { KeyValuePair.Create("long", "this value continues") },
properties);
}
[Fact]
public void CaseInsensitiveRoot()
{
var config = ParseConfigFile(@"
RoOt = TruE");
Assert.True(config.IsRoot);
}
[Fact]
public void ReservedValues()
{
int index = 0;
var config = ParseConfigFile(string.Join(Environment.NewLine,
AnalyzerConfig.ReservedValues.Select(v => "MY_KEY" + (index++) + " = " + v.ToUpperInvariant())));
index = 0;
AssertEx.SetEqual(
AnalyzerConfig.ReservedValues.Select(v => KeyValuePair.Create("my_key" + (index++), v)).ToList(),
config.GlobalSection.Properties);
}
[Fact]
public void ReservedKeys()
{
var config = ParseConfigFile(string.Join(Environment.NewLine,
AnalyzerConfig.ReservedKeys.Select(k => k + " = MY_VAL")));
AssertEx.SetEqual(
AnalyzerConfig.ReservedKeys.Select(k => KeyValuePair.Create(k, "my_val")).ToList(),
config.GlobalSection.Properties);
}
#endregion
#region Section Matching Tests
[Fact]
public void SimpleNameMatch()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("abc").Value;
Assert.Equal("^.*/abc$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/abc"));
Assert.False(matcher.IsMatch("/aabc"));
Assert.False(matcher.IsMatch("/ abc"));
Assert.False(matcher.IsMatch("/cabc"));
}
[Fact]
public void StarOnlyMatch()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("*").Value;
Assert.Equal("^.*/[^/]*$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/abc"));
Assert.True(matcher.IsMatch("/123"));
Assert.True(matcher.IsMatch("/abc/123"));
}
[Fact]
public void StarNameMatch()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("*.cs").Value;
Assert.Equal("^.*/[^/]*\\.cs$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/abc.cs"));
Assert.True(matcher.IsMatch("/123.cs"));
Assert.True(matcher.IsMatch("/dir/subpath.cs"));
// Only '/' is defined as a directory separator, so the caller
// is responsible for converting any other machine directory
// separators to '/' before matching
Assert.True(matcher.IsMatch("/dir\\subpath.cs"));
Assert.False(matcher.IsMatch("/abc.vb"));
}
[Fact]
public void StarStarNameMatch()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("**.cs").Value;
Assert.Equal("^.*/.*\\.cs$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/abc.cs"));
Assert.True(matcher.IsMatch("/dir/subpath.cs"));
}
[Fact]
public void EscapeDot()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("...").Value;
Assert.Equal("^.*/\\.\\.\\.$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/..."));
Assert.True(matcher.IsMatch("/subdir/..."));
Assert.False(matcher.IsMatch("/aaa"));
Assert.False(matcher.IsMatch("/???"));
Assert.False(matcher.IsMatch("/abc"));
}
[Fact]
public void EndBackslashMatch()
{
SectionNameMatcher? matcher = TryCreateSectionNameMatcher("abc\\");
Assert.Null(matcher);
}
[Fact]
public void QuestionMatch()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("ab?def").Value;
Assert.Equal("^.*/ab.def$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/abcdef"));
Assert.True(matcher.IsMatch("/ab?def"));
Assert.True(matcher.IsMatch("/abzdef"));
Assert.True(matcher.IsMatch("/ab/def"));
Assert.True(matcher.IsMatch("/ab\\def"));
}
[Fact]
public void LiteralBackslash()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("ab\\\\c").Value;
Assert.Equal("^.*/ab\\\\c$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/ab\\c"));
Assert.False(matcher.IsMatch("/ab/c"));
Assert.False(matcher.IsMatch("/ab\\\\c"));
}
[Fact]
public void LiteralStars()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("\\***\\*\\**").Value;
Assert.Equal("^.*/\\*.*\\*\\*[^/]*$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/*ab/cd**efg*"));
Assert.False(matcher.IsMatch("/ab/cd**efg*"));
Assert.False(matcher.IsMatch("/*ab/cd*efg*"));
Assert.False(matcher.IsMatch("/*ab/cd**ef/gh"));
}
[Fact]
public void LiteralQuestions()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("\\??\\?*\\??").Value;
Assert.Equal("^.*/\\?.\\?[^/]*\\?.$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/?a?cde?f"));
Assert.True(matcher.IsMatch("/???????f"));
Assert.False(matcher.IsMatch("/aaaaaaaa"));
Assert.False(matcher.IsMatch("/aa?cde?f"));
Assert.False(matcher.IsMatch("/?a?cdexf"));
Assert.False(matcher.IsMatch("/?axcde?f"));
}
[Fact]
public void LiteralBraces()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("abc\\{\\}def").Value;
Assert.Equal(@"^.*/abc\{}def$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/abc{}def"));
Assert.True(matcher.IsMatch("/subdir/abc{}def"));
Assert.False(matcher.IsMatch("/abcdef"));
Assert.False(matcher.IsMatch("/abc}{def"));
}
[Fact]
public void LiteralComma()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("abc\\,def").Value;
Assert.Equal("^.*/abc,def$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/abc,def"));
Assert.True(matcher.IsMatch("/subdir/abc,def"));
Assert.False(matcher.IsMatch("/abcdef"));
Assert.False(matcher.IsMatch("/abc\\,def"));
Assert.False(matcher.IsMatch("/abc`def"));
}
[Fact]
public void SimpleChoice()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("*.{cs,vb,fs}").Value;
Assert.Equal("^.*/[^/]*\\.(?:cs|vb|fs)$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/abc.cs"));
Assert.True(matcher.IsMatch("/abc.vb"));
Assert.True(matcher.IsMatch("/abc.fs"));
Assert.True(matcher.IsMatch("/subdir/abc.cs"));
Assert.True(matcher.IsMatch("/subdir/abc.vb"));
Assert.True(matcher.IsMatch("/subdir/abc.fs"));
Assert.False(matcher.IsMatch("/abcxcs"));
Assert.False(matcher.IsMatch("/abcxvb"));
Assert.False(matcher.IsMatch("/abcxfs"));
Assert.False(matcher.IsMatch("/subdir/abcxcs"));
Assert.False(matcher.IsMatch("/subdir/abcxcb"));
Assert.False(matcher.IsMatch("/subdir/abcxcs"));
}
[Fact]
public void OneChoiceHasSlashes()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("{*.cs,subdir/test.vb}").Value;
// This is an interesting case that may be counterintuitive. A reasonable understanding
// of the section matching could interpret the choice as generating multiple identical
// sections, so [{a, b, c}] would be equivalent to [a] ... [b] ... [c] with all of the
// same properties in each section. This is somewhat true, but the rules of how the matching
// prefixes are constructed violate this assumption because they are defined as whether or
// not a section contains a slash, not whether any of the choices contain a slash. So while
// [*.cs] usually translates into '**/*.cs' because it contains no slashes, the slashes in
// the second choice make this into '/*.cs', effectively matching only files in the root
// directory of the match, instead of all subdirectories.
Assert.Equal("^/(?:[^/]*\\.cs|subdir/test\\.vb)$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/test.cs"));
Assert.True(matcher.IsMatch("/subdir/test.vb"));
Assert.False(matcher.IsMatch("/subdir/test.cs"));
Assert.False(matcher.IsMatch("/subdir/subdir/test.vb"));
Assert.False(matcher.IsMatch("/test.vb"));
}
[Fact]
public void EmptyChoice()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("{}").Value;
Assert.Equal("^.*/(?:)$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/"));
Assert.True(matcher.IsMatch("/subdir/"));
Assert.False(matcher.IsMatch("/."));
Assert.False(matcher.IsMatch("/anything"));
}
[Fact]
public void SingleChoice()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("{*.cs}").Value;
Assert.Equal("^.*/(?:[^/]*\\.cs)$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/test.cs"));
Assert.True(matcher.IsMatch("/subdir/test.cs"));
Assert.False(matcher.IsMatch("test.vb"));
Assert.False(matcher.IsMatch("testxcs"));
}
[Fact]
public void UnmatchedBraces()
{
SectionNameMatcher? matcher = TryCreateSectionNameMatcher("{{{{}}");
Assert.Null(matcher);
}
[Fact]
public void CommaOutsideBraces()
{
SectionNameMatcher? matcher = TryCreateSectionNameMatcher("abc,def");
Assert.Null(matcher);
}
[Fact]
public void RecursiveChoice()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("{test{.cs,.vb},other.{a{bb,cc}}}").Value;
Assert.Equal("^.*/(?:test(?:\\.cs|\\.vb)|other\\.(?:a(?:bb|cc)))$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/test.cs"));
Assert.True(matcher.IsMatch("/test.vb"));
Assert.True(matcher.IsMatch("/subdir/test.cs"));
Assert.True(matcher.IsMatch("/subdir/test.vb"));
Assert.True(matcher.IsMatch("/other.abb"));
Assert.True(matcher.IsMatch("/other.acc"));
Assert.False(matcher.IsMatch("/test.fs"));
Assert.False(matcher.IsMatch("/other.bbb"));
Assert.False(matcher.IsMatch("/other.ccc"));
Assert.False(matcher.IsMatch("/subdir/other.bbb"));
Assert.False(matcher.IsMatch("/subdir/other.ccc"));
}
[Fact]
public void DashChoice()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("ab{-}cd{-,}ef").Value;
Assert.Equal("^.*/ab(?:-)cd(?:-|)ef$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/ab-cd-ef"));
Assert.True(matcher.IsMatch("/ab-cdef"));
Assert.False(matcher.IsMatch("/abcdef"));
Assert.False(matcher.IsMatch("/ab--cd-ef"));
Assert.False(matcher.IsMatch("/ab--cd--ef"));
}
[Fact]
public void MiddleMatch()
{
SectionNameMatcher matcher = TryCreateSectionNameMatcher("ab{cs,vb,fs}cd").Value;
Assert.Equal("^.*/ab(?:cs|vb|fs)cd$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/abcscd"));
Assert.True(matcher.IsMatch("/abvbcd"));
Assert.True(matcher.IsMatch("/abfscd"));
Assert.False(matcher.IsMatch("/abcs"));
Assert.False(matcher.IsMatch("/abcd"));
Assert.False(matcher.IsMatch("/vbcd"));
}
private static IEnumerable<(string, string)> RangeAndInverse(string s1, string s2)
{
yield return (s1, s2);
yield return (s2, s1);
}
[Fact]
public void NumberMatch()
{
foreach (var (i1, i2) in RangeAndInverse("0", "10"))
{
var matcher = TryCreateSectionNameMatcher($"{{{i1}..{i2}}}").Value;
Assert.True(matcher.IsMatch("/0"));
Assert.True(matcher.IsMatch("/10"));
Assert.True(matcher.IsMatch("/5"));
Assert.True(matcher.IsMatch("/000005"));
Assert.False(matcher.IsMatch("/-1"));
Assert.False(matcher.IsMatch("/-00000001"));
Assert.False(matcher.IsMatch("/11"));
}
}
[Fact]
public void NumberMatchNegativeRange()
{
foreach (var (i1, i2) in RangeAndInverse("-10", "0"))
{
var matcher = TryCreateSectionNameMatcher($"{{{i1}..{i2}}}").Value;
Assert.True(matcher.IsMatch("/0"));
Assert.True(matcher.IsMatch("/-10"));
Assert.True(matcher.IsMatch("/-5"));
Assert.False(matcher.IsMatch("/1"));
Assert.False(matcher.IsMatch("/-11"));
Assert.False(matcher.IsMatch("/--0"));
}
}
[Fact]
public void NumberMatchNegToPos()
{
foreach (var (i1, i2) in RangeAndInverse("-10", "10"))
{
var matcher = TryCreateSectionNameMatcher($"{{{i1}..{i2}}}").Value;
Assert.True(matcher.IsMatch("/0"));
Assert.True(matcher.IsMatch("/-5"));
Assert.True(matcher.IsMatch("/5"));
Assert.True(matcher.IsMatch("/-10"));
Assert.True(matcher.IsMatch("/10"));
Assert.False(matcher.IsMatch("/-11"));
Assert.False(matcher.IsMatch("/11"));
Assert.False(matcher.IsMatch("/--0"));
}
}
[Fact]
public void MultipleNumberRanges()
{
foreach (var matchString in new[] { "a{-10..0}b{0..10}", "a{0..-10}b{10..0}" })
{
var matcher = TryCreateSectionNameMatcher(matchString).Value;
Assert.True(matcher.IsMatch("/a0b0"));
Assert.True(matcher.IsMatch("/a-5b0"));
Assert.True(matcher.IsMatch("/a-5b5"));
Assert.True(matcher.IsMatch("/a-5b10"));
Assert.True(matcher.IsMatch("/a-10b10"));
Assert.True(matcher.IsMatch("/a-10b0"));
Assert.True(matcher.IsMatch("/a-0b0"));
Assert.True(matcher.IsMatch("/a-0b-0"));
Assert.False(matcher.IsMatch("/a-11b10"));
Assert.False(matcher.IsMatch("/a-11b10"));
Assert.False(matcher.IsMatch("/a-10b11"));
}
}
[Fact]
public void BadNumberRanges()
{
var matcherOpt = TryCreateSectionNameMatcher("{0..");
Assert.Null(matcherOpt);
var matcher = TryCreateSectionNameMatcher("{0..}").Value;
Assert.True(matcher.IsMatch("/0.."));
Assert.False(matcher.IsMatch("/0"));
Assert.False(matcher.IsMatch("/0."));
Assert.False(matcher.IsMatch("/0abc"));
matcher = TryCreateSectionNameMatcher("{0..A}").Value;
Assert.True(matcher.IsMatch("/0..A"));
Assert.False(matcher.IsMatch("/0"));
Assert.False(matcher.IsMatch("/0abc"));
// The reference implementation uses atoi here so we can presume
// numbers out of range of Int32 are not well supported
matcherOpt = TryCreateSectionNameMatcher($"{{0..{UInt32.MaxValue}}}");
Assert.Null(matcherOpt);
}
[Fact]
public void CharacterClassSimple()
{
var matcher = TryCreateSectionNameMatcher("*.[cf]s").Value;
Assert.Equal(@"^.*/[^/]*\.[cf]s$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/abc.cs"));
Assert.True(matcher.IsMatch("/abc.fs"));
Assert.False(matcher.IsMatch("/abc.vs"));
}
[Fact]
public void CharacterClassNegative()
{
var matcher = TryCreateSectionNameMatcher("*.[!cf]s").Value;
Assert.Equal(@"^.*/[^/]*\.[^cf]s$", matcher.Regex.ToString());
Assert.False(matcher.IsMatch("/abc.cs"));
Assert.False(matcher.IsMatch("/abc.fs"));
Assert.True(matcher.IsMatch("/abc.vs"));
Assert.True(matcher.IsMatch("/abc.xs"));
Assert.False(matcher.IsMatch("/abc.vxs"));
}
[Fact]
public void CharacterClassCaret()
{
var matcher = TryCreateSectionNameMatcher("*.[^cf]s").Value;
Assert.Equal(@"^.*/[^/]*\.[\^cf]s$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/abc.cs"));
Assert.True(matcher.IsMatch("/abc.fs"));
Assert.True(matcher.IsMatch("/abc.^s"));
Assert.False(matcher.IsMatch("/abc.vs"));
Assert.False(matcher.IsMatch("/abc.xs"));
Assert.False(matcher.IsMatch("/abc.vxs"));
}
[Fact]
public void CharacterClassRange()
{
var matcher = TryCreateSectionNameMatcher("[0-9]x").Value;
Assert.Equal("^.*/[0-9]x$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/0x"));
Assert.True(matcher.IsMatch("/1x"));
Assert.True(matcher.IsMatch("/9x"));
Assert.False(matcher.IsMatch("/yx"));
Assert.False(matcher.IsMatch("/00x"));
}
[Fact]
public void CharacterClassNegativeRange()
{
var matcher = TryCreateSectionNameMatcher("[!0-9]x").Value;
Assert.Equal("^.*/[^0-9]x$", matcher.Regex.ToString());
Assert.False(matcher.IsMatch("/0x"));
Assert.False(matcher.IsMatch("/1x"));
Assert.False(matcher.IsMatch("/9x"));
Assert.True(matcher.IsMatch("/yx"));
Assert.False(matcher.IsMatch("/00x"));
}
[Fact]
public void CharacterClassRangeAndChoice()
{
var matcher = TryCreateSectionNameMatcher("[ab0-9]x").Value;
Assert.Equal("^.*/[ab0-9]x$", matcher.Regex.ToString());
Assert.True(matcher.IsMatch("/ax"));
Assert.True(matcher.IsMatch("/bx"));
Assert.True(matcher.IsMatch("/0x"));
Assert.True(matcher.IsMatch("/1x"));
Assert.True(matcher.IsMatch("/9x"));
Assert.False(matcher.IsMatch("/yx"));
Assert.False(matcher.IsMatch("/0ax"));
}
[Fact]
public void CharacterClassOpenEnded()
{
var matcher = TryCreateSectionNameMatcher("[");
Assert.Null(matcher);
}
[Fact]
public void CharacterClassEscapedOpenEnded()
{
var matcher = TryCreateSectionNameMatcher(@"[\]");
Assert.Null(matcher);
}
[Fact]
public void CharacterClassEscapeAtEnd()
{
var matcher = TryCreateSectionNameMatcher(@"[\");
Assert.Null(matcher);
}
[Fact]
public void CharacterClassOpenBracketInside()
{
var matcher = TryCreateSectionNameMatcher(@"[[a]bc").Value;
Assert.True(matcher.IsMatch("/abc"));
Assert.True(matcher.IsMatch("/[bc"));
Assert.False(matcher.IsMatch("/ab"));
Assert.False(matcher.IsMatch("/[b"));
Assert.False(matcher.IsMatch("/bc"));
Assert.False(matcher.IsMatch("/ac"));
Assert.False(matcher.IsMatch("/[c"));
Assert.Equal(@"^.*/[\[a]bc$", matcher.Regex.ToString());
}
[Fact]
public void CharacterClassStartingDash()
{
var matcher = TryCreateSectionNameMatcher(@"[-ac]bd").Value;
Assert.True(matcher.IsMatch("/abd"));
Assert.True(matcher.IsMatch("/cbd"));
Assert.True(matcher.IsMatch("/-bd"));
Assert.False(matcher.IsMatch("/bbd"));
Assert.False(matcher.IsMatch("/-cd"));
Assert.False(matcher.IsMatch("/bcd"));
Assert.Equal(@"^.*/[-ac]bd$", matcher.Regex.ToString());
}
[Fact]
public void CharacterClassEndingDash()
{
var matcher = TryCreateSectionNameMatcher(@"[ac-]bd").Value;
Assert.True(matcher.IsMatch("/abd"));
Assert.True(matcher.IsMatch("/cbd"));
Assert.True(matcher.IsMatch("/-bd"));
Assert.False(matcher.IsMatch("/bbd"));
Assert.False(matcher.IsMatch("/-cd"));
Assert.False(matcher.IsMatch("/bcd"));
Assert.Equal(@"^.*/[ac-]bd$", matcher.Regex.ToString());
}
[Fact]
public void CharacterClassEndBracketAfter()
{
var matcher = TryCreateSectionNameMatcher(@"[ab]]cd").Value;
Assert.True(matcher.IsMatch("/a]cd"));
Assert.True(matcher.IsMatch("/b]cd"));
Assert.False(matcher.IsMatch("/acd"));
Assert.False(matcher.IsMatch("/bcd"));
Assert.False(matcher.IsMatch("/acd"));
Assert.Equal(@"^.*/[ab]]cd$", matcher.Regex.ToString());
}
[Fact]
public void CharacterClassEscapeBackslash()
{
var matcher = TryCreateSectionNameMatcher(@"[ab\\]cd").Value;
Assert.True(matcher.IsMatch("/acd"));
Assert.True(matcher.IsMatch("/bcd"));
Assert.True(matcher.IsMatch("/\\cd"));
Assert.False(matcher.IsMatch("/dcd"));
Assert.False(matcher.IsMatch("/\\\\cd"));
Assert.False(matcher.IsMatch("/cd"));
Assert.Equal(@"^.*/[ab\\]cd$", matcher.Regex.ToString());
}
[Fact]
public void EscapeOpenBracket()
{
var matcher = TryCreateSectionNameMatcher(@"ab\[cd").Value;
Assert.True(matcher.IsMatch("/ab[cd"));
Assert.False(matcher.IsMatch("/ab[[cd"));
Assert.False(matcher.IsMatch("/abc"));
Assert.False(matcher.IsMatch("/abd"));
Assert.Equal(@"^.*/ab\[cd$", matcher.Regex.ToString());
}
#endregion
#region Processing of dotnet_diagnostic rules
[Fact]
public void EditorConfigToDiagnostics()
{
var configs = ArrayBuilder<AnalyzerConfig>.GetInstance();
configs.Add(Parse(@"
[*.cs]
dotnet_diagnostic.cs000.severity = none
[*.vb]
dotnet_diagnostic.cs000.severity = error", "/.editorconfig"));
var options = GetAnalyzerConfigOptions(
new[] { "/test.cs", "/test.vb", "/test" },
configs);
configs.Free();
Assert.Equal(new[] {
CreateImmutableDictionary(("cs000", ReportDiagnostic.Suppress)),
CreateImmutableDictionary(("cs000", ReportDiagnostic.Error)),
SyntaxTree.EmptyDiagnosticOptions
}, options.Select(o => o.TreeOptions).ToArray());
}
[Fact]
public void LaterSectionOverrides()
{
var configs = ArrayBuilder<AnalyzerConfig>.GetInstance();
configs.Add(Parse(@"
[*.cs]
dotnet_diagnostic.cs000.severity = none
[test.*]
dotnet_diagnostic.cs000.severity = error", "/.editorconfig"));
var options = GetAnalyzerConfigOptions(
new[] { "/test.cs", "/test.vb", "/test" },
configs);
configs.Free();
Assert.Equal(new[] {
CreateImmutableDictionary(("cs000", ReportDiagnostic.Error)),
CreateImmutableDictionary(("cs000", ReportDiagnostic.Error)),
SyntaxTree.EmptyDiagnosticOptions
}, options.Select(o => o.TreeOptions).ToArray());
}
[Fact]
public void BadSectionInConfigIgnored()
{
var configs = ArrayBuilder<AnalyzerConfig>.GetInstance();
configs.Add(Parse(@"
[*.cs]
dotnet_diagnostic.cs000.severity = none
[*.vb]
dotnet_diagnostic.cs000.severity = error
[{test.*]
dotnet_diagnostic.cs000.severity = suggestion"
, "/.editorconfig"));
var options = GetAnalyzerConfigOptions(
new[] { "/test.cs", "/test.vb", "/test" },
configs);
configs.Free();
Assert.Equal(new[] {
CreateImmutableDictionary(("cs000", ReportDiagnostic.Suppress)),
CreateImmutableDictionary(("cs000", ReportDiagnostic.Error)),
SyntaxTree.EmptyDiagnosticOptions
}, options.Select(o => o.TreeOptions).ToArray());
}
[Fact]
public void TwoSettingsSameSection()
{
var configs = ArrayBuilder<AnalyzerConfig>.GetInstance();
configs.Add(Parse(@"
[*.cs]
dotnet_diagnostic.cs000.severity = none
dotnet_diagnostic.cs001.severity = suggestion", "/.editorconfig"));
var options = GetAnalyzerConfigOptions(
new[] { "/test.cs" },
configs);
configs.Free();
Assert.Equal(new[]
{
CreateImmutableDictionary(
("cs000", ReportDiagnostic.Suppress),
("cs001", ReportDiagnostic.Info)),
}, options.Select(o => o.TreeOptions).ToArray());
}
[Fact]
public void TwoTermsForHidden()
{
var configs = ArrayBuilder<AnalyzerConfig>.GetInstance();
configs.Add(Parse(@"
[*.cs]
dotnet_diagnostic.cs000.severity = silent
dotnet_diagnostic.cs001.severity = refactoring", "/.editorconfig"));
var options = GetAnalyzerConfigOptions(
new[] { "/test.cs" },
configs);
configs.Free();
Assert.Equal(new[]
{
CreateImmutableDictionary(
("cs000", ReportDiagnostic.Hidden),
("cs001", ReportDiagnostic.Hidden)),
}, options.Select(o => o.TreeOptions).ToArray());
}
[Fact]
public void TwoSettingsDifferentSections()
{
var configs = ArrayBuilder<AnalyzerConfig>.GetInstance();
configs.Add(Parse(@"
[*.cs]
dotnet_diagnostic.cs000.severity = none
[test.*]
dotnet_diagnostic.cs001.severity = suggestion", "/.editorconfig"));
var options = GetAnalyzerConfigOptions(
new[] { "/test.cs" },
configs);
configs.Free();
Assert.Equal(new[]
{
CreateImmutableDictionary(
("cs000", ReportDiagnostic.Suppress),
("cs001", ReportDiagnostic.Info))
}, options.Select(o => o.TreeOptions).ToArray());
}
[Fact]
public void MultipleEditorConfigs()
{
var configs = ArrayBuilder<AnalyzerConfig>.GetInstance();
configs.Add(Parse(@"
[**/*]