From ea59af172f0bfeaad41ed982bfe2c8b889920eab Mon Sep 17 00:00:00 2001 From: Mohammad Moattar Date: Thu, 17 Jun 2021 19:10:42 +0100 Subject: [PATCH] Added support for Width & Height scale to the pictures --- .../DataSheetPicture.cs | 24 ++++++++ .../ClosedXml/DataSheetPictureTests.cs | 57 +++++++++++++++++++ .../Easify.Excel.UnitTests.csproj | 2 + src/Easify.Excel/IDataSheetPicture.cs | 4 ++ 4 files changed, 87 insertions(+) create mode 100644 src/Easify.Excel.UnitTests/ClosedXml/DataSheetPictureTests.cs diff --git a/src/Easify.Excel.ClosedXml/DataSheetPicture.cs b/src/Easify.Excel.ClosedXml/DataSheetPicture.cs index 4175c52..39d3aef 100644 --- a/src/Easify.Excel.ClosedXml/DataSheetPicture.cs +++ b/src/Easify.Excel.ClosedXml/DataSheetPicture.cs @@ -58,8 +58,32 @@ public IDataSheetPicture Scale(double scaleValue) public IDataSheetPicture WithSize(int width, int height) { + if (width <= 0) throw new ArgumentOutOfRangeException(nameof(width)); + if (height <= 0) throw new ArgumentOutOfRangeException(nameof(height)); + _picture.WithSize(width, height); return this; } + + public IDataSheetPicture WithWidth(int width) + { + if (width <= 0) throw new ArgumentOutOfRangeException(nameof(width)); + + var height = width/(decimal)_picture.Width * _picture.Height; + _picture.WithSize(width, (int)height); + return this; + } + + public IDataSheetPicture WithHeight(int height) + { + if (height <= 0) throw new ArgumentOutOfRangeException(nameof(height)); + + var width = (height/(decimal)_picture.Height) * _picture.Width; + _picture.WithSize((int)width, height); + return this; + } + + public int Width => _picture.Width; + public int Height => _picture.Height; } } \ No newline at end of file diff --git a/src/Easify.Excel.UnitTests/ClosedXml/DataSheetPictureTests.cs b/src/Easify.Excel.UnitTests/ClosedXml/DataSheetPictureTests.cs new file mode 100644 index 0000000..454f402 --- /dev/null +++ b/src/Easify.Excel.UnitTests/ClosedXml/DataSheetPictureTests.cs @@ -0,0 +1,57 @@ +using ClosedXML.Excel.Drawings; +using Easify.Excel.ClosedXml; +using FluentAssertions; +using NSubstitute; +using Xunit; + +namespace Easify.Excel.UnitTests.ClosedXml +{ + public class DataSheetPictureTests + { + [Theory] + [InlineData(250, 50)] + [InlineData(100, 20)] + [InlineData(500, 100)] + [InlineData(200, 40)] + [InlineData(180, 36)] + public void Should_WithWidth_CalculateHeightCorrectly(int width, int expectedHeight) + { + // Arrange + var fakePicture = Substitute.For(); + fakePicture.Height.Returns(100); + fakePicture.Width.Returns(500); + + var sut = new DataSheetPicture(fakePicture); + + // Act + var actual = sut.WithWidth(width); + + // Assert + actual.Should().NotBeNull(); + fakePicture.Received(1).WithSize(width, expectedHeight); + } + + [Theory] + [InlineData(50 , 250)] + [InlineData(20, 100)] + [InlineData(100, 500)] + [InlineData(40, 200)] + [InlineData(36, 180)] + public void Should_WithHeight_CalculateWidthCorrectly(int height, int expectedWidth) + { + // Arrange + var fakePicture = Substitute.For(); + fakePicture.Height.Returns(100); + fakePicture.Width.Returns(500); + + var sut = new DataSheetPicture(fakePicture); + + // Act + var actual = sut.WithHeight(height); + + // Assert + actual.Should().NotBeNull(); + fakePicture.Received(1).WithSize(expectedWidth, height); + } + } +} \ No newline at end of file diff --git a/src/Easify.Excel.UnitTests/Easify.Excel.UnitTests.csproj b/src/Easify.Excel.UnitTests/Easify.Excel.UnitTests.csproj index 95a4d5a..4f02992 100644 --- a/src/Easify.Excel.UnitTests/Easify.Excel.UnitTests.csproj +++ b/src/Easify.Excel.UnitTests/Easify.Excel.UnitTests.csproj @@ -4,6 +4,7 @@ false + @@ -14,6 +15,7 @@ + diff --git a/src/Easify.Excel/IDataSheetPicture.cs b/src/Easify.Excel/IDataSheetPicture.cs index 4705b2e..2e4694d 100644 --- a/src/Easify.Excel/IDataSheetPicture.cs +++ b/src/Easify.Excel/IDataSheetPicture.cs @@ -23,5 +23,9 @@ public interface IDataSheetPicture IDataSheetPicture MoveTo(int row, int column); IDataSheetPicture Scale(double scaleValue); IDataSheetPicture WithSize(int width, int height); + IDataSheetPicture WithWidth(int width); + IDataSheetPicture WithHeight(int height); + int Width { get; } + int Height { get; } } } \ No newline at end of file