-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic unit test for workspace viewer (#1305)
- Introduces basic unit testing for the workspace viewer - Proposes unit testing through mocking the vscode API via the sinon library
- Loading branch information
Showing
9 changed files
with
3,020 additions
and
2,627 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
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 @@ | ||
export * from './mockvscode'; |
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,37 @@ | ||
import * as vscode from 'vscode'; | ||
import sinon = require('sinon'); | ||
import path = require('path'); | ||
import * as ext from '../../extension'; | ||
|
||
export function mockActiveTextEditor(document: vscode.TextDocument, sandbox: sinon.SinonSandbox) { | ||
return sandbox.stub(vscode.window, 'activeTextEditor').value({ | ||
document | ||
}); | ||
} | ||
|
||
export function mockExtensionContext(extension_root: string, sandbox: sinon.SinonSandbox) { | ||
const mockExtensionContext = { | ||
environmentVariableCollection: sandbox.stub(), | ||
extension: sandbox.stub(), | ||
extensionMode: sandbox.stub(), | ||
extensionPath: sandbox.stub(), | ||
extensionUri: sandbox.stub(), | ||
globalState: { | ||
get: sinon.stub(), | ||
set: sinon.stub() | ||
}, | ||
globalStorageUri: sandbox.stub(), | ||
logUri: sandbox.stub(), | ||
secrets: sandbox.stub(), | ||
storageUri: sandbox.stub(), | ||
subscriptions: [], | ||
workspaceState: { | ||
get: sinon.stub(), | ||
update: sinon.stub() | ||
}, | ||
asAbsolutePath: (relativePath: string) => { | ||
return path.join(extension_root, relativePath); | ||
} | ||
}; | ||
return sandbox.stub(ext, 'extensionContext').value(mockExtensionContext); | ||
} |
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,55 @@ | ||
import vscode = require('vscode'); | ||
import sinon = require('sinon'); | ||
import path = require('path'); | ||
import * as assert from 'assert'; | ||
import * as fs from 'fs-extra'; | ||
|
||
import { mockExtensionContext } from '../common'; | ||
import * as session from '../../session'; | ||
import * as workspace from '../../workspaceViewer'; | ||
|
||
const extension_root: string = path.join(__dirname, '..', '..', '..'); | ||
const workspaceFile = path.join(extension_root, 'test', 'rFiles', 'session', 'workspace.json'); | ||
|
||
function mockWorkspaceData(sandbox: sinon.SinonSandbox) { | ||
const content = fs.readFileSync(workspaceFile, 'utf8'); | ||
const workspaceData = JSON.parse(content) as session.WorkspaceData; | ||
return sandbox.stub(session, 'workspaceData').value(workspaceData); | ||
} | ||
|
||
suite('Workspace Viewer', () => { | ||
let sandbox: sinon.SinonSandbox; | ||
let workspaceViewer: workspace.WorkspaceDataProvider; | ||
let nodes: vscode.TreeItem[]; | ||
|
||
setup(() => { | ||
sandbox = sinon.createSandbox(); | ||
}); | ||
teardown(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
test('has 3 nodes', async () => { | ||
mockExtensionContext(extension_root, sandbox); | ||
mockWorkspaceData(sandbox); | ||
workspaceViewer = new workspace.WorkspaceDataProvider(); | ||
workspaceViewer.refresh(); | ||
nodes = await workspaceViewer.getChildren(); | ||
assert.strictEqual(nodes.length, 3); | ||
}); | ||
|
||
test('search node', async () => { | ||
const search = await workspaceViewer.getChildren(nodes[0]); | ||
assert.strictEqual(search.length, 10); | ||
}); | ||
|
||
test('attached node', async () => { | ||
const attached = await workspaceViewer.getChildren(nodes[1]); | ||
assert.strictEqual(attached.length, 14); | ||
}); | ||
|
||
test('env node', async () => { | ||
const env: workspace.GlobalEnvItem[] = await workspaceViewer.getChildren(nodes[2]) as workspace.GlobalEnvItem[]; | ||
assert.strictEqual(env.length, 9); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
title: Hello World | ||
--- | ||
|
||
Hello world |
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,147 @@ | ||
{ | ||
"search": [ | ||
"tools:vscode", | ||
"package:stats", | ||
"package:graphics", | ||
"package:grDevices", | ||
"package:datasets", | ||
"tools:custom", | ||
"package:utils", | ||
"package:methods", | ||
"Autoloads", | ||
"package:base" | ||
], | ||
"loaded_namespaces": [ | ||
"compiler", | ||
"cli", | ||
"graphics", | ||
"tools", | ||
"utils", | ||
"grDevices", | ||
"crayon", | ||
"stats", | ||
"datasets", | ||
"methods", | ||
"jsonlite", | ||
"rlang", | ||
"base", | ||
"pak" | ||
], | ||
"globalenv": { | ||
"a": { | ||
"class": [ | ||
"character" | ||
], | ||
"type": "character", | ||
"length": 1, | ||
"size": 112, | ||
"str": "chr \"a\"" | ||
}, | ||
"b": { | ||
"class": [ | ||
"integer" | ||
], | ||
"type": "integer", | ||
"length": 1, | ||
"size": 56, | ||
"str": "int 1" | ||
}, | ||
"c": { | ||
"class": [ | ||
"numeric" | ||
], | ||
"type": "double", | ||
"length": 1, | ||
"size": 56, | ||
"str": "num 1.5" | ||
}, | ||
"d": { | ||
"class": [ | ||
"complex" | ||
], | ||
"type": "complex", | ||
"length": 1, | ||
"size": 64, | ||
"str": "cplx 0+0i" | ||
}, | ||
"e": { | ||
"class": [ | ||
"logical" | ||
], | ||
"type": "logical", | ||
"length": 1, | ||
"size": 56, | ||
"str": "logi TRUE" | ||
}, | ||
"f": { | ||
"class": [ | ||
"function" | ||
], | ||
"type": "closure", | ||
"length": 1, | ||
"size": 1240, | ||
"str": "function (x, ...)" | ||
}, | ||
"g": { | ||
"class": [ | ||
"environment" | ||
], | ||
"type": "environment", | ||
"length": 11, | ||
"size": 56, | ||
"str": "<environment: R_GlobalEnv>", | ||
"names": [ | ||
"a", | ||
"b", | ||
"c", | ||
"d", | ||
"e", | ||
"f", | ||
".local.name", | ||
"g", | ||
".local", | ||
"h", | ||
"i" | ||
] | ||
}, | ||
"h": { | ||
"class": [ | ||
"list" | ||
], | ||
"type": "list", | ||
"length": 2, | ||
"size": 520, | ||
"str": "List of 2\n $ a: num 1\n $ b: chr \"foo\"", | ||
"names": [ | ||
"a", | ||
"b" | ||
] | ||
}, | ||
"i": { | ||
"class": [ | ||
"data.frame" | ||
], | ||
"type": "list", | ||
"length": 11, | ||
"size": 7208, | ||
"str": "'data.frame':\t32 obs. of 11 variables:\n $ mpg : num 21 21 ...\n $ cyl : num 6 6 ...\n $ disp: num 160 160 ...\n $ hp : num 110 110 ...\n $ drat: num 3.9 3.9 ...\n $ wt : num 2.62 ...\n $ qsec: num 16.5 ...\n $ vs : num 0 0 ...\n $ am : num 1 1 ...\n $ gear: num 4 4 ...\n $ carb: num 4 4 ...", | ||
"names": [ | ||
"mpg", | ||
"cyl", | ||
"disp", | ||
"hp", | ||
"drat", | ||
"wt", | ||
"qsec", | ||
"vs", | ||
"am", | ||
"gear", | ||
"carb" | ||
], | ||
"dim": [ | ||
32, | ||
11 | ||
] | ||
} | ||
} | ||
} |
Oops, something went wrong.