Summary

Class:WireMock.Matchers.RegexMatcher
Assembly:WireMock.Net
File(s):C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Matchers\RegexMatcher.cs
Covered lines:38
Uncovered lines:3
Coverable lines:41
Total lines:104
Line coverage:92.6%
Branch coverage:100%

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
IsMatch(...)000.7691
GetPatterns()0010
.ctor(...)0010
.ctor(...)0010
.ctor(...)0010
.ctor(...)0011

File(s)

C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Matchers\RegexMatcher.cs

#LineLine coverage
 1using System;
 2using System.Linq;
 3using System.Text.RegularExpressions;
 4using JetBrains.Annotations;
 5using WireMock.Validation;
 6
 7namespace WireMock.Matchers
 8{
 9    /// <summary>
 10    /// Regular Expression Matcher
 11    /// </summary>
 12    /// <inheritdoc cref="IStringMatcher"/>
 13    /// <inheritdoc cref="IIgnoreCaseMatcher"/>
 14    public class RegexMatcher : IStringMatcher, IIgnoreCaseMatcher
 15    {
 16        private readonly string[] _patterns;
 17        private readonly Regex[] _expressions;
 18
 19        /// <inheritdoc cref="IMatcher.MatchBehaviour"/>
 37020        public MatchBehaviour MatchBehaviour { get; }
 21
 22        /// <summary>
 23        /// Initializes a new instance of the <see cref="RegexMatcher"/> class.
 24        /// </summary>
 25        /// <param name="pattern">The pattern.</param>
 26        /// <param name="ignoreCase">Ignore the case from the pattern.</param>
 827        public RegexMatcher([NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(new[] { pattern }, i
 828        {
 829        }
 30
 31        /// <summary>
 32        /// Initializes a new instance of the <see cref="RegexMatcher"/> class.
 33        /// </summary>
 34        /// <param name="matchBehaviour">The match behaviour.</param>
 35        /// <param name="pattern">The pattern.</param>
 36        /// <param name="ignoreCase">Ignore the case from the pattern.</param>
 9837        public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string pattern, bool ignoreCase = fal
 9838        {
 9839        }
 40
 41        /// <summary>
 42        /// Initializes a new instance of the <see cref="RegexMatcher"/> class.
 43        /// </summary>
 44        /// <param name="patterns">The patterns.</param>
 45        /// <param name="ignoreCase">Ignore the case from the pattern.</param>
 846        public RegexMatcher([NotNull, RegexPattern] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.Ac
 847        {
 848        }
 49
 50        /// <summary>
 51        /// Initializes a new instance of the <see cref="RegexMatcher"/> class.
 52        /// </summary>
 53        /// <param name="matchBehaviour">The match behaviour.</param>
 54        /// <param name="patterns">The patterns.</param>
 55        /// <param name="ignoreCase">Ignore the case from the pattern.</param>
 40756        public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string[] patterns, bool ignoreCase = 
 40857        {
 40858            Check.NotNull(patterns, nameof(patterns));
 59
 40860            _patterns = patterns;
 40861            IgnoreCase = ignoreCase;
 40762            MatchBehaviour = matchBehaviour;
 63
 40864            RegexOptions options = RegexOptions.Compiled;
 40865            if (ignoreCase)
 6466            {
 6467                options |= RegexOptions.IgnoreCase;
 6468            }
 69
 81870            _expressions = patterns.Select(p => new Regex(p, options)).ToArray();
 40771        }
 72
 73        /// <inheritdoc cref="IStringMatcher.IsMatch"/>
 74        public double IsMatch(string input)
 36875        {
 36876            double match = MatchScores.Mismatch;
 36877            if (input != null)
 36778            {
 79                try
 36780                {
 110381                    match = MatchScores.ToScore(_expressions.Select(e => e.IsMatch(input)));
 36782                }
 083                catch (Exception)
 084                {
 85                    // just ignore exception
 086                }
 36787            }
 88
 36889            return MatchBehaviourHelper.Convert(MatchBehaviour, match);
 36890        }
 91
 92        /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
 93        public virtual string[] GetPatterns()
 394        {
 395            return _patterns;
 396        }
 97
 98        /// <inheritdoc cref="IMatcher.Name"/>
 299        public virtual string Name => "RegexMatcher";
 100
 101        /// <inheritdoc cref="IIgnoreCaseMatcher.IgnoreCase"/>
 3102        public bool IgnoreCase { get; }
 103    }
 104}