Skip to content

Commit 3fd29fb

Browse files
authored
Merge pull request #54 from VerifyTests/add-docx-support
add docx support
2 parents 6eaa9ac + 91d557b commit 3fd29fb

22 files changed

+458
-15
lines changed

.claude/settings.local.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
"WebFetch(domain:api.github.com)",
77
"Bash(git restore:*)",
88
"Bash(dotnet build:*)",
9-
"Bash([ -d \"D:\\Code\\VerifyTests\\Verify.OpenXml\\src\\Verify.OpenXml\\Converters\" ])"
9+
"Bash([ -d \"D:\\Code\\VerifyTests\\Verify.OpenXml\\src\\Verify.OpenXml\\Converters\" ])",
10+
"Bash(ls:*)",
11+
"Bash(dotnet run:*)",
12+
"Bash(dotnet test:*)",
13+
"Bash(for f in *.received.*)",
14+
"Bash(do mv \"$f\" \"$f/received/verified\")",
15+
"Bash(done)"
1016
]
1117
}
1218
}

readme.md

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,26 @@
66

77
Extends [Verify](https://github.com/VerifyTests/Verify) to allow verification of Excel documents via [OpenXML](https://github.com/dotnet/Open-XML-SDK/).<!-- singleLineInclude: intro. path: /docs/intro.include.md -->
88

9-
Converts Excel documents (xlsx) to CSV for verification.
9+
Supports Excel (xlsx) and Word (docx) documents.
1010

1111
## Features
1212

13+
### Excel (xlsx)
14+
1315
* Converts workbooks to CSV format for each worksheet
1416
* Extracts formulas and displays them alongside cell values
1517
* Captures document metadata (title, subject, creator, keywords, category, etc.)
1618
* Supports date scrubbing and GUID scrubbing for deterministic tests
1719
* Generates deterministic XLSX output using DeterministicIoPackaging
1820

21+
### Word (docx)
22+
23+
* Extracts document text content from paragraphs and tables
24+
* Captures document properties (title, subject, creator, keywords, etc.)
25+
* Captures custom document properties
26+
* Extracts font information
27+
* Generates deterministic DOCX output using DeterministicIoPackaging
28+
1929
**See [Milestones](../../milestones?state=closed) for release notes.**
2030

2131

@@ -100,7 +110,7 @@ public Task VerifySpreadsheetDocument()
100110
<!-- endSnippet -->
101111

102112

103-
### Example snapshot
113+
#### Example snapshot
104114

105115
<!-- snippet: Samples.VerifyExcel.verified.csv -->
106116
<a id='snippet-Samples.VerifyExcel.verified.csv'></a>
@@ -115,3 +125,52 @@ public Task VerifySpreadsheetDocument()
115125
```
116126
<sup><a href='/src/Tests/Samples.VerifyExcel.verified.csv#L1-L7' title='Snippet source file'>snippet source</a> | <a href='#snippet-Samples.VerifyExcel.verified.csv' title='Start of snippet'>anchor</a></sup>
117127
<!-- endSnippet -->
128+
129+
130+
### Word
131+
132+
133+
#### Verify a file
134+
135+
<!-- snippet: VerifyWord -->
136+
<a id='snippet-VerifyWord'></a>
137+
```cs
138+
[Test]
139+
public Task VerifyWord() =>
140+
VerifyFile("sample.docx");
141+
```
142+
<sup><a href='/src/Tests/Samples.cs#L44-L50' title='Snippet source file'>snippet source</a> | <a href='#snippet-VerifyWord' title='Start of snippet'>anchor</a></sup>
143+
<!-- endSnippet -->
144+
145+
146+
#### Verify a Stream
147+
148+
<!-- snippet: VerifyWordStream -->
149+
<a id='snippet-VerifyWordStream'></a>
150+
```cs
151+
[Test]
152+
public Task VerifyWordStream()
153+
{
154+
var stream = new MemoryStream(File.ReadAllBytes("sample.docx"));
155+
return Verify(stream, "docx");
156+
}
157+
```
158+
<sup><a href='/src/Tests/Samples.cs#L64-L73' title='Snippet source file'>snippet source</a> | <a href='#snippet-VerifyWordStream' title='Start of snippet'>anchor</a></sup>
159+
<!-- endSnippet -->
160+
161+
162+
#### Verify a WordprocessingDocument
163+
164+
<!-- snippet: WordprocessingDocument -->
165+
<a id='snippet-WordprocessingDocument'></a>
166+
```cs
167+
[Test]
168+
public Task VerifyWordprocessingDocument()
169+
{
170+
using var stream = File.OpenRead("sample.docx");
171+
using var reader = WordprocessingDocument.Open(stream, false);
172+
return Verify(reader);
173+
}
174+
```
175+
<sup><a href='/src/Tests/Samples.cs#L52-L62' title='Snippet source file'>snippet source</a> | <a href='#snippet-WordprocessingDocument' title='Start of snippet'>anchor</a></sup>
176+
<!-- endSnippet -->

src/Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33
<PropertyGroup>
44
<NoWarn>CS1591;CS0649;CA1416;NU1608;NU1109</NoWarn>
5-
<Version>0.1.0</Version>
5+
<Version>0.2.0</Version>
66
<LangVersion>preview</LangVersion>
77
<AssemblyVersion>1.0.0</AssemblyVersion>
88
<PackageTags>OpenXML, Verify</PackageTags>
@@ -15,4 +15,4 @@
1515
<ItemGroup>
1616
<Using Include="System.ReadOnlySpan&lt;System.Char&gt;" Alias="CharSpan" />
1717
</ItemGroup>
18-
</Project>
18+
</Project>

src/Tests/CreateSampleDocx.csx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#r "nuget: DocumentFormat.OpenXml, 3.0.0"
2+
using DocumentFormat.OpenXml;
3+
using DocumentFormat.OpenXml.Packaging;
4+
using DocumentFormat.OpenXml.Wordprocessing;
5+
6+
using (var doc = WordprocessingDocument.Create("sample.docx", WordprocessingDocumentType.Document))
7+
{
8+
doc.PackageProperties.Title = "Sample Document";
9+
doc.PackageProperties.Creator = "Test Author";
10+
doc.PackageProperties.Subject = "Test Subject";
11+
12+
var mainPart = doc.AddMainDocumentPart();
13+
mainPart.Document = new Document(
14+
new Body(
15+
new Paragraph(
16+
new Run(
17+
new Text("Hello World! This is a sample Word document.")
18+
)
19+
),
20+
new Paragraph(
21+
new Run(
22+
new Text("This is the second paragraph with some more text.")
23+
)
24+
)
25+
)
26+
);
27+
mainPart.Document.Save();
28+
}
29+
Console.WriteLine("Created sample.docx");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
Properties: {
3+
Creator: Test Author,
4+
Subject: Test Subject,
5+
Title: Sample Document
6+
},
7+
Text:
8+
Hello World! This is a sample Word document.
9+
This is the second paragraph with some more text.
10+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello World! This is a sample Word document.
2+
This is the second paragraph with some more text.
1.04 KB
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
Properties: {
3+
Creator: Test Author,
4+
Subject: Test Subject,
5+
Title: Sample Document
6+
},
7+
Text:
8+
Hello World! This is a sample Word document.
9+
This is the second paragraph with some more text.
10+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello World! This is a sample Word document.
2+
This is the second paragraph with some more text.
1.04 KB
Binary file not shown.

0 commit comments

Comments
 (0)