Skip to content

Commit 1e2540b

Browse files
Add a few tests
1 parent 32b6de6 commit 1e2540b

File tree

7 files changed

+189
-10
lines changed

7 files changed

+189
-10
lines changed

src/features/PowerShellNotebooks.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,21 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
8585
} else if (lines[i] === "<#") {
8686
// If we found the start of a block comment,
8787
// insert what we saw leading up to this.
88-
notebookData.cells.push({
89-
cellKind: cellKind!,
90-
language: cellKind === vscode.CellKind.Markdown ? 'markdown' : 'powershell',
91-
outputs: [],
92-
source: currentCellSource.join("\n"),
93-
metadata: {
94-
custom: {
95-
commentType: cellKind === vscode.CellKind.Markdown ? CommentType.LineComment : CommentType.Disabled,
88+
// If cellKind is undefined, that means we
89+
// are starting the file with a BlockComment.
90+
if (cellKind) {
91+
notebookData.cells.push({
92+
cellKind,
93+
language: cellKind === vscode.CellKind.Markdown ? 'markdown' : 'powershell',
94+
outputs: [],
95+
source: currentCellSource.join("\n"),
96+
metadata: {
97+
custom: {
98+
commentType: cellKind === vscode.CellKind.Markdown ? CommentType.LineComment : CommentType.Disabled,
99+
}
96100
}
97-
}
98-
});
101+
});
102+
}
99103

100104
// reset state because we're starting a new Markdown cell.
101105
currentCellSource = [];
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*---------------------------------------------------------
2+
* Copyright (C) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------*/
4+
5+
import * as assert from "assert";
6+
import * as path from "path";
7+
import * as vscode from "vscode";
8+
import { PowerShellNotebooksFeature } from "../../src/features/PowerShellNotebooks";
9+
import { before } from "mocha";
10+
import os = require("os");
11+
import { readFileSync } from "fs";
12+
13+
const notebookDir = [
14+
__dirname,
15+
"..",
16+
"..",
17+
"..",
18+
"test",
19+
"features",
20+
"testNotebookFiles"
21+
];
22+
23+
const notebookOnlyCode = vscode.Uri.file(
24+
path.join(...notebookDir, "onlyCode.ps1"));
25+
const notebookOnlyMarkdown = vscode.Uri.file(
26+
path.join(...notebookDir,"onlyMarkdown.ps1"));
27+
const notebookSimpleBlockComments = vscode.Uri.file(
28+
path.join(...notebookDir,"simpleBlockComments.ps1"));
29+
const notebookSimpleLineComments = vscode.Uri.file(
30+
path.join(...notebookDir,"simpleLineComments.ps1"));
31+
const notebookSimpleMixedComments = vscode.Uri.file(
32+
path.join(...notebookDir,"simpleMixedComments.ps1"));
33+
34+
const notebookTestData = new Map<vscode.Uri, vscode.NotebookCellData[]>();
35+
36+
suite("PowerShellNotebooks tests", () => {
37+
notebookTestData.set(notebookOnlyCode, [
38+
{
39+
cellKind: vscode.CellKind.Code,
40+
language: "powershell",
41+
source: readBackingFile(notebookOnlyCode),
42+
outputs: [],
43+
metadata: {}
44+
}
45+
]);
46+
47+
notebookTestData.set(notebookOnlyMarkdown, [
48+
{
49+
cellKind: vscode.CellKind.Markdown,
50+
language: "markdown",
51+
source: readBackingFile(notebookOnlyMarkdown),
52+
outputs: [],
53+
metadata: {}
54+
}
55+
]);
56+
57+
let content = readBackingFile(notebookSimpleBlockComments).split(os.EOL);
58+
notebookTestData.set(notebookSimpleBlockComments, [
59+
{
60+
cellKind: vscode.CellKind.Markdown,
61+
language: "markdown",
62+
source: content.slice(0, 5).join(os.EOL),
63+
outputs: [],
64+
metadata: {}
65+
},
66+
{
67+
cellKind: vscode.CellKind.Code,
68+
language: "powershell",
69+
source: content.slice(5, 6).join(os.EOL),
70+
outputs: [],
71+
metadata: {}
72+
},
73+
{
74+
cellKind: vscode.CellKind.Markdown,
75+
language: "markdown",
76+
source: content.slice(6, 11).join(os.EOL),
77+
outputs: [],
78+
metadata: {}
79+
},
80+
]);
81+
82+
content = readBackingFile(notebookSimpleLineComments).split(os.EOL);
83+
notebookTestData.set(notebookSimpleLineComments, [
84+
{
85+
cellKind: vscode.CellKind.Markdown,
86+
language: "markdown",
87+
source: content.slice(0, 3).join(os.EOL),
88+
outputs: [],
89+
metadata: {}
90+
},
91+
{
92+
cellKind: vscode.CellKind.Code,
93+
language: "powershell",
94+
source: content.slice(3, 4).join(os.EOL),
95+
outputs: [],
96+
metadata: {}
97+
},
98+
{
99+
cellKind: vscode.CellKind.Markdown,
100+
language: "markdown",
101+
source: content.slice(4, 7).join(os.EOL),
102+
outputs: [],
103+
metadata: {}
104+
},
105+
]);
106+
107+
content = readBackingFile(notebookSimpleMixedComments).split(os.EOL);
108+
notebookTestData.set(notebookSimpleMixedComments, [
109+
{
110+
cellKind: vscode.CellKind.Markdown,
111+
language: "markdown",
112+
source: content.slice(0, 3).join(os.EOL),
113+
outputs: [],
114+
metadata: {}
115+
},
116+
{
117+
cellKind: vscode.CellKind.Code,
118+
language: "powershell",
119+
source: content.slice(3, 4).join(os.EOL),
120+
outputs: [],
121+
metadata: {}
122+
},
123+
{
124+
cellKind: vscode.CellKind.Markdown,
125+
language: "markdown",
126+
source: content.slice(4, 9).join(os.EOL),
127+
outputs: [],
128+
metadata: {}
129+
},
130+
]);
131+
132+
const feature = new PowerShellNotebooksFeature();
133+
134+
for (const [uri, cells] of notebookTestData) {
135+
test(`Can open a notebook with expected cells - ${uri.fsPath}`, async () => {
136+
const notebookData = await feature.openNotebook(uri, {});
137+
assert.deepStrictEqual(notebookData.cells.length, cells.length);
138+
});
139+
}
140+
});
141+
142+
function readBackingFile(uri: vscode.Uri): string {
143+
return readFileSync(uri.fsPath).toString();
144+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Get-ChildItem
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# # h1
2+
# **bold**
3+
# text
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<#
2+
Foo
3+
bar
4+
baz
5+
#>
6+
Get-ChildItem
7+
<#
8+
Foo
9+
bar
10+
baz
11+
#>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Foo
2+
# bar
3+
# baz
4+
Get-ChildItem
5+
# Foo
6+
# bar
7+
# baz
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Foo
2+
# bar
3+
# baz
4+
Get-ChildItem
5+
<#
6+
Foo
7+
bar
8+
baz
9+
#>

0 commit comments

Comments
 (0)