Skip to content

Commit d2264b5

Browse files
committed
Rename unit testing to make the output more obvious
1 parent 999bafb commit d2264b5

24 files changed

+512
-463
lines changed

src/Html2OpenXml/WordDocumentStyle.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ public sealed class WordDocumentStyle
2727
public event EventHandler<StyleEventArgs>? StyleMissing;
2828

2929
private readonly MainDocumentPart mainPart;
30-
private readonly OpenXmlDocumentStyleCollection knownStyles = new();
31-
private readonly ISet<string> lazyPredefinedStyles;
30+
private readonly OpenXmlDocumentStyleCollection knownStyles = [];
31+
private readonly HashSet<string> lazyPredefinedStyles;
3232

3333
private DefaultStyles? defaultStyles;
3434

3535

3636
internal WordDocumentStyle(MainDocumentPart mainPart)
3737
{
3838
PrepareStyles(mainPart);
39-
lazyPredefinedStyles = new HashSet<string>() {
39+
lazyPredefinedStyles = [
4040
PredefinedStyles.Caption,
4141
PredefinedStyles.EndnoteReference,
4242
PredefinedStyles.EndnoteText,
@@ -54,7 +54,7 @@ internal WordDocumentStyle(MainDocumentPart mainPart)
5454
PredefinedStyles.Quote,
5555
PredefinedStyles.QuoteChar,
5656
PredefinedStyles.TableGrid
57-
};
57+
];
5858
this.mainPart = mainPart;
5959
}
6060

test/HtmlToOpenXml.Tests/AbbrTests.cs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class AbbrTests : HtmlConverterTestBase
1414
[TestCase(@"<abbr title='National Aeronautics and Space Administration'>NASA</abbr>")]
1515
[TestCase(@"<acronym title='National Aeronautics and Space Administration'>NASA</acronym>")]
1616
[TestCase(@"<acronym title='www.nasa.gov'>NASA</acronym>")]
17-
public void ParseAbbr(string html)
17+
public void WithTitle_ReturnsFootnote(string html)
1818
{
1919
var elements = converter.Parse(html);
2020
Assert.That(elements, Has.Count.EqualTo(1));
@@ -24,17 +24,17 @@ public void ParseAbbr(string html)
2424
Assert.That(elements[0].InnerText, Is.EqualTo("NASA"));
2525
});
2626

27-
var noteRef = elements[0].GetLastChild<Run>().GetFirstChild<FootnoteReference>();
27+
var noteRef = elements[0].GetLastChild<Run>()?.GetFirstChild<FootnoteReference>();
2828
Assert.That(noteRef, Is.Not.Null);
2929
Assert.Multiple(() =>
3030
{
31-
Assert.That(noteRef.Id.HasValue, Is.EqualTo(true));
31+
Assert.That(noteRef.Id?.HasValue, Is.EqualTo(true));
3232
Assert.That(mainPart.FootnotesPart, Is.Not.Null);
3333
});
3434

3535
Assert.That(mainPart.FootnotesPart.HyperlinkRelationships.Count(), Is.EqualTo(0));
3636

37-
var fnotes = mainPart.FootnotesPart.Footnotes.Elements<Footnote>().FirstOrDefault(f => f.Id.Value == noteRef.Id.Value);
37+
var fnotes = mainPart.FootnotesPart.Footnotes.Elements<Footnote>().FirstOrDefault(f => f.Id?.Value == noteRef.Id.Value);
3838
Assert.That(fnotes, Is.Not.Null);
3939
}
4040

@@ -43,7 +43,7 @@ public void ParseAbbr(string html)
4343
[TestCase(@"<abbr title='\\server01\share\NASA.html'>NASA</abbr>", "file://server01/share/NASA.html")]
4444
[TestCase(@"<abbr title='ftp://server01/share/NASA.html'>NASA</abbr>", "ftp://server01/share/NASA.html")]
4545
[TestCase(@"<blockquote cite='https://en.wikipedia.org/wiki/NASA'>NASA</blockquote>", "https://en.wikipedia.org/wiki/NASA")]
46-
public void ParseWithLinks(string html, string expectedUri)
46+
public void WithLink_ReturnsFootnote_WithHyperlink(string html, string expectedUri)
4747
{
4848
var elements = converter.Parse(html);
4949
Assert.That(elements, Has.Count.EqualTo(1));
@@ -53,18 +53,18 @@ public void ParseWithLinks(string html, string expectedUri)
5353
Assert.That(elements[0].InnerText, Is.EqualTo("NASA"));
5454
});
5555

56-
var noteRef = elements[0].GetLastChild<Run>().GetFirstChild<FootnoteReference>();
56+
var noteRef = elements[0].GetLastChild<Run>()?.GetFirstChild<FootnoteReference>();
5757
Assert.That(noteRef, Is.Not.Null);
5858
Assert.Multiple(() =>
5959
{
60-
Assert.That(noteRef.Id.HasValue, Is.EqualTo(true));
60+
Assert.That(noteRef.Id?.HasValue, Is.EqualTo(true));
6161
Assert.That(mainPart.FootnotesPart, Is.Not.Null);
6262
});
6363

64-
var fnotes = mainPart.FootnotesPart.Footnotes.Elements<Footnote>().FirstOrDefault(f => f.Id.Value == noteRef.Id.Value);
64+
var fnotes = mainPart.FootnotesPart.Footnotes.Elements<Footnote>().FirstOrDefault(f => f.Id?.Value == noteRef.Id.Value);
6565
Assert.That(fnotes, Is.Not.Null);
6666

67-
var link = fnotes.FirstChild.GetFirstChild<Hyperlink>();
67+
var link = fnotes.FirstChild?.GetFirstChild<Hyperlink>();
6868
Assert.That(link, Is.Not.Null);
6969

7070
var extLink = mainPart.FootnotesPart.HyperlinkRelationships.FirstOrDefault(r => r.Id == link.Id);
@@ -77,25 +77,25 @@ public void ParseWithLinks(string html, string expectedUri)
7777
}
7878

7979
[Test]
80-
public void ParseDocumentEnd()
80+
public void WithPositionToDocumentEnd_ReturnsEndnote()
8181
{
8282
converter.AcronymPosition = AcronymPosition.DocumentEnd;
8383
var elements = converter.Parse(@"<acronym title='www.nasa.gov'>NASA</acronym>");
8484

85-
var noteRef = elements[0].GetLastChild<Run>().GetFirstChild<EndnoteReference>();
85+
var noteRef = elements[0].GetLastChild<Run>()?.GetFirstChild<EndnoteReference>();
8686
Assert.That(noteRef, Is.Not.Null);
8787
Assert.Multiple(() =>
8888
{
89-
Assert.That(noteRef.Id.HasValue, Is.EqualTo(true));
89+
Assert.That(noteRef.Id?.HasValue, Is.EqualTo(true));
9090
Assert.That(mainPart.EndnotesPart, Is.Not.Null);
9191
});
9292

93-
var fnotes = mainPart.EndnotesPart.Endnotes.Elements<Endnote>().FirstOrDefault(f => f.Id.Value == noteRef.Id.Value);
93+
var fnotes = mainPart.EndnotesPart.Endnotes.Elements<Endnote>().FirstOrDefault(f => f.Id?.Value == noteRef.Id.Value);
9494
Assert.That(fnotes, Is.Not.Null);
9595
}
9696

9797
[Test]
98-
public void ParseIgnore()
98+
public void Empty_ShouldBeIgnored()
9999
{
100100
var elements = converter.Parse("<abbr></abbr>");
101101
Assert.That(elements, Is.Empty);
@@ -104,72 +104,76 @@ public void ParseIgnore()
104104
[TestCase("<abbr><a href='www.google.com'>Placeholder</a></abbr>")]
105105
[TestCase("<abbr>Placeholder</abbr>")]
106106
[TestCase("<blockquote>Placeholder</blockquote>")]
107-
public void ParseNoDescription(string html)
107+
public void WithNoDescription_ReturnsSimpleParagraph(string html)
108108
{
109109
// description nor title was defined - fallback to normal run
110110
var elements = converter.Parse(html);
111111
Assert.That(elements, Has.Count.EqualTo(1));
112+
Assert.That(elements, Is.All.TypeOf<Paragraph>());
112113
}
113114

114115
[TestCase("<abbr title='HyperText Markup Language'>HTML</abbr>", AcronymPosition.DocumentEnd, Description = "Read existing endnotes references")]
115116
[TestCase("<abbr title='HyperText Markup Language'>HTML</abbr>", AcronymPosition.PageEnd, Description = "Read existing footnotes references")]
116117
[TestCase("<blockquote cite='HyperText Markup Language'>HTML</blockquote>", AcronymPosition.DocumentEnd, Description = "Read existing endnotes references")]
117118
[TestCase("<blockquote cite='HyperText Markup Language'>HTML</blockquote>", AcronymPosition.PageEnd, Description = "Read existing footnotes references")]
118-
public void ParseExistingEndnotes(string html, AcronymPosition acronymPosition)
119+
public void WithExistingEndnotes_ReturnsUniqueRefId(string html, AcronymPosition acronymPosition)
119120
{
120121
using var generatedDocument = new MemoryStream();
121122
using (var buffer = ResourceHelper.GetStream("Resources.DocWithNotes.docx"))
122123
buffer.CopyTo(generatedDocument);
123124

124125
generatedDocument.Position = 0L;
125126
using WordprocessingDocument package = WordprocessingDocument.Open(generatedDocument, true);
126-
MainDocumentPart mainPart = package.MainDocumentPart;
127-
HtmlConverter converter = new(mainPart);
128-
converter.AcronymPosition = acronymPosition;
127+
MainDocumentPart mainPart = package.MainDocumentPart!;
128+
HtmlConverter converter = new(mainPart)
129+
{
130+
AcronymPosition = acronymPosition
131+
};
129132

130133
var elements = converter.Parse(html);
131134
Assert.That(elements, Has.Count.EqualTo(1));
132135

133-
FootnoteEndnoteReferenceType noteRef;
136+
FootnoteEndnoteReferenceType? noteRef;
134137

135138
if (acronymPosition == AcronymPosition.PageEnd)
136139
{
137-
noteRef = elements[0].GetLastChild<Run>().GetFirstChild<FootnoteReference>();
138-
Assert.That(mainPart.FootnotesPart.Footnotes.Elements<Footnote>().Select(fn => fn.Id.Value), Is.Unique);
140+
noteRef = elements[0].GetLastChild<Run>()?.GetFirstChild<FootnoteReference>();
141+
Assert.That(mainPart.FootnotesPart!.Footnotes.Elements<Footnote>().Select(fn => fn.Id?.Value), Is.Unique);
139142
}
140143
else
141144
{
142-
noteRef = elements[0].GetLastChild<Run>().GetFirstChild<EndnoteReference>();
143-
Assert.That(mainPart.EndnotesPart.Endnotes.Elements<Endnote>().Select(fn => fn.Id.Value), Is.Unique);
145+
noteRef = elements[0].GetLastChild<Run>()?.GetFirstChild<EndnoteReference>();
146+
Assert.That(mainPart.EndnotesPart!.Endnotes.Elements<Endnote>().Select(fn => fn.Id?.Value), Is.Unique);
144147
}
145148

146149
Assert.That(noteRef, Is.Not.Null);
147-
Assert.That(noteRef.Id.HasValue, Is.EqualTo(true));
150+
Assert.That(noteRef.Id?.HasValue, Is.EqualTo(true));
148151

149-
FootnoteEndnoteType note;
152+
FootnoteEndnoteType? note;
150153
if (acronymPosition == AcronymPosition.PageEnd)
151154
{
152-
note = mainPart.FootnotesPart.Footnotes.Elements<Footnote>()
153-
.FirstOrDefault(fn => fn.Id.Value == noteRef.Id.Value);
155+
note = mainPart.FootnotesPart!.Footnotes.Elements<Footnote>()
156+
.FirstOrDefault(fn => fn.Id?.Value == noteRef.Id.Value);
154157
}
155158
else
156159
{
157-
note = mainPart.EndnotesPart.Endnotes.Elements<Endnote>()
158-
.FirstOrDefault(fn => fn.Id.Value == noteRef.Id.Value);
160+
note = mainPart.EndnotesPart!.Endnotes.Elements<Endnote>()
161+
.FirstOrDefault(fn => fn.Id?.Value == noteRef.Id.Value);
159162
}
163+
Assert.That(note, Is.Not.Null);
160164
Assert.That(note.InnerText, Is.EqualTo(" " + "HyperText Markup Language"));
161165
}
162166

163167
[Test]
164-
public void ParseInline()
168+
public void InsideParagraph_ReturnsMultipleRuns()
165169
{
166170
var elements = converter.Parse(@"<p>The
167171
<abbr title='National Aeronautics and Space Administration'>NASA</abbr>
168172
is an independent agency of the U.S. federal government responsible for the civil space program, aeronautics research, and space research.</p>");
169173
Assert.That(elements, Has.Count.EqualTo(1));
170174
Assert.Multiple(() => {
171175
Assert.That(elements[0], Is.TypeOf(typeof(Paragraph)));
172-
Assert.That(elements[0].Elements<Run>().Count(), Is.GreaterThan(2));
176+
Assert.That(elements[0].Elements<Run>().Count(), Is.EqualTo(6), "3 textual runs + 3 breaks");
173177
Assert.That(elements[0].Elements<Run>().Any(r => r.HasChild<FootnoteReference>()), Is.True);
174178
});
175179
}

test/HtmlToOpenXml.Tests/BodyTests.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using NUnit.Framework;
2+
using DocumentFormat.OpenXml.Wordprocessing;
3+
using DocumentFormat.OpenXml.Packaging;
4+
5+
namespace HtmlToOpenXml.Tests
6+
{
7+
/// <summary>
8+
/// Tests on <c>body</c> elements.
9+
/// </summary>
10+
[TestFixture]
11+
public class BodyTests : HtmlConverterTestBase
12+
{
13+
[TestCase("landscape", ExpectedResult = true)]
14+
[TestCase("portrait", ExpectedResult = false)]
15+
public bool PageOrientation_ReturnsLandscapeDimension(string orientation)
16+
{
17+
var _ = converter.Parse($@"<body style=""page-orientation:{orientation}""><body>");
18+
var sectionProperties = mainPart.Document.Body!.GetFirstChild<SectionProperties>();
19+
Assert.That(sectionProperties, Is.Not.Null);
20+
var pageSize = sectionProperties.GetFirstChild<PageSize>();
21+
Assert.That(pageSize, Is.Not.Null);
22+
return pageSize.Width > pageSize.Height;
23+
}
24+
25+
[TestCase("portrait", ExpectedResult = true)]
26+
[TestCase("landscape", ExpectedResult = false)]
27+
public bool PageOrientation_OverrideExistingLayout_ReturnsLandscapeDimension(string orientation)
28+
{
29+
using var generatedDocument = new MemoryStream();
30+
using (var buffer = ResourceHelper.GetStream("Resources.DocWithLandscape.docx"))
31+
buffer.CopyTo(generatedDocument);
32+
33+
generatedDocument.Position = 0L;
34+
using WordprocessingDocument package = WordprocessingDocument.Open(generatedDocument, true);
35+
MainDocumentPart mainPart = package.MainDocumentPart!;
36+
HtmlConverter converter = new(mainPart);
37+
38+
var _ = converter.Parse($@"<body style=""page-orientation:{orientation}""><body>");
39+
var sectionProperties = mainPart.Document.Body!.GetFirstChild<SectionProperties>();
40+
Assert.That(sectionProperties, Is.Not.Null);
41+
var pageSize = sectionProperties.GetFirstChild<PageSize>();
42+
Assert.That(pageSize, Is.Not.Null);
43+
return pageSize.Height > pageSize.Width;
44+
}
45+
}
46+
}
Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
using NUnit.Framework;
22
using DocumentFormat.OpenXml.Wordprocessing;
3-
using DocumentFormat.OpenXml.Packaging;
43

54
namespace HtmlToOpenXml.Tests
65
{
76
/// <summary>
87
/// Tests on <c>div</c> and other block elements.
98
/// </summary>
109
[TestFixture]
11-
public class FlowTests : HtmlConverterTestBase
10+
public class DivTests : HtmlConverterTestBase
1211
{
1312
[Test]
14-
public void ParseStyles()
13+
public void StyleAttribute_WithMultipleValues_ShouldBeAllApplied()
1514
{
1615
var elements = converter.Parse(@"<div style='text-indent:1em;border:1px dotted red;text-align:center'>Lorem</div>");
1716
Assert.That(elements, Has.Count.EqualTo(1));
1817
Assert.That(elements, Has.All.TypeOf<Paragraph>());
19-
var p = elements[0] as Paragraph;
18+
var p = (Paragraph) elements[0];
2019
Assert.Multiple(() =>
2120
{
2221
Assert.That(p.ParagraphProperties?.Indentation?.FirstLine?.HasValue, Is.True);
2322
Assert.That(p.ParagraphProperties?.ParagraphBorders, Is.Not.Null);
2423
Assert.That(p.ParagraphProperties?.Justification?.Val?.Value, Is.EqualTo(JustificationValues.Center));
2524
});
2625

27-
var borders = p.ParagraphProperties?.ParagraphBorders.Elements<BorderType>();
26+
var borders = p.ParagraphProperties?.ParagraphBorders?.Elements<BorderType>();
27+
Assert.That(borders, Is.Not.Null);
2828
Assert.Multiple(() =>
2929
{
3030
Assert.That(borders.Count(), Is.EqualTo(4));
@@ -34,7 +34,7 @@ public void ParseStyles()
3434
}
3535

3636
[Test]
37-
public void ParsePageBreakBefore()
37+
public void PageBreakBefore_ReturnsOneParagraphThenTwo()
3838
{
3939
var elements = converter.Parse(@"Lorem
4040
<div style='page-break-before:always'>Placeholder</div>
@@ -62,7 +62,7 @@ public void ParsePageBreakBefore()
6262
}
6363

6464
[Test]
65-
public void ParsePageBreakAfter()
65+
public void PageBreakAfter_ReturnsTwoParagraphsThenOne()
6666
{
6767
var elements = converter.Parse(@"Lorem
6868
<div style='page-break-after:always'>Placeholder</div>
@@ -79,41 +79,8 @@ public void ParsePageBreakAfter()
7979
Assert.That(elements[2].ChildElements, Has.All.TypeOf<Run>());
8080
Assert.That(elements[2].InnerText, Is.EqualTo("Ipsum"));
8181
});
82-
Assert.That(elements[1].LastChild.HasChild<Break>(), Is.True);
83-
Assert.That(elements[1].LastChild.HasChild<LastRenderedPageBreak>(), Is.False);
84-
}
85-
86-
[TestCase("landscape")]
87-
[TestCase("portrait")]
88-
public void ParsePageOrientation(string orientation)
89-
{
90-
var _ = converter.Parse($@"<body style=""page-orientation:{orientation}""><body>");
91-
var sectionProperties = mainPart.Document.Body!.GetFirstChild<SectionProperties>();
92-
Assert.That(sectionProperties, Is.Not.Null);
93-
var pageSize = sectionProperties.GetFirstChild<PageSize>();
94-
if (orientation == "landscape")
95-
Assert.That(pageSize.Width, Is.GreaterThan(pageSize.Height));
96-
else
97-
Assert.That(pageSize.Height, Is.GreaterThan(pageSize.Width));
98-
}
99-
100-
[Test]
101-
public void ParsePageExistingOrientation()
102-
{
103-
using var generatedDocument = new MemoryStream();
104-
using (var buffer = ResourceHelper.GetStream("Resources.DocWithLandscape.docx"))
105-
buffer.CopyTo(generatedDocument);
106-
107-
generatedDocument.Position = 0L;
108-
using WordprocessingDocument package = WordprocessingDocument.Open(generatedDocument, true);
109-
MainDocumentPart mainPart = package.MainDocumentPart;
110-
HtmlConverter converter = new(mainPart);
111-
112-
var _ = converter.Parse($@"<body style=""page-orientation:portrait""><body>");
113-
var sectionProperties = mainPart.Document.Body!.GetFirstChild<SectionProperties>();
114-
Assert.That(sectionProperties, Is.Not.Null);
115-
var pageSize = sectionProperties.GetFirstChild<PageSize>();
116-
Assert.That(pageSize.Height, Is.GreaterThan(pageSize.Width));
82+
Assert.That(elements[1].LastChild?.HasChild<Break>(), Is.True);
83+
Assert.That(elements[1].LastChild?.HasChild<LastRenderedPageBreak>(), Is.False);
11784
}
11885
}
11986
}

0 commit comments

Comments
 (0)