CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/jupyter/test/completion.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/*
7
Test completion API
8
*/
9
10
import {} from "mocha";
11
import * as expect from "expect";
12
import * as common from "./common";
13
14
// global kernel being tested at any point.
15
let kernel: common.JupyterKernel;
16
17
// This checks that on input the given obj={code:?, cursor_pos:?}
18
// the resulting matches *contains* matches
19
function check(obj: { code: string; cursor_pos: any }, matches?: string[]) {
20
it(`checks that ${JSON.stringify(obj)} includes ${
21
matches ? JSON.stringify(matches) : "nothing"
22
}`, async function () {
23
const resp = await kernel.complete({
24
code: obj.code,
25
cursor_pos: obj.cursor_pos != null ? obj.cursor_pos : obj.code.length,
26
});
27
if (matches === undefined) {
28
expect(resp.matches.length).toBe(0);
29
} else {
30
for (const m of matches) {
31
expect(resp.matches).toContain(m);
32
}
33
}
34
});
35
}
36
37
describe("complete some things using python2 kernel -- ", function () {
38
this.timeout(10000);
39
40
it("creates a python2 kernel", function () {
41
kernel = common.kernel("test-python2");
42
});
43
44
it("completes 'imp'", async function () {
45
const resp = await kernel.complete({
46
code: "imp",
47
cursor_pos: 2,
48
});
49
expect(resp).toEqual({
50
matches: ["import"],
51
status: "ok",
52
cursor_start: 0,
53
cursor_end: 2,
54
});
55
});
56
57
check({ code: "imp", cursor_pos: 3 }, ["import"]);
58
check({ code: "in", cursor_pos: 2 }, ["in", "input", "int", "intern"]);
59
check({ code: "in", cursor_pos: 1 }, [
60
"id",
61
"if",
62
"import",
63
"in",
64
"input",
65
"int",
66
"intern",
67
"is",
68
"isinstance",
69
"issubclass",
70
"iter",
71
]);
72
73
check({ code: "alsdfl", cursor_pos: 5 });
74
75
it("creates a new identifier", async function () {
76
await common.exec(kernel, 'alsdfl = {"foo":"bar"}');
77
});
78
79
check({ code: "alsdfl", cursor_pos: 6 }, ["alsdfl"]);
80
81
check({ code: "alsdfl._", cursor_pos: 8 }, [
82
"alsdfl.__class__",
83
"alsdfl.__cmp__",
84
]);
85
86
it("closes the kernel", () => kernel.close());
87
});
88
89
describe("complete some things using sage kernel -- ", function () {
90
this.timeout(30000); // sage can be very slow to start.
91
92
it("creates a sage kernel", function () {
93
kernel = common.kernel("test-sagemath");
94
});
95
96
check({ code: "Ell", cursor_pos: 3 }, [
97
"Ellipsis",
98
"EllipticCurve",
99
"EllipticCurve_from_c4c6",
100
"EllipticCurve_from_cubic",
101
"EllipticCurve_from_j",
102
"EllipticCurve_from_plane_curve",
103
"EllipticCurveIsogeny",
104
"EllipticCurves_with_good_reduction_outside_S",
105
]);
106
107
check({ code: "e.", cursor_pos: 2 }, [
108
"e.abs",
109
"e.add",
110
"e.add_to_both_sides",
111
"e.additive_order",
112
"e.arccos",
113
]);
114
check({ code: "e.fac", cursor_pos: 5 }, ["e.factor"]);
115
116
it("closes the kernel", () => kernel.close());
117
});
118
119