Skip to content

Commit c2f847e

Browse files
authored
add initial snapshot tests (#781)
1 parent c0a6d2a commit c2f847e

File tree

7 files changed

+147
-23
lines changed

7 files changed

+147
-23
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
\[missing close curly bracket\]{
2+
3+
\[missing close parenthesis\](https://example.com
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
- Item one
2+
- Item two
3+
- Item three
4+
5+
This is `inline code` text
6+
7+
```
8+
function hello() {
9+
return "world"
10+
}
11+
```
12+
13+
This is *emphasized* text
14+
15+
# Main Header
16+
17+
Line one Line two
18+
19+
[a link](./hello "out"){#fig .class key="value"}
20+
21+
This is inline math $E = mc^2$ and more text
22+
23+
This is display math
24+
25+
$$\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}$$
26+
27+
More inline math $\alpha + \beta = \gamma$ here
28+
29+
This is a simple paragraph
30+
31+
'single quote' "double quote"
32+
33+
a [span]{#with-id .class key="value"}
34+
35+
~~strikeout~~
36+
37+
This is **strong** text
38+
39+
^superscript^
40+
41+
[underlined]{.underline} [underline]{.underline}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[missing close curly bracket]{
2+
3+
[missing close parenthesis](https://example.com
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
- Item one
2+
- Item two
3+
- Item three
4+
5+
This is `inline code` text
6+
7+
```
8+
function hello() {
9+
return "world"
10+
}
11+
```
12+
13+
This is *emphasized* text
14+
15+
# Main Header
16+
17+
Line one
18+
Line two
19+
20+
[a link](./hello "out"){#fig .class key=value}
21+
22+
This is inline math $E = mc^2$ and more text
23+
24+
This is display math
25+
26+
$$\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}$$
27+
28+
More inline math $\alpha + \beta = \gamma$ here
29+
30+
This is a simple paragraph
31+
32+
'single quote' "double quote"
33+
34+
a [span]{#with-id .class key=value}
35+
36+
~~strikeout~~
37+
38+
This is **strong** text
39+
40+
^superscript^
41+
42+
[underlined]{.underline} [underline]{.ul}
Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import * as vscode from "vscode";
22
import * as assert from "assert";
3-
import { WORKSPACE_PATH, examplesOutUri, wait } from "./test-utils";
3+
import { WORKSPACE_PATH, readOrCreateSnapshot, examplesOutUri, wait, roundtrip, openAndShowTextDocument } from "./test-utils";
44
import { isQuartoDoc } from "../core/doc";
5-
import { extension } from "./extension";
65

7-
const APPROX_TIME_TO_OPEN_VISUAL_EDITOR = 1700;
86

97
suite("Quarto basics", function () {
108
// Before running tests, copy `./examples` to a new folder `./examples-out`.
@@ -15,8 +13,7 @@ suite("Quarto basics", function () {
1513
});
1614

1715
test("Can open a Quarto document", async function () {
18-
const doc = await vscode.workspace.openTextDocument(examplesOutUri("hello.qmd"));
19-
const editor = await vscode.window.showTextDocument(doc);
16+
const { editor } = await openAndShowTextDocument("hello.qmd");
2017

2118
assert.strictEqual(editor?.document.languageId, "quarto");
2219
assert.strictEqual(isQuartoDoc(editor?.document), true);
@@ -26,40 +23,38 @@ suite("Quarto basics", function () {
2623
// test. That's okay for this test, but could cause issues if you expect a qmd to look how it
2724
// does in `/examples`.
2825
test("Roundtrip doesn't change hello.qmd", async function () {
29-
const doc = await vscode.workspace.openTextDocument(examplesOutUri("hello.qmd"));
30-
const editor = await vscode.window.showTextDocument(doc);
26+
const { doc } = await openAndShowTextDocument("hello.qmd");
3127

3228
const { before, after } = await roundtrip(doc);
3329

3430
assert.equal(before, after);
3531
});
3632

37-
test("Roundtrip does change roundtrip-failures.qmd", async function () {
33+
test("Roundtrip changes roundtrip-changes.qmd", async function () {
3834
// We want this test to fail locally so that we can reference the
3935
// before/affter diff that Mocha logs, but we dont wan't CI to fail.
4036
if (process.env['CI']) this.skip();
4137

42-
const doc = await vscode.workspace.openTextDocument(examplesOutUri("roundtrip-failures.qmd"));
43-
const editor = await vscode.window.showTextDocument(doc);
38+
const { doc } = await openAndShowTextDocument("roundtrip-changes.qmd");
4439

4540
const { before, after } = await roundtrip(doc);
4641

4742
assert.equal(before, after);
4843
});
49-
});
5044

51-
async function roundtrip(doc: vscode.TextDocument) {
52-
const before = doc.getText();
45+
test("Roundtripped valid-basics.qmd matches snapshot", async function () {
46+
const { doc } = await openAndShowTextDocument("valid-basics.qmd");
47+
48+
const { after } = await roundtrip(doc);
49+
50+
assert.equal(after, await readOrCreateSnapshot("roundtripped-valid-basics.qmd", after));
51+
});
5352

54-
// switch to visual editor and back
55-
await vscode.commands.executeCommand("quarto.test_setkVisualModeConfirmedTrue");
56-
await wait(300);
57-
await vscode.commands.executeCommand("quarto.editInVisualMode");
58-
await wait(APPROX_TIME_TO_OPEN_VISUAL_EDITOR);
59-
await vscode.commands.executeCommand("quarto.editInSourceMode");
60-
await wait(300);
53+
test("Roundtripped invalid.qmd matches snapshot", async function () {
54+
const { doc } = await openAndShowTextDocument("invalid.qmd");
6155

62-
const after = doc.getText();
56+
const { after } = await roundtrip(doc);
6357

64-
return { before, after };
65-
}
58+
assert.equal(after, await readOrCreateSnapshot("roundtripped-invalid.qmd", after));
59+
});
60+
});

apps/vscode/src/test/test-utils.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,50 @@ export const TEST_PATH = path.join(EXTENSION_ROOT_DIR, "src", "test");
1515
export const WORKSPACE_PATH = path.join(TEST_PATH, "examples");
1616
export const WORKSPACE_OUT_PATH = path.join(TEST_PATH, "examples-out");
1717

18+
function examplesUri(fileName: string = ''): vscode.Uri {
19+
return vscode.Uri.file(path.join(WORKSPACE_PATH, fileName));
20+
}
1821
export function examplesOutUri(fileName: string = ''): vscode.Uri {
1922
return vscode.Uri.file(path.join(WORKSPACE_OUT_PATH, fileName));
2023
}
2124

2225
export function wait(ms: number) {
2326
return new Promise(resolve => setTimeout(resolve, ms));
2427
}
28+
29+
export async function openAndShowTextDocument(fileName: string) {
30+
const doc = await vscode.workspace.openTextDocument(examplesOutUri(fileName));
31+
const editor = await vscode.window.showTextDocument(doc);
32+
return { doc, editor };
33+
}
34+
35+
const APPROX_TIME_TO_OPEN_VISUAL_EDITOR = 1700;
36+
export async function roundtrip(doc: vscode.TextDocument) {
37+
const before = doc.getText();
38+
39+
// switch to visual editor and back
40+
await vscode.commands.executeCommand("quarto.test_setkVisualModeConfirmedTrue");
41+
await wait(300);
42+
await vscode.commands.executeCommand("quarto.editInVisualMode");
43+
await wait(APPROX_TIME_TO_OPEN_VISUAL_EDITOR);
44+
await vscode.commands.executeCommand("quarto.editInSourceMode");
45+
await wait(300);
46+
47+
const after = doc.getText();
48+
49+
return { before, after };
50+
}
51+
52+
export async function readOrCreateSnapshot(fileName: string, content: string) {
53+
const snapshotUri = examplesUri(path.join('generated_snapshots', fileName));
54+
try {
55+
const doc = await vscode.workspace.openTextDocument(snapshotUri);
56+
return doc.getText();
57+
} catch {
58+
await vscode.workspace.fs.writeFile(
59+
snapshotUri,
60+
Buffer.from(content, 'utf8')
61+
);
62+
return content;
63+
}
64+
}

0 commit comments

Comments
 (0)