|
| 1 | +using System; |
| 2 | +using System.Text.Json; |
| 3 | +using Shouldly; |
| 4 | + |
| 5 | +namespace ManagedCode.Communication.Tests.Results; |
| 6 | + |
| 7 | +public class ResultSuccessValueContractTests |
| 8 | +{ |
| 9 | + private const string Payload = "payload"; |
| 10 | + private const string Failure = "missing"; |
| 11 | + private const string ValueParameter = "value"; |
| 12 | + private const string NullSuccessJson = "{\"isSuccess\":true,\"value\":null}"; |
| 13 | + |
| 14 | + [Test] |
| 15 | + public void Succeed_WithNullReference_RejectsNullPayload() |
| 16 | + { |
| 17 | + Should.Throw<ArgumentNullException>(() => Result<string>.Succeed((string)null!)) |
| 18 | + .ParamName.ShouldBe(ValueParameter); |
| 19 | + } |
| 20 | + |
| 21 | + [Test] |
| 22 | + public void Succeed_WithNullableNull_RejectsNullPayload() |
| 23 | + { |
| 24 | + Should.Throw<ArgumentNullException>(() => Result<string?>.Succeed((string?)null)); |
| 25 | + Should.Throw<ArgumentNullException>(() => Result<int?>.Succeed((int?)null)); |
| 26 | + } |
| 27 | + |
| 28 | + [Test] |
| 29 | + public void Succeed_WithDefaultReference_RejectsNullPayload() |
| 30 | + { |
| 31 | + Should.Throw<ArgumentNullException>(() => Result<string>.Succeed()); |
| 32 | + } |
| 33 | + |
| 34 | + [Test] |
| 35 | + public void Succeed_WithDefaultValueType_PreservesValidDefault() |
| 36 | + { |
| 37 | + var result = Result<int>.Succeed(); |
| 38 | + result.IsSuccess.ShouldBeTrue(); |
| 39 | + result.Value.ShouldBe(0); |
| 40 | + } |
| 41 | + |
| 42 | + [Test] |
| 43 | + public void Initializer_WithNullSuccessPayload_RejectsOverride() |
| 44 | + { |
| 45 | + var success = Result<string>.Succeed(Payload); |
| 46 | + Should.Throw<ArgumentNullException>(() => { _ = success with { Value = null }; }); |
| 47 | + success.IsSuccess.ShouldBeTrue(); |
| 48 | + success.Value.ShouldBe(Payload); |
| 49 | + } |
| 50 | + |
| 51 | + [Test] |
| 52 | + public void Failure_WithNullPayload_PreservesFailure() |
| 53 | + { |
| 54 | + var result = Result<string>.FailNotFound(Failure) with { Value = null }; |
| 55 | + result.IsFailed.ShouldBeTrue(); |
| 56 | + result.Value.ShouldBeNull(); |
| 57 | + result.Problem.ShouldNotBeNull(); |
| 58 | + } |
| 59 | + |
| 60 | + [Test] |
| 61 | + public void Deserialize_WithNullSuccessPayload_RejectsInvalidResult() |
| 62 | + { |
| 63 | + Should.Throw<ArgumentNullException>(() => JsonSerializer.Deserialize<Result<string>>(NullSuccessJson)); |
| 64 | + } |
| 65 | +} |
0 commit comments