Summary

Class:WireMock.Matchers.XPathMatcher
Assembly:WireMock.Net
File(s):C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Matchers\XPathMatcher.cs
Covered lines:25
Uncovered lines:3
Coverable lines:28
Total lines:77
Line coverage:89.2%
Branch coverage:100%

Metrics

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

File(s)

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

#LineLine coverage
 1using System;
 2using System.Linq;
 3using System.Xml;
 4using JetBrains.Annotations;
 5using WireMock.Validation;
 6#if !NETSTANDARD1_3
 7using Wmhelp.XPath2;
 8#endif
 9
 10namespace WireMock.Matchers
 11{
 12    /// <summary>
 13    /// XPath2Matcher
 14    /// </summary>
 15    /// <seealso cref="IStringMatcher" />
 16    public class XPathMatcher : IStringMatcher
 17    {
 18        private readonly string[] _patterns;
 19
 20        /// <inheritdoc cref="IMatcher.MatchBehaviour"/>
 421        public MatchBehaviour MatchBehaviour { get; }
 22
 23        /// <summary>
 24        /// Initializes a new instance of the <see cref="XPathMatcher"/> class.
 25        /// </summary>
 26        /// <param name="patterns">The patterns.</param>
 527        public XPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns)
 528        {
 529        }
 30
 31        /// <summary>
 32        /// Initializes a new instance of the <see cref="XPathMatcher"/> class.
 33        /// </summary>
 34        /// <param name="matchBehaviour">The match behaviour.</param>
 35        /// <param name="patterns">The patterns.</param>
 636        public XPathMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] patterns)
 637        {
 638            Check.NotNull(patterns, nameof(patterns));
 39
 640            MatchBehaviour = matchBehaviour;
 641            _patterns = patterns;
 642        }
 43
 44        /// <inheritdoc cref="IStringMatcher.IsMatch"/>
 45        public double IsMatch(string input)
 446        {
 447            double match = MatchScores.Mismatch;
 448            if (input != null)
 449            {
 50                try
 451                {
 452                    var nav = new XmlDocument { InnerXml = input }.CreateNavigator();
 53#if NETSTANDARD1_3
 54                    match = MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.Evaluate($"boolean({p})"))));
 55#else
 1256                    match = MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.XPath2Evaluate($"boolean({p})"))))
 57#endif
 458                }
 059                catch (Exception)
 060                {
 61                    // just ignore exception
 062                }
 463            }
 64
 465            return MatchBehaviourHelper.Convert(MatchBehaviour, match);
 466        }
 67
 68        /// <inheritdoc cref="IStringMatcher.GetPatterns"/>
 69        public string[] GetPatterns()
 170        {
 171            return _patterns;
 172        }
 73
 74        /// <inheritdoc cref="IMatcher.Name"/>
 175        public string Name => "XPathMatcher";
 76    }
 77}