mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-21 00:28:59 +01:00
Add CSharpCodeMatcher (#324)
* wip * fix * . * windows-2019 * <Target Name="CheckIfShouldKillVBCSCompiler" /> * <!--https://github.com/aspnet/RoslynCodeDomProvider/issues/51--> * AllowCSharpCodeMatcher * CSharpCodeMatcher : IObjectMatcher * TemplateForIsMatchWithDynamic * RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_CSharpCodeMatcher * fix * } * Better Exception Handling
This commit is contained in:
92
test/WireMock.Net.Tests/Matchers/CSharpCodeMatcherTests.cs
Normal file
92
test/WireMock.Net.Tests/Matchers/CSharpCodeMatcherTests.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Matchers
|
||||
{
|
||||
public class CSharpCodeMatcherTests
|
||||
{
|
||||
[Fact]
|
||||
public void CSharpCodeMatcher_For_String_SinglePattern_IsMatch_Positive()
|
||||
{
|
||||
// Assign
|
||||
string input = "x";
|
||||
|
||||
// Act
|
||||
var matcher = new CSharpCodeMatcher("return it == \"x\";");
|
||||
|
||||
// Assert
|
||||
Check.That(matcher.IsMatch(input)).IsEqualTo(1.0d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CSharpCodeMatcher_For_String_IsMatch_Negative()
|
||||
{
|
||||
// Assign
|
||||
string input = "y";
|
||||
|
||||
// Act
|
||||
var matcher = new CSharpCodeMatcher("return it == \"x\";");
|
||||
|
||||
// Assert
|
||||
Check.That(matcher.IsMatch(input)).IsEqualTo(0.0d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CSharpCodeMatcher_For_String_IsMatch_RejectOnMatch()
|
||||
{
|
||||
// Assign
|
||||
string input = "x";
|
||||
|
||||
// Act
|
||||
var matcher = new CSharpCodeMatcher(MatchBehaviour.RejectOnMatch, "return it == \"x\";");
|
||||
|
||||
// Assert
|
||||
Check.That(matcher.IsMatch(input)).IsEqualTo(0.0d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CSharpCodeMatcher_For_Object_IsMatch()
|
||||
{
|
||||
// Assign
|
||||
var input = new
|
||||
{
|
||||
Id = 9,
|
||||
Name = "Test"
|
||||
};
|
||||
|
||||
// Act
|
||||
var matcher = new CSharpCodeMatcher("return it.Id > 1 && it.Name == \"Test\";");
|
||||
double match = matcher.IsMatch(input);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CSharpCodeMatcher_GetName()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new CSharpCodeMatcher("x");
|
||||
|
||||
// Act
|
||||
string name = matcher.Name;
|
||||
|
||||
// Assert
|
||||
Check.That(name).Equals("CSharpCodeMatcher");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CSharpCodeMatcher_GetPatterns()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new CSharpCodeMatcher("x");
|
||||
|
||||
// Act
|
||||
string[] patterns = matcher.GetPatterns();
|
||||
|
||||
// Assert
|
||||
Check.That(patterns).ContainsExactly("x");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -187,6 +187,28 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
objectMatcherMock.Verify(m => m.IsMatch(42), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_CSharpCodeMatcher()
|
||||
{
|
||||
// Assign
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsJson = new { value = 42 },
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
|
||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||
|
||||
var matcher = new RequestMessageBodyMatcher(new CSharpCodeMatcher(MatchBehaviour.AcceptOnMatch, "return it.value == 42;"));
|
||||
|
||||
// Act
|
||||
var result = new RequestMatchResult();
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
Check.That(score).IsEqualTo(1.0d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsBytes_IObjectMatcher()
|
||||
{
|
||||
|
||||
@@ -23,6 +23,10 @@
|
||||
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Matchers\CSScriptMatcherTests.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\WireMock.Net.StandAlone\WireMock.Net.StandAlone.csproj" />
|
||||
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" />
|
||||
|
||||
Reference in New Issue
Block a user