Skip to content

Commit

Permalink
Use .Which, instead of .And.Value.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Sep 14, 2023
1 parent 0347e6a commit 7dd7080
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 40 deletions.
2 changes: 0 additions & 2 deletions Src/FluentAssertions/Json/JsonElementAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,4 @@ public AndConstraint<JsonElementAssertions> BeFalse(string because = "", params

return new(this);

Check notice on line 148 in Src/FluentAssertions/Json/JsonElementAssertions.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use preferred style of 'new' expression when created type is not evident

Missing type specification
}


}
10 changes: 5 additions & 5 deletions Src/FluentAssertions/Json/JsonSerializerOptionsAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public JsonSerializerOptionsAssertions(JsonSerializerOptions subject)
/// 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>(Stream json, string because = "", params object[] becauseArgs)
public AndWhichConstraint<JsonSerializerOptionsAssertions, T> Deserialize<T>(Stream json, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(json);

Expand All @@ -54,11 +54,11 @@ public AndConstraint<ValueWrapper<T>> Deserialize<T>(Stream json, string because
.ForCondition(failure is null)
.FailWith("Expected {context:the options} to deserialize {0}{reason}, but it failed: {1}.", json, failure?.Message);

return new(new(deserialzed));
return new AndWhichConstraint<JsonSerializerOptionsAssertions, T>(this, deserialzed);
}

/// <inheritdoc cref="Deserialize{T}(Stream, string, object[])"/>
public AndConstraint<ValueWrapper<T>> Deserialize<T>(string json, string because = "", params object[] becauseArgs)
public AndWhichConstraint<JsonSerializerOptionsAssertions, T> Deserialize<T>(string json, string because = "", params object[] becauseArgs)
{
Stream stream = json is null ? null : new MemoryStream(Encoding.UTF8.GetBytes(json));
return Deserialize<T>(stream, because, becauseArgs);
Expand All @@ -76,7 +76,7 @@ public AndConstraint<ValueWrapper<T>> Deserialize<T>(string json, string because
/// <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)
public AndWhichConstraint<JsonSerializerOptionsAssertions, JsonElement> Serialize<T>(T value, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject is { })

Check notice on line 82 in Src/FluentAssertions/Json/JsonSerializerOptionsAssertions.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Arrange null checking pattern

Inconsistent null checking pattern style
Expand All @@ -89,7 +89,7 @@ public AndConstraint<ValueWrapper<JsonElement>> Serialize<T>(T value, string bec
.ForCondition(failure is null)
.FailWith("Expected {context:the options} to serialize {0}{reason}, but it failed: {1}.", value, failure?.Message);

return new(new(serialized));
return new AndWhichConstraint<JsonSerializerOptionsAssertions, JsonElement>(this, serialized);
}

private T TryDeserialize<T>(Stream json, out Exception failure)
Expand Down
12 changes: 0 additions & 12 deletions Src/FluentAssertions/ValueWrapper.cs

This file was deleted.

26 changes: 13 additions & 13 deletions Tests/FluentAssertions.Specs/Json/JsonElementAssertionsSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void Throws_for_unexcepected_value_kind()

// Act
Action act = () => options.Should().Serialize(42)
.And.Value.Should().HaveValueKind(JsonValueKind.String, because: "why not?");
.Which.Should().HaveValueKind(JsonValueKind.String, because: "why not?");

// Assert
act.Should().Throw<XunitException>()
Expand All @@ -31,7 +31,7 @@ public void Guards_excepected_value_kinds()
JsonSerializerOptions options = new();

// Act + assert
options.Should().Serialize(42).And.Value.Should().HaveValueKind(JsonValueKind.Number);
options.Should().Serialize(42).Which.Should().HaveValueKind(JsonValueKind.Number);
}
}

Expand All @@ -44,7 +44,7 @@ public void Throws_for_other_elements()
JsonSerializerOptions options = new();

// Act
Action act = () => options.Should().Serialize("null").And.Value.Should().BeNull(because: "why not?");
Action act = () => options.Should().Serialize("null").Which.Should().BeNull(because: "why not?");

// Assert
act.Should().Throw<XunitException>()
Expand All @@ -58,7 +58,7 @@ public void Guards_null_node()
JsonSerializerOptions options = new();

// Act + assert
options.Should().Serialize<object>(null).And.Value.Should().BeNull();
options.Should().Serialize<object>(null).Which.Should().BeNull();
}
}

Expand All @@ -71,7 +71,7 @@ public void Throws_for_other_elements()
JsonSerializerOptions options = new();

// Act
Action act = () => options.Should().Serialize<object>(null).And.Value.Should().BeString("null", because: "why not?");
Action act = () => options.Should().Serialize<object>(null).Which.Should().BeString("null", because: "why not?");

// Assert
act.Should().Throw<XunitException>()
Expand All @@ -85,7 +85,7 @@ public void Guards_string_node()
JsonSerializerOptions options = new();

// Act + assert
options.Should().Serialize(Guid.Parse("61088fce-43c5-4327-b591-0d7e862075d8")).And.Value.Should().BeString("61088fce-43c5-4327-b591-0d7e862075d8");
options.Should().Serialize(Guid.Parse("61088fce-43c5-4327-b591-0d7e862075d8")).Which.Should().BeString("61088fce-43c5-4327-b591-0d7e862075d8");
}
}

Expand All @@ -98,7 +98,7 @@ public void Throws_for_other_elements()
JsonSerializerOptions options = new();

// Act
Action act = () => options.Should().Serialize("42").And.Value.Should().BeNumber(42, because: "why not?");
Action act = () => options.Should().Serialize("42").Which.Should().BeNumber(42, because: "why not?");

// Assert
act.Should().Throw<XunitException>()
Expand All @@ -112,7 +112,7 @@ public void Guards_integer_number_node()
JsonSerializerOptions options = new();

// Act + assert
options.Should().Serialize(42).And.Value.Should().BeNumber(42);
options.Should().Serialize(42).Which.Should().BeNumber(42);
}

[Fact]
Expand All @@ -122,7 +122,7 @@ public void Guards_floating_point_number_node()
JsonSerializerOptions options = new();

// Act + assert
options.Should().Serialize(42.17).And.Value.Should().BeNumber(42.17m);
options.Should().Serialize(42.17).Which.Should().BeNumber(42.17m);
}
}

Expand All @@ -135,7 +135,7 @@ public void Throws_for_other_elements()
JsonSerializerOptions options = new();

// Act
Action act = () => options.Should().Serialize("true").And.Value.Should().BeTrue(because: "why not?");
Action act = () => options.Should().Serialize("true").Which.Should().BeTrue(because: "why not?");

// Assert
act.Should().Throw<XunitException>()
Expand All @@ -149,7 +149,7 @@ public void Guards_true_node()
JsonSerializerOptions options = new();

// Act + assert
options.Should().Serialize(true).And.Value.Should().BeTrue();
options.Should().Serialize(true).Which.Should().BeTrue();
}
}

Expand All @@ -162,7 +162,7 @@ public void Throws_for_other_elements()
JsonSerializerOptions options = new();

// Act
Action act = () => options.Should().Serialize("false").And.Value.Should().BeFalse(because: "why not?");
Action act = () => options.Should().Serialize("false").Which.Should().BeFalse(because: "why not?");

// Assert
act.Should().Throw<XunitException>()
Expand All @@ -176,7 +176,7 @@ public void Guards_false_node()
JsonSerializerOptions options = new();

// Act + assert
options.Should().Serialize(false).And.Value.Should().BeFalse();
options.Should().Serialize(false).Which.Should().BeFalse();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Throws_for_null_JSON()
JsonSerializerOptions options = new();

// Act
Action act = () => options.Should().Deserialize<int>(json: null);
Action act = () => options.Should().Deserialize<int>(json: (string)null);

// Assert
act.Should().Throw<ArgumentNullException>()
Expand Down
13 changes: 6 additions & 7 deletions docs/_pages/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class Serializes
JsonSerializerOptions options = GetOptions();

options.Should().Serialize(new DateOnly(2017, 06, 11))
.And.Value.Should().BeString("2017-06-11");
.Which.Should().BeString("2017-06-11");
}

[Fact]
Expand All @@ -52,7 +52,7 @@ public class Serializes
JsonSerializerOptions options = GetOptions();

options.Should().Serialize(SomeEnum.SomeValue)
.And.Value.Should().BeNumber(42);
.Which.Should().BeNumber(42);
}

[Fact]
Expand All @@ -61,7 +61,7 @@ public class Serializes
JsonSerializerOptions options = GetOptions();

options.Should().Serialize(default(CustomStruct))
.And.Value.Should().BeNull();
.Which.Should().BeNull();
}
}
```
Expand All @@ -77,17 +77,16 @@ public class Deserializes
JsonSerializerOptions options = GetOptions();

options.Should().Deserialize<DateOnly>("2017-06-11")
.And.Value.Should().Be(new DateOnly(2017-06-11));
.Which.Should().Be(new DateOnly(2017-06-11));
}

[Fact]
public void Enum_from_number()
{

JsonSerializerOptions options = GetOptions();

options.Should().Deserialize<SomeEnum>("42")
.And.Value.Should().BeNumber(SomeEnum.SomeValue);
.Which.Should().BeNumber(SomeEnum.SomeValue);
}

[Fact]
Expand All @@ -97,7 +96,7 @@ public class Deserializes
JsonSerializerOptions options = GetOptions();

options.Should().Deserialize<SomeEnum>("\"SomeValue\"")
.And.Value.Should().BeNumber(SomeEnum.SomeValue);
.Which.Should().BeNumber(SomeEnum.SomeValue);
}
}
```

0 comments on commit 7dd7080

Please sign in to comment.