64 lines
2.0 KiB
C#
64 lines
2.0 KiB
C#
namespace Raliway.Tests.Errors;
|
|
|
|
public class Serialization
|
|
{
|
|
[Fact]
|
|
public void WhenSerializingManyErrors()
|
|
{
|
|
// Given
|
|
Error many_errors = new ManyErrors(
|
|
[
|
|
Error.New("err1", "msg1",
|
|
[
|
|
new("ext", "ext_value"),
|
|
]),
|
|
Error.New(new Exception("msg2")),
|
|
]);
|
|
// When
|
|
var result = JsonSerializer.Serialize(many_errors);
|
|
// Then
|
|
result.ShouldBe(
|
|
"[{\"type\":\"err1\",\"msg\":\"msg1\",\"ext\":\"ext_value\"},{\"type\":\"System.Exception\",\"msg\":\"msg2\"}]");
|
|
}
|
|
|
|
[Fact]
|
|
public void WhenDeserializingManyErrorsAsError()
|
|
{
|
|
// Given
|
|
var json = "[{\"type\":\"err1\",\"msg\":\"msg1\",\"ext1\":\"ext_value1\",\"ext2\":\"ext_value2\"},{\"type\":\"System.Exception\",\"msg\":\"msg2\"}]";
|
|
// When
|
|
var result = JsonSerializer.Deserialize<Error>(json);
|
|
// Then
|
|
result.ShouldBeOfType<ManyErrors>();
|
|
ManyErrors manyErrors = (ManyErrors)result;
|
|
|
|
manyErrors.Count.ShouldBe(2);
|
|
manyErrors.ShouldBe(
|
|
Error.Many(
|
|
Error.New("err1", "msg1"),
|
|
Error.New(new Exception("msg2"))
|
|
).ToEnumerable());
|
|
manyErrors[0]["ext1"].ShouldBe("ext_value1");
|
|
manyErrors[0]["ext2"].ShouldBe("ext_value2");
|
|
}
|
|
|
|
[Fact]
|
|
public void WhenDeserializingManyErrorsAsManyErrors()
|
|
{
|
|
// Given
|
|
var json = "[{\"type\":\"err1\",\"msg\":\"msg1\",\"ext1\":\"ext_value1\",\"ext2\":\"ext_value2\"},{\"type\":\"System.Exception\",\"msg\":\"msg2\"}]";
|
|
// When
|
|
var result = JsonSerializer.Deserialize<ManyErrors>(json);
|
|
// Then
|
|
result.ShouldNotBeNull();
|
|
result.Count.ShouldBe(2);
|
|
result.ShouldBe(
|
|
Error.Many(
|
|
Error.New("err1", "msg1"),
|
|
Error.New(new Exception("msg2"))
|
|
).ToEnumerable());
|
|
result[0]["ext1"].ShouldBe("ext_value1");
|
|
result[0]["ext2"].ShouldBe("ext_value2");
|
|
}
|
|
}
|