Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/python/python-wasm/src/test/python.test.ts
1067 views
1
import { syncPython } from "../node";
2
3
test("add 2+3", async () => {
4
const { exec, repr } = await syncPython();
5
exec("a = 2+3");
6
expect(repr("a")).toBe("5");
7
});
8
9
10
test("Exec involving multiline statement", async () => {
11
const { exec, repr } = await syncPython();
12
exec(`
13
def double(n : int) -> int:
14
return 2*n
15
16
def f(n : int) -> int:
17
s = 0
18
for i in range(1,n+1):
19
s += i
20
return double(s);
21
`);
22
expect(repr("f(100)")).toBe(`${5050 * 2}`);
23
});
24
25
test("sys.platform is wasi", async () => {
26
const { exec, repr } = await syncPython();
27
exec("import sys");
28
expect(repr("sys.platform")).toBe("'wasi'");
29
});
30
31
test("sleeping for a quarter of a second", async () => {
32
const { exec } = await syncPython();
33
const t0 = new Date().valueOf();
34
exec("import time; time.sleep(0.25)");
35
const t = new Date().valueOf() - t0;
36
expect(t >= 240 && t <= 500).toBe(true);
37
});
38
39
test("that sys.executable is set to something", async () => {
40
const { exec, repr } = await syncPython();
41
exec("import sys");
42
const executable = eval(repr("sys.executable"));
43
expect(existsSync(executable)).toBe(true);
44
expect(executable.length).toBeGreaterThan(0);
45
});
46
47
import { execFileSync } from "child_process";
48
import { existsSync } from "fs";
49
test("that sys.executable is set and exists -- when running python-wasm via command line", () => {
50
const stdout = execFileSync("./bin/python-wasm", [
51
"-c",
52
"import sys; print(sys.executable)",
53
])
54
.toString()
55
.trim();
56
expect(existsSync(stdout)).toBe(true);
57
});
58
59
test("test that getcwd works", async () => {
60
const { exec, repr, kernel } = await syncPython();
61
await exec("import os; os.chdir('/tmp')");
62
const cwd = eval(repr('os.path.abspath(".")'));
63
expect(cwd).toBe("/tmp");
64
// this is the interesting call:
65
expect(kernel.getcwd()).toBe("/tmp");
66
});
67
68
test("test that getcwd is fast", async () => {
69
const { exec, repr, kernel } = await syncPython();
70
await exec("import os; os.chdir('/tmp')");
71
const t0 = new Date().valueOf();
72
for (let i = 0; i < 10 ** 5; i++) {
73
kernel.getcwd();
74
}
75
// Doing 10**6 of them takes about a second on my laptop,
76
// so 2s should be safe for a test.
77
expect(new Date().valueOf() - t0).toBeLessThan(2000);
78
const cwd = eval(repr('os.path.abspath(".")'));
79
expect(cwd).toBe("/tmp");
80
});
81
82
test("an annoying-to-real-mathematicians new 'feature' of Python can be disabled", async () => {
83
// See https://discuss.python.org/t/int-str-conversions-broken-in-latest-python-bugfix-releases/18889/184
84
const { exec, repr } = await syncPython();
85
exec("import sys; sys.set_int_max_str_digits(0)");
86
expect(repr("len(str(10**5000))")).toEqual("5001");
87
});
88
89