@@ -145,14 +145,14 @@ public class Construction
|
||||
public class Many_Factory
|
||||
{
|
||||
[Fact]
|
||||
public void With_both_null_returns_empty_ManyErrors()
|
||||
public void With_both_null_returns_EmptyError()
|
||||
{
|
||||
// Given
|
||||
// When
|
||||
var error = Error.Many(null!, null!);
|
||||
|
||||
// Then
|
||||
error.ShouldBeOfType<ManyErrors>();
|
||||
error.ShouldBeOfType<EmptyError>();
|
||||
error.IsEmpty.ShouldBeTrue();
|
||||
error.Count.ShouldBe(0);
|
||||
}
|
||||
@@ -216,14 +216,14 @@ public class Construction
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Params_with_empty_array_returns_empty_ManyErrors()
|
||||
public void Params_with_empty_array_returns_EmptyError()
|
||||
{
|
||||
// Given
|
||||
// When
|
||||
var result = Error.Many(Array.Empty<Error>());
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<ManyErrors>();
|
||||
result.ShouldBeOfType<EmptyError>();
|
||||
result.IsEmpty.ShouldBeTrue();
|
||||
result.Count.ShouldBe(0);
|
||||
}
|
||||
@@ -365,9 +365,10 @@ public class Construction
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ManyErrors_IsEmpty_when_empty()
|
||||
public void EmptyError_IsEmpty()
|
||||
{
|
||||
var error = Error.Many(Array.Empty<Error>());
|
||||
error.ShouldBeOfType<EmptyError>();
|
||||
error.IsEmpty.ShouldBeTrue();
|
||||
}
|
||||
|
||||
@@ -569,4 +570,167 @@ public class Construction
|
||||
message.ShouldBe("custom message");
|
||||
}
|
||||
}
|
||||
|
||||
public class EmptyError_Tests
|
||||
{
|
||||
[Fact]
|
||||
public void Error_Empty_returns_EmptyError_singleton()
|
||||
{
|
||||
Error.Empty.ShouldBeOfType<EmptyError>();
|
||||
Error.Empty.ShouldBeSameAs(EmptyError.Instance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Properties_are_correct()
|
||||
{
|
||||
var error = Error.Empty;
|
||||
error.Type.ShouldBe("empty");
|
||||
error.Message.ShouldBe("");
|
||||
error.Count.ShouldBe(0);
|
||||
error.IsEmpty.ShouldBeTrue();
|
||||
error.IsExpected.ShouldBeTrue();
|
||||
error.IsExceptional.ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToEnumerable_returns_empty()
|
||||
{
|
||||
Error.Empty.ToEnumerable().ShouldBeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToException_returns_ErrorException()
|
||||
{
|
||||
var ex = Error.Empty.ToException();
|
||||
ex.ShouldBeOfType<ErrorException>();
|
||||
var errEx = (ErrorException)ex;
|
||||
errEx.Type.ShouldBe("empty");
|
||||
errEx.Message.ShouldBe("");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Deconstruct_produces_type_and_message()
|
||||
{
|
||||
var (type, message) = Error.Empty;
|
||||
type.ShouldBe("empty");
|
||||
message.ShouldBe("");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExtensionData_is_empty()
|
||||
{
|
||||
Error.Empty.ExtensionData.ShouldBeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Indexer_returns_null()
|
||||
{
|
||||
Error.Empty["anything"].ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_self()
|
||||
{
|
||||
Error.Empty.Equals(Error.Empty).ShouldBeTrue();
|
||||
Error.Empty.Equals(EmptyError.Instance).ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_other_empty_error()
|
||||
{
|
||||
// An empty ManyErrors is also IsEmpty=true, so EmptyError equals it
|
||||
var emptyMany = new ManyErrors(Array.Empty<Error>());
|
||||
Error.Empty.Equals(emptyMany).ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_non_empty_is_false()
|
||||
{
|
||||
var realError = Error.New("test");
|
||||
Error.Empty.Equals(realError).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsSimilarTo_empty_error()
|
||||
{
|
||||
var emptyMany = new ManyErrors(Array.Empty<Error>());
|
||||
Error.Empty.IsSimilarTo(emptyMany).ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsSimilarTo_non_empty_is_false()
|
||||
{
|
||||
Error.Empty.IsSimilarTo(Error.New("test")).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CompareTo_sorts_before_non_empty()
|
||||
{
|
||||
Error.Empty.CompareTo(Error.New("test")).ShouldBeLessThan(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CompareTo_self_is_zero()
|
||||
{
|
||||
Error.Empty.CompareTo(Error.Empty).ShouldBe(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToString_returns_empty_message()
|
||||
{
|
||||
Error.Empty.ToString().ShouldBe("");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Operator_equals()
|
||||
{
|
||||
(Error.Empty == EmptyError.Instance).ShouldBeTrue();
|
||||
(Error.Empty == Error.New("test")).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Operator_not_equals()
|
||||
{
|
||||
(Error.Empty != Error.New("test")).ShouldBeTrue();
|
||||
(Error.Empty != EmptyError.Instance).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Append_EmptyError_returns_other()
|
||||
{
|
||||
var real = Error.New("test");
|
||||
Error.Empty.Append(real).ShouldBe(real);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Append_to_EmptyError_returns_real()
|
||||
{
|
||||
var real = Error.New("test");
|
||||
real.Append(Error.Empty).ShouldBe(real);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Operator_plus_EmptyError()
|
||||
{
|
||||
var real = Error.New("test");
|
||||
(Error.Empty + real).ShouldBe(real);
|
||||
(real + Error.Empty).ShouldBe(real);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Many_with_EmptyError_absorbs_it()
|
||||
{
|
||||
var real = Error.New("test");
|
||||
var result = Error.Many(Error.Empty, real);
|
||||
result.ShouldBeOfType<ExpectedError>();
|
||||
result.ShouldBe(real);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHashCode_is_stable()
|
||||
{
|
||||
Error.Empty.GetHashCode().ShouldBe(0);
|
||||
EmptyError.Instance.GetHashCode().ShouldBe(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,19 +152,31 @@ public class Serialization_RoundTrip
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Empty_ManyErrors_round_trip()
|
||||
public void EmptyError_serializes_to_null()
|
||||
{
|
||||
// Given
|
||||
Error original = Error.Many(Array.Empty<Error>());
|
||||
var error = Error.Empty;
|
||||
|
||||
// When
|
||||
var json = JsonSerializer.Serialize(original);
|
||||
var deserialized = JsonSerializer.Deserialize<Error>(json);
|
||||
var json = JsonSerializer.Serialize(error);
|
||||
|
||||
// Then
|
||||
deserialized.ShouldBeOfType<ManyErrors>();
|
||||
deserialized!.IsEmpty.ShouldBeTrue();
|
||||
deserialized.Count.ShouldBe(0);
|
||||
json.ShouldBe("null");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EmptyError_filtered_from_ManyErrors_serialization()
|
||||
{
|
||||
// Given
|
||||
var real = Error.New("real_error");
|
||||
var errors = Error.Many(Error.Empty, real);
|
||||
|
||||
// When
|
||||
var json = JsonSerializer.Serialize(errors);
|
||||
|
||||
// Then
|
||||
json.ShouldNotContain("empty");
|
||||
json.ShouldContain("real_error");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
Reference in New Issue
Block a user