mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-20 16:44:31 +01:00
LinqMatcher and JsonUtils
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using NFluent;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using Xunit;
|
||||
|
||||
@@ -45,43 +46,41 @@ namespace WireMock.Net.Tests.Matchers
|
||||
Check.That(matcher.IsMatch(input)).IsEqualTo(0.0d);
|
||||
}
|
||||
|
||||
//[Fact]
|
||||
//public void LinqMatcher_For_Object_IsMatch()
|
||||
//{
|
||||
// // Assign
|
||||
// var input = new
|
||||
// {
|
||||
// Id = 9,
|
||||
// Name = "Test"
|
||||
// };
|
||||
[Fact]
|
||||
public void LinqMatcher_For_Object_IsMatch()
|
||||
{
|
||||
// Assign
|
||||
var input = new
|
||||
{
|
||||
Id = 9,
|
||||
Name = "Test"
|
||||
};
|
||||
|
||||
// // Act
|
||||
// var matcher = new LinqMatcher("Id > 1 AND Name == \"Test\"");
|
||||
// Act
|
||||
var matcher = new LinqMatcher("Id > 1 AND Name == \"Test\"");
|
||||
double match = matcher.IsMatch(input);
|
||||
|
||||
// double match = matcher.IsMatch(input);
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
// // Assert
|
||||
// Assert.Equal(1.0, match);
|
||||
//}
|
||||
[Fact]
|
||||
public void LinqMatcher_For_JObject_IsMatch()
|
||||
{
|
||||
// Assign
|
||||
var input = new JObject
|
||||
{
|
||||
{ "Id", new JValue(9) },
|
||||
{ "Name", new JValue("Test") }
|
||||
};
|
||||
|
||||
//[Fact]
|
||||
//public void LinqMatcher_For_JObject_IsMatch()
|
||||
//{
|
||||
// // Assign
|
||||
// var input = new JObject
|
||||
// {
|
||||
// { "Id", new JValue(9) },
|
||||
// { "Name", new JValue("Test") }
|
||||
// };
|
||||
// Act
|
||||
var matcher = new LinqMatcher("Id > 1 AND Name == \"Test\"");
|
||||
double match = matcher.IsMatch(input);
|
||||
|
||||
// // Act
|
||||
// var matcher = new LinqMatcher("it.Id > 1 AND it.Name == \"Test\"");
|
||||
|
||||
// double match = matcher.IsMatch(input);
|
||||
|
||||
// // Assert
|
||||
// Assert.Equal(1.0, match);
|
||||
//}
|
||||
// Assert
|
||||
Assert.Equal(1.0, match);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LinqMatcher_GetName()
|
||||
|
||||
62
test/WireMock.Net.Tests/Util/JsonUtilsTests.cs
Normal file
62
test/WireMock.Net.Tests/Util/JsonUtilsTests.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NFluent;
|
||||
using System.Linq.Dynamic.Core;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Util
|
||||
{
|
||||
public class JsonUtilsTests
|
||||
{
|
||||
[Fact]
|
||||
public void JsonUtils_GenerateDynamicLinqStatement()
|
||||
{
|
||||
// Assign
|
||||
var j = new JObject
|
||||
{
|
||||
{"U", new JValue(new Uri("http://localhost:80/abc?a=5"))},
|
||||
{"N", new JValue((object) null)},
|
||||
{"G", new JValue(Guid.NewGuid())},
|
||||
{"Flt", new JValue(10.0f)},
|
||||
{"Dbl", new JValue(Math.PI)},
|
||||
{"Check", new JValue(true)},
|
||||
{"Items", new JArray(new[] {new JValue(4), new JValue(8)})},
|
||||
{
|
||||
"Child", new JObject
|
||||
{
|
||||
{"ChildId", new JValue(4)},
|
||||
{"ChildDateTime", new JValue(new DateTime(2018, 2, 17))},
|
||||
{"TS", new JValue(TimeSpan.FromMilliseconds(999))}
|
||||
}
|
||||
},
|
||||
{"Id", new JValue(9)},
|
||||
{"Name", new JValue("Test")}
|
||||
};
|
||||
|
||||
// Act
|
||||
string line = JsonUtils.GenerateDynamicLinqStatement(j);
|
||||
|
||||
// Assert
|
||||
var queryable = new[] {j}.AsQueryable().Select(line);
|
||||
bool result = queryable.Any("Id > 4");
|
||||
Check.That(result).IsTrue();
|
||||
|
||||
Check.That(line).IsEqualTo("new (Uri(U) as U, null as N, Guid(G) as G, double(Flt) as Flt, double(Dbl) as Dbl, bool(Check) as Check, (new [] { int(Items[0]), int(Items[1])}) as Items, new (int(Child.ChildId) as ChildId, DateTime(Child.ChildDateTime) as ChildDateTime, TimeSpan(Child.TS) as TS) as Child, int(Id) as Id, string(Name) as Name)");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonUtils_GenerateDynamicLinqStatement_Throws()
|
||||
{
|
||||
// Assign
|
||||
var j = new JObject
|
||||
{
|
||||
{"B", new JValue(new byte[] {48, 49})}
|
||||
};
|
||||
|
||||
// Act and Assert
|
||||
Check.ThatCode(() => JsonUtils.GenerateDynamicLinqStatement(j)).Throws<NotSupportedException>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,8 @@
|
||||
<PackageReference Include="SimMetrics.Net" Version="1.0.4" />
|
||||
<PackageReference Include="System.Threading" Version="4.3.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
||||
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.8.18" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'net452'">
|
||||
|
||||
Reference in New Issue
Block a user