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

Improve tests, fix HyperLink order, add method ValidateDocument() #87

Merged
merged 5 commits into from
Nov 25, 2022
Merged
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
4 changes: 3 additions & 1 deletion OfficeIMO.Tests/Word.Borders.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -284,6 +284,8 @@ public void Test_CreatingWordDocumentWithBordersBuiltin() {
document.Sections[3].SetBorders(WordBorder.None);
Assert.True(document.Sections[3].Borders.Type == WordBorder.None);
document.Save(false);

Assert.True(HasUnexpectedElements(document) == false, "Document has unexpected elements. Order of elements matters!");
}

using (WordDocument document = WordDocument.Load(Path.Combine(_directoryWithFiles, "CreatedDocumentWithBordersBuiltin.docx"))) {
Expand Down
2 changes: 1 addition & 1 deletion OfficeIMO.Tests/Word.Fields.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down
4 changes: 3 additions & 1 deletion OfficeIMO.Tests/Word.Hyperlinks.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -76,6 +76,8 @@ public void Test_CreatingWordWithHyperlinks() {
Assert.True(document.Sections[0].Bookmarks.Count == 1);

document.Save(false);

Assert.True(HasUnexpectedElements(document) == false, "Document has unexpected elements. Order of elements matters!");
}

using (WordDocument document = WordDocument.Load(Path.Combine(_directoryWithFiles, "HyperlinksTests.docx"))) {
Expand Down
18 changes: 16 additions & 2 deletions OfficeIMO.Tests/Word.Lists.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Linq;
using DocumentFormat.OpenXml.Wordprocessing;
Expand Down Expand Up @@ -106,6 +106,8 @@ public void Test_CreatingWordDocumentWithLists() {
Assert.Equal(28, section.Paragraphs.Count);

document.Save(false);

Assert.True(HasUnexpectedElements(document) == false, "Document has unexpected elements. Order of elements matters!");
}

using (var document = WordDocument.Load(Path.Combine(_directoryWithFiles, "CreatedDocumentWithLists.docx"))) {
Expand Down Expand Up @@ -169,6 +171,8 @@ public void Test_CreatingWordDocumentWithLists() {
Assert.Equal(45, section.Paragraphs.Count);

document.Save();

Assert.True(HasUnexpectedElements(document) == false, "Document has unexpected elements. Order of elements matters!");
}

using (var document = WordDocument.Load(Path.Combine(_directoryWithFiles, "CreatedDocumentWithLists.docx"))) {
Expand All @@ -191,6 +195,8 @@ public void Test_CreatingWordDocumentWithLists() {
Assert.Equal(45, section.Paragraphs.Count);

document.Save();

Assert.True(HasUnexpectedElements(document) == false, "Document has unexpected elements. Order of elements matters!");
}
}

Expand Down Expand Up @@ -325,6 +331,8 @@ public void Test_CreatingWordDocumentWithLists2() {
Assert.Single(document.Sections[1].Lists);

document.Save(false);

Assert.True(HasUnexpectedElements(document) == false, "Document has unexpected elements. Order of elements matters!");
}

using (var document = WordDocument.Load(Path.Combine(_directoryWithFiles, "CreatedDocumentWithLists2.docx"))) {
Expand All @@ -342,20 +350,24 @@ public void Test_CreatingWordDocumentWithLists2() {
Assert.Equal(2, document.Sections[1].Lists.Count);

document.Save();

Assert.True(HasUnexpectedElements(document) == false, "Document has unexpected elements. Order of elements matters!");
}

using (var document = WordDocument.Load(Path.Combine(_directoryWithFiles, "CreatedDocumentWithLists2.docx"))) {
Assert.Equal(7, document.Lists.Count);
Assert.Equal(5, document.Sections[0].Lists.Count);
Assert.Equal(2, document.Sections[1].Lists.Count);
document.Save();

Assert.True(HasUnexpectedElements(document) == false, "Document has unexpected elements. Order of elements matters!");
}
}

[Fact]
public void Test_SavingWordDocumentWithListsToStream() {
var filePath = Path.Combine(_directoryWithFiles, "CreatedDocumentWithListsToStream.docx");
var wordListStyles = (WordListStyle[]) Enum.GetValues(typeof(WordListStyle));
var wordListStyles = (WordListStyle[])Enum.GetValues(typeof(WordListStyle));
using (var document = WordDocument.Create()) {
foreach (var listStyle in wordListStyles) {
var paragraph = document.AddParagraph(listStyle.ToString());
Expand All @@ -369,6 +381,8 @@ public void Test_SavingWordDocumentWithListsToStream() {
using var outputStream = new MemoryStream();
document.Save(outputStream);
File.WriteAllBytes(filePath, outputStream.ToArray());

Assert.True(HasUnexpectedElements(document) == false, "Document has unexpected elements. Order of elements matters!");
}

using (var document = WordDocument.Load(filePath)) {
Expand Down
4 changes: 3 additions & 1 deletion OfficeIMO.Tests/Word.Margins.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -62,6 +62,8 @@ public void Test_CreatingWordDocumentWithPageMargins2() {
Assert.True(section2.Paragraphs[0].Text == "Section 6");

document.Save(false);

Assert.True(HasUnexpectedElements(document) == false, "Document has unexpected elements. Order of elements matters!");
}
using (WordDocument document = WordDocument.Load(Path.Combine(_directoryWithFiles, "CreatedDocumentWithSectionsPageMargins2.docx"))) {

Expand Down
4 changes: 3 additions & 1 deletion OfficeIMO.Tests/Word.PageSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Wordprocessing;
using OfficeIMO.Word;
using System.IO;
using Xunit;
Expand Down Expand Up @@ -80,6 +80,8 @@ public void Test_CreatingWordDocumentWithPageSettings() {
Assert.True(section2.Paragraphs[0].Text == "Section 6");

document.Save(false);

Assert.True(HasUnexpectedElements(document) == false, "Document has unexpected elements. Order of elements matters!");
}
using (WordDocument document = WordDocument.Load(Path.Combine(_directoryWithFiles, "CreateDocumentPageSettings.docx"))) {
Assert.True(document.Sections[0].PageSettings.Orientation == PageOrientationValues.Landscape);
Expand Down
7 changes: 4 additions & 3 deletions OfficeIMO.Tests/Word.Save.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.IO;
using System.IO;
using OfficeIMO.Word;
using Xunit;

Expand Down Expand Up @@ -52,6 +52,8 @@ public void Test_Save() {

Assert.True(File.Exists(filePath3));
Assert.True(filePath3.IsFileLocked());

Assert.True(HasUnexpectedElements(document) == false, "Document has unexpected elements. Order of elements matters!");
}

Assert.False(filePath1.IsFileLocked());
Expand Down Expand Up @@ -122,8 +124,7 @@ public void Test_Dispose() {
}

[Fact]
public void Test_SaveToStream()
{
public void Test_SaveToStream() {
var document = WordDocument.Create();
document.AddParagraph("Hello world!");

Expand Down
21 changes: 20 additions & 1 deletion OfficeIMO.Tests/Word.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Office.CustomUI;
using DocumentFormat.OpenXml.Wordprocessing;
using OfficeIMO.Word;
using Xunit;

namespace OfficeIMO.Tests {
Expand All @@ -28,5 +31,21 @@ public Word() {
_directoryWithFiles = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TempDocuments2");
Setup(_directoryWithFiles);
}

/// <summary>
/// This helps finding unexpected elements during validation. Should prevent unexpected changes
/// </summary>
/// <param name="document"></param>
/// <returns></returns>
public bool HasUnexpectedElements(WordDocument document) {
bool found = false;
foreach (var e in document.DocumentValidationErrors) {
if (e.Description.StartsWith("The element has unexpected child element")) {
found = true;
break;
}
}
return found;
}
}
}
15 changes: 9 additions & 6 deletions OfficeIMO.Word/WordDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -712,14 +712,17 @@ public bool DocumentIsValid {

public List<ValidationErrorInfo> DocumentValidationErrors {
get {
List<ValidationErrorInfo> listErrors = new List<ValidationErrorInfo>();
OpenXmlValidator validator = new OpenXmlValidator();
foreach (ValidationErrorInfo error in validator.Validate(this._wordprocessingDocument)) {
listErrors.Add(error);
}
return ValidateDocument();
}
}

return listErrors;
public List<ValidationErrorInfo> ValidateDocument(FileFormatVersions fileFormatVersions = FileFormatVersions.Microsoft365) {
List<ValidationErrorInfo> listErrors = new List<ValidationErrorInfo>();
OpenXmlValidator validator = new OpenXmlValidator(fileFormatVersions);
foreach (ValidationErrorInfo error in validator.Validate(this._wordprocessingDocument)) {
listErrors.Add(error);
}
return listErrors;
}

public WordCompatibilitySettings CompatibilitySettings { get; set; }
Expand Down
18 changes: 9 additions & 9 deletions OfficeIMO.Word/WordHyperLink.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
Expand Down Expand Up @@ -202,8 +202,8 @@ public static WordParagraph AddHyperLink(WordParagraph paragraph, string text, s
if (addStyle) {
RunProperties runPropertiesHyperLink = new RunProperties(
new RunStyle { Val = "Hyperlink", },
new Underline { Val = UnderlineValues.Single },
new Color { ThemeColor = ThemeColorValues.Hyperlink }
new Color { ThemeColor = ThemeColorValues.Hyperlink, Val = "0000FF" },
new Underline { Val = UnderlineValues.Single }
);
run.RunProperties = runPropertiesHyperLink;
}
Expand Down Expand Up @@ -246,12 +246,12 @@ public static WordParagraph AddHyperLink(WordParagraph paragraph, string text, U

// Styling for the hyperlink
if (addStyle) {
RunProperties runPropertiesHyperLink = new RunProperties(
new RunStyle { Val = "Hyperlink", },
new Underline { Val = UnderlineValues.Single },
new Color { ThemeColor = ThemeColorValues.Hyperlink }
);
run.RunProperties = runPropertiesHyperLink;
//RunProperties runPropertiesHyperLink = new RunProperties(
// new RunStyle { Val = "Hyperlink", }
// //new Underline { Val = UnderlineValues.Single }
// //new Color { ThemeColor = ThemeColorValues.Hyperlink }
//);
//run.RunProperties = runPropertiesHyperLink;
}

if (tooltip != "") {
Expand Down