1
1
using DotNet . Globbing ;
2
- using Microsoft . CodeAnalysis . Text ;
3
- using Stryker . Core . ProjectComponents ;
4
2
using System ;
5
3
using System . Collections . Generic ;
6
4
using System . Linq ;
7
- using System . Text . RegularExpressions ;
8
5
9
6
namespace Stryker . Core
10
7
{
8
+ public record MutantSpan ( int Start , int End ) ;
9
+
11
10
/// <summary>
12
11
/// Contains information about which files and which parts of a file should be in- or excluded.
13
12
/// </summary>
14
13
public sealed class FilePattern : IEquatable < FilePattern >
15
14
{
16
- private static readonly Regex _textSpanGroupRegex = new Regex ( "(\\ {(\\ d+)\\ .\\ .(\\ d+)\\ })+$" ) ;
17
- private static readonly Regex _textSpanRegex = new Regex ( "\\ {(\\ d+)\\ .\\ .(\\ d+)\\ }" ) ;
18
- private static readonly TextSpan _textSpanMaxValue = new TextSpan ( 0 , int . MaxValue ) ;
19
-
20
- public FilePattern ( Glob glob , bool isExclude , IReadOnlyCollection < TextSpan > textSpans )
15
+ public FilePattern ( Glob glob , bool isExclude , IReadOnlyCollection < MutantSpan > mutantSpans )
21
16
{
22
17
Glob = glob ;
23
18
IsExclude = isExclude ;
24
- TextSpans = textSpans ;
19
+ MutantSpans = mutantSpans ;
25
20
}
26
21
27
22
/// <summary>
@@ -37,81 +32,9 @@ public FilePattern(Glob glob, bool isExclude, IReadOnlyCollection<TextSpan> text
37
32
/// <summary>
38
33
/// Gets the the text spans of the file this pattern matches.
39
34
/// </summary>
40
- public IReadOnlyCollection < TextSpan > TextSpans { get ; }
41
-
42
- /// <summary>
43
- /// Parses a given file pattern string.
44
- /// Format: (!)<glob>({<spanStart>..<spanEnd>})*
45
- /// </summary>
46
- /// <param name="pattern">The pattern to parse.</param>
47
- /// <returns>The <see cref="FilePattern"/></returns>
48
- public static FilePattern Parse ( string pattern ) => Parse ( pattern , spansEnabled : true ) ;
49
-
50
- /// <summary>
51
- /// Parses a given file pattern string.
52
- /// Format: (!)<glob>({<spanStart>..<spanEnd>})*
53
- /// </summary>
54
- /// <param name="pattern">The pattern to parse.</param>
55
- /// <param name="spansEnabled">Enable or disable span parsing.</param>
56
- /// <returns>The <see cref="FilePattern"/></returns>
57
- public static FilePattern Parse ( string pattern , bool spansEnabled )
58
- {
59
- var exclude = false ;
60
- IReadOnlyCollection < TextSpan > textSpans ;
61
-
62
- if ( pattern . StartsWith ( '!' ) )
63
- {
64
- exclude = true ;
65
- pattern = pattern [ 1 ..] ;
66
- }
67
-
68
- var textSpanGroupMatch = _textSpanGroupRegex . Match ( pattern ) ;
69
- if ( ! spansEnabled || ! textSpanGroupMatch . Success )
70
- {
71
- // If there are no spans specified, we add one that will cover the whole file.
72
- textSpans = new [ ] { _textSpanMaxValue } ;
73
- }
74
- else
75
- {
76
- // If we have one ore more spans we parse them.
77
- var textSpansMatches = _textSpanRegex . Matches ( textSpanGroupMatch . Value ) ;
78
- textSpans = textSpansMatches
79
- . Select ( x => TextSpan . FromBounds ( int . Parse ( x . Groups [ 1 ] . Value ) , int . Parse ( x . Groups [ 2 ] . Value ) ) )
80
- . Reduce ( )
81
- . ToList ( ) ;
82
-
83
- pattern = pattern . Substring ( 0 , pattern . Length - textSpanGroupMatch . Length ) ;
84
- }
85
-
86
- var glob = Glob . Parse ( FilePathUtils . NormalizePathSeparators ( pattern ) ) ;
87
-
88
- return new FilePattern ( glob , exclude , textSpans ) ;
89
- }
90
-
91
- /// <summary>
92
- /// Checks whether a given file path and span matches the current file pattern.
93
- /// </summary>
94
- /// <param name="filePath">The full file path.</param>
95
- /// <param name="textSpan">The span of the text to check.</param>
96
- /// <returns>True if the file and span matches the pattern.</returns>
97
- public bool IsMatch ( string filePath , TextSpan textSpan )
98
- {
99
- // Check if the file path is matched.
100
- if ( ! Glob . IsMatch ( FilePathUtils . NormalizePathSeparators ( filePath ) ) )
101
- {
102
- return false ;
103
- }
104
-
105
- // Check if any span fully contains the specified span
106
- if ( TextSpans . Any ( span => span . Contains ( textSpan ) ) )
107
- {
108
- return true ;
109
- }
110
-
111
- return false ;
112
- }
35
+ public IReadOnlyCollection < MutantSpan > MutantSpans { get ; }
113
36
114
- public bool Equals ( FilePattern other ) => Glob . ToString ( ) == other . Glob . ToString ( ) && IsExclude == other . IsExclude && TextSpans . SequenceEqual ( other . TextSpans ) ;
37
+ public bool Equals ( FilePattern other ) => Glob . ToString ( ) == other . Glob . ToString ( ) && IsExclude == other . IsExclude && MutantSpans . SequenceEqual ( other . MutantSpans ) ;
115
38
116
39
public override bool Equals ( object obj )
117
40
{
@@ -139,7 +62,7 @@ public override int GetHashCode()
139
62
{
140
63
var hashCode = Glob != null ? Glob . GetHashCode ( ) : 0 ;
141
64
hashCode = ( hashCode * 397 ) ^ IsExclude . GetHashCode ( ) ;
142
- hashCode = ( hashCode * 397 ) ^ ( TextSpans != null ? UncheckedSum ( TextSpans . Select ( t => t . GetHashCode ( ) ) ) : 0 ) ;
65
+ hashCode = ( hashCode * 397 ) ^ ( MutantSpans != null ? UncheckedSum ( MutantSpans . Select ( t => t . GetHashCode ( ) ) ) : 0 ) ;
143
66
return hashCode ;
144
67
}
145
68
0 commit comments