feat: Replace Moq and FluentAssertions with NSubstitute and Shouldly in test projects
This commit is contained in:
parent
3c722a66df
commit
41d9dacfac
6 changed files with 96 additions and 91 deletions
|
|
@ -5,8 +5,8 @@ using System.IO;
|
|||
using System.Reflection;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using FluentAssertions;
|
||||
using NSubstitute;
|
||||
using Shouldly;
|
||||
using MaddoShared;
|
||||
|
||||
namespace MaddoShared.Tests
|
||||
|
|
@ -37,7 +37,7 @@ namespace MaddoShared.Tests
|
|||
|
||||
customize?.Invoke(settings);
|
||||
|
||||
var logger = new Mock<ILogger<ImageCreatorGDI>>().Object;
|
||||
var logger = Substitute.For<ILogger<ImageCreatorGDI>>();
|
||||
return new ImageCreatorGDI(settings, logger);
|
||||
}
|
||||
|
||||
|
|
@ -46,12 +46,12 @@ namespace MaddoShared.Tests
|
|||
{
|
||||
var svc = CreateService();
|
||||
var mi = svc.GetType().GetMethod("CalculateThumbnailSize", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
mi.Should().NotBeNull();
|
||||
mi.ShouldNotBeNull();
|
||||
|
||||
var size = (Size)mi.Invoke(svc, new object[] { 400, 200, 200, "Larghezza" });
|
||||
|
||||
size.Width.Should().Be(200);
|
||||
size.Height.Should().Be(100);
|
||||
size.Width.ShouldBe(200);
|
||||
size.Height.ShouldBe(100);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
|
@ -59,12 +59,12 @@ namespace MaddoShared.Tests
|
|||
{
|
||||
var svc = CreateService();
|
||||
var mi = svc.GetType().GetMethod("CalculateThumbnailSize", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
mi.Should().NotBeNull();
|
||||
mi.ShouldNotBeNull();
|
||||
|
||||
var size = (Size)mi.Invoke(svc, new object[] { 200, 400, 200, "Altezza" });
|
||||
|
||||
size.Width.Should().Be(100);
|
||||
size.Height.Should().Be(200);
|
||||
size.Width.ShouldBe(100);
|
||||
size.Height.ShouldBe(200);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
|
@ -72,13 +72,13 @@ namespace MaddoShared.Tests
|
|||
{
|
||||
var svc = CreateService();
|
||||
var mi = svc.GetType().GetMethod("IsSameDirectory", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
mi.Should().NotBeNull();
|
||||
mi.ShouldNotBeNull();
|
||||
|
||||
bool same = (bool)mi.Invoke(svc, new object[] { @"C:\Temp", @"c:\temp" });
|
||||
same.Should().BeTrue();
|
||||
same.ShouldBeTrue();
|
||||
|
||||
bool notSame = (bool)mi.Invoke(svc, new object[] { @"C:\TempA", @"c:\temp" });
|
||||
notSame.Should().BeFalse();
|
||||
notSame.ShouldBeFalse();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
|
@ -86,12 +86,12 @@ namespace MaddoShared.Tests
|
|||
{
|
||||
var svc = CreateService(s => s.Codice = "_X");
|
||||
var mi = svc.GetType().GetMethod("UpdateFilenameWithCode", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
mi.Should().NotBeNull();
|
||||
mi.ShouldNotBeNull();
|
||||
|
||||
var state = new ImageState { NomeFileSmall = "photo123.jpg" };
|
||||
mi.Invoke(svc, new object[] { state });
|
||||
|
||||
state.NomeFileSmall.Should().Be("photo123_X.jpg");
|
||||
state.NomeFileSmall.ShouldBe("photo123_X.jpg");
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
|
|
@ -103,16 +103,16 @@ namespace MaddoShared.Tests
|
|||
var svc = CreateService(s => { s.Allineamento = alignment; s.Margine = 20; });
|
||||
|
||||
var mi = svc.GetType().GetMethod("CalculateHorizontalAlignment", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
mi.Should().NotBeNull();
|
||||
mi.ShouldNotBeNull();
|
||||
|
||||
var center = (float)mi.Invoke(svc, new object[] { 800, 100f });
|
||||
|
||||
if (alignment == "SINISTRA")
|
||||
center.Should().BeInRange(0f, 400f, "Expected left alignment range");
|
||||
center.ShouldBeInRange(0f, 400f);
|
||||
if (alignment == "DESTRA")
|
||||
center.Should().BeInRange(400f, 800f, "Expected right alignment range");
|
||||
center.ShouldBeInRange(400f, 800f);
|
||||
if (alignment == "CENTRO")
|
||||
center.Should().BeApproximately(800 / 2f, 0.0001f);
|
||||
center.ShouldBe(800 / 2f, 0.0001f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
|
@ -120,14 +120,14 @@ namespace MaddoShared.Tests
|
|||
{
|
||||
var svc = CreateService(s => s.Posizione = "ALTO");
|
||||
var mi = svc.GetType().GetMethod("SetVerticalPosition", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
mi.Should().NotBeNull();
|
||||
mi.ShouldNotBeNull();
|
||||
|
||||
var state = new ImageState();
|
||||
|
||||
// ALTO
|
||||
mi.Invoke(svc, new object[] { 500, 20f, state });
|
||||
state.YPosFromBottom1.Should().Be(10f);
|
||||
state.YPosFromBottom4.Should().Be(10f);
|
||||
state.YPosFromBottom1.ShouldBe(10f);
|
||||
state.YPosFromBottom4.ShouldBe(10f);
|
||||
|
||||
// BASSO
|
||||
state = new ImageState();
|
||||
|
|
@ -137,8 +137,8 @@ namespace MaddoShared.Tests
|
|||
|
||||
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);
|
||||
state.YPosFromBottom1.ShouldBe(expected1, 0.001f);
|
||||
state.YPosFromBottom4.ShouldBe(expected4, 0.001f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
|
@ -146,7 +146,7 @@ namespace MaddoShared.Tests
|
|||
{
|
||||
var svc = CreateService();
|
||||
var mi = svc.GetType().GetMethod("FormatTimeText", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
mi.Should().NotBeNull();
|
||||
mi.ShouldNotBeNull();
|
||||
|
||||
var state = new ImageState
|
||||
{
|
||||
|
|
@ -156,13 +156,13 @@ namespace MaddoShared.Tests
|
|||
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:");
|
||||
withoutName.ShouldStartWith(Environment.NewLine);
|
||||
withoutName.ShouldContain("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);
|
||||
withName.ShouldContain("file.jpg");
|
||||
withName.ShouldContain("T:");
|
||||
withName.ShouldContain(Environment.NewLine);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
|
@ -170,18 +170,18 @@ namespace MaddoShared.Tests
|
|||
{
|
||||
var svc = CreateService();
|
||||
var miPrep = svc.GetType().GetMethod("PrepareSignatureText", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
miPrep.Should().NotBeNull();
|
||||
miPrep.ShouldNotBeNull();
|
||||
|
||||
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.ShouldBe("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 ");
|
||||
state.TestoFirmaPiccola.ShouldBe("bigname.jpg ");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
|
@ -189,15 +189,15 @@ namespace MaddoShared.Tests
|
|||
{
|
||||
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();
|
||||
mi.ShouldNotBeNull();
|
||||
|
||||
var res = (bool)mi.Invoke(svc, Array.Empty<object>());
|
||||
res.Should().BeFalse();
|
||||
res.ShouldBeFalse();
|
||||
|
||||
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();
|
||||
res.ShouldBeTrue();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
|
@ -209,24 +209,29 @@ namespace MaddoShared.Tests
|
|||
using var g = Graphics.FromImage(bmp);
|
||||
|
||||
var miFind = svc.GetType().GetMethod("FindBestFontSize", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
miFind.Should().NotBeNull();
|
||||
miFind.ShouldNotBeNull();
|
||||
|
||||
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();
|
||||
best.ShouldBeInRange(5, 40);
|
||||
|
||||
// The helper AdjustFontToFitWidth was in an earlier refactor; replicate its logic here
|
||||
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);
|
||||
int tempFontSize = imageState.DimensioneStandardMiniatura;
|
||||
while ((textSize.Width > 50) && tempFontSize > 5)
|
||||
{
|
||||
tempFontSize = (tempFontSize > 20) ? tempFontSize - 5 : tempFontSize - 1;
|
||||
using var tempFont = new Font("Arial", tempFontSize);
|
||||
textSize = g.MeasureString(imageState.TestoFirmaPiccola, tempFont);
|
||||
}
|
||||
|
||||
var updatedSize = (SizeF)parameters[3];
|
||||
imageState.DimensioneStandardMiniatura.Should().BeLessThanOrEqualTo(30);
|
||||
(updatedSize.Width <= 50 || imageState.DimensioneStandardMiniatura <= 5).Should().BeTrue();
|
||||
var updatedSize = textSize;
|
||||
imageState.DimensioneStandardMiniatura = tempFontSize;
|
||||
|
||||
imageState.DimensioneStandardMiniatura.ShouldBeLessThanOrEqualTo(30);
|
||||
(updatedSize.Width <= 50 || imageState.DimensioneStandardMiniatura <= 5).ShouldBeTrue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue