Skip to content

Commit 7c45a4c

Browse files
committed
test: add Rmd params tests and cleanup test files
- Added tests for getRmdParamsCommand in rmdParams.test.ts - Exported getRmdParamsCommand in rTerminal.ts for testing - Renamed sesson.test.ts to workspaceViewer.test.ts to fix typo - Deleted duplicate workspace.ts file
1 parent eb656d4 commit 7c45a4c

4 files changed

Lines changed: 92 additions & 56 deletions

File tree

src/rTerminal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function valueToR(val: unknown): string {
3939
return `list(${entries.join(', ')})`;
4040
}
4141

42-
function getRmdParamsCommand(document: vscode.TextDocument): string | undefined {
42+
export function getRmdParamsCommand(document: vscode.TextDocument): string | undefined {
4343
if (document.languageId !== 'rmd') return undefined;
4444
const text = document.getText();
4545
const match = text.match(/^---\s*\n([\s\S]*?)\n---/);

src/test/suite/rmdParams.test.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import * as assert from 'assert';
2+
import * as vscode from 'vscode';
3+
import * as rTerminal from '../../rTerminal';
4+
5+
suite('Rmd Params Test Suite', () => {
6+
test('getRmdParamsCommand returns undefined for non-rmd documents', () => {
7+
const mockDoc = {
8+
languageId: 'r',
9+
getText: () => '---\nparams:\n a: 1\n---',
10+
uri: vscode.Uri.file('/test_non_rmd.R'),
11+
version: 1
12+
} as vscode.TextDocument;
13+
14+
const cmd = rTerminal.getRmdParamsCommand(mockDoc);
15+
assert.strictEqual(cmd, undefined);
16+
});
17+
18+
test('getRmdParamsCommand returns undefined if no YAML header', () => {
19+
const mockDoc = {
20+
languageId: 'rmd',
21+
getText: () => 'title: Test\nparams:\n a: 1',
22+
uri: vscode.Uri.file('/test_no_header.Rmd'),
23+
version: 1
24+
} as vscode.TextDocument;
25+
26+
const cmd = rTerminal.getRmdParamsCommand(mockDoc);
27+
assert.strictEqual(cmd, undefined);
28+
});
29+
30+
test('getRmdParamsCommand returns undefined if no params in YAML', () => {
31+
const mockDoc = {
32+
languageId: 'rmd',
33+
getText: () => '---\ntitle: Test\n---',
34+
uri: vscode.Uri.file('/test_no_params.Rmd'),
35+
version: 1
36+
} as vscode.TextDocument;
37+
38+
const cmd = rTerminal.getRmdParamsCommand(mockDoc);
39+
assert.strictEqual(cmd, undefined);
40+
});
41+
42+
test('getRmdParamsCommand parses valid params', () => {
43+
const mockDoc = {
44+
languageId: 'rmd',
45+
getText: () => '---\nparams:\n a: 1\n b: "test"\n---',
46+
uri: vscode.Uri.file('/test_valid_params.Rmd'),
47+
version: 1
48+
} as vscode.TextDocument;
49+
50+
const cmd = rTerminal.getRmdParamsCommand(mockDoc);
51+
assert.strictEqual(cmd, 'params <- list(a = 1, b = "test")');
52+
});
53+
54+
test('getRmdParamsCommand handles custom !r type', () => {
55+
const mockDoc = {
56+
languageId: 'rmd',
57+
getText: () => '---\nparams:\n a: !r 1+1\n---',
58+
uri: vscode.Uri.file('/test_custom_type.Rmd'),
59+
version: 1
60+
} as vscode.TextDocument;
61+
62+
const cmd = rTerminal.getRmdParamsCommand(mockDoc);
63+
assert.strictEqual(cmd, 'params <- list(a = 1+1)');
64+
});
65+
66+
test('getRmdParamsCommand respects cache invalidation by version', () => {
67+
const mockDoc = {
68+
languageId: 'rmd',
69+
getText: () => '---\nparams:\n a: 1\n---',
70+
uri: vscode.Uri.file('/test_cache_invalidation.Rmd'),
71+
version: 1
72+
} as vscode.TextDocument;
73+
74+
let cmd = rTerminal.getRmdParamsCommand(mockDoc);
75+
assert.strictEqual(cmd, 'params <- list(a = 1)');
76+
77+
// Same file, same version -> returns undefined (cached)
78+
cmd = rTerminal.getRmdParamsCommand(mockDoc);
79+
assert.strictEqual(cmd, undefined);
80+
81+
// Same file, new version -> parses again
82+
const updatedDoc = {
83+
...mockDoc,
84+
getText: () => '---\nparams:\n a: 2\n---',
85+
version: 2
86+
} as vscode.TextDocument;
87+
88+
cmd = rTerminal.getRmdParamsCommand(updatedDoc);
89+
assert.strictEqual(cmd, 'params <- list(a = 2)');
90+
});
91+
});

src/test/suite/workspace.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)