Skip to content

Commit

Permalink
Add more tests for SpacingBefore and SpacingAfter properties
Browse files Browse the repository at this point in the history
  • Loading branch information
y0ung3r committed Apr 26, 2024
1 parent 4908fd4 commit 437366a
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static ParagraphFormat Default
= HorizontalAlignment.Left;

public IParagraphSpacing SpacingBefore { get; set; }
= ParagraphSpacing.Exactly<Points>(0);
= ParagraphSpacing.Exactly<Points>(0.0);

public IParagraphSpacing SpacingAfter { get; set; }
= ParagraphSpacing.Exactly<Points>(8.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,26 @@ public void Should_write_body_element_properly()
var expectedXml = new XElement(OpenXmlNamespaces.Word + "document",
new XElement(OpenXmlNamespaces.Word + "body",
new XElement(OpenXmlNamespaces.Word + "p",
new XElement(OpenXmlNamespaces.Word + "pPr")),
new XElement(OpenXmlNamespaces.Word + "pPr",
new XElement(OpenXmlNamespaces.Word + "spacing",
new XAttribute(OpenXmlNamespaces.Word + "before", "0"),
new XAttribute(OpenXmlNamespaces.Word + "after", "160")))),
new XElement(OpenXmlNamespaces.Word + "p",
new XElement(OpenXmlNamespaces.Word + "pPr")),
new XElement(OpenXmlNamespaces.Word + "pPr",
new XElement(OpenXmlNamespaces.Word + "spacing",
new XAttribute(OpenXmlNamespaces.Word + "before", "0"),
new XAttribute(OpenXmlNamespaces.Word + "after", "160")))),
new XElement(OpenXmlNamespaces.Word + "p",
new XElement(OpenXmlNamespaces.Word + "sectPr"),
new XElement(OpenXmlNamespaces.Word + "pPr")),
new XElement(OpenXmlNamespaces.Word + "pPr",
new XElement(OpenXmlNamespaces.Word + "spacing",
new XAttribute(OpenXmlNamespaces.Word + "before", "0"),
new XAttribute(OpenXmlNamespaces.Word + "after", "160")))),
new XElement(OpenXmlNamespaces.Word + "p",
new XElement(OpenXmlNamespaces.Word + "pPr")),
new XElement(OpenXmlNamespaces.Word + "pPr",
new XElement(OpenXmlNamespaces.Word + "spacing",
new XAttribute(OpenXmlNamespaces.Word + "before", "0"),
new XAttribute(OpenXmlNamespaces.Word + "after", "160")))),
new XElement(OpenXmlNamespaces.Word + "sectPr")));

var sut = new OpenXmlElementWriter(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Xml.Linq;
using FluentAssertions;
using OfficeFlow.MeasureUnits.Absolute;
using OfficeFlow.Word.Core.Elements.Paragraphs;
using OfficeFlow.Word.Core.Elements.Paragraphs.Enums;
using OfficeFlow.Word.Core.Elements.Paragraphs.Spacing;
using Xunit;

namespace OfficeFlow.Word.OpenXml.Tests.OpenXmlElementWriterTests;
Expand All @@ -18,7 +20,10 @@ public void Should_write_horizontal_alignment_properly(string expectedValue, Hor
// Arrange
var expectedXml = new XElement(OpenXmlNamespaces.Word + "pPr",
new XElement(OpenXmlNamespaces.Word + "jc",
new XAttribute(OpenXmlNamespaces.Word + "val", expectedValue)));
new XAttribute(OpenXmlNamespaces.Word + "val", expectedValue)),
new XElement(OpenXmlNamespaces.Word + "spacing",
new XAttribute(OpenXmlNamespaces.Word + "before", "0"),
new XAttribute(OpenXmlNamespaces.Word + "after", "160")));

var sut = new OpenXmlElementWriter(
new XElement(OpenXmlNamespaces.Word + "pPr"));
Expand All @@ -37,11 +42,145 @@ public void Should_write_horizontal_alignment_properly(string expectedValue, Hor
.Be(expectedXml);
}

[Fact]
public void Should_write_paragraph_spacing_properly()
{
// Arrange
var expectedXml = new XElement(OpenXmlNamespaces.Word + "pPr",
new XElement(OpenXmlNamespaces.Word + "spacing",
new XAttribute(OpenXmlNamespaces.Word + "beforeAutospacing", "true"),
new XAttribute(OpenXmlNamespaces.Word + "after", "240")));

var sut = new OpenXmlElementWriter(
new XElement(OpenXmlNamespaces.Word + "pPr"));

var paragraphFormat = new ParagraphFormat
{
SpacingBefore = ParagraphSpacing.Auto,
SpacingAfter = ParagraphSpacing.Exactly<Points>(12.0)
};

// Act
sut.Visit(paragraphFormat);

// Assert
sut.Xml
.Should()
.Be(expectedXml);
}

[Fact]
public void Spacing_before_should_be_calculated_automatically()
{
// Arrange
var expectedXml = new XElement(OpenXmlNamespaces.Word + "pPr",
new XElement(OpenXmlNamespaces.Word + "spacing",
new XAttribute(OpenXmlNamespaces.Word + "beforeAutospacing", "true"),
new XAttribute(OpenXmlNamespaces.Word + "after", "160")));

var sut = new OpenXmlElementWriter(
new XElement(OpenXmlNamespaces.Word + "pPr"));

var paragraphFormat = new ParagraphFormat
{
SpacingBefore = ParagraphSpacing.Auto
};

// Act
sut.Visit(paragraphFormat);

// Assert
sut.Xml
.Should()
.Be(expectedXml);
}

[Fact]
public void Should_write_exact_value_of_spacing_before_properly()
{
// Arrange
var expectedXml = new XElement(OpenXmlNamespaces.Word + "pPr",
new XElement(OpenXmlNamespaces.Word + "spacing",
new XAttribute(OpenXmlNamespaces.Word + "before", "240"),
new XAttribute(OpenXmlNamespaces.Word + "after", "160")));

var sut = new OpenXmlElementWriter(
new XElement(OpenXmlNamespaces.Word + "pPr"));

var paragraphFormat = new ParagraphFormat
{
SpacingBefore = ParagraphSpacing.Exactly<Points>(12.0)
};

// Act
sut.Visit(paragraphFormat);

// Assert
sut.Xml
.Should()
.Be(expectedXml);
}

[Fact]
public void Spacing_after_should_be_calculated_automatically()
{
// Arrange
var expectedXml = new XElement(OpenXmlNamespaces.Word + "pPr",
new XElement(OpenXmlNamespaces.Word + "spacing",
new XAttribute(OpenXmlNamespaces.Word + "before", "0"),
new XAttribute(OpenXmlNamespaces.Word + "afterAutospacing", "true")));

var sut = new OpenXmlElementWriter(
new XElement(OpenXmlNamespaces.Word + "pPr"));

var paragraphFormat = new ParagraphFormat
{
SpacingAfter = ParagraphSpacing.Auto
};

// Act
sut.Visit(paragraphFormat);

// Assert
sut.Xml
.Should()
.Be(expectedXml);
}

[Fact]
public void Should_write_exact_value_of_spacing_after_properly()
{
// Arrange
var expectedXml = new XElement(OpenXmlNamespaces.Word + "pPr",
new XElement(OpenXmlNamespaces.Word + "spacing",
new XAttribute(OpenXmlNamespaces.Word + "before", "0"),
new XAttribute(OpenXmlNamespaces.Word + "after", "240")));

var sut = new OpenXmlElementWriter(
new XElement(OpenXmlNamespaces.Word + "pPr"));

var paragraphFormat = new ParagraphFormat
{
SpacingAfter = ParagraphSpacing.Exactly<Points>(12.0)
};

// Act
sut.Visit(paragraphFormat);

// Assert
sut.Xml
.Should()
.Be(expectedXml);
}

[Fact]
public void Default_value_of_horizontal_alignment_should_not_be_written()
{
// Arrange
var expectedXml = new XElement(OpenXmlNamespaces.Word + "pPr");
var expectedXml = new XElement(OpenXmlNamespaces.Word + "pPr",
new XElement(OpenXmlNamespaces.Word + "spacing",
new XAttribute(OpenXmlNamespaces.Word + "before", "0"),
new XAttribute(OpenXmlNamespaces.Word + "after", "160")));

var sut = new OpenXmlElementWriter(
new XElement(OpenXmlNamespaces.Word + "pPr"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public void Should_write_paragraph_format_properly()
{
// Assert
var expectedXml = new XElement(OpenXmlNamespaces.Word + "p",
new XElement(OpenXmlNamespaces.Word + "pPr"));
new XElement(OpenXmlNamespaces.Word + "pPr",
new XElement(OpenXmlNamespaces.Word + "spacing",
new XAttribute(OpenXmlNamespaces.Word + "before", "0"),
new XAttribute(OpenXmlNamespaces.Word + "after", "160"))));

var sut = new OpenXmlElementWriter(
new XElement(OpenXmlNamespaces.Word + "p"));
Expand All @@ -34,7 +37,10 @@ public void Should_write_runs_properly()
{
// Assert
var expectedXml = new XElement(OpenXmlNamespaces.Word + "p",
new XElement(OpenXmlNamespaces.Word + "pPr"),
new XElement(OpenXmlNamespaces.Word + "pPr",
new XElement(OpenXmlNamespaces.Word + "spacing",
new XAttribute(OpenXmlNamespaces.Word + "before", "0"),
new XAttribute(OpenXmlNamespaces.Word + "after", "160"))),
new XElement(OpenXmlNamespaces.Word + "r",
new XElement(OpenXmlNamespaces.Word + "rPr")),
new XElement(OpenXmlNamespaces.Word + "r",
Expand Down
2 changes: 1 addition & 1 deletion src/Word/OfficeFlow.Word.OpenXml/OpenXmlElementWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private void VisitSpacing(ParagraphFormat paragraphFormat)
spacingXml.Add(valueXml);
}

if (spacingXml.IsEmpty)
if (!spacingXml.HasAttributes)
return;

Xml.Add(spacingXml);
Expand Down

0 comments on commit 437366a

Please sign in to comment.