-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcljParser.test.ts
More file actions
137 lines (128 loc) · 5.53 KB
/
Copy pathcljParser.test.ts
File metadata and controls
137 lines (128 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import * as assert from 'assert';
import * as path from 'path';
import * as vscode from 'vscode';
import { cljParser } from '../src/cljParser';
import { setLongTimeout } from './utils';
const testFolderLocation = '/../../test/documents/';
suite('cljParser.getNamespace', function() {
let cases = [
['user', ''],
['foo', '(ns foo)'],
['foo', '\n(ns foo)'],
['foo', '\t(ns foo)'],
['foo', '\t(ns\tfoo)'],
['foo-bar', '(ns foo-bar)'],
['bar', '(ns bar)'],
['baz', '(ns baz "docstring")'],
['qux', `(ns qux
"docstring")`],
['foo.bar', '(ns foo.bar)'],
['foo.bar-baz', '(ns foo.bar-baz)'],
['foo.bar', `(ns foo.bar
(:refer-clojure :exclude [ancestors printf])
(:require (clojure.contrib sql combinatorics))
(:use (my.lib this that))
(:import (java.util Date Timer Random)
(java.sql Connection Statement)))`],
['bar', '(in-ns \'bar)'],
];
for (let [want, input] of cases) {
test(`getNamespace("${input}") should be "${want}"`, () => {
assert.equal(cljParser.getNamespace(input), want);
});
}
});
suite('cljParser.getCurrentBlock', function() {
// title, line, character, expected
setLongTimeout(this);
let cases: [string, number, number, string | undefined][] = [
['Position on the same line', 16, 9, '(prn "test")'],
['Position at the middle of multiline block', 22, 5,
'(->> numbers\n' +
' (map inc)\n' +
' (prn))'],
['Ignore inline clj comments', 19, 16,
'(let [numbers [1 2 3]\n' +
' VAL (atom {:some "DATA"})]\n' +
' ; missing left bracket prn "hided text") in comment\n' +
' (prn [@VAL])\n' +
' (->> numbers\n' +
' (map inc)\n' +
' (prn)))'],
['Comment form will be evaluated', 27, 14, '(prn "COMMENT")'],
['Eval only outside bracket from right side', 23, 11, '(prn)'],
['Eval only outside bracket from left side', 28, 5,
'(comp #(str % "!") name)'],
['Eval only round bracket block', 20, 12, '(prn [@VAL])'],
['Eval when only inside of the block', 24, 0, undefined],
['Begin of file', 0, 0,
'(ns user\n' +
' (:require [clojure.tools.namespace.repl :refer [set-refresh-dirs]]\n' +
' [reloaded.repl :refer [system stop go reset]]\n' +
' [myproject.config :refer [config]]\n' +
' [myproject.core :refer [new-system]]))'],
['End of file', 37, 0, undefined]
];
testBlockSelection('Eval current block', cljParser.getCurrentBlock, 'evalBlock.clj', cases);
});
suite('cljParser.getOuterBlock', function() {
// title, line, character, expected
setLongTimeout(this);
let cases: [string, number, number, string | undefined][] = [
['Get outermost block of function definition', 11, 20,
'(defn new-system-dev\n' +
' []\n' +
' (let [_ 1]\n' +
' (new-system (config))))'],
['Outer block from outside left bracket', 8, 0,
'(defn new-system-dev\n' +
' []\n' +
' (let [_ 1]\n' +
' (new-system (config))))'],
['Outer block from outside right bracket', 28, 38,
'(comment\n' +
' (do\n' +
' #_(prn "COMMENT")\n' +
' ((comp #(str % "!") name) :test)))'],
['Outer block not found', 24, 0, undefined],
['Begin of file', 0, 0,
'(ns user\n' +
' (:require [clojure.tools.namespace.repl :refer [set-refresh-dirs]]\n' +
' [reloaded.repl :refer [system stop go reset]]\n' +
' [myproject.config :refer [config]]\n' +
' [myproject.core :refer [new-system]]))'],
['End of file', 37, 0, undefined],
];
testBlockSelection('Eval outer block', cljParser.getOuterBlock, 'evalBlock.clj', cases);
});
function testBlockSelection(
title: string,
getBlockFn: (editor: vscode.TextEditor) => vscode.Selection | undefined,
fileName: string,
cases: [string, number, number, string | undefined][]) {
test(title, async () => {
const editor = await getEditor(fileName);
for (let [title, line, character, expected] of cases) {
const currentPosition = new vscode.Position(line, character);
editor.selection = new vscode.Selection(currentPosition, currentPosition);
const blockSelection = getBlockFn(editor),
text = blockSelection ? editor.document.getText(blockSelection) : blockSelection;
assert.equal(text, expected, title)
};
vscode.commands.executeCommand('workbench.action.closeActiveEditor');
});
};
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
async function getEditor(fileName: string): Promise<vscode.TextEditor> {
const configuration = vscode.workspace.getConfiguration();
configuration.update("clojureVSCode.autoStartNRepl", false);
const uri = vscode.Uri.file(path.join(__dirname + testFolderLocation + fileName)),
document = await vscode.workspace.openTextDocument(uri),
editor = await vscode.window.showTextDocument(document);
await sleep(1500);
return editor;
};