IdentityShroud/IdentityShroud.TestUtils.Tests/Asserts/JsonObjectAssertTests.cs

70 lines
1.9 KiB
C#
Raw Permalink Normal View History

using System.Text.Json.Nodes;
using IdentityShroud.TestUtils.Asserts;
using Xunit.Sdk;
namespace IdentityShroud.TestUtils.Tests.Asserts;
public class JsonObjectAssertTests
{
[Theory]
[InlineData("foo", new string[] { "foo" })]
[InlineData("foo.bar", new string[] { "foo", "bar" })]
[InlineData("foo[1].bar", new string[] { "foo", "1", "bar" })]
public void ParsePath(string path, string[] expected)
{
var result = JsonObjectAssert.ParsePath(path);
Assert.Equal(expected, result);
}
[Fact]
public void NavigateToPath_Success()
{
JsonObject foo = new();
foo["bar"] = 1;
JsonObject obj = new();
obj["foo"] = foo;
JsonNode? node = JsonObjectAssert.NavigateToPath(obj, ["foo", "bar"]);
Assert.NotNull(node);
}
[Fact]
public void NavigateToPath_PathDoesNotExist()
{
JsonObject obj = new();
Assert.Throws<XunitException>(
() => JsonObjectAssert.NavigateToPath(obj, ["test"]),
ex => ex.Message.StartsWith("Path 'test' does not exist") ? null : ex.Message);
}
[Fact]
public void NavigateToPath_MemberOfNullObject()
{
JsonObject obj = new();
obj["foo"] = null;
Assert.Throws<XunitException>(
() => JsonObjectAssert.NavigateToPath(obj, ["foo", "bar"]),
ex => ex.Message.StartsWith("Path 'foo.bar' does not exist") ? null : ex.Message);
}
[Fact]
public void Equal_WrongType()
{
JsonObject obj = new();
obj["test"] = new JsonObject();
Assert.Throws<XunitException>(
() => JsonObjectAssert.Equal("str", obj, ["test"]),
ex => ex.Message.StartsWith("Type mismatch") ? null : ex.Message);
}
[Fact]
public void Equal_Match()
{
JsonObject obj = new();
obj["test"] = "str";
JsonObjectAssert.Equal("str", obj, ["test"]);
}
}