-
-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update documentation, and test coverage.
- Loading branch information
Showing
8 changed files
with
276 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 90 additions & 21 deletions
111
Src/FluentAssertions/Json/JsonSerializerOptionsAssertions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,115 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
using System; | ||
using System.Diagnostics.Contracts; | ||
using System; | ||
using System.Diagnostics; | ||
using System.Text.Json; | ||
using FluentAssertions.Common; | ||
using FluentAssertions.Execution; | ||
using FluentAssertions.Primitives; | ||
|
||
namespace FluentAssertions.Json; | ||
|
||
/// <summary> | ||
/// Contains a number of methods to assert that an <see cref="JsonSerializerOptions" /> is in the expected state. | ||
/// </summary> | ||
// [DebuggerNonUserCode] | ||
public class JsonSerializerOptionsAssertions : ReferenceTypeAssertions<JsonSerializerOptions, JsonSerializerOptionsAssertions> | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="JsonSerializerOptionsAssertions" /> class. | ||
/// </summary> | ||
/// <param name="subject">The subject</param> | ||
public JsonSerializerOptionsAssertions(JsonSerializerOptions subject) | ||
: base(subject) | ||
{ | ||
} | ||
|
||
[Pure] | ||
public AndConstraint<Deserialized<T>> Deserialize<T>(string json, string because = "", params object[] becauseArgs) | ||
protected override string Identifier => "options"; | ||
|
||
/// <summary> | ||
/// Asserts that the current <see cref="JsonSerializerOptions"/> can be used to deserialize the specified JSON string. | ||
/// </summary> | ||
/// <typeparam name="T">The type to serialize to.</typeparam> | ||
/// <param name="json">The JSON string.</param> | ||
/// <param name="because"> | ||
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion | ||
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. | ||
/// </param> | ||
/// <param name="becauseArgs"> | ||
/// Zero or more objects to format using the placeholders in <see paramref="because" />. | ||
/// </param> | ||
/// <exception cref="ArgumentNullException"><paramref name="json"/> is <see langword="null"/>.</exception> | ||
public AndConstraint<ValueWrapper<T>> Deserialize<T>(string json, string because = "", params object[] becauseArgs) | ||
{ | ||
Guard.ThrowIfArgumentIsNull(json); | ||
|
||
Execute.Assertion | ||
.ForCondition(Subject is { }) | ||
.FailWith("Can not use {context} to deserialize from JSON as it is <null>."); | ||
|
||
T deserialzed = TryDeserialize<T>(json, out Exception failure); | ||
|
||
Execute.Assertion | ||
.BecauseOf(because, becauseArgs) | ||
.ForCondition(failure is null) | ||
.FailWith("Expected {context:the options} to deserialize {0}{reason}, but it failed: {1}.", json, failure?.Message); | ||
|
||
return new(new(deserialzed)); | ||
} | ||
|
||
/// <summary> | ||
/// Asserts that the current <see cref="JsonSerializerOptions"/> can be used to serialize the specified value. | ||
/// </summary> | ||
/// <typeparam name="T">The type to serialize to.</typeparam> | ||
/// <param name="value">The value to serialize.</param> | ||
/// <param name="because"> | ||
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion | ||
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically. | ||
/// </param> | ||
/// <param name="becauseArgs"> | ||
/// Zero or more objects to format using the placeholders in <see paramref="because" />. | ||
/// </param> | ||
public AndConstraint<ValueWrapper<JsonElement>> Serialize<T>(T value, string because = "", params object[] becauseArgs) | ||
{ | ||
Execute.Assertion | ||
.ForCondition(Subject is { }) | ||
.FailWith("Can not use {context} to serialize to JSON as it is <null>."); | ||
|
||
JsonElement serialized = TrySerialize(value, out Exception failure); | ||
|
||
Execute.Assertion | ||
.BecauseOf(because, becauseArgs) | ||
.ForCondition(failure is null) | ||
.FailWith("Expected {context:the options} to serialize {0}{reason}, but it failed: {1}.", value, failure?.Message); | ||
|
||
return new(new(serialized)); | ||
} | ||
|
||
private T TryDeserialize<T>(string json, out Exception failure) | ||
{ | ||
try | ||
{ | ||
return new(new(JsonSerializer.Deserialize<T>(json, Subject))); | ||
failure = null; | ||
return JsonSerializer.Deserialize<T>(json, Subject); | ||
} | ||
catch (Exception exception) | ||
{ | ||
Execute.Assertion | ||
.BecauseOf(because, becauseArgs) | ||
.FailWith("Expected {context:the options} to deserialize {0}{reason}, but it failed: {1}.", json, exception.Message); | ||
|
||
throw; | ||
failure = exception; | ||
return default; | ||
} | ||
} | ||
|
||
protected override string Identifier => "options"; | ||
|
||
[Pure] | ||
public AndConstraint<Serialized> Serialize<T>(T value) | ||
private JsonElement TrySerialize<T>(T value, out Exception failure) | ||
{ | ||
var bytes = JsonSerializer.SerializeToUtf8Bytes(value, Subject); | ||
using var doc = JsonDocument.Parse(bytes); | ||
return new(new(doc.RootElement.Clone())); | ||
try | ||
{ | ||
failure = null; | ||
var bytes = JsonSerializer.SerializeToUtf8Bytes(value, Subject); | ||
using var doc = JsonDocument.Parse(bytes); | ||
return doc.RootElement.Clone(); | ||
} | ||
catch (Exception exception) | ||
{ | ||
failure = exception; | ||
return default; | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace FluentAssertions; | ||
|
||
public sealed class ValueWrapper<TValue> | ||
{ | ||
public ValueWrapper(TValue value) => Value = value; | ||
|
||
public TValue Value { get; } | ||
|
||
public override string ToString() => Value?.ToString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.