Skip to content

Commit 09419b6

Browse files
committed
Add a test for powerpoint using XPATH
1 parent 7253555 commit 09419b6

File tree

3 files changed

+110
-19
lines changed

3 files changed

+110
-19
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
title: Parsed HTML table in PPTX
3+
format: pptx
4+
_quarto:
5+
tests:
6+
pptx:
7+
ensurePptxXpath:
8+
- 2
9+
- ['//a:tbl']
10+
- []
11+
---
12+
13+
# Test table
14+
15+
```{=html}
16+
<table>
17+
<thead>
18+
<tr>
19+
<th>Sepal.Length</th>
20+
<th>Sepal.Width</th>
21+
</tr>
22+
</thead>
23+
<tbody>
24+
<tr>
25+
<td>5.1</td>
26+
<td>3.5</td>
27+
</tr>
28+
<tr>
29+
<td>4.9</td>
30+
<td>3.0</td>
31+
</tr>
32+
</tbody>
33+
</table>
34+
```
35+

tests/smoke/smoke-all.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
fileExists,
2929
noErrors,
3030
noErrorsOrWarnings,
31+
ensurePptxXpath,
3132
} from "../verify.ts";
3233
import { readYaml, readYamlFromMarkdown } from "../../src/core/yaml.ts";
3334
import { outputForInput } from "../utils.ts";
@@ -98,6 +99,7 @@ function resolveTestSpecs(
9899
ensureOdtXpath,
99100
ensureJatsXpath,
100101
ensurePptxRegexMatches,
102+
ensurePptxXpath,
101103
ensureSnapshotMatches
102104
};
103105

tests/verify.ts

Lines changed: 73 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,55 @@ import { isWindows } from "../src/core/platform.ts";
2222
import { execProcess } from "../src/core/process.ts";
2323
import { canonicalizeSnapshot, checkSnapshot } from "./verify-snapshot.ts";
2424

25-
export const withDocxContent = async <T>(file: string,
26-
k: (xml: string) => Promise<T>) => {
27-
const [_dir, stem] = dirAndStem(file);
28-
const temp = await Deno.makeTempDir();
29-
try {
30-
// Move the docx to a temp dir and unzip it
31-
const zipFile = join(temp, stem + ".zip");
32-
await Deno.copyFile(file, zipFile);
33-
await unzip(zipFile);
34-
35-
// Open the core xml document and match the matches
36-
const docXml = join(temp, "word", "document.xml");
37-
const xml = await Deno.readTextFile(docXml);
38-
const result = await k(xml);
39-
return result;
40-
} finally {
41-
await Deno.remove(temp, { recursive: true });
42-
}
43-
};
25+
export const withDocxContent = async <T>(
26+
file: string,
27+
k: (xml: string) => Promise<T>
28+
) => {
29+
const [_dir, stem] = dirAndStem(file);
30+
const temp = await Deno.makeTempDir();
31+
try {
32+
// Move the docx to a temp dir and unzip it
33+
const zipFile = join(temp, stem + ".zip");
34+
await Deno.copyFile(file, zipFile);
35+
await unzip(zipFile);
36+
37+
// Open the core xml document and match the matches
38+
const docXml = join(temp, "word", "document.xml");
39+
const xml = await Deno.readTextFile(docXml);
40+
const result = await k(xml);
41+
return result;
42+
} finally {
43+
await Deno.remove(temp, { recursive: true });
44+
}
45+
};
46+
47+
export const withPptxContent = async <T>(
48+
file: string,
49+
slideNumber: number,
50+
k: (xml: string) => Promise<T>
51+
) => {
52+
const [_dir, stem] = dirAndStem(file);
53+
const temp = await Deno.makeTempDir();
54+
try {
55+
// Move the pptx to a temp dir and unzip it
56+
const zipFile = join(temp, stem + ".zip");
57+
await Deno.copyFile(file, zipFile);
58+
await unzip(zipFile);
59+
60+
// Open the core xml document and match the matches
61+
const slidePath = join(temp, "ppt", "slides");
62+
const slideFile = join(slidePath, `slide${slideNumber}.xml`);
63+
assert(
64+
existsSync(slideFile),
65+
`Slide number ${slideNumber} is not in the Pptx`,
66+
);
67+
const xml = await Deno.readTextFile(slideFile);
68+
const result = await k(xml);
69+
return result;
70+
} finally {
71+
await Deno.remove(temp, { recursive: true });
72+
}
73+
};
4474

4575
export const noErrors: Verify = {
4676
name: "No Errors",
@@ -432,6 +462,18 @@ export const verifyDocXDocument = (
432462
});
433463
};
434464

465+
export const verifyPptxDocument = (
466+
callback: (doc: string) => Promise<void>,
467+
name?: string,
468+
): (file: string, slideNumber: number) => Verify => {
469+
return (file: string, slideNumber: number) => ({
470+
name: name ?? "Inspecting Pptx",
471+
verify: async (_output: ExecuteOutput[]) => {
472+
return await withPptxContent(file, slideNumber, callback);
473+
},
474+
});
475+
};
476+
435477
const xmlChecker = (
436478
selectors: string[],
437479
noMatchSelectors?: string[],
@@ -493,6 +535,18 @@ export const ensureDocxXpath = (
493535
)(file);
494536
};
495537

538+
export const ensurePptxXpath = (
539+
file: string,
540+
slideNumber: number,
541+
selectors: string[],
542+
noMatchSelectors?: string[],
543+
): Verify => {
544+
return verifyPptxDocument(
545+
xmlChecker(selectors, noMatchSelectors),
546+
"Inspecting Pptx for XPath selectors",
547+
)(file, slideNumber);
548+
};
549+
496550
export const ensureDocxRegexMatches = (
497551
file: string,
498552
regexes: (string | RegExp)[],

0 commit comments

Comments
 (0)