Skip to content

Commit

Permalink
fix: errors from static code analysis (#477)
Browse files Browse the repository at this point in the history
Fix the errors from the [sonar cloud static code
analysis](https://github.com/Testably/Testably.Abstractions/runs/22481531017).
  • Loading branch information
vbreuss authored Mar 10, 2024
1 parent 82ada1b commit b706272
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 84 deletions.
16 changes: 9 additions & 7 deletions Source/Testably.Abstractions.Testing/FileSystem/FileMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#if FEATURE_FILESYSTEM_ASYNC
using System.Threading;
using System.Threading.Tasks;
// ReSharper disable PossibleMultipleEnumeration
#endif

namespace Testably.Abstractions.Testing.FileSystem;
Expand Down Expand Up @@ -1359,25 +1360,26 @@ private static void ThrowIfCancelled(CancellationToken cancellationToken)
}
#endif

private IDisposable Register(string name)
=> _fileSystem.StatisticsRegistration.File.Register(name);

private IDisposable Register<T1>(string name, T1 parameter1)
private IDisposable Register<T1>(string name,
T1 parameter1)
=> _fileSystem.StatisticsRegistration.File.Register(name,
ParameterDescription.FromParameter(parameter1));

private IDisposable Register<T1, T2>(string name, T1 parameter1, T2 parameter2)
private IDisposable Register<T1, T2>(string name,
T1 parameter1, T2 parameter2)
=> _fileSystem.StatisticsRegistration.File.Register(name,
ParameterDescription.FromParameter(parameter1),
ParameterDescription.FromParameter(parameter2));

private IDisposable Register<T1, T2, T3>(string name, T1 parameter1, T2 parameter2, T3 parameter3)
private IDisposable Register<T1, T2, T3>(string name,
T1 parameter1, T2 parameter2, T3 parameter3)
=> _fileSystem.StatisticsRegistration.File.Register(name,
ParameterDescription.FromParameter(parameter1),
ParameterDescription.FromParameter(parameter2),
ParameterDescription.FromParameter(parameter3));

private IDisposable Register<T1, T2, T3, T4>(string name, T1 parameter1, T2 parameter2, T3 parameter3, T4 parameter4)
private IDisposable Register<T1, T2, T3, T4>(string name,
T1 parameter1, T2 parameter2, T3 parameter3, T4 parameter4)
=> _fileSystem.StatisticsRegistration.File.Register(name,
ParameterDescription.FromParameter(parameter1),
ParameterDescription.FromParameter(parameter2),
Expand Down
2 changes: 2 additions & 0 deletions Source/Testably.Abstractions.Testing/FileSystem/PathMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,9 @@ private IDisposable Register<T1>(string name, T1 parameter1)
private IDisposable Register<T1>(string name, ReadOnlySpan<T1> parameter1)
=> _fileSystem.StatisticsRegistration.Path.Register(name,
ParameterDescription.FromParameter(parameter1));
#endif

#if FEATURE_PATH_ADVANCED
private IDisposable Register<T1, T2>(string name, ReadOnlySpan<T1> parameter1,
ReadOnlySpan<T2> parameter2)
=> _fileSystem.StatisticsRegistration.Path.Register(name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public int GetCounter()

#endregion

private class TemporaryDisable : IDisposable
private sealed class TemporaryDisable : IDisposable
{
public static IDisposable None { get; } = new NoOpDisposable();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ public bool Is<T>(T[] value)
value.SequenceEqual(d.Value);
}

private static bool IsEqual<T>(T value1, T value2)
{
if (value1 is null)
{
return value2 is null;
}

return value1.Equals(value2);
}
#if FEATURE_SPAN
/// <summary>
/// Checks, if the span value of the parameter equals <paramref name="value" />.
Expand Down Expand Up @@ -111,7 +102,17 @@ public static ParameterDescription FromOutParameter<T>(T value)
return new GenericParameterDescription<T>(value, true);
}

private class GenericParameterDescription<T> : ParameterDescription
private static bool IsEqual<T>(T value1, T value2)
{
if (value1 is null)
{
return value2 is null;
}

return value1.Equals(value2);
}

private sealed class GenericParameterDescription<T> : ParameterDescription
{
public T Value { get; }

Expand All @@ -133,7 +134,7 @@ public GenericParameterDescription(T value, bool isOutParameter) : base(isOutPar
}

#if FEATURE_SPAN
private class SpanParameterDescription<T> : ParameterDescription
private sealed class SpanParameterDescription<T> : ParameterDescription
{
public T[] Value { get; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#if NET48
using System.Diagnostics.CodeAnalysis;

// ReSharper disable once CheckNamespace
namespace Testably.Abstractions.Polyfills;

/// <summary>
/// Provides extension methods to simplify writing platform independent tests.
/// </summary>
[ExcludeFromCodeCoverage]
internal static class StringExtensionMethods
{
/// <summary>
/// Reports the zero-based index of the first occurrence of the specified string in the current
/// <see langword="string" /> object. A parameter specifies the type of search to use for the specified string.
/// </summary>
/// <returns>
/// The index position of the <paramref name="value" /> parameter if that string is found, or <c>-1</c> if it is not.
/// If <paramref name="value" /> is <see cref="string.Empty" />, the return value is <c>0</c>.
/// </returns>
internal static int IndexOf(
this string @this,
char value,
StringComparison comparison)
{
return @this.IndexOf($"{value}", comparison);
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void New_SafeFileHandle_FileAccess_ShouldRegisterCall()
SafeFileHandle handle = new();
FileAccess access = FileAccess.ReadWrite;

sut.FileStream.New(handle, access);
using FileSystemStream result = sut.FileStream.New(handle, access);

sut.Statistics.FileStream.ShouldOnlyContain(nameof(IFileStreamFactory.New),
handle, access);
Expand All @@ -34,7 +34,7 @@ public void New_String_FileMode_ShouldRegisterCall()
string path = "foo";
FileMode mode = FileMode.OpenOrCreate;

sut.FileStream.New(path, mode);
using FileSystemStream result = sut.FileStream.New(path, mode);

sut.Statistics.FileStream.ShouldOnlyContain(nameof(IFileStreamFactory.New),
path, mode);
Expand All @@ -49,7 +49,7 @@ public void New_String_FileStreamOptions_ShouldRegisterCall()
string path = "foo";
FileStreamOptions options = new();

sut.FileStream.New(path, options);
using FileSystemStream result = sut.FileStream.New(path, options);

sut.Statistics.FileStream.ShouldOnlyContain(nameof(IFileStreamFactory.New),
path, options);
Expand All @@ -68,7 +68,7 @@ public void New_SafeFileHandle_FileAccess_Int_ShouldRegisterCall()
FileAccess access = FileAccess.ReadWrite;
int bufferSize = 42;

sut.FileStream.New(handle, access, bufferSize);
using FileSystemStream result = sut.FileStream.New(handle, access, bufferSize);

sut.Statistics.FileStream.ShouldOnlyContain(nameof(IFileStreamFactory.New),
handle, access, bufferSize);
Expand All @@ -83,7 +83,7 @@ public void New_String_FileMode_FileAccess_ShouldRegisterCall()
FileMode mode = FileMode.OpenOrCreate;
FileAccess access = FileAccess.ReadWrite;

sut.FileStream.New(path, mode, access);
using FileSystemStream result = sut.FileStream.New(path, mode, access);

sut.Statistics.FileStream.ShouldOnlyContain(nameof(IFileStreamFactory.New),
path, mode, access);
Expand All @@ -102,7 +102,7 @@ public void New_SafeFileHandle_FileAccess_Int_Bool_ShouldRegisterCall()
int bufferSize = 42;
bool isAsync = true;

sut.FileStream.New(handle, access, bufferSize, isAsync);
using FileSystemStream result = sut.FileStream.New(handle, access, bufferSize, isAsync);

sut.Statistics.FileStream.ShouldOnlyContain(nameof(IFileStreamFactory.New),
handle, access, bufferSize, isAsync);
Expand All @@ -118,7 +118,7 @@ public void New_String_FileMode_FileAccess_FileShare_ShouldRegisterCall()
FileAccess access = FileAccess.ReadWrite;
FileShare share = FileShare.ReadWrite;

sut.FileStream.New(path, mode, access, share);
using FileSystemStream result = sut.FileStream.New(path, mode, access, share);

sut.Statistics.FileStream.ShouldOnlyContain(nameof(IFileStreamFactory.New),
path, mode, access, share);
Expand All @@ -134,7 +134,7 @@ public void New_String_FileMode_FileAccess_FileShare_Int_ShouldRegisterCall()
FileShare share = FileShare.ReadWrite;
int bufferSize = 42;

sut.FileStream.New(path, mode, access, share, bufferSize);
using FileSystemStream result = sut.FileStream.New(path, mode, access, share, bufferSize);

sut.Statistics.FileStream.ShouldOnlyContain(nameof(IFileStreamFactory.New),
path, mode, access, share, bufferSize);
Expand All @@ -151,7 +151,7 @@ public void New_String_FileMode_FileAccess_FileShare_Int_Bool_ShouldRegisterCall
int bufferSize = 42;
bool useAsync = true;

sut.FileStream.New(path, mode, access, share, bufferSize, useAsync);
using FileSystemStream result = sut.FileStream.New(path, mode, access, share, bufferSize, useAsync);

sut.Statistics.FileStream.ShouldOnlyContain(nameof(IFileStreamFactory.New),
path, mode, access, share, bufferSize, useAsync);
Expand All @@ -168,7 +168,7 @@ public void New_String_FileMode_FileAccess_FileShare_Int_FileOptions_ShouldRegis
int bufferSize = 42;
FileOptions options = new();

sut.FileStream.New(path, mode, access, share, bufferSize, options);
using FileSystemStream result = sut.FileStream.New(path, mode, access, share, bufferSize, options);

sut.Statistics.FileStream.ShouldOnlyContain(nameof(IFileStreamFactory.New),
path, mode, access, share, bufferSize, options);
Expand All @@ -182,7 +182,7 @@ public void Wrap_FileStream_ShouldRegisterCall()

try
{
sut.FileStream.Wrap(fileStream);
using FileSystemStream result = sut.FileStream.Wrap(fileStream);
}
catch (NotSupportedException)
{
Expand Down
Loading

0 comments on commit b706272

Please sign in to comment.