-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
258 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,62 @@ | ||
import * as vscode from 'vscode'; | ||
import * as assert from 'assert'; | ||
import { withRandomFileEditor, CURSOR, type } from './editor-utils'; | ||
import * as fs from 'fs'; | ||
import { CURSOR, type, withFileEditor } from './editor-utils'; | ||
|
||
const snapshotsFolder = `${__dirname}/../../src/test/snapshots`; | ||
const snippetsPath = `${snapshotsFolder}/indentation-cases.R`; | ||
const snapshotsPath = `${snapshotsFolder}/indentation-snapshots.R`; | ||
|
||
// FIXME: This should normally be run as part of tests setup in `before()` but | ||
// it's somehow not defined | ||
async function init() { | ||
// Open workspace with custom configuration for snapshots. If you need | ||
// custom settings set them there via `config.update()`. | ||
const uri = vscode.Uri.file(snapshotsFolder); | ||
await vscode.commands.executeCommand('vscode.openFolder', uri, false); | ||
const config = vscode.workspace.getConfiguration(); | ||
|
||
// Prevents `ENOENT: no such file or directory` errors caused by us | ||
// deleting temporary editor files befor Code had the opportunity to | ||
// save the user history of these files (see ) | ||
config.update('workbench.localHistory.enabled', false, vscode.ConfigurationTarget.Workspace); | ||
} | ||
|
||
suite('Indentation', () => { | ||
test('test', async () => { | ||
return withRandomFileEditor(`1 +${CURSOR}`, 'R', async (_editor, document) => { | ||
await type(document, '\n2'); | ||
assert.strictEqual(document.getText(), '1 +\n 2'); | ||
}); | ||
// This regenerates snapshots in place. If the snapshots differ from last | ||
// run, a failure is emitted. You can either commit the new output or discard | ||
// it if that's a bug to fix. | ||
test('Regenerate and check', async () => { | ||
await init(); | ||
const expected = fs.readFileSync(snapshotsPath, 'utf8'); | ||
const current = await regenerateIndentSnapshots(); | ||
|
||
// Update snapshot file | ||
fs.writeFileSync(snapshotsPath, current, 'utf8'); | ||
|
||
// Notify if snapshots were outdated | ||
assert.strictEqual(expected, current); | ||
console.log('dirname: ', __dirname); | ||
}); | ||
}); | ||
|
||
async function regenerateIndentSnapshots() { | ||
const snippets = fs. | ||
readFileSync(snippetsPath, 'utf8'). | ||
split('# ---\n'); | ||
|
||
// Remove documentation snippet | ||
snippets.splice(0, 1); | ||
|
||
const snapshots: string[] = ['# File generated from `indentation-cases.R`.\n\n']; | ||
|
||
for (const snippet of snippets) { | ||
await withFileEditor(snippet, 'R', async (_editor, doc) => { | ||
// Type one newline character to trigger indentation | ||
await type(doc, `\n${CURSOR}`); | ||
snapshots.push(doc.getText()); | ||
}); | ||
} | ||
|
||
return snapshots.join('# --- \n'); | ||
} |
3 changes: 3 additions & 0 deletions
3
extensions/positron-r/src/test/snapshots/.vscode/settings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"workbench.localHistory.enabled": false | ||
} |
82 changes: 82 additions & 0 deletions
82
extensions/positron-r/src/test/snapshots/indentation-cases.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Indentation snapshots. Edit cases in `indentation-cases.R` and observe results | ||
# in `indentation-snapshots.R`. | ||
# | ||
# The cursor position is represented by a string containing angular brackets. A | ||
# newline is typed at that position, triggering indentation rules. The result is | ||
# saved in the snapshot file. | ||
# | ||
# Snippets are separated by `# ---`. This makes it possible to extract them and | ||
# process them separately to prevent interferences between test cases. | ||
|
||
# --- | ||
1 +"<>" | ||
|
||
# --- | ||
1 + | ||
2 +"<>" | ||
|
||
# --- | ||
data |>"<>" | ||
|
||
# --- | ||
data |> | ||
fn()"<>" | ||
|
||
# --- | ||
# https://github.com/posit-dev/positron/issues/1727 | ||
# FIXME | ||
data |> | ||
fn() | ||
"<>" | ||
|
||
# --- | ||
# https://github.com/posit-dev/positron/issues/1316 | ||
data |> | ||
fn() |>"<>" | ||
|
||
# --- | ||
# With trailing whitespace | ||
# https://github.com/posit-dev/positron/pull/1655#issuecomment-1780093395 | ||
data |> | ||
fn() |> "<>" | ||
|
||
# --- | ||
data |> | ||
fn1() |> | ||
fn2() |>"<>" | ||
|
||
# --- | ||
# FIXME | ||
data |> | ||
fn1() |> | ||
fn2( | ||
"arg" | ||
)"<>" | ||
|
||
# --- | ||
# https://github.com/posit-dev/positron-beta/discussions/46 | ||
# FIXME | ||
data |> | ||
fn("<>") | ||
|
||
# --- | ||
# FIXME | ||
{ | ||
fn(function() {}"<>") | ||
} | ||
|
||
# --- | ||
# FIXME | ||
{ | ||
fn(function() { | ||
# | ||
}"<>") | ||
} | ||
|
||
# --- | ||
for (i in NA) NULL"<>" | ||
|
||
# --- | ||
# https://github.com/posit-dev/positron/issues/1880 | ||
# FIXME | ||
for (i in 1) fn()"<>" |
88 changes: 88 additions & 0 deletions
88
extensions/positron-r/src/test/snapshots/indentation-snapshots.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# File generated from `indentation-cases.R`. | ||
|
||
# --- | ||
1 + | ||
"<>" | ||
|
||
# --- | ||
1 + | ||
2 + | ||
"<>" | ||
|
||
# --- | ||
data |> | ||
"<>" | ||
|
||
# --- | ||
data |> | ||
fn() | ||
"<>" | ||
|
||
# --- | ||
# https://github.com/posit-dev/positron/issues/1727 | ||
# FIXME | ||
data |> | ||
fn() | ||
|
||
"<>" | ||
|
||
# --- | ||
# https://github.com/posit-dev/positron/issues/1316 | ||
data |> | ||
fn() |> | ||
"<>" | ||
|
||
# --- | ||
# With trailing whitespace | ||
# https://github.com/posit-dev/positron/pull/1655#issuecomment-1780093395 | ||
data |> | ||
fn() |> | ||
"<>" | ||
|
||
# --- | ||
data |> | ||
fn1() |> | ||
fn2() |> | ||
"<>" | ||
|
||
# --- | ||
# FIXME | ||
data |> | ||
fn1() |> | ||
fn2( | ||
"arg" | ||
) | ||
"<>" | ||
|
||
# --- | ||
# https://github.com/posit-dev/positron-beta/discussions/46 | ||
# FIXME | ||
data |> | ||
fn( | ||
"<>") | ||
|
||
# --- | ||
# FIXME | ||
{ | ||
fn(function() {} | ||
"<>") | ||
} | ||
|
||
# --- | ||
# FIXME | ||
{ | ||
fn(function() { | ||
# | ||
} | ||
"<>") | ||
} | ||
|
||
# --- | ||
for (i in NA) NULL | ||
"<>" | ||
|
||
# --- | ||
# https://github.com/posit-dev/positron/issues/1880 | ||
# FIXME | ||
for (i in 1) fn() | ||
"<>" |