Added unit tests

This commit is contained in:
MaddoScientisto 2026-02-04 19:01:00 +01:00
commit 0c1bb50dce
5 changed files with 285 additions and 2 deletions

View file

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35312.102
# Visual Studio Version 18
VisualStudioVersion = 18.2.11415.280 d18.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImageCatalog 2", "imagecatalog\ImageCatalog 2.csproj", "{3F1E23DB-435E-0590-1EF5-735E898DBA3C}"
EndProject
@ -9,6 +9,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Libraries", "Libraries", "{
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MaddoShared", "MaddoShared\MaddoShared.csproj", "{AEBFE9E3-277C-4A7B-8448-145D1B11998B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{5F0BEF23-B1EA-4100-A772-DC455D40B1C1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MaddoShared.Tests", "MaddoShared.Tests\MaddoShared.Tests.csproj", "{59952BE8-20B4-4BF2-9367-705F41395265}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -43,12 +47,25 @@ Global
{AEBFE9E3-277C-4A7B-8448-145D1B11998B}.Release|x64.Build.0 = Release|Any CPU
{AEBFE9E3-277C-4A7B-8448-145D1B11998B}.Release|x86.ActiveCfg = Release|Any CPU
{AEBFE9E3-277C-4A7B-8448-145D1B11998B}.Release|x86.Build.0 = Release|Any CPU
{59952BE8-20B4-4BF2-9367-705F41395265}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{59952BE8-20B4-4BF2-9367-705F41395265}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59952BE8-20B4-4BF2-9367-705F41395265}.Debug|x64.ActiveCfg = Debug|Any CPU
{59952BE8-20B4-4BF2-9367-705F41395265}.Debug|x64.Build.0 = Debug|Any CPU
{59952BE8-20B4-4BF2-9367-705F41395265}.Debug|x86.ActiveCfg = Debug|Any CPU
{59952BE8-20B4-4BF2-9367-705F41395265}.Debug|x86.Build.0 = Debug|Any CPU
{59952BE8-20B4-4BF2-9367-705F41395265}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59952BE8-20B4-4BF2-9367-705F41395265}.Release|Any CPU.Build.0 = Release|Any CPU
{59952BE8-20B4-4BF2-9367-705F41395265}.Release|x64.ActiveCfg = Release|Any CPU
{59952BE8-20B4-4BF2-9367-705F41395265}.Release|x64.Build.0 = Release|Any CPU
{59952BE8-20B4-4BF2-9367-705F41395265}.Release|x86.ActiveCfg = Release|Any CPU
{59952BE8-20B4-4BF2-9367-705F41395265}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{AEBFE9E3-277C-4A7B-8448-145D1B11998B} = {A3D50937-74F6-4DC8-8D89-B534B484C0F9}
{59952BE8-20B4-4BF2-9367-705F41395265} = {5F0BEF23-B1EA-4100-A772-DC455D40B1C1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0E3ABC63-8601-4DAC-AFEA-33F3E8E36757}

View file

@ -0,0 +1,232 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Extensions.Logging;
using Moq;
using FluentAssertions;
using MaddoShared;
namespace MaddoShared.Tests
{
[TestClass]
public class ImageCreatorSharpTests
{
private ImageCreatorSharp CreateService(Action<PicSettings> customize = null)
{
var settings = new PicSettings
{
DimStandard = 20,
DimStandardMiniatura = 10,
LarghezzaSmall = 100,
AltezzaSmall = 100,
LarghezzaBig = 800,
AltezzaBig = 600,
Trasparenza = 50,
IlFont = "Arial",
Grassetto = false,
Posizione = "CENTRO",
Allineamento = "CENTRO",
Margine = 10,
MargVert = 10,
TestoMin = false,
AggNumTempMin = false
};
customize?.Invoke(settings);
var logger = new Mock<ILogger<ImageCreatorSharp>>().Object;
return new ImageCreatorSharp(settings, logger);
}
[TestMethod]
public void CalculateThumbnailSize_Larghezza_UsesWidthScaling()
{
var svc = CreateService();
var mi = svc.GetType().GetMethod("CalculateThumbnailSize", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Should().NotBeNull();
var size = (Size)mi.Invoke(svc, new object[] { 400, 200, 200, "Larghezza" });
size.Width.Should().Be(200);
size.Height.Should().Be(100);
}
[TestMethod]
public void CalculateThumbnailSize_Altezza_UsesHeightScaling()
{
var svc = CreateService();
var mi = svc.GetType().GetMethod("CalculateThumbnailSize", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Should().NotBeNull();
var size = (Size)mi.Invoke(svc, new object[] { 200, 400, 200, "Altezza" });
size.Width.Should().Be(100);
size.Height.Should().Be(200);
}
[TestMethod]
public void IsSameDirectory_IsCaseInsensitive()
{
var svc = CreateService();
var mi = svc.GetType().GetMethod("IsSameDirectory", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Should().NotBeNull();
bool same = (bool)mi.Invoke(svc, new object[] { @"C:\Temp", @"c:\temp" });
same.Should().BeTrue();
bool notSame = (bool)mi.Invoke(svc, new object[] { @"C:\TempA", @"c:\temp" });
notSame.Should().BeFalse();
}
[TestMethod]
public void UpdateFilenameWithCode_InsertsCodeBeforeExtension()
{
var svc = CreateService(s => s.Codice = "_X");
var mi = svc.GetType().GetMethod("UpdateFilenameWithCode", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Should().NotBeNull();
var state = new ImageState { NomeFileSmall = "photo123.jpg" };
mi.Invoke(svc, new object[] { state });
state.NomeFileSmall.Should().Be("photo123_X.jpg");
}
[DataTestMethod]
[DataRow("SINISTRA")]
[DataRow("CENTRO")]
[DataRow("DESTRA")]
public void CalculateHorizontalAlignment_RespectsAlignment(string alignment)
{
var svc = CreateService(s => { s.Allineamento = alignment; s.Margine = 20; });
var mi = svc.GetType().GetMethod("CalculateHorizontalAlignment", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Should().NotBeNull();
var center = (float)mi.Invoke(svc, new object[] { 800, 100f });
if (alignment == "SINISTRA")
center.Should().BeInRange(0f, 400f, "Expected left alignment range");
if (alignment == "DESTRA")
center.Should().BeInRange(400f, 800f, "Expected right alignment range");
if (alignment == "CENTRO")
center.Should().BeApproximately(800 / 2f, 0.0001f);
}
[TestMethod]
public void SetVerticalPosition_AltoAndBasso_SetExpectedValues()
{
var svc = CreateService(s => s.Posizione = "ALTO");
var mi = svc.GetType().GetMethod("SetVerticalPosition", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Should().NotBeNull();
var state = new ImageState();
// ALTO
mi.Invoke(svc, new object[] { 500, 20f, state });
state.YPosFromBottom1.Should().Be(10f);
state.YPosFromBottom4.Should().Be(10f);
// BASSO
state = new ImageState();
svc = CreateService(s => { s.Posizione = "BASSO"; s.Margine = 10; s.MargVert = 5; });
mi = svc.GetType().GetMethod("SetVerticalPosition", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(svc, new object[] { 200, 20f, state });
var expected1 = (float)(200 - 20 - (200 * 10 / 100.0));
var expected4 = (float)(200 - 20 - (200 * 5 / 100.0));
state.YPosFromBottom1.Should().BeApproximately(expected1, 0.001f);
state.YPosFromBottom4.Should().BeApproximately(expected4, 0.001f);
}
[TestMethod]
public void FormatTimeText_WithAndWithoutFileName_ProducesExpectedStrings()
{
var svc = CreateService();
var mi = svc.GetType().GetMethod("FormatTimeText", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Should().NotBeNull();
var state = new ImageState
{
NomeFileBig = "file.jpg",
TestoOrario = "T:",
DataPartenzaI = new DateTime(2024, 01, 01, 12, 0, 0),
DataFoto = new DateTime(2024, 01, 01, 11, 59, 0)
};
var withoutName = (string)mi.Invoke(svc, new object[] { state, false });
withoutName.Should().StartWith(Environment.NewLine);
withoutName.Should().Contain("T:");
var withName = (string)mi.Invoke(svc, new object[] { state, true });
withName.Should().Contain("file.jpg");
withName.Should().Contain("T:");
withName.Should().Contain(Environment.NewLine);
}
[TestMethod]
public void PrepareSignatureText_SetsSmallSignature_AccordingFlags()
{
var svc = CreateService();
var miPrep = svc.GetType().GetMethod("PrepareSignatureText", BindingFlags.NonPublic | BindingFlags.Instance);
miPrep.Should().NotBeNull();
var state = new ImageState { NomeFileBig = "bigname.jpg" };
svc = CreateService(s => s.TestoMin = true);
miPrep.Invoke(svc, new object[] { state });
state.TestoFirmaPiccola.Should().Be("bigname.jpg");
state.TestoFirmaPiccola = "";
svc = CreateService(s => { s.TestoMin = false; s.AggNumTempMin = true; });
miPrep.Invoke(svc, new object[] { state });
state.TestoFirmaPiccola.Should().Be("bigname.jpg ");
}
[TestMethod]
public void ShouldRenderText_ReturnsCorrectFlag()
{
var svc = CreateService(s => { s.UsaOrarioMiniatura = false; s.TestoMin = false; s.AggTempoGaraMin = false; s.AggNumTempMin = false; });
var mi = svc.GetType().GetMethod("ShouldRenderText", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Should().NotBeNull();
var res = (bool)mi.Invoke(svc, Array.Empty<object>());
res.Should().BeFalse();
svc = CreateService(s => s.TestoMin = true);
mi = svc.GetType().GetMethod("ShouldRenderText", BindingFlags.NonPublic | BindingFlags.Instance);
res = (bool)mi.Invoke(svc, Array.Empty<object>());
res.Should().BeTrue();
}
[TestMethod]
public void FindBestFontSize_And_AdjustFontToFitWidth_ModifySizes()
{
var svc = CreateService(s => { s.IlFont = "Arial"; s.DimStandardMiniatura = 30; });
using var bmp = new Bitmap(400, 100);
using var g = Graphics.FromImage(bmp);
var miFind = svc.GetType().GetMethod("FindBestFontSize", BindingFlags.NonPublic | BindingFlags.Instance);
miFind.Should().NotBeNull();
int best = (int)miFind.Invoke(svc, new object[] { g, "A very long text that won't fit", "Arial", 40, false, 50, 5 });
best.Should().BeInRange(5, 40);
var miAdjust = svc.GetType().GetMethod("AdjustFontToFitWidth", BindingFlags.NonPublic | BindingFlags.Instance);
miAdjust.Should().NotBeNull();
var imageState = new ImageState { DimensioneStandardMiniatura = 30, TestoFirmaPiccola = "A very long test string" };
var initialFont = new Font("Arial", imageState.DimensioneStandardMiniatura);
var textSize = g.MeasureString(imageState.TestoFirmaPiccola, initialFont);
object[] parameters = new object[] { g, 50, imageState, textSize };
miAdjust.Invoke(svc, parameters);
var updatedSize = (SizeF)parameters[3];
imageState.DimensioneStandardMiniatura.Should().BeLessOrEqualTo(30);
(updatedSize.Width <= 50 || imageState.DimensioneStandardMiniatura <= 5).Should().BeTrue();
}
}
}

View file

@ -0,0 +1 @@
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]

View file

@ -0,0 +1,22 @@
<Project Sdk="MSTest.Sdk/4.0.1">
<PropertyGroup>
<TargetFramework>net10.0-windows</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseVSTest>true</UseVSTest>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Moq" Version="4.20.2" />
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.7" />
<PackageReference Include="System.Drawing.Common" Version="9.0.7" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MaddoShared\MaddoShared.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,11 @@
namespace MaddoShared.Tests
{
[TestClass]
public sealed class Test1
{
[TestMethod]
public void TestMethod1()
{
}
}
}