Skip to content

Commit

Permalink
Addressing warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Psichorex committed Jan 20, 2024
1 parent b5bb1db commit 5a40871
Show file tree
Hide file tree
Showing 35 changed files with 165 additions and 272 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ namespace Lombiq.Tests.UI.Exceptions;

// Here we only need fileName and customMessage.
#pragma warning disable CA1032 // Implement standard exception constructors
public class FailureDumpItemAlreadyExistsException : Exception
public class FailureDumpItemAlreadyExistsException(string fileName, string customMessage, Exception innerException) : Exception(
$"A failure dump item with the same file name already exists. fileName: {fileName}"
+ Environment.NewLine
+ (customMessage ?? string.Empty),
innerException)
#pragma warning restore CA1032 // Implement standard exception constructors
{
public FailureDumpItemAlreadyExistsException(string fileName)
Expand All @@ -21,13 +25,4 @@ public FailureDumpItemAlreadyExistsException(string fileName, string customMessa
: this(fileName, customMessage, innerException: null)
{
}

public FailureDumpItemAlreadyExistsException(string fileName, string customMessage, Exception innerException)
: base(
$"A failure dump item with the same file name already exists. fileName: {fileName}"
+ Environment.NewLine
+ (customMessage ?? string.Empty),
innerException)
{
}
}
14 changes: 5 additions & 9 deletions Lombiq.Tests.UI/Exceptions/SetupFailedFastException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
namespace Lombiq.Tests.UI.Exceptions;

[SuppressMessage("Design", "CA1032:Implement standard exception constructors", Justification = "Used in a very specific case.")]
public class SetupFailedFastException : Exception
{
public int FailureCount { get; }

public SetupFailedFastException(int failureCount, Exception latestException)
: base(
$"The given setup operation failed {failureCount.ToTechnicalString()} times and won't be retried any " +
public class SetupFailedFastException(int failureCount, Exception latestException) : Exception(
$"The given setup operation failed {failureCount.ToTechnicalString()} times and won't be retried any " +
$"more. All tests using this operation for setup will instantly fail.",
latestException) =>
FailureCount = failureCount;
latestException)
{
public int FailureCount { get; } = failureCount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ namespace Lombiq.Tests.UI.Exceptions;

// Here we only need path instead of message.
#pragma warning disable CA1032 // Implement standard exception constructors
public class VisualVerificationBaselineImageNotFoundException : Exception
public class VisualVerificationBaselineImageNotFoundException(
string path,
int maxRetryCount,
Exception innerException = null) : Exception(GetExceptionMessage(path, maxRetryCount), innerException)
#pragma warning restore CA1032 // Implement standard exception constructors
{
public VisualVerificationBaselineImageNotFoundException(
string path,
int maxRetryCount,
Exception innerException = null)
: base(GetExceptionMessage(path, maxRetryCount), innerException)
{
}

private static string GetExceptionMessage(string path, int maxRetryCount) =>
maxRetryCount == 0 ? $"Baseline image file not found, thus it was created automatically under the path {path}."
+ " Please set its \"Build action\" to \"Embedded resource\" if you want to deploy a self-contained"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,9 @@ await context
public static Task ExecuteTestAsync(
this UITestContext context, string testName, Func<Task> testFunctionAsync)
{
if (context is null) throw new ArgumentNullException(nameof(context));
if (testName is null) throw new ArgumentNullException(nameof(testName));
if (testFunctionAsync is null) throw new ArgumentNullException(nameof(testFunctionAsync));
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(testName);
ArgumentNullException.ThrowIfNull(testFunctionAsync);

return ExecuteTestInnerAsync(context, testName, testFunctionAsync);
}
Expand Down
2 changes: 1 addition & 1 deletion Lombiq.Tests.UI/Extensions/ExtendedLoggingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private static TResult ExecuteSection<TResult>(this UITestContext context, LogSe
throw new InvalidOperationException("Impossible to reach.");
});

private static Task ExecuteSectionAsync(this UITestContext context, LogSection section, Func<Task> functionAsync) =>
private static Task<bool> ExecuteSectionAsync(this UITestContext context, LogSection section, Func<Task> functionAsync) =>
context.ExecuteSectionAsync(
section,
async () =>
Expand Down
2 changes: 1 addition & 1 deletion Lombiq.Tests.UI/Extensions/ShouldlyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void ShouldBeEmptyWhen<TItem, TMessage>(
JsonSerializerOptions jsonSerializerOptions = null)
{
var results = enumerable.Where(condition).ToList();
if (!results.Any()) return;
if (results.Count == 0) return;

var message = messageTransform == null
? JsonSerializer.Serialize(results, jsonSerializerOptions)
Expand Down
29 changes: 22 additions & 7 deletions Lombiq.Tests.UI/Extensions/TypedRouteUITestContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,18 @@ public static string GetRelativeUrlOfAction<TController>(
this UITestContext context,
Expression<Action<TController>> actionExpression,
params (string Key, object Value)[] additionalArguments)
where TController : ControllerBase =>
TypedRoute
.CreateFromExpression(actionExpression, additionalArguments, CreateServiceProvider(context))
where TController : ControllerBase
{
var serviceProvider = CreateServiceProvider(context);
var route = TypedRoute
.CreateFromExpression(actionExpression, additionalArguments, serviceProvider)
.ToString();

serviceProvider.Dispose();

return route;
}

/// <summary>
/// Gets the relative URL generated by <see cref="TypedRoute"/> for the <paramref name="actionExpressionAsync"/> in
/// the <typeparamref name="TController"/>.
Expand All @@ -57,11 +64,19 @@ public static string GetRelativeUrlOfAction<TController>(
this UITestContext context,
Expression<Func<TController, Task>> actionExpressionAsync,
params (string Key, object Value)[] additionalArguments)
where TController : ControllerBase =>
TypedRoute
.CreateFromExpression(actionExpressionAsync.StripResult(), additionalArguments, CreateServiceProvider(context))
where TController : ControllerBase
{
var serviceProvider = CreateServiceProvider(context);

var route = TypedRoute
.CreateFromExpression(actionExpressionAsync.StripResult(), additionalArguments, serviceProvider)
.ToString();

serviceProvider.Dispose();

return route;
}

/// <summary>
/// Gets the absolute URL generated by <see cref="TypedRoute"/> for the <paramref name="actionExpression"/> in the
/// <typeparamref name="TController"/>.
Expand All @@ -84,7 +99,7 @@ public static Uri GetAbsoluteUrlOfAction<TController>(
where TController : ControllerBase =>
context.GetAbsoluteUri(context.GetRelativeUrlOfAction(actionExpressionAsync, additionalArguments));

private static IServiceProvider CreateServiceProvider(UITestContext context)
private static ServiceProvider CreateServiceProvider(UITestContext context)
{
var services = new ServiceCollection();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static void AssertVisualVerificationOnAllResolutions(

if (exceptions.Count == 1) throw exceptions.Single();

if (exceptions.Any())
if (exceptions.Count != 0)
{
// The UITestExecutionSession doesn't support AggregateException with multiple inner exceptions, so we just
// concatenate the exceptions if there are multiple.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static async Task LogsShouldBeEmptyAsync(
.SplitByNewLines()
.Where(line => line.Contains("|ERROR|"));

if (permittedErrorLines.Any())
if (permittedErrorLines.Count != 0)
{
errorLines = errorLines.Where(line => !permittedErrorLines.Any(line.ContainsOrdinalIgnoreCase));
}
Expand Down
4 changes: 3 additions & 1 deletion Lombiq.Tests.UI/Helpers/WebAppConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Lombiq.Tests.UI.Helpers;

public static class WebAppConfigHelper
{
private static readonly string[] Separator = ["src", "test"];

/// <summary>
/// Retrieves the absolute path to the assembly (DLL) of the application being tested.
/// </summary>
Expand All @@ -24,7 +26,7 @@ public static string GetAbsoluteApplicationAssemblyPath(string webAppName, strin
else
{
var outputFolderContainingPath = Path.Combine(
AppContext.BaseDirectory.Split(new[] { "src", "test" }, StringSplitOptions.RemoveEmptyEntries)[0],
AppContext.BaseDirectory.Split(Separator, StringSplitOptions.RemoveEmptyEntries)[0],
"src",
webAppName,
"bin");
Expand Down
22 changes: 6 additions & 16 deletions Lombiq.Tests.UI/Models/FailureDumpItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,16 @@

namespace Lombiq.Tests.UI.Models;

public class FailureDumpItem : IFailureDumpItem
public class FailureDumpItem(
Func<Task<Stream>> getStream,
Action dispose = null) : IFailureDumpItem
{
private readonly Func<Task<Stream>> _getStream;
private readonly Action _dispose;
private readonly Func<Task<Stream>> _getStream = getStream ?? throw new ArgumentNullException(nameof(getStream));
private bool _disposed;

public FailureDumpItem(
Func<Task<Stream>> getStream,
Action dispose = null)
{
_getStream = getStream ?? throw new ArgumentNullException(nameof(getStream));
_dispose = dispose;
}

public Task<Stream> GetStreamAsync()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(FailureDumpItem));
}
ObjectDisposedException.ThrowIf(_disposed, this);

return _getStream();
}
Expand All @@ -34,7 +24,7 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_dispose?.Invoke();
dispose?.Invoke();
}

_disposed = true;
Expand Down
5 changes: 1 addition & 4 deletions Lombiq.Tests.UI/Models/FailureDumpItemGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public FailureDumpItemGeneric(

public Task<Stream> GetStreamAsync()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(FailureDumpItemGeneric<TContent>));
}
ObjectDisposedException.ThrowIf(_disposed, nameof(FailureDumpItemGeneric<TContent>));

if (_content is Stream stream && _getStream == null)
{
Expand Down
8 changes: 2 additions & 6 deletions Lombiq.Tests.UI/Models/PageNavigationState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ namespace Lombiq.Tests.UI.Models;
/// <summary>
/// Represents the current web page in terms of whether the browser has navigated away from it yet.
/// </summary>
public class PageNavigationState : IWebContentState
public class PageNavigationState(IWebElement root) : IWebContentState
{
private readonly IWebElement _root;

public PageNavigationState(IWebElement root) => _root = root;

public PageNavigationState(UITestContext context)
: this(context.Get(By.TagName("html")))
{
Expand All @@ -26,7 +22,7 @@ public bool CheckIfNavigationHasOccurred()
{
// Just any element operation to cause a StaleElementReferenceException if it's stale. If it isn't then this
// will always return false.
return _root.Size.Width < 0;
return root.Size.Width < 0;
}
catch (StaleElementReferenceException)
{
Expand Down
21 changes: 7 additions & 14 deletions Lombiq.Tests.UI/Models/RunningContextContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@

namespace Lombiq.Tests.UI.Models;

public class RunningContextContainer
public class RunningContextContainer(
SqlServerRunningContext sqlServerContext,
SmtpServiceRunningContext smtpContext,
AzureBlobStorageRunningContext blobStorageContext)
{
public SqlServerRunningContext SqlServerRunningContext { get; }
public SmtpServiceRunningContext SmtpServiceRunningContext { get; }
public AzureBlobStorageRunningContext AzureBlobStorageRunningContext { get; }

public RunningContextContainer(
SqlServerRunningContext sqlServerContext,
SmtpServiceRunningContext smtpContext,
AzureBlobStorageRunningContext blobStorageContext)
{
SqlServerRunningContext = sqlServerContext;
SmtpServiceRunningContext = smtpContext;
AzureBlobStorageRunningContext = blobStorageContext;
}
public SqlServerRunningContext SqlServerRunningContext { get; } = sqlServerContext;
public SmtpServiceRunningContext SmtpServiceRunningContext { get; } = smtpContext;
public AzureBlobStorageRunningContext AzureBlobStorageRunningContext { get; } = blobStorageContext;
}
24 changes: 9 additions & 15 deletions Lombiq.Tests.UI/Models/SafeWaitAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ namespace Atata;
/// Represents the retriable operation to wait for async condition safely (without throwing exception on timeout).
/// </summary>
/// <typeparam name="T">The type of object used to detect the condition.</typeparam>
public class SafeWaitAsync<T> : IWait<T>
/// <remarks>
/// <para>Initializes a new instance of the <see cref="SafeWaitAsync{T}"/> class.</para>
/// </remarks>
/// <param name="input">The input value to pass to the evaluated conditions.</param>
/// <param name="clock">The clock to use when measuring the timeout.</param>
public class SafeWaitAsync<T>(T input, IClock clock) : IWait<T>
{
private readonly T _input;
private readonly T _input = input.CheckNotNull(nameof(input));

private readonly IClock _clock;
private readonly IClock _clock = clock.CheckNotNull(nameof(clock));

private readonly List<Type> _ignoredExceptions = new();
private readonly List<Type> _ignoredExceptions = [];

/// <summary>
/// Initializes a new instance of the <see cref="SafeWaitAsync{T}"/> class.
Expand All @@ -30,17 +35,6 @@ public SafeWaitAsync(T input)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="SafeWaitAsync{T}"/> class.
/// </summary>
/// <param name="input">The input value to pass to the evaluated conditions.</param>
/// <param name="clock">The clock to use when measuring the timeout.</param>
public SafeWaitAsync(T input, IClock clock)
{
_input = input.CheckNotNull(nameof(input));
_clock = clock.CheckNotNull(nameof(clock));
}

/// <summary>
/// Gets or sets how long to wait for the evaluated condition to be true. The default timeout is taken from <see
/// cref="RetrySettings.Timeout"/>.
Expand Down
2 changes: 1 addition & 1 deletion Lombiq.Tests.UI/MonkeyTesting/MonkeyTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal sealed class MonkeyTester
private readonly UITestContext _context;
private readonly MonkeyTestingOptions _options;
private readonly NonSecurityRandomizer _randomizer;
private readonly List<PageMonkeyTestInfo> _visitedPages = new();
private readonly List<PageMonkeyTestInfo> _visitedPages = [];

private ILogManager Log => _context.Scope.AtataContext.Log;

Expand Down
Loading

0 comments on commit 5a40871

Please sign in to comment.