Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/project/jupyter/test/completion.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Test completion API7*/89import {} from "mocha";10import * as expect from "expect";11import * as common from "./common";1213// global kernel being tested at any point.14let kernel: common.JupyterKernel;1516// This checks that on input the given obj={code:?, cursor_pos:?}17// the resulting matches *contains* matches18function check(obj: { code: string; cursor_pos: any }, matches?: string[]) {19it(`checks that ${JSON.stringify(obj)} includes ${20matches ? JSON.stringify(matches) : "nothing"21}`, async function () {22const resp = await kernel.complete({23code: obj.code,24cursor_pos: obj.cursor_pos != null ? obj.cursor_pos : obj.code.length,25});26if (matches === undefined) {27expect(resp.matches.length).toBe(0);28} else {29for (const m of matches) {30expect(resp.matches).toContain(m);31}32}33});34}3536describe("complete some things using python2 kernel -- ", function () {37this.timeout(10000);3839it("creates a python2 kernel", function () {40kernel = common.kernel("test-python2");41});4243it("completes 'imp'", async function () {44const resp = await kernel.complete({45code: "imp",46cursor_pos: 2,47});48expect(resp).toEqual({49matches: ["import"],50status: "ok",51cursor_start: 0,52cursor_end: 2,53});54});5556check({ code: "imp", cursor_pos: 3 }, ["import"]);57check({ code: "in", cursor_pos: 2 }, ["in", "input", "int", "intern"]);58check({ code: "in", cursor_pos: 1 }, [59"id",60"if",61"import",62"in",63"input",64"int",65"intern",66"is",67"isinstance",68"issubclass",69"iter",70]);7172check({ code: "alsdfl", cursor_pos: 5 });7374it("creates a new identifier", async function () {75await common.exec(kernel, 'alsdfl = {"foo":"bar"}');76});7778check({ code: "alsdfl", cursor_pos: 6 }, ["alsdfl"]);7980check({ code: "alsdfl._", cursor_pos: 8 }, [81"alsdfl.__class__",82"alsdfl.__cmp__",83]);8485it("closes the kernel", () => kernel.close());86});8788describe("complete some things using sage kernel -- ", function () {89this.timeout(30000); // sage can be very slow to start.9091it("creates a sage kernel", function () {92kernel = common.kernel("test-sagemath");93});9495check({ code: "Ell", cursor_pos: 3 }, [96"Ellipsis",97"EllipticCurve",98"EllipticCurve_from_c4c6",99"EllipticCurve_from_cubic",100"EllipticCurve_from_j",101"EllipticCurve_from_plane_curve",102"EllipticCurveIsogeny",103"EllipticCurves_with_good_reduction_outside_S",104]);105106check({ code: "e.", cursor_pos: 2 }, [107"e.abs",108"e.add",109"e.add_to_both_sides",110"e.additive_order",111"e.arccos",112]);113check({ code: "e.fac", cursor_pos: 5 }, ["e.factor"]);114115it("closes the kernel", () => kernel.close());116});117118119