Summary

Class:WireMock.Matchers.RegexMatcher
Assembly:WireMock.Net
File(s):C:\Users\azureuser\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\azureuser\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"/>
 38920        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>
 1127        public RegexMatcher([NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(new[] { pattern }, i
 1128        {
 1129        }
 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>
 9937        public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string pattern, bool ignoreCase = fal
 9938        {
 9939        }
 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>
 1146        public RegexMatcher([NotNull, RegexPattern] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.Ac
 1147        {
 1148        }
 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>
 36656        public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string[] patterns, bool ignoreCase = 
 36657        {
 36658            Check.NotNull(patterns, nameof(patterns));
 59
 36660            _patterns = patterns;
 36661            IgnoreCase = ignoreCase;
 36662            MatchBehaviour = matchBehaviour;
 63
 36664            RegexOptions options = RegexOptions.Compiled;
 36665            if (ignoreCase)
 4866            {
 4867                options |= RegexOptions.IgnoreCase;
 4868            }
 69
 73470            _expressions = patterns.Select(p => new Regex(p, options)).ToArray();
 36671        }
 72
 73        /// <inheritdoc cref="IStringMatcher.IsMatch"/>
 74        public double IsMatch(string input)
 38875        {
 38876            double match = MatchScores.Mismatch;
 38877            if (input != null)
 38778            {
 79                try
 38780                {
 116381                    match = MatchScores.ToScore(_expressions.Select(e => e.IsMatch(input)));
 38782                }
 083                catch (Exception)
 084                {
 85                    // just ignore exception
 086                }
 38787            }
 88
 38889            return MatchBehaviourHelper.Convert(MatchBehaviour, match);
 38890        }
 91
 92        /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
 93        public virtual string[] GetPatterns()
 294        {
 295            return _patterns;
 296        }
 97
 98        /// <inheritdoc cref="IMatcher.Name"/>
 199        public virtual string Name => "RegexMatcher";
 100
 101        /// <inheritdoc cref="IIgnoreCaseMatcher.IgnoreCase"/>
 3102        public bool IgnoreCase { get; }
 103    }
 104}