Skip to content

Commit 8992c20

Browse files
committed
Fixed deserialization and added DeserializationTests
1 parent ad16124 commit 8992c20

6 files changed

Lines changed: 123 additions & 14 deletions

File tree

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1616
<PackageReadmeFile>README.md</PackageReadmeFile>
1717
<Product>Managed Code - Communication</Product>
18-
<Version>1.0.2</Version>
19-
<PackageVersion>1.0.2</PackageVersion>
18+
<Version>1.0.3</Version>
19+
<PackageVersion>1.0.3</PackageVersion>
2020
</PropertyGroup>
2121
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
2222
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using FluentAssertions;
4+
using Newtonsoft.Json;
5+
using Xunit;
6+
using JsonSerializer = System.Text.Json.JsonSerializer;
7+
8+
namespace ManagedCode.Communication.Tests;
9+
10+
public class DeserializationTests
11+
{
12+
[Theory]
13+
[MemberData(nameof(GetResults))]
14+
public void DeserializeResult_WithNewtonsoftJson(Result result)
15+
{
16+
// Act
17+
var serialized = JsonConvert.SerializeObject(result);
18+
var deserialized = JsonConvert.DeserializeObject<Result<int>>(serialized);
19+
20+
// Assert
21+
deserialized.Should().BeEquivalentTo(result);
22+
}
23+
24+
[Theory]
25+
[MemberData(nameof(GetValueResults))]
26+
public void DeserializeValueResult_WithNewtonsoftJson<T>(Result<T> result)
27+
{
28+
// Act
29+
var serialized = JsonConvert.SerializeObject(result);
30+
var deserialized = JsonConvert.DeserializeObject<Result<T>>(serialized);
31+
32+
// Assert
33+
deserialized.Should().BeEquivalentTo(result);
34+
}
35+
36+
[Theory]
37+
[MemberData(nameof(GetResults))]
38+
public void DeserializeResult_WithTextJson(Result result)
39+
{
40+
// Act
41+
var serialized = JsonSerializer.Serialize(result);
42+
var deserialized = JsonSerializer.Deserialize<Result>(serialized);
43+
44+
// Assert
45+
deserialized.Should().BeEquivalentTo(result);
46+
}
47+
48+
[Theory]
49+
[MemberData(nameof(GetValueResults))]
50+
public void DeserializeValueResult_WithTextJson<T>(Result<T> result)
51+
{
52+
// Act
53+
var serialized = JsonSerializer.Serialize(result);
54+
var deserialized = JsonSerializer.Deserialize<Result<T>>(serialized);
55+
56+
// Assert
57+
deserialized.Should().BeEquivalentTo(result);
58+
}
59+
60+
public static IEnumerable<object[]> GetResults()
61+
{
62+
yield return new object[] {Result.Succeed()};
63+
yield return new object[] {Result.Fail()};
64+
yield return new object[] {Result.Fail(new Exception("Test exception"))};
65+
yield return new object[] {Result.Fail(new Error<ErrorCode>("Test error", ErrorCode.InvalidState))};
66+
}
67+
68+
public static IEnumerable<object[]> GetValueResults()
69+
{
70+
yield return new object[] {Result<int>.Succeed(2)};
71+
yield return new object[] {Result<string>.Succeed("Test string")};
72+
yield return new object[] {Result<int>.Fail()};
73+
yield return new object[] {Result<int>.Fail(new Exception("Test exception"))};
74+
yield return new object[] {Result<int>.Fail(new Error<ErrorCode>("Test error", ErrorCode.InvalidState))};
75+
}
76+
}

ManagedCode.Communication.Tests/ManagedCode.Communication.Tests.csproj

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
</None>
1818
</ItemGroup>
1919
<ItemGroup>
20-
<PackageReference Include="FluentAssertions" Version="6.5.1"/>
21-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0"/>
22-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0"/>
23-
<PackageReference Include="xunit" Version="2.4.1"/>
20+
<PackageReference Include="FluentAssertions" Version="6.5.1" />
21+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
22+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
23+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
24+
<PackageReference Include="System.Text.Json" Version="6.0.6" />
25+
<PackageReference Include="xunit" Version="2.4.1" />
2426
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
2527
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2628
<PrivateAssets>all</PrivateAssets>
@@ -36,7 +38,7 @@
3638
</ItemGroup>
3739

3840
<ItemGroup>
39-
<ProjectReference Include="..\ManagedCode.Communication\ManagedCode.Communication.csproj"/>
41+
<ProjectReference Include="..\ManagedCode.Communication\ManagedCode.Communication.csproj" />
4042
</ItemGroup>
4143

4244
</Project>

ManagedCode.Communication/BaseResult.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,22 @@ protected BaseResult(bool isSuccess)
1414
protected BaseResult(Error<TErrorCode> error)
1515
{
1616
IsSuccess = false;
17-
Errors = new List<Error<TErrorCode>> { error };
17+
Errors = new List<Error<TErrorCode>> {error};
1818
}
1919

2020
protected BaseResult(List<Error<TErrorCode>> errors)
2121
{
2222
IsSuccess = false;
2323
Errors = errors;
2424
}
25-
25+
26+
protected BaseResult(bool isSuccess, List<Error<TErrorCode>> errors)
27+
{
28+
IsSuccess = isSuccess;
29+
Errors = errors;
30+
}
31+
32+
2633
public bool IsSuccess { get; }
2734
public bool IsFail => !IsSuccess;
2835
public Error<TErrorCode>? Error => Errors?.FirstOrDefault();
@@ -39,7 +46,7 @@ protected BaseResult(T value) : base(true)
3946
protected BaseResult(bool isSuccess) : base(isSuccess)
4047
{
4148
}
42-
49+
4350
protected BaseResult(bool isSuccess, T value) : base(isSuccess)
4451
{
4552
Value = value;
@@ -48,7 +55,7 @@ protected BaseResult(bool isSuccess, T value) : base(isSuccess)
4855
protected BaseResult(Error<TErrorCode> error) : base(error)
4956
{
5057
}
51-
58+
5259
protected BaseResult(Error<TErrorCode> error, T value) : base(error)
5360
{
5461
Value = value;
@@ -57,12 +64,22 @@ protected BaseResult(Error<TErrorCode> error, T value) : base(error)
5764
protected BaseResult(List<Error<TErrorCode>> errors) : base(errors)
5865
{
5966
}
60-
67+
6168
protected BaseResult(List<Error<TErrorCode>> errors, T value) : base(errors)
6269
{
6370
Value = value;
6471
}
6572

73+
protected BaseResult(bool isSuccess, List<Error<TErrorCode>> errors, T value) : base(isSuccess, errors)
74+
{
75+
if (isSuccess && value is null)
76+
{
77+
throw new InvalidOperationException($"{nameof(Value)} value cannot be null if the result is successful");
78+
}
79+
80+
Value = value;
81+
}
82+
6683
public T? Value { get; }
6784

6885
public T? ValueOrDefault(T? defaultValue = default)

ManagedCode.Communication/Error.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ namespace ManagedCode.Communication;
44

55
public sealed class Error<TErrorCode> where TErrorCode : Enum
66
{
7+
public Error()
8+
{
9+
Message = string.Empty;
10+
}
11+
712
public Error(string message, TErrorCode? errorCode = default)
813
{
914
Message = message;

ManagedCode.Communication/Result/Result.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ namespace ManagedCode.Communication;
55

66
public sealed partial class Result : BaseResult<ErrorCode>
77
{
8+
public Result(bool isSuccess, List<Error<ErrorCode>> errors) : base(isSuccess, errors)
9+
{
10+
}
11+
812
internal Result(bool isSuccess) : base(isSuccess)
913
{
1014
}
@@ -67,26 +71,31 @@ internal Result(T value) : base(value)
6771
internal Result(bool isSuccess) : base(isSuccess)
6872
{
6973
}
74+
7075
internal Result(bool isSuccess, T value) : base(isSuccess, value)
7176
{
7277
}
7378

7479
internal Result(Error<ErrorCode> error) : base(error)
7580
{
7681
}
77-
82+
7883
internal Result(Error<ErrorCode> error, T value) : base(error, value)
7984
{
8085
}
8186

8287
internal Result(List<Error<ErrorCode>> errors) : base(errors)
8388
{
8489
}
85-
90+
8691
internal Result(List<Error<ErrorCode>> errors, T value) : base(errors, value)
8792
{
8893
}
8994

95+
public Result(bool isSuccess, List<Error<ErrorCode>> errors, T value) : base(isSuccess, errors, value)
96+
{
97+
}
98+
9099
public static implicit operator Result<T>(T value)
91100
{
92101
return new Result<T>(value);

0 commit comments

Comments
 (0)