Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/python/pylang/test/repl.py
1396 views
1
# DISABLED -- because mysteriously the instrumentation of the console, etc., isn't working.
2
3
stdout = []
4
5
DEBUG = False
6
7
8
def clear():
9
stdout.length = 0
10
11
12
class FakeConsole:
13
def log(self):
14
def f(arg):
15
if DEBUG: console.log("stdout: '%s'", arg)
16
stdout.push((arg or '').toString())
17
18
Array.prototype.slice.call(arguments).forEach(f)
19
stdout.push('\n')
20
21
def error(self):
22
def f(arg):
23
console.error(arg) # we also just show these as normal
24
stdout.push((arg or '').toString())
25
26
Array.prototype.slice.call(arguments).forEach(f)
27
stdout.push('\n')
28
29
30
class FakeReadline:
31
def __init__(self):
32
self.listeners = {}
33
self._prompt = ''
34
35
def setPrompt(self, prompt):
36
self._prompt = prompt
37
38
def write(self, data):
39
stdout.push(data)
40
41
def clearLine(self):
42
pass
43
44
def on(self, event, callback):
45
self.listeners[event] = callback
46
return self
47
48
def prompt(self):
49
stdout.push(self._prompt)
50
51
def send_line(self, text):
52
if DEBUG: console.log("send: '%s'", text)
53
self.listeners['line'](text)
54
55
56
repl = require('./repl').default
57
rl = FakeReadline()
58
59
send_line = rl.send_line.bind(rl)
60
61
eq = assrt.equal
62
63
64
def send_text(text):
65
for line in text.split('\n'):
66
send_line(line)
67
68
69
def check(text, output):
70
send_text(text)
71
eq(output, stdout[0])
72
clear()
73
74
75
def check_in(text, output):
76
send_text(text)
77
assrt.ok(output in stdout, output + ' not in ' + stdout)
78
clear()
79
80
81
def check_not_in(text, output):
82
send_text(text)
83
assrt.ok(output not in stdout)
84
clear()
85
86
87
def mockReadline(options):
88
rl.completer = options.completer
89
return rl
90
91
92
repl({
93
'console': FakeConsole(),
94
'mockReadline': mockReadline,
95
'terminal': False,
96
'show_js': False,
97
'histfile': False,
98
'ps1': '>>>'
99
})
100
101
102
def test_basics():
103
print(stdout)
104
eq('>>> ', stdout[-1])
105
clear()
106
check('1', '1')
107
check_in('if 1:\n 2\n \n ', '2')
108
check_in('1 +\n1\n\n', '2')
109
check('max(1, 2)', '2')
110
send_text('''
111
class A:
112
def __init__(self, a):
113
self.a = a
114
''')
115
clear()
116
check_in('b = A(1)\nb.a', '1')
117
check_in('c = A(2)\nc.a', '2')
118
send_text('from __python__ import dict_literals\nd={1:1}')
119
check_in('isinstance(d, dict)', 'True')
120
send_text('from __python__ import no_dict_literals\nd={1:1}')
121
check_in('isinstance(d, dict)', 'False')
122
123
124
test_basics()
125
126
127
def test_completions():
128
# Test completions
129
def completions(line):
130
return rl.completer(line)[0]
131
132
def check_completions():
133
items = completions(arguments[0])
134
for x in Array.prototype.slice.call(arguments, 1):
135
assrt.ok(items and x in items,
136
x + ' not in completions for: ' + arguments[0])
137
138
check_completions('', 'return', 'A')
139
check_completions('Array.', 'isArray', 'apply')
140
send_text('x = ""\ny = []'), clear()
141
check_completions('x.', 'substr', 'trim')
142
check_completions('y.', 'concat', 'push')
143
check_completions('x.sl', 'slice')
144
send_text('y = {"x":1}'), clear()
145
check_completions('y.', 'x')
146
147
# Test docstrings
148
clear()
149
send_text('def ds():\n "xxx"\n\n')
150
clear()
151
check('ds.__doc__', "xxx")
152
153
154
test_completions()
155
156
157
def test_semicolons():
158
check("a=2;b=3;print(a+b)", "5")
159
check("2+3", "5")
160
check("import math; print(math.sin(1))", "0.8414709848078965")
161
162
163
test_semicolons()
164
165