Add XmlPath2 / RegEx matchers

Solves issue #5
This commit is contained in:
Stef Heyenrath
2017-01-19 21:51:22 +01:00
parent 1b2e5368a9
commit 72335d48d6
8 changed files with 214 additions and 44 deletions

View File

@@ -0,0 +1,17 @@
namespace WireMock.Matchers
{
/// <summary>
/// IMatcher
/// </summary>
public interface IMatcher
{
/// <summary>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>
/// <c>true</c> if the specified input is match; otherwise, <c>false</c>.
/// </returns>
bool IsMatch(string input);
}
}

View File

@@ -0,0 +1,38 @@
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers
{
/// <summary>
/// Regular Expression Matcher
/// </summary>
/// <seealso cref="WireMock.Matchers.IMatcher" />
public class RegexMatcher : IMatcher
{
private readonly Regex _expression;
/// <summary>
/// Initializes a new instance of the <see cref="RegexMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</param>
public RegexMatcher([NotNull] string pattern)
{
Check.NotNull(pattern, nameof(pattern));
_expression = new Regex(pattern, RegexOptions.Compiled);
}
/// <summary>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>
/// <c>true</c> if the specified input is match; otherwise, <c>false</c>.
/// </returns>
public bool IsMatch(string input)
{
return input != null && _expression.IsMatch(input);
}
}
}

View File

@@ -0,0 +1,42 @@
using System.Xml;
using JetBrains.Annotations;
using WireMock.Validation;
using Wmhelp.XPath2;
namespace WireMock.Matchers
{
/// <summary>
/// XPath2Matcher
/// </summary>
/// <seealso cref="WireMock.Matchers.IMatcher" />
public class XPathMatcher : IMatcher
{
private readonly string _pattern;
/// <summary>
/// Initializes a new instance of the <see cref="XPathMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</param>
public XPathMatcher([NotNull] string pattern)
{
Check.NotNull(pattern, nameof(pattern));
_pattern = pattern;
}
/// <summary>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>
/// <c>true</c> if the specified input is match; otherwise, <c>false</c>.
/// </returns>
public bool IsMatch(string input)
{
var nav = new XmlDocument { InnerXml = input }.CreateNavigator();
object result = nav.XPath2Evaluate($"boolean({_pattern})");
return true.Equals(result);
}
}
}