Add some more tests

This commit is contained in:
Stef Heyenrath
2018-03-14 21:24:20 +01:00
parent c2183ab40c
commit 15500a812c
10 changed files with 202 additions and 9 deletions

View File

@@ -0,0 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IP/@EntryIndexedValue">IP</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SSL/@EntryIndexedValue">SSL</s:String></wpf:ResourceDictionary>

View File

@@ -0,0 +1,13 @@
namespace WireMock.Matchers
{
/// <summary>
/// IIgnoreCaseMatcher
/// </summary>
public interface IIgnoreCaseMatcher : IMatcher
{
/// <summary>
/// Ignore the case.
/// </summary>
bool IgnoreCase { get; }
}
}

View File

@@ -10,7 +10,7 @@ namespace WireMock.Matchers
/// Regular Expression Matcher /// Regular Expression Matcher
/// </summary> /// </summary>
/// <seealso cref="IStringMatcher" /> /// <seealso cref="IStringMatcher" />
public class RegexMatcher : IStringMatcher public class RegexMatcher : IStringMatcher, IIgnoreCaseMatcher
{ {
private readonly string[] _patterns; private readonly string[] _patterns;
private readonly Regex[] _expressions; private readonly Regex[] _expressions;
@@ -34,6 +34,7 @@ namespace WireMock.Matchers
Check.NotNull(patterns, nameof(patterns)); Check.NotNull(patterns, nameof(patterns));
_patterns = patterns; _patterns = patterns;
IgnoreCase = ignoreCase;
RegexOptions options = RegexOptions.Compiled; RegexOptions options = RegexOptions.Compiled;
if (ignoreCase) if (ignoreCase)
@@ -73,5 +74,8 @@ namespace WireMock.Matchers
{ {
return "RegexMatcher"; return "RegexMatcher";
} }
/// <inheritdoc cref="IIgnoreCaseMatcher.IgnoreCase"/>
public bool IgnoreCase { get; }
} }
} }

View File

@@ -104,15 +104,13 @@ namespace WireMock.Matchers.Request
{ {
if (requestMessage.Body != null) if (requestMessage.Body != null)
{ {
var stringMatcher = Matcher as IStringMatcher; if (Matcher is IStringMatcher stringMatcher)
if (stringMatcher != null)
{ {
return stringMatcher.IsMatch(requestMessage.Body); return stringMatcher.IsMatch(requestMessage.Body);
} }
} }
var objectMatcher = Matcher as IObjectMatcher; if (Matcher is IObjectMatcher objectMatcher)
if (objectMatcher != null)
{ {
if (requestMessage.BodyAsJson != null) if (requestMessage.BodyAsJson != null)
{ {

View File

@@ -147,7 +147,7 @@ namespace WireMock.Serialization
return newDictionary; return newDictionary;
} }
private static string[] Map<T>([CanBeNull] IEnumerable<Func<T, bool>> funcs) private static string[] Map<T>([CanBeNull] IEnumerable<Func<T, bool>> funcs)
{ {

View File

@@ -20,15 +20,16 @@ namespace WireMock.Serialization
return null; return null;
} }
IStringMatcher stringMatcher = matcher as IStringMatcher; string[] patterns = matcher is IStringMatcher stringMatcher ? stringMatcher.GetPatterns() : new string[0];
string[] patterns = stringMatcher != null ? stringMatcher.GetPatterns() : new string[0]; bool? ignorecase = matcher is IIgnoreCaseMatcher ignoreCaseMatcher ? ignoreCaseMatcher.IgnoreCase : (bool?)null;
return new MatcherModel return new MatcherModel
{ {
IgnoreCase = ignorecase,
Name = matcher.GetName(), Name = matcher.GetName(),
Pattern = patterns.Length == 1 ? patterns.First() : null, Pattern = patterns.Length == 1 ? patterns.First() : null,
Patterns = patterns.Length > 1 ? patterns : null Patterns = patterns.Length > 1 ? patterns : null
}; };
} }
} }
} }

View File

@@ -0,0 +1,35 @@
using NFluent;
using WireMock.Matchers;
using Xunit;
namespace WireMock.Net.Tests.Matchers
{
public class JsonPathMatcherTests
{
[Fact]
public void JsonPathMatcher_GetName()
{
// Assign
var matcher = new JsonPathMatcher("X");
// Act
string name = matcher.GetName();
// Assert
Check.That(name).Equals("JsonPathMatcher");
}
[Fact]
public void JsonPathMatcher_GetPatterns()
{
// Assign
var matcher = new JsonPathMatcher("X");
// Act
string[] patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("X");
}
}
}

View File

@@ -0,0 +1,35 @@
using NFluent;
using WireMock.Matchers;
using Xunit;
namespace WireMock.Net.Tests.Matchers
{
public class XPathMatcherTests
{
[Fact]
public void XPathMatcher_GetName()
{
// Assign
var matcher = new XPathMatcher("X");
// Act
string name = matcher.GetName();
// Assert
Check.That(name).Equals("XPathMatcher");
}
[Fact]
public void XPathMatcher_GetPatterns()
{
// Assign
var matcher = new XPathMatcher("X");
// Act
string[] patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("X");
}
}
}

View File

@@ -0,0 +1,27 @@
using System;
using NFluent;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Serialization;
using Xunit;
namespace WireMock.Net.Tests.Serialization
{
public class MappingConverterTests
{
[Fact]
public void MappingConverter_ToMappingModel()
{
// Assign
var request = Request.Create();
var response = Response.Create();
var mapping = new Mapping(Guid.NewGuid(), "", null, request, response, 0, null, null, null);
// Act
var model = MappingConverter.ToMappingModel(mapping);
// Assert
Check.That(model).IsNotNull();
}
}
}

View File

@@ -0,0 +1,77 @@
using Moq;
using NFluent;
using WireMock.Matchers;
using WireMock.Serialization;
using Xunit;
namespace WireMock.Net.Tests.Serialization
{
public class MatcherMapperTests
{
[Fact]
public void MatcherMapper_Map_IMatcher_Null()
{
// Act
var model = MatcherMapper.Map((IMatcher)null);
// Assert
Check.That(model).IsNull();
}
[Fact]
public void MatcherMapper_Map_IMatchers_Null()
{
// Act
var model = MatcherMapper.Map((IMatcher[])null);
// Assert
Check.That(model).IsNull();
}
[Fact]
public void MatcherMapper_Map_IMatchers()
{
// Assign
var matcherMock1 = new Mock<IStringMatcher>();
var matcherMock2 = new Mock<IStringMatcher>();
// Act
var models = MatcherMapper.Map(new [] { matcherMock1.Object, matcherMock2.Object });
// Assert
Check.That(models).HasSize(2);
}
[Fact]
public void MatcherMapper_Map_IStringMatcher()
{
// Assign
var matcherMock = new Mock<IStringMatcher>();
matcherMock.Setup(m => m.GetName()).Returns("test");
matcherMock.Setup(m => m.GetPatterns()).Returns(new[] { "p1", "p2" });
// Act
var model = MatcherMapper.Map(matcherMock.Object);
// Assert
Check.That(model.IgnoreCase).IsNull();
Check.That(model.Name).Equals("test");
Check.That(model.Pattern).IsNull();
Check.That(model.Patterns).ContainsExactly("p1", "p2");
}
[Fact]
public void MatcherMapper_Map_IIgnoreCaseMatcher()
{
// Assign
var matcherMock = new Mock<IIgnoreCaseMatcher>();
matcherMock.Setup(m => m.IgnoreCase).Returns(true);
// Act
var model = MatcherMapper.Map(matcherMock.Object);
// Assert
Check.That(model.IgnoreCase).Equals(true);
}
}
}