Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Width & Height scale to the pictures #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Easify.Excel.ClosedXml/DataSheetPicture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
57 changes: 57 additions & 0 deletions src/Easify.Excel.UnitTests/ClosedXml/DataSheetPictureTests.cs
Original file line number Diff line number Diff line change
@@ -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<IXLPicture>();
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<IXLPicture>();
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);
}
}
}
2 changes: 2 additions & 0 deletions src/Easify.Excel.UnitTests/Easify.Excel.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ClosedXML" Version="0.95.4" />
<PackageReference Include="Easify.Testing" Version="1.1.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand All @@ -14,6 +15,7 @@
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Easify.Excel.ClosedXml\Easify.Excel.ClosedXml.csproj" />
<ProjectReference Include="..\Easify.Excel\Easify.Excel.csproj" />
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions src/Easify.Excel/IDataSheetPicture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}