Summary

Class:WireMock.Transformers.HandleBarsJsonPath
Assembly:WireMock.Net
File(s):C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Transformers\HandleBarsJsonPath.cs
Covered lines:44
Uncovered lines:1
Coverable lines:45
Total lines:73
Line coverage:97.7%
Branch coverage:71.4%

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
Register()0011
ParseArguments(...)000.9170.6

File(s)

C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Transformers\HandleBarsJsonPath.cs

#LineLine coverage
 1using System;
 2using System.Linq;
 3using HandlebarsDotNet;
 4using Newtonsoft.Json;
 5using Newtonsoft.Json.Linq;
 6using WireMock.Validation;
 7
 8namespace WireMock.Transformers
 9{
 10    internal static class HandleBarsJsonPath
 11    {
 12        public static void Register()
 113        {
 114            Handlebars.RegisterHelper("JsonPath.SelectToken", (writer, context, arguments) =>
 515            {
 516                (JObject valueToProcess, string jsonpath) = ParseArguments(arguments);
 117
 118                try
 519                {
 520                    var result = valueToProcess.SelectToken(jsonpath);
 521                    writer.WriteSafeString(result);
 522                }
 123                catch (JsonException)
 124                {
 125                    // Ignore JsonException
 126                }
 527            });
 28
 129            Handlebars.RegisterHelper("JsonPath.SelectTokens", (writer, options, context, arguments) =>
 430            {
 431                (JObject valueToProcess, string jsonpath) = ParseArguments(arguments);
 132
 133                try
 334                {
 335                    var values = valueToProcess.SelectTokens(jsonpath);
 336                    if (values != null)
 337                    {
 1138                        options.Template(writer, values.ToDictionary(value => value.Path, value => value));
 339                    }
 340                }
 141                catch (JsonException)
 142                {
 143                    // Ignore JsonException
 144                }
 345            });
 146        }
 47
 48        private static (JObject valueToProcess, string jsonpath) ParseArguments(object[] arguments)
 749        {
 1450            Check.Condition(arguments, args => args.Length == 2, nameof(arguments));
 751            Check.NotNull(arguments[0], "arguments[0]");
 652            Check.NotNullOrEmpty(arguments[1] as string, "arguments[1]");
 53
 54            JObject valueToProcess;
 55
 656            switch (arguments[0])
 57            {
 58                case string jsonAsString:
 459                    valueToProcess = JObject.Parse(jsonAsString);
 460                    break;
 61
 62                case JObject jsonAsJObject:
 263                    valueToProcess = jsonAsJObject;
 264                    break;
 65
 66                default:
 067                    throw new NotSupportedException($"The value '{arguments[0]}' with type '{arguments[0]?.GetType()}' c
 68            }
 69
 670            return (valueToProcess, (string) arguments[1]);
 671        }
 72    }
 73}