Skip to content

Commit

Permalink
code smells 18 & fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Oct 18, 2024
1 parent a94d909 commit 1d8e47a
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,14 @@ public async Task Preflight_ValidLatLong()
}

[TestMethod]
[ExpectedException(typeof(MissingFieldException))]
public void UpdateServiceTest_CompareAllLabelsAndRotation_NullMissingFieldException()
{
MetaPreflight.
CompareAllLabelsAndRotation(null!, null!,
null!, false, 0);
// ==>> MissingFieldException
Assert.ThrowsException<MissingFieldException>(() =>
MetaPreflight.
CompareAllLabelsAndRotation(null!, null!,
null!, false, 0));
}


[TestMethod]
public void UpdateServiceTest_CompareAllLabelsAndRotation_AppendIsFalse()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,12 @@ public void SolveConcurrencyException_should_callDelegate()
}

[TestMethod]
[ExpectedException(typeof(NotSupportedException))]
public void Query_UpdateItem_NotSupportedException()
{
SolveConcurrency.SolveConcurrencyException(null!,
new FakePropertyValues(null!), new FakePropertyValues(null!),
"", _ => IsWrittenConcurrencyException = true);
// expect error
Assert.ThrowsException<NotSupportedException>(() =>
SolveConcurrency.SolveConcurrencyException(null!,
new FakePropertyValues(null!), new FakePropertyValues(null!),
"", _ => IsWrittenConcurrencyException = true));
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Win32;
Expand All @@ -6,84 +7,92 @@
using starskytest.FakeCreateAn.CreateFakeStarskyExe;
using System.Text.RegularExpressions;

namespace starskytest.starsky.foundation.native.OpenApplicationNative.Helpers
namespace starskytest.starsky.foundation.native.OpenApplicationNative.Helpers;

/// <summary>
/// Only for Windows - test the WindowsSetFileAssociationsWindows
/// </summary>
[TestClass]
public partial class WindowsSetFileAssociationsTests
{
/// <summary>
/// Only for Windows - test the WindowsSetFileAssociationsWindows
/// </summary>
[TestClass]
public class WindowsSetFileAssociationsTests
private const string Extension = ".starsky";
private const string ProgId = "starskytest";
private const string FileTypeDescription = "Starsky Test File";

[TestInitialize]
public void TestInitialize()
{
CleanSetup();
}

[TestCleanup]
public void TestCleanup()
{
private const string Extension = ".starsky";
private const string ProgId = "starskytest";
private const string FileTypeDescription = "Starsky Test File";
CleanSetup();
}

[TestInitialize]
public void TestInitialize()
[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability",
"CA1416:Validate platform compatibility", Justification = "Check does exists")]
private static void CleanSetup()
{
if ( !new AppSettings().IsWindows )
{
CleanSetup();
return;
}

[TestCleanup]
public void TestCleanup()
// Ensure no keys exist before the test starts
try
{
CleanSetup();
Registry.CurrentUser.DeleteSubKeyTree($@"Software\Classes\{Extension}", false);
Registry.CurrentUser.DeleteSubKeyTree($@"Software\Classes\{ProgId}", false);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability",
"CA1416:Validate platform compatibility", Justification = "Check does exists")]
private static void CleanSetup()
catch ( IOException )
{
if ( !new AppSettings().IsWindows )
{
return;
}

// Ensure no keys exist before the test starts
try
{
Registry.CurrentUser.DeleteSubKeyTree($@"Software\Classes\{Extension}", false);
Registry.CurrentUser.DeleteSubKeyTree($@"Software\Classes\{ProgId}", false);
}
catch ( IOException )
{
// Ignore if the key does not exist
}
// Ignore if the key does not exist
}
}

[TestMethod]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability",
"CA1416:Validate platform compatibility",
Justification = "Check if test for windows only")]
public void EnsureAssociationsSet()
[TestMethod]
public void WindowsSetFileAssociations_EnsureAssociationsSet()
{
if ( !new AppSettings().IsWindows )
{
if ( !new AppSettings().IsWindows )
{
Assert.Inconclusive("This test if for Windows Only");
return;
}
Assert.Inconclusive("This test if for Windows Only");
return;
}

var filePath = new CreateFakeStarskyWindowsExe().FullFilePath;
WindowsSetFileAssociations.EnsureAssociationsSet(
new FileAssociation
{
Extension = Extension,
ProgId = ProgId,
FileTypeDescription = FileTypeDescription,
ExecutableFilePath = filePath
});
var filePath = new CreateFakeStarskyWindowsExe().FullFilePath;
WindowsSetFileAssociations.EnsureAssociationsSet(
new FileAssociation
{
Extension = Extension,
ProgId = ProgId,
FileTypeDescription = FileTypeDescription,
ExecutableFilePath = filePath
});

var registryKeyPath = $@"Software\Classes\{ProgId}\shell\open\command";
var valueKey = GetRegistryValue();
valueKey ??= GetRegistryValue();

using var key = Registry.CurrentUser.OpenSubKey(registryKeyPath);
Assert.IsNotNull(valueKey);
var match = GetFilePathFromRegistryRegex().Match(valueKey);
var value = match.Groups[1].Value;

var valueKey = key?.GetValue(string.Empty)?.ToString();
var pattern = "\"([^\"]*)\"";
Assert.IsNotNull(valueKey);
var match = Regex.Match(valueKey, pattern);
var value = match.Groups[1].Value;
Assert.AreEqual(filePath, value);
}

Assert.AreEqual(filePath, value);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability",
"CA1416:Validate platform compatibility",
Justification = "Check if test for windows only")]
private static string? GetRegistryValue()
{
const string registryKeyPath = $@"Software\Classes\{ProgId}\shell\open\command";
using var key = Registry.CurrentUser.OpenSubKey(registryKeyPath);
var valueKey = key?.GetValue(string.Empty)?.ToString();
Console.WriteLine($"GetRegistryValue {valueKey}");
return valueKey;
}

[GeneratedRegex("\"([^\"]*)\"")]
private static partial Regex GetFilePathFromRegistryRegex();
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void GetPathListFormArgsTest_CurrentDirectory()
}

[TestMethod]
public void GetPathListFormArgsTest__FieldAccessException()
public void GetPathListFormArgsTest_FieldAccessException()
{
// Arrange
var args = new List<string> { "-p", "/" }.ToArray();
Expand Down Expand Up @@ -347,6 +347,14 @@ public void ArgsHelper_SetEnvironmentByArgsLongTestListTest()
}
}

[TestMethod]
public void ArgsHelper_GetRelativeValue_Null_Test()
{
var args = new List<string> { "--subpathrelative", "1" }.ToArray();
Assert.ThrowsException<FieldAccessException>(() =>
new ArgsHelper(null!).GetRelativeValue(args));
}

[TestMethod]
public void ArgsHelper_GetSubPathRelativeTest()
{
Expand Down Expand Up @@ -501,7 +509,7 @@ public void NeedHelpShowDialog_WebHtml_Verbose()

Assert.IsTrue(contains);
}

[TestMethod]
public void ArgsHelper_NeedHelpShowDialog_OpenTelemetry()
{
Expand All @@ -518,30 +526,29 @@ public void ArgsHelper_NeedHelpShowDialog_OpenTelemetry()
Verbose = true
};
new ArgsHelper(appSettings, console).NeedHelpShowDialog();

Assert.IsTrue(console.WrittenLines.Exists(p => p.Contains($"OpenTelemetry LogsEndpoint: " +
$"{appSettings.OpenTelemetry.LogsEndpoint}")));
Assert.IsTrue(console.WrittenLines.Exists(p => p.Contains($"OpenTelemetry TracesEndpoint: " +
$"{appSettings.OpenTelemetry.TracesEndpoint}")));
Assert.IsTrue(console.WrittenLines.Exists(p => p.Contains($"OpenTelemetry MetricsEndpoint: " +
$"{appSettings.OpenTelemetry.MetricsEndpoint}")));
$"{appSettings.OpenTelemetry.LogsEndpoint}")));
Assert.IsTrue(console.WrittenLines.Exists(p =>
p.Contains($"OpenTelemetry TracesEndpoint: " +
$"{appSettings.OpenTelemetry.TracesEndpoint}")));
Assert.IsTrue(console.WrittenLines.Exists(p =>
p.Contains($"OpenTelemetry MetricsEndpoint: " +
$"{appSettings.OpenTelemetry.MetricsEndpoint}")));
}

[TestMethod]
[ExpectedException(typeof(FieldAccessException))]
public void ArgsHelper_NeedHelpShowDialog_Null_Test()
{
new ArgsHelper(null!).NeedHelpShowDialog();
// FieldAccessException
Assert.ThrowsException<FieldAccessException>(() =>
new ArgsHelper(null!).NeedHelpShowDialog());
}


[TestMethod]
[ExpectedException(typeof(FieldAccessException))]
public void ArgsHelper_SetEnvironmentToAppSettings_Null_Test()
{
new ArgsHelper(null!).SetEnvironmentToAppSettings();
// FieldAccessException
Assert.ThrowsException<FieldAccessException>(() =>
new ArgsHelper(null!).SetEnvironmentToAppSettings());
}

[TestMethod]
Expand Down Expand Up @@ -664,11 +671,11 @@ public void ArgsHelper_GetProfile()
var value = ArgsHelper.GetProfile(args);
Assert.AreEqual("test", value);
}

[TestMethod]
public void ArgsHelper_GetProfile_StringEmpty()
{
var args = new List<string> { "--profile"}.ToArray();
var args = new List<string> { "--profile" }.ToArray();
var value = ArgsHelper.GetProfile(args);
Assert.AreEqual(string.Empty, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,10 @@ public void AppSettingsProviderTest_NoFolderMissingFirstSlash()
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void AppSettingsProviderTest_Null()
{
AppSettings.StructureCheck(string.Empty);
// >= ArgumentException
Assert.ThrowsException<ArgumentNullException>(() =>
AppSettings.StructureCheck(string.Empty));
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,11 @@ public async Task ResizeThumbnailToStream_CorruptImage_Status()
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public async Task ResizeThumbnailImageFormat_NullInput()
{
await Thumbnail.SaveThumbnailImageFormat(null!,
ExtensionRolesHelper.ImageFormat.bmp, null!);
// ArgumentNullException
await Assert.ThrowsExceptionAsync<ArgumentNullException>(async () =>
await Thumbnail.SaveThumbnailImageFormat(null!,
ExtensionRolesHelper.ImageFormat.bmp, null!));
}

[TestMethod]
Expand Down

0 comments on commit 1d8e47a

Please sign in to comment.