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:28
Uncovered lines:4
Coverable lines:32
Total lines:81
Line coverage:87.5%
Branch coverage:100%

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
.ctor(...)10100100
.ctor(...)22100100
IsMatch(...)2270100
GetPatterns()10100100
GetName()10100100

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    /// <seealso cref="IStringMatcher" />
 13    public class RegexMatcher : IStringMatcher, IIgnoreCaseMatcher
 14    {
 15        private readonly string[] _patterns;
 16        private readonly Regex[] _expressions;
 17
 18        /// <summary>
 19        /// Initializes a new instance of the <see cref="RegexMatcher"/> class.
 20        /// </summary>
 21        /// <param name="pattern">The pattern.</param>
 22        /// <param name="ignoreCase">IgnoreCase</param>
 10123        public RegexMatcher([NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(new [] { pattern }, 
 10124        {
 10125        }
 26
 27        /// <summary>
 28        /// Initializes a new instance of the <see cref="RegexMatcher"/> class.
 29        /// </summary>
 30        /// <param name="patterns">The patterns.</param>
 31        /// <param name="ignoreCase">IgnoreCase</param>
 26332        public RegexMatcher([NotNull, RegexPattern] string[] patterns, bool ignoreCase = false)
 26333        {
 26334            Check.NotNull(patterns, nameof(patterns));
 35
 26336            _patterns = patterns;
 26337            IgnoreCase = ignoreCase;
 38
 26339            RegexOptions options = RegexOptions.Compiled;
 26340             if (ignoreCase)
 2841            {
 2842                options |= RegexOptions.IgnoreCase;
 2843            }
 44
 52845            _expressions = patterns.Select(p => new Regex(p, options)).ToArray();
 26346        }
 47
 48        /// <inheritdoc cref="IStringMatcher.IsMatch"/>
 49        public double IsMatch(string input)
 21650        {
 21651             if (input == null)
 152            {
 153                return MatchScores.Mismatch;
 54            }
 55
 56            try
 21557            {
 64758                return MatchScores.ToScore(_expressions.Select(e => e.IsMatch(input)));
 59            }
 060            catch (Exception)
 061            {
 062                return MatchScores.Mismatch;
 63            }
 21664        }
 65
 66        /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
 67        public virtual string[] GetPatterns()
 268        {
 269            return _patterns;
 270        }
 71
 72        /// <inheritdoc cref="IMatcher.GetName"/>
 73        public virtual string GetName()
 174        {
 175            return "RegexMatcher";
 176        }
 77
 78        /// <inheritdoc cref="IIgnoreCaseMatcher.IgnoreCase"/>
 079        public bool IgnoreCase { get; }
 80    }
 81}