Skip to content

Commit ef3f9bc

Browse files
committed
feat: a new hope
1 parent ab6a89d commit ef3f9bc

File tree

10 files changed

+126
-51
lines changed

10 files changed

+126
-51
lines changed

src/DDS.Tools/Extensions/ConfiguratorExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace DDS.Tools.Extensions;
99
/// <summary>
1010
/// The configurator extensions class.
1111
/// </summary>
12+
[ExcludeFromCodeCoverage(Justification = "Not relevant here.")]
1213
[SuppressMessage("Style", "IDE0058", Justification = "Not relevant here.")]
1314
internal static class ConfiguratorExtensions
1415
{

src/DDS.Tools/Services/TodoService.cs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ namespace DDS.Tools.Services;
1818
/// <summary>
1919
/// The todo service class.
2020
/// </summary>
21-
/// <param name="logger">The logger service instance to use.</param>
22-
/// <param name="provider">The service provider instance to use.</param>
23-
internal sealed class TodoService(ILoggerService<TodoService> logger, IServiceProvider provider) : ITodoService
21+
/// <param name="loggerService">The logger service instance to use.</param>
22+
/// <param name="serviceProvider">The service provider instance to use.</param>
23+
internal sealed class TodoService(ILoggerService<TodoService> loggerService, IServiceProvider serviceProvider) : ITodoService
2424
{
25-
private readonly ILoggerService<TodoService> _logger = logger;
26-
private readonly IServiceProvider _provider = provider;
25+
private readonly ILoggerService<TodoService> _loggerService = loggerService;
26+
private readonly IServiceProvider _serviceProvider = serviceProvider;
2727
private readonly List<string> _todosDone = [];
2828
private int _todosDuplicateCount = 0;
2929

@@ -38,8 +38,7 @@ public TodoCollection GetTodos(ConvertSettingsBase settings, ImageType imageType
3838
{
3939
TodoCollection todos = [];
4040

41-
string searchPattern = $"*.{imageType}";
42-
string[] files = Directory.GetFiles(settings.SourceFolder, searchPattern, SearchOption.AllDirectories);
41+
string[] files = Directory.GetFiles(settings.SourceFolder, $"*.{imageType}", SearchOption.AllDirectories);
4342

4443
if (files.Length.Equals(0))
4544
return todos;
@@ -50,7 +49,7 @@ public TodoCollection GetTodos(ConvertSettingsBase settings, ImageType imageType
5049
}
5150
catch (Exception ex)
5251
{
53-
_logger.Log(LogException, ex);
52+
_loggerService.Log(LogException, ex);
5453
throw new ServiceException($"Something went wrong in {nameof(GetTodos)}!", ex);
5554
}
5655
}
@@ -73,7 +72,7 @@ public TodoCollection GetTodosFromJson(ConvertSettingsBase settings, ImageType i
7372
}
7473
catch (Exception ex)
7574
{
76-
_logger.Log(LogException, ex);
75+
_loggerService.Log(LogException, ex);
7776
throw new ServiceException($"Something went wrong in {nameof(GetTodosFromJson)}!", ex);
7877
}
7978
}
@@ -98,7 +97,7 @@ public void GetTodosDone(TodoCollection todos, ConvertSettingsBase settings, Ima
9897
}
9998
catch (Exception ex)
10099
{
101-
_logger.Log(LogException, ex);
100+
_loggerService.Log(LogException, ex);
102101
throw new ServiceException($"Something went wrong in {nameof(GetTodosDone)}!", ex);
103102
}
104103
}
@@ -113,7 +112,7 @@ public void GetTodosDoneFromJson(TodoCollection todos, ConvertSettingsBase setti
113112
}
114113
catch (Exception ex)
115114
{
116-
_logger.Log(LogException, ex);
115+
_loggerService.Log(LogException, ex);
117116
throw new ServiceException($"Something went wrong in {nameof(GetTodosDoneFromJson)}!", ex);
118117
}
119118
}
@@ -122,7 +121,7 @@ private void GetTodo(TodoCollection todos, ConvertSettingsBase settings, ImageTy
122121
{
123122
FileInfo fileInfo = new(file);
124123

125-
IImageModel image = _provider.GetRequiredKeyedService<IImageModel>(imageType);
124+
IImageModel image = _serviceProvider.GetRequiredKeyedService<IImageModel>(imageType);
126125
image.Load(file);
127126

128127
TodoModel todo = new(
@@ -171,7 +170,7 @@ private void GetTodoDone(TodoModel todo, ConvertSettingsBase settings, ImageType
171170

172171
private void SaveImage(ConvertSettingsBase settings, TodoModel todo, ImageType imageType)
173172
{
174-
IImageModel image = _provider.GetRequiredKeyedService<IImageModel>(imageType);
173+
IImageModel image = _serviceProvider.GetRequiredKeyedService<IImageModel>(imageType);
175174
image.Load(todo.FullPathName);
176175

177176
string targetFolder = PrepareTargetFolder(settings, image, todo.TargetFolder);
@@ -213,11 +212,9 @@ private static string GetTargetFileName(ConvertSettingsBase settings, TodoModel
213212
return todo.FileHash;
214213
}
215214

216-
private static string GetTargetFileExtensions(ImageType imageType)
217-
=> imageType switch
218-
{
219-
ImageType.DDS => $"{ImageType.PNG}",
220-
ImageType.PNG => $"{ImageType.DDS}",
221-
_ => string.Empty,
222-
};
215+
private static string GetTargetFileExtensions(ImageType imageType) => imageType switch
216+
{
217+
ImageType.DDS => $"{ImageType.PNG}",
218+
_ => $"{ImageType.DDS}"
219+
};
223220
}

tests/DDS.ToolsTests/DDS.ToolsTests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
<None Update="Resources\DDS\Red\64A.DDS">
1818
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1919
</None>
20+
<None Update="Resources\JSON\Result.json">
21+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
22+
</None>
2023
<None Update="Resources\PNG\32.png">
2124
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2225
</None>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using DDS.Tools.Exceptions;
2+
3+
namespace DDS.ToolsTests.Exceptions;
4+
5+
[TestClass]
6+
public sealed class CommandExceptionTests : UnitTestBase
7+
{
8+
[TestMethod]
9+
public void CommandExceptionTest()
10+
{
11+
CommandException exception = new("Test");
12+
13+
Assert.IsNotNull(exception);
14+
Assert.AreEqual("Test", exception.Message);
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using DDS.Tools.Exceptions;
2+
3+
namespace DDS.ToolsTests.Exceptions;
4+
5+
[TestClass]
6+
public sealed class ServiceExceptionTests : UnitTestBase
7+
{
8+
[TestMethod]
9+
public void ServiceExceptionTest()
10+
{
11+
ServiceException exception = new("Test");
12+
13+
Assert.IsNotNull(exception);
14+
Assert.AreEqual("Test", exception.Message);
15+
}
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"fileName": "32.dds",
4+
"relativePath": "",
5+
"fileHash": "3B6242065CC51D558E45CD5868A18A36"
6+
}
7+
]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using DDS.Tools.Enumerators;
2+
using DDS.Tools.Exceptions;
3+
using DDS.Tools.Models;
4+
using DDS.Tools.Settings;
5+
6+
namespace DDS.ToolsTests.Services;
7+
8+
public sealed partial class TodoServiceTests
9+
{
10+
[DataTestMethod]
11+
[DynamicData(nameof(GetTodosData))]
12+
public void GetTodosTest(string resourcePath, ImageType type, int expected)
13+
{
14+
DdsConvertSettings settings = new() { SourceFolder = resourcePath, TargetFolder = resourcePath };
15+
16+
TodoCollection todos = s_todoService.GetTodos(settings, type);
17+
18+
Assert.AreEqual(expected, todos.Count);
19+
}
20+
21+
[TestMethod]
22+
[ExpectedException(typeof(ServiceException))]
23+
public void GetTodosSourceFolderNotFoundTest()
24+
=> s_todoService.GetTodos(new DdsConvertSettings() { SourceFolder = @"X:\FooBar" }, ImageType.DDS);
25+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using DDS.Tools.Enumerators;
2+
using DDS.Tools.Exceptions;
3+
using DDS.Tools.Models;
4+
using DDS.Tools.Settings;
5+
6+
namespace DDS.ToolsTests.Services;
7+
8+
public sealed partial class TodoServiceTests
9+
{
10+
private static readonly string JsonFilePath = Path.Combine(TestConstants.JsonResourcePath, "Result.json");
11+
12+
[TestMethod]
13+
public void GetTodosFromJsonTest()
14+
{
15+
DdsConvertSettings settings = new() { SourceFolder = @"X:\FooBar" };
16+
17+
TodoCollection todos = s_todoService.GetTodosFromJson(settings, ImageType.DDS, JsonFilePath);
18+
19+
Assert.AreEqual(1, todos.Count);
20+
Assert.AreEqual("3B6242065CC51D558E45CD5868A18A36.PNG", todos.First().FileName);
21+
Assert.AreEqual("32.dds", todos.First().FullPathName);
22+
}
23+
24+
[TestMethod]
25+
[ExpectedException(typeof(ServiceException))]
26+
public void GetTodosFromJsonExceptionTest()
27+
{
28+
DdsConvertSettings settings = new() { SourceFolder = @"X:\FooBar" };
29+
30+
_ = s_todoService.GetTodosFromJson(settings, ImageType.PNG, string.Empty);
31+
}
32+
}
Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,23 @@
11
using DDS.Tools.Enumerators;
22
using DDS.Tools.Interfaces.Services;
3-
using DDS.Tools.Models;
4-
using DDS.Tools.Settings;
5-
using DDS.Tools.Settings.Base;
63

74
using Microsoft.Extensions.DependencyInjection;
85

96
namespace DDS.ToolsTests.Services;
107

118
[TestClass]
12-
public sealed class TodoServiceTests : UnitTestBase
9+
public sealed partial class TodoServiceTests : UnitTestBase
1310
{
14-
private readonly ITodoService _todoService;
11+
private static ITodoService s_todoService = default!;
1512

16-
public TodoServiceTests()
17-
=> _todoService = ServiceProvider.GetRequiredService<ITodoService>();
13+
[ClassInitialize]
14+
public static void ClassInitialize(TestContext context)
15+
=> s_todoService = ServiceProvider.GetRequiredService<ITodoService>();
1816

19-
[DataTestMethod]
20-
[DynamicData(nameof(GetResourceData))]
21-
public void GetTodosTest(string resourcePath, ImageType type, int expected)
22-
{
23-
DdsConvertSettings settings = new() { SourceFolder = resourcePath, TargetFolder = resourcePath };
24-
25-
TodoCollection todos = _todoService.GetTodos(settings, type);
26-
27-
Assert.AreEqual(expected, todos.Count);
28-
}
29-
30-
[TestMethod]
31-
public void GetTodosSourceFolderNotFoundTest()
32-
{
33-
ConvertSettingsBase settings = new DdsConvertSettings() { SourceFolder = @"X:\FooBar" };
34-
35-
TodoCollection todos = _todoService.GetTodos(settings, ImageType.DDS);
36-
37-
Assert.AreEqual(0, todos.Count);
38-
}
39-
40-
public static IEnumerable<object[]> GetResourceData
17+
private static IEnumerable<object[]> GetTodosData
4118
=> new[]
4219
{
43-
new object[] { TestConstants.PngResourcePath, ImageType.PNG, 2 },
44-
new object[] { TestConstants.PngResourcePath, ImageType.DDS, 0 }
20+
new object[] { TestConstants.PngResourcePath, ImageType.PNG, 4 },
21+
[TestConstants.PngResourcePath, ImageType.DDS, 0]
4522
};
4623
}

tests/DDS.ToolsTests/UnitTestBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ public static class TestConstants
4141
public static readonly string ResourcePath = Path.Combine(Environment.CurrentDirectory, "Resources");
4242
public static readonly string PngResourcePath = Path.Combine(ResourcePath, "PNG");
4343
public static readonly string DdsResourcePath = Path.Combine(ResourcePath, "DDS");
44+
public static readonly string JsonResourcePath = Path.Combine(ResourcePath, "JSON");
4445
}

0 commit comments

Comments
 (0)