Skip to content

Commit

Permalink
patterns in Box.Size*. rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenyagusev committed Dec 14, 2019
1 parent a831202 commit be9bca6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
29 changes: 29 additions & 0 deletions src/BookFx.Tests/Renders/BoxColSizesRenderTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace BookFx.Tests.Renders
{
using System.Linq;
using BookFx.Calculation;
using BookFx.Epplus;
using BookFx.Functional;
using BookFx.Renders;
using FluentAssertions;
using Xunit;
Expand Down Expand Up @@ -47,6 +49,17 @@ public void ColSizesRender_SomeColSize_Set(double size, double expected) =>
excelSheet.Column(1).Width.Should().Be(expected);
});

[Fact]
public void ColSizesRender_NoColSizes_NotSet() =>
Packer.OnSheet(excelSheet =>
{
var box = Make.Value().Get.Place();
box.ColSizesRender()(excelSheet);
excelSheet.Column(1).Width.Should().Be(excelSheet.DefaultColWidth);
});

[Fact]
public void ColSizesRender_NoneColSize_Set() =>
Packer.OnSheet(excelSheet =>
Expand Down Expand Up @@ -94,5 +107,21 @@ public void ColSizesRender_TooManyColSizes_Invalid() =>
result.Should().Be(
Invalid<Unit>(Errors.Box.ColSizeCountIsInvalid(sizeCount: 2, boxWidth: 1)));
});

[Fact]
public void ColSizesRender_PatternLessThanBoxWidth_Repeats() =>
Packer.OnSheet(excelSheet =>
{
var box = Make.Value().SizeCols(10, 20).SpanCols(5).Get.Place();
box.ColSizesRender()(excelSheet);
var sizes = Enumerable.Range(1, 5).Map(excelSheet.Column).Map(x => x.Width).ToList();
sizes[0].Should().BeApproximately(10, 1);
sizes[1].Should().BeApproximately(20, 1);
sizes[2].Should().BeApproximately(10, 1);
sizes[3].Should().BeApproximately(20, 1);
sizes[4].Should().BeApproximately(10, 1);
});
}
}
25 changes: 24 additions & 1 deletion src/BookFx.Tests/Renders/BoxRowSizesRenderTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace BookFx.Tests.Renders
{
using System.Linq;
using BookFx.Calculation;
using BookFx.Cores;
using BookFx.Epplus;
using BookFx.Functional;
using BookFx.Renders;
using FluentAssertions;
using Xunit;
Expand All @@ -24,6 +25,17 @@ public void RowSizesRender_SomeRowSize_Set(double size) =>
excelSheet.Row(1).Height.Should().Be(size);
});

[Fact]
public void RowSizesRender_NoRowSizes_NotSet() =>
Packer.OnSheet(excelSheet =>
{
var box = Make.Value().Get.Place();
box.RowSizesRender()(excelSheet);
excelSheet.Row(1).Height.Should().Be(excelSheet.DefaultRowHeight);
});

[Fact]
public void RowSizesRender_NoneRowSize_Set() =>
Packer.OnSheet(excelSheet =>
Expand Down Expand Up @@ -57,5 +69,16 @@ public void RowSizesRender_TooManyRowSizes_Invalid() =>
result.Should().Be(
Invalid<Unit>(Errors.Box.RowSizeCountIsInvalid(sizeCount: 2, boxHeight: 1)));
});

[Fact]
public void RowSizesRender_PatternLessThanBoxHeight_Repeats() =>
Packer.OnSheet(excelSheet =>
{
var box = Make.Value().SizeRows(10, 20).SpanRows(5).Get.Place();
box.RowSizesRender()(excelSheet);
Enumerable.Range(1, 5).Map(excelSheet.Row).Map(x => x.Height).Should().Equal(10, 20, 10, 20, 10);
});
}
}
13 changes: 12 additions & 1 deletion src/BookFx/Renders/BoxColSizesRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
{
using System;
using System.Collections.Generic;
using System.Linq;
using BookFx.Cores;
using BookFx.Epplus;
using BookFx.Functional;
using OfficeOpenXml;
using static BookFx.Functional.F;
using static BookFx.Functional.TeeComposition;

internal static class BoxColSizesRender
Expand All @@ -15,6 +17,11 @@ internal static class BoxColSizesRender
public static Tee<ExcelWorksheet> ColSizesRender(this BoxCore box) =>
excelSheet =>
{
if (box.ColSizes.IsEmpty)
{
return Unit();
}
if (box.ColSizes.Count > box.Placement.Dimension.Width)
{
return Errors.Box.ColSizeCountIsInvalid(
Expand All @@ -26,7 +33,11 @@ public static Tee<ExcelWorksheet> ColSizesRender(this BoxCore box) =>
};

private static IEnumerable<Tee<ExcelWorksheet>> ColSizeRenders(this BoxCore box) =>
box.ColSizes.Map((size, i) => ColSizeRender(size, box.Placement.Position.Col + i));
Enumerable
.Range(0, box.Placement.Dimension.Width)
.Map(offset => ColSizeRender(
box.ColSizes[offset % box.ColSizes.Count],
box.Placement.Position.Col + offset));

private static Tee<ExcelWorksheet> ColSizeRender(TrackSize size, int col) =>
excelSheet => size.ForEach(
Expand Down
16 changes: 13 additions & 3 deletions src/BookFx/Renders/BoxRowSizesRender.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace BookFx.Renders
{
using System.Linq;
using BookFx.Cores;
using BookFx.Functional;
using OfficeOpenXml;
Expand All @@ -10,16 +11,25 @@ internal static class BoxRowSizesRender
public static Tee<ExcelWorksheet> RowSizesRender(this BoxCore box) =>
excelSheet =>
{
if (box.RowSizes.IsEmpty)
{
return Unit();
}
if (box.RowSizes.Count > box.Placement.Dimension.Height)
{
return Errors.Box.RowSizeCountIsInvalid(
sizeCount: box.RowSizes.Count,
boxHeight: box.Placement.Dimension.Height);
}
box.RowSizes.ForEach((size, i) => size.ForEach(
fit: () => excelSheet.Row(box.Placement.Position.Row + i).CustomHeight = false,
some: value => excelSheet.Row(box.Placement.Position.Row + i).Height = value));
Enumerable
.Range(0, box.Placement.Dimension.Height)
.ForEach(offset => box
.RowSizes[offset % box.RowSizes.Count]
.ForEach(
fit: () => excelSheet.Row(box.Placement.Position.Row + offset).CustomHeight = false,
some: value => excelSheet.Row(box.Placement.Position.Row + offset).Height = value));
return Unit();
};
Expand Down

0 comments on commit be9bca6

Please sign in to comment.