Skip to content

Commit 1fd6109

Browse files
committed
Fix the unit testing after new changes from #197 and #199
1 parent 1228eb4 commit 1fd6109

File tree

4 files changed

+49
-28
lines changed

4 files changed

+49
-28
lines changed

src/Html2OpenXml/Expressions/FigureCaptionExpression.cs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using System.Collections.Generic;
1313
using System.Globalization;
1414
using System.Linq;
15-
using AngleSharp.Dom;
1615
using AngleSharp.Html.Dom;
1716
using DocumentFormat.OpenXml;
1817
using DocumentFormat.OpenXml.Wordprocessing;
@@ -31,30 +30,25 @@ public override IEnumerable<OpenXmlElement> Interpret (ParsingContext context)
3130
ComposeStyles(context);
3231
var childElements = Interpret(context.CreateChild(this), node.ChildNodes);
3332

34-
var figNumRef = new List<OpenXmlElement>()
35-
{
33+
var figNumRef = new List<OpenXmlElement>() {
3634
new Run(
3735
new Text("Figure ") { Space = SpaceProcessingModeValues.Preserve }
3836
),
3937
new SimpleField(
4038
new Run(
4139
new Text(AddFigureCaption(context).ToString(CultureInfo.InvariantCulture)))
42-
)
43-
{ Instruction = " SEQ Figure \\* ARABIC " }
40+
) { Instruction = " SEQ Figure \\* ARABIC " }
4441
};
4542

4643

4744
if (!childElements.Any())
4845
{
49-
return
50-
[new Paragraph(figNumRef)
51-
{
52-
ParagraphProperties = new ParagraphProperties
53-
{
54-
ParagraphStyleId = context.DocumentStyle.GetParagraphStyle(context.DocumentStyle.DefaultStyles.CaptionStyle),
55-
KeepNext = DetermineKeepNext(node),
56-
}
57-
}];
46+
return [new Paragraph(figNumRef) {
47+
ParagraphProperties = new ParagraphProperties {
48+
ParagraphStyleId = context.DocumentStyle.GetParagraphStyle(context.DocumentStyle.DefaultStyles.CaptionStyle),
49+
KeepNext = DetermineKeepNext(node),
50+
}
51+
}];
5852
}
5953

6054
//Add the figure number references to the start of the first paragraph.
@@ -69,12 +63,12 @@ [new Paragraph(figNumRef)
6963
}
7064
else
7165
{
72-
//The first child of the figure caption is a table or something. Just prepend a new paragraph with the figure number reference.
73-
childElements =
74-
[
75-
new Paragraph(figNumRef),
76-
..childElements
77-
];
66+
// The first child of the figure caption is a table or something.
67+
// Just prepend a new paragraph with the figure number reference.
68+
childElements = [
69+
new Paragraph(figNumRef),
70+
..childElements
71+
];
7872
}
7973

8074
foreach (var paragraph in childElements.OfType<Paragraph>())
@@ -118,8 +112,7 @@ private static int AddFigureCaption(ParsingContext context)
118112
/// <summary>
119113
/// Determines whether the KeepNext property should apply this this caption.
120114
/// </summary>
121-
/// <param name="node"></param>
122-
/// <returns>A new <see cref="KeepNext"/> or null./></returns>
115+
/// <returns>A new <see cref="KeepNext"/> or null.</returns>
123116
private static KeepNext? DetermineKeepNext(IHtmlElement node)
124117
{
125118
// A caption at the end of a figure will have no next sibling.

test/HtmlToOpenXml.Tests/ElementTests.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public void PhrasingTag_ReturnsRunWithDefaultStyle<T> (string html) where T : Op
2828
[TestCase(@"<sup>Superscript</sup>", ExpectedResult = "superscript")]
2929
public string? SubSup_ReturnsRunWithVerticalAlignment (string html)
3030
{
31-
//var val = new VerticalPositionValues(tagName);
3231
var textAlign = ParsePhrasing<VerticalTextAlignment>(html);
3332
Assert.That(textAlign.Val?.HasValue, Is.True);
3433
return textAlign.Val.InnerText;
@@ -154,9 +153,32 @@ public void FigCaption_ReturnsRunWithSimpleField()
154153

155154
Assert.Multiple(() =>
156155
{
157-
Assert.That(elements[0].ChildElements, Has.Count.EqualTo(3));
158156
Assert.That(elements[0].HasChild<Run>(), Is.True);
159157
Assert.That(elements[0].HasChild<SimpleField>(), Is.True);
158+
Assert.That(elements[0].Elements<Run>().Count(), Is.EqualTo(3));
159+
Assert.That(elements[0].GetFirstChild<ParagraphProperties>()!.KeepNext, Is.Null);
160+
Assert.That(elements[0].GetFirstChild<SimpleField>()!.Instruction?.Value, Does.Contain("SEQ Figure \\* ARABIC"));
161+
});
162+
}
163+
164+
[Test]
165+
public void FigCaption_WithHeading_ReturnsParagraphWithSimpleField()
166+
{
167+
var elements = converter.Parse(@"<figcaption>
168+
<h2>Puppy School</h2>
169+
<p>Championship Class of 2016</p>
170+
</figcaption>");
171+
172+
Assert.That(elements, Has.Count.EqualTo(2));
173+
Assert.That(elements, Is.All.TypeOf<Paragraph>());
174+
175+
Assert.Multiple(() =>
176+
{
177+
Assert.That(elements[0].HasChild<Run>(), Is.True);
178+
Assert.That(elements[0].HasChild<SimpleField>(), Is.True);
179+
Assert.That(elements[0].GetFirstChild<ParagraphProperties>()!.KeepNext, Is.Not.Null);
180+
Assert.That(elements[1].HasChild<Run>(), Is.True);
181+
Assert.That(elements[1].HasChild<SimpleField>(), Is.False);
160182
});
161183
}
162184

test/HtmlToOpenXml.Tests/LinkTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void ImageFigcaptionLink_ReturnsHyperlinkWithTextAndImage ()
6161
Assert.That(elements[0].FirstChild, Is.TypeOf(typeof(Hyperlink)));
6262

6363
var hyperlink = (Hyperlink) elements[0].FirstChild;
64-
Assert.That(hyperlink.ChildElements, Has.Count.EqualTo(4));
64+
Assert.That(hyperlink.ChildElements, Has.Count.EqualTo(6));
6565
Assert.That(hyperlink.ChildElements, Has.All.TypeOf(typeof(Run)), "Hyperlinks don't accept inner paragraphs");
6666
Assert.That(hyperlink.Descendants<Drawing>(), Is.Not.Null);
6767
}

test/HtmlToOpenXml.Tests/TableTests.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public void CellSpacing_ReturnsTableCellWithSpacing()
247247
}
248248

249249
[TestCaseSource(nameof(BorderWidthCases))]
250-
public void HtmlBorders_ShouldSucceed(string borderAtrribute, IEnumerable<string> expectedBorderValue, IEnumerable<uint?> expectedBorderWidth)
250+
public void HtmlBorders_ShouldSucceed(string borderAtrribute, IEnumerable<string>? expectedBorderValue, IEnumerable<uint?>? expectedBorderWidth)
251251
{
252252
// we specify a style which doesn't handle borders
253253
converter.HtmlStyles.AddStyle(new Style {
@@ -260,6 +260,12 @@ public void HtmlBorders_ShouldSucceed(string borderAtrribute, IEnumerable<string
260260
Assert.That(elements, Has.Count.EqualTo(1));
261261
Assert.That(elements, Has.All.TypeOf<Table>());
262262
var borders = elements[0].GetFirstChild<TableProperties>()?.TableBorders;
263+
if (expectedBorderValue is null)
264+
{
265+
Assert.That(borders, Is.Null);
266+
return;
267+
}
268+
263269
Assert.That(borders, Is.Not.Null);
264270
Assert.That(borders.HasChild<BorderType>(), Is.True);
265271
Assert.That(new string?[] { borders.TopBorder?.Val?.InnerText,
@@ -289,8 +295,8 @@ public void HtmlBorders_ShouldSucceed(string borderAtrribute, IEnumerable<string
289295
static readonly object[] BorderWidthCases =
290296
[
291297
// Negative border should be considered as zero
292-
new object[] { "border='-1'", Enumerable.Repeat("none", 6), null! },
293-
new object[] { "border='0'", Enumerable.Repeat("none", 6), null! },
298+
new object[] { "border='-1'", null!, null! },
299+
new object[] { "border='0'", null!, null! },
294300
new object[] { "border='1'",
295301
new string[] { "none", "none", "none", "none", "single", "single" },
296302
new uint?[] { null, null, null, null, 14, 14 } },

0 commit comments

Comments
 (0)