10000 Add global usings, use file scoped namespaces by adrianiftode · Pull Request #111 · adrianiftode/FluentAssertions.Web · GitHub
[go: up one dir, main page]

Skip to content

Add global usings, use file scoped namespaces #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
global using Newtonsoft.Json;
global using System;
global using System.IO;
global using System.Text;
global using System.Threading.Tasks;
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
// ReSharper disable once CheckNamespace
using Newtonsoft.Json;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace FluentAssertions;

namespace FluentAssertions
/// <summary>
/// Newtonsoft.Json based serializer
/// </summary>
public class NewtonsoftJsonSerializer : ISerializer
{
/// <summary>
/// Newtonsoft.Json based serializer
/// </summary>
public class NewtonsoftJsonSerializer : ISerializer
/// <inheritdoc/>
public Task<object?> Deserialize(Stream content, Type modelType)
{
/// <inheritdoc/>
public Task<object?> Deserialize(Stream content, Type modelType)
try
{
try
{
var serializer = JsonSerializer.Create(NewtonsoftJsonSerializerConfig.Options);
var serializer = JsonSerializer.Create(NewtonsoftJsonSerializerConfig.Options);

using var sr = new StreamReader(content, Encoding.UTF8, true, 1024, leaveOpen: true);
using var reader = new JsonTextReader(sr);
var result = serializer.Deserialize(reader, modelType);
return Task.FromResult((object?)result);
}
using var sr = new StreamReader(content, Encoding.UTF8, true, 1024, leaveOpen: true);
using var reader = new JsonTextReader(sr);
var result = serializer.Deserialize(reader, modelType);
return Task.FromResult((object?)result);
}

catch (JsonException ex)
{
throw new DeserializationException($"Exception while deserializing the model with {nameof(NewtonsoftJsonSerializer)}", ex);
}
catch (JsonException ex)
{
throw new DeserializationException($"Exception while deserializing the model with {nameof(NewtonsoftJsonSerializer)}", ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
using System.Text.Json;
using Newtonsoft.Json;
// ReSharper disable once CheckNamespace
namespace FluentAssertions;

// ReSharper disable once CheckNamespace
namespace FluentAssertions
/// <summary>
/// Holder of the global <see cref="Newtonsoft.Json.JsonSerializerSettings"/>
/// </summary>
public static class NewtonsoftJsonSerializerConfig
{
/// <summary>
/// Holder of the global <see cref="JsonSerializerOptions"/>
/// The options used to deserialize a JSON into a C# object
/// </summary>
public static class NewtonsoftJsonSerializerConfig
{
/// <summary>
/// The options used to deserialize a JSON into a C# object
/// </summary>
public static readonly JsonSerializerSettings Options = new();
}
public static readonly JsonSerializerSettings Options = new();
}
515 changes: 253 additions & 262 deletions src/FluentAssertions.Web/BadRequestAssertions.cs

Large diffs are not rendered by default.

194 changes: 95 additions & 99 deletions src/FluentAssertions.Web/BadRequestAssertionsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,115 +1,111 @@
using FluentAssertions.Web;
using System.Diagnostics;
// ReSharper disable once CheckNamespace
namespace FluentAssertions;

// ReSharper disable once CheckNamespace
namespace FluentAssertions
/// <summary>
/// Contains extension methods for custom assertions in unit tests related to <see cref="BadRequestAssertions"/>.
/// </summary>
[DebuggerNonUserCode]
public static class BadRequestAssertionsExtensions
{
/// <summary>
/// Contains extension methods for custom assertions in unit tests related to <see cref="BadRequestAssertions"/>.
/// Asserts that a Bad Request HTTP response content contains an error message identifiable by an expected field name and a wildcard error text.
/// </summary>
[DebuggerNonUserCode]
public static class BadRequestAssertionsExtensions
{
/// <summary>
/// Asserts that a Bad Request HTTP response content contains an error message identifiable by an expected field name and a wildcard error text.
/// </summary>
/// <remarks>
/// This assertion considers the HTTP response content a JSON generated by the ASP.NET Core 3.0 framework or less
/// </remarks>
/// <param name="expectedErrorField">
/// The expected field name.
/// </param>
/// <param name="expectedWildcardErrorMessage">
/// The wildcard pattern with which the error field associated error message is matched, where * and ? have special meanings.
/// </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>
[CustomAssertion]
public static AndConstraint<BadRequestAssertions> HaveError(
/// <remarks>
/// This assertion considers the HTTP response content a JSON generated by the ASP.NET Core 3.0 framework or less
/// </remarks>
/// <param name="expectedErrorField">
/// The expected field name.
/// </param>
/// <param name="expectedWildcardErrorMessage">
/// The wildcard pattern with which the error field associated error message is matched, where * and ? have special meanings.
/// </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>
[CustomAssertion]
public static AndConstraint<BadRequestAssertions> HaveError(
#pragma warning disable 1573
this Primitives.HttpResponseMessageAssertions<Primitives.HttpResponseMessageAssertions> parent,
this Primitives.HttpResponseMessageAssertions<Primitives.HttpResponseMessageAssertions> parent,
#pragma warning restore 1573
string expectedErrorField, string expectedWildcardErrorMessage,
string because = "", params object[] becauseArgs)
=> new BadRequestAssertions(parent.Subject).HaveError(expectedErrorField, expectedWildcardErrorMessage, because, becauseArgs);
string expectedErrorField, string expectedWildcardErrorMessage,
string because = "", params object[] becauseArgs)
=> new BadRequestAssertions(parent.Subject).HaveError(expectedErrorField, expectedWildcardErrorMessage, because, becauseArgs);

/// <summary>
/// Asserts that a Bad Request HTTP response content contains only a single error message identifiable by an expected field name and a wildcard error text.
/// </summary>
/// <remarks>
/// This assertion considers the HTTP response content a JSON generated by the ASP.NET Core 3.0 framework or less
/// </remarks>
/// <param name="expectedErrorField">
/// The expected field name.
/// </param>
/// <param name="expectedWildcardErrorMessage">
/// The wildcard pattern with which the error field associated error message is matched, where * and ? have special meanings.
/// </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>
[CustomAssertion]
public static AndConstraint<BadRequestAssertions> OnlyHaveError(
/// <summary>
/// Asserts that a Bad Request HTTP response content contains only a single error message identifiable by an expected field name and a wildcard error text.
/// </summary>
/// <remarks>
/// This assertion considers the HTTP response content a JSON generated by the ASP.NET Core 3.0 framework or less
/// </remarks>
/// <param name="expectedErrorField">
/// The expected field name.
/// </param>
/// <param name="expectedWildcardErrorMessage">
/// The wildcard pattern with which the error field associated error message is matched, where * and ? have special meanings.
/// </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>
[CustomAssertion]
public static AndConstraint<BadRequestAssertions> OnlyHaveError(
#pragma warning disable 1573
this Primitives.HttpResponseMessageAssertions<Primitives.HttpResponseMessageAssertions> parent,
this Primitives.HttpResponseMessageAssertions<Primitives.HttpResponseMessageAssertions> parent,
#pragma warning restore 1573
string expectedErrorField, string expectedWildcardErrorMessage,
string because = "", params object[] becauseArgs)
=> new BadRequestAssertions(parent.Subject).OnlyHaveError(expectedErrorField, expectedWildcardErrorMessage, because, becauseArgs);
string expectedErrorField, string expectedWildcardErrorMessage,
string because = "", params object[] becauseArgs)
=> new BadRequestAssertions(parent.Subject).OnlyHaveError(expectedErrorField, expectedWildcardErrorMessage, because, becauseArgs);

/// <summary>
/// Asserts that a Bad Request HTTP response content does not contain an error message identifiable by an expected field name.
/// </summary>
/// <remarks>
/// This assertion considers the HTTP response content a JSON generated by the ASP.NET Core 3.0 framework or less
/// </remarks>
/// <param name="expectedErrorField">
/// The expected field name.
/// </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>
[CustomAssertion]
public static AndConstraint<BadRequestAssertions> NotHaveError(
/// <summary>
/// Asserts that a Bad Request HTTP response content does not contain an error message identifiable by an expected field name.
/// </summary>
/// <remarks>
/// This assertion considers the HTTP response content a JSON generated by the ASP.NET Core 3.0 framework or less
/// </remarks>
/// <param name="expectedErrorField">
/// The expected field name.
/// </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>
[CustomAssertion]
public static AndConstraint<BadRequestAssertions> NotHaveError(
#pragma warning disable 1573
this Primitives.HttpResponseMessageAssertions<Primitives.HttpResponseMessageAssertions> parent,
this Primitives.HttpResponseMessageAssertions<Primitives.HttpResponseMessageAssertions> parent,
#pragma warning restore 1573
string expectedErrorField, string because = "", params object[] becauseArgs)
=> new BadRequestAssertions(parent.Subject).NotHaveError(expectedErrorField, because, becauseArgs);
string expectedErrorField, string because = "", params object[] becauseArgs)
=> new BadRequestAssertions(parent.Subject).NotHaveError(expectedErrorField, because, becauseArgs);

/// <summary>
/// Asserts that a Bad Request HTTP response content contains an error message identifiable by an wildcard error text.
/// </summary>
/// <param name="expectedWildcardErrorMessage">
/// The wildcard pattern with which the error field associated error message is matched, where * and ? have special meanings.
/// </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>
[CustomAssertion]
public static AndConstraint<BadRequestAssertions> HaveErrorMessage(
/// <summary>
/// Asserts that a Bad Request HTTP response content contains an error message identifiable by an wildcard error text.
/// </summary>
/// <param name="expectedWildcardErrorMessage">
/// The wildcard pattern with which the error field associated error message is matched, where * and ? have special meanings.
/// </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>
[CustomAssertion]
public static AndConstraint<BadRequestAssertions> HaveErrorMessage(
#pragma warning disable 1573
this Primitives.HttpResponseMessageAssertions<Primitives.HttpResponseMessageAssertions> parent,
this Primitives.HttpResponseMessageAssertions<Primitives.HttpResponseMessageAssertions> parent,
#pragma warning restore 1573
string expectedWildcardErrorMessage,
string because = "", params object[] becauseArgs)
=> new BadRequestAssertions(parent.Subject).HaveErrorMessage(expectedWildcardErrorMessage, because, becauseArgs);
}
string expectedWildcardErrorMessage,
string because = "", params object[] becauseArgs)
=> new BadRequestAssertions(parent.Subject).HaveErrorMessage(expectedWildcardErrorMessage, because, becauseArgs);
}
78 changes: 38 additions & 40 deletions src/FluentAssertions.Web/DeserializationException.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@
// ReSharper disable once CheckNamespace
using System;
using System.Runtime.Serialization;

namespace FluentAssertions
namespace FluentAssertions;

/// <summary>
/// Captures serialization exceptions.
/// </summary>
[Serializable]
public class DeserializationException : Exception
{
/// <summary>
/// Captures serialization exceptions.
/// Argless constructor used to created new instances of this class.
/// </summary>
public DeserializationException()
{
}

/// <summary>
/// Constructor used to created new instances of this class given a certain message.
/// </summary>
[Serializable]
public class DeserializationException : Exception
/// <param name="message">The exception message.</param>

public DeserializationException(string message) : base(message)
{
}

/// <summary>
/// Constructor used to created new instances of this class given a certain message and an exception.
/// </summary>
/// <param name="message">The exception message.</param>
/// <param name="innerException">Usually the caught inner exception.</param>

public DeserializationException(string message, Exception innerException) : base(message, innerException)
{
}

/// <summary>
/// Specialized constructor used to deserialize eventually serialized exceptions of this type.
/// </summary>
/// <param name="info">Serialization info.</param>
/// <param name="context">Streaming context</param>

protected DeserializationException(SerializationInfo info, StreamingContext context) : base(info, context)
{
/// <summary>
/// Argless constructor used to created new instances of this class.
/// </summary>
public DeserializationException()
{
}

/// <summary>
/// Constructor used to created new instances of this class given a certain message.
/// </summary>
/// <param name="message">The exception message.</param>

public DeserializationException(string message) : base(message)
{
}

/// <summary>
/// Constructor used to created new instances of this class given a certain message and an exception.
/// </summary>
/// <param name="message">The exception message.</param>
/// <param name="innerException">Usually the caught inner exception.</param>

public DeserializationException(string message, Exception innerException) : base(message, innerException)
{
}

/// <summary>
/// Specialized constructor used to deserialize eventually serialized exceptions of this type.
/// </summary>
/// <param name="info">Serialization info.</param>
/// <param name="context">Streaming context</param>

protected DeserializationException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
Loading
0