Summary

Class:WireMock.Serialization.MatcherMapper
Assembly:WireMock.Net
File(s):C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Serialization\MatcherMapper.cs
Covered lines:43
Uncovered lines:3
Coverable lines:46
Total lines:96
Line coverage:93.4%
Branch coverage:86.7%

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
Map(...)000.8750.86
Map(...)0011
Map(...)0010.857

File(s)

C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Serialization\MatcherMapper.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using JetBrains.Annotations;
 5using SimMetrics.Net;
 6using WireMock.Admin.Mappings;
 7using WireMock.Matchers;
 8
 9namespace WireMock.Serialization
 10{
 11    internal static class MatcherMapper
 12    {
 13        public static IMatcher Map([CanBeNull] MatcherModel matcher)
 2614        {
 2615            if (matcher == null)
 216            {
 217                return null;
 18            }
 19
 2420            string[] parts = matcher.Name.Split('.');
 2421            string matcherName = parts[0];
 2422            string matcherType = parts.Length > 1 ? parts[1] : null;
 23
 2424            string[] stringPatterns = matcher.Patterns != null ? matcher.Patterns.Cast<string>().ToArray() : new[] { mat
 2425            MatchBehaviour matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviou
 26
 2427            switch (matcherName)
 28            {
 29                case "LinqMatcher":
 230                    return new LinqMatcher(matchBehaviour, stringPatterns);
 31
 32                case "ExactMatcher":
 1133                    return new ExactMatcher(matchBehaviour, stringPatterns);
 34
 35                case "RegexMatcher":
 136                    return new RegexMatcher(matchBehaviour, stringPatterns, matcher.IgnoreCase == true);
 37
 38                case "JsonMatcher":
 039                    return new JsonMatcher(matchBehaviour, matcher.Pattern);
 40
 41                case "JsonPathMatcher":
 042                    return new JsonPathMatcher(matchBehaviour, stringPatterns);
 43
 44                case "XPathMatcher":
 045                    return new XPathMatcher(matchBehaviour, (string)matcher.Pattern);
 46
 47                case "WildcardMatcher":
 548                    return new WildcardMatcher(matchBehaviour, stringPatterns, matcher.IgnoreCase == true);
 49
 50                case "SimMetricsMatcher":
 351                    SimMetricType type = SimMetricType.Levenstein;
 352                    if (!string.IsNullOrEmpty(matcherType) && !Enum.TryParse(matcherType, out type))
 153                    {
 154                        throw new NotSupportedException($"Matcher '{matcherName}' with Type '{matcherType}' is not suppo
 55                    }
 56
 257                    return new SimMetricsMatcher(matchBehaviour, (string)matcher.Pattern, type);
 58
 59                default:
 260                    throw new NotSupportedException($"Matcher '{matcherName}' is not supported.");
 61            }
 2362        }
 63
 64        public static MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers)
 365        {
 666            return matchers?.Select(Map).Where(x => x != null).ToArray();
 367        }
 68
 69        public static MatcherModel Map([CanBeNull] IMatcher matcher)
 870        {
 871            if (matcher == null)
 272            {
 273                return null;
 74            }
 75
 76            // If the matcher is a IStringMatcher, get the patterns.
 77            // If the matcher is a IValueMatcher, get the value (can be string or object).
 78            // Else empty array
 679            object[] patterns = matcher is IStringMatcher stringMatcher ?
 680                stringMatcher.GetPatterns().Cast<object>().ToArray() :
 681                matcher is IValueMatcher valueMatcher ? new[] { valueMatcher.Value } :
 682                new object[0];
 683            bool? ignorecase = matcher is IIgnoreCaseMatcher ignoreCaseMatcher ? ignoreCaseMatcher.IgnoreCase : (bool?)n
 684            bool? rejectOnMatch = matcher.MatchBehaviour == MatchBehaviour.RejectOnMatch ? true : (bool?)null;
 85
 686            return new MatcherModel
 687            {
 688                RejectOnMatch = rejectOnMatch,
 689                IgnoreCase = ignorecase,
 690                Name = matcher.Name,
 691                Pattern = patterns.Length == 1 ? patterns.First() : null,
 692                Patterns = patterns.Length > 1 ? patterns : null
 693            };
 894        }
 95    }
 96}