Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50640 views
1
# test_sagews_modes.py
2
# tests of sage worksheet modes
3
import pytest
4
import conftest
5
import re
6
import os
7
from textwrap import dedent
8
9
class TestP3Mode:
10
"""
11
not the same as python3 mode, which is an alias for anaconda3
12
"""
13
def test_p3a(self, exec2):
14
exec2("p3 = jupyter('python3')")
15
def test_p3b(self, exec2):
16
exec2("%p3\nimport sys\nprint(sys.version)", pattern=r"^3\.5\.\d+ ")
17
18
class TestSingularMode:
19
def test_singular_version(self, exec2):
20
exec2('%singular_kernel\nsystem("version");','4103\n')
21
def test_singular_factor_polynomial(self, exec2):
22
code = dedent('''
23
%singular_kernel
24
ring R1 = 0,(x,y),dp;
25
poly f = 9x16 - 18x13y2 - 9x12y3 + 9x10y4 - 18x11y2 + 36x8y4 + 18x7y5 - 18x5y6 + 9x6y4 - 18x3y6 - 9x2y7 + 9y8;
26
factorize(f);''').strip()
27
exec2(code,
28
u'[1]:\n _[1]=9\n _[2]=x6-2x3y2-x2y3+y4\n _[3]=-x5+y2\n[2]:\n 1,1,2\n')
29
30
class TestScalaMode:
31
def test_scala_list(self, exec2):
32
exec2("%scala\nList(1,2,3)", html_pattern="res0.*List.*Int.*List.*1.*2.*3")
33
34
class TestScala211Mode:
35
# example from ScalaTour-1.6, p. 31, Pattern Matching
36
# http://www.scala-lang.org/docu/files/ScalaTour-1.6.pdf
37
def test_scala211_pat1(self, exec2):
38
code = dedent('''
39
%scala211
40
object MatchTest1 extends App {
41
def matchTest(x: Int): String = x match {
42
case 1 => "one"
43
case 2 => "two"
44
case _ => "many"
45
}
46
println(matchTest(3))
47
}
48
''').strip()
49
exec2(code, html_pattern="defined.*object.*MatchTest1")
50
51
def test_scala211_pat2(self, exec2):
52
exec2("%scala211\nMatchTest1.main(Array())", pattern="many")
53
54
def test_scala_version(self, exec2):
55
exec2("%scala211\nutil.Properties.versionString", html_pattern="2.11.11")
56
57
class TestPython3Mode:
58
def test_p3_max(self, exec2):
59
exec2("%python3\nmax([],default=9)", "9")
60
61
def test_p3_version(self, exec2):
62
exec2("%python3\nimport sys\nprint(sys.version)", pattern=r"^3\.5\.\d+ ")
63
64
def test_capture_p3_01(self, exec2):
65
exec2("%capture(stdout='output')\n%python3\nimport numpy as np\nnp.arange(9).reshape(3,3).trace()")
66
def test_capture_p3_02(self, exec2):
67
exec2("print(output)", "12\n")
68
69
def test_p3_latex(self, exec2):
70
code = r"""%python3
71
from IPython.display import Math
72
Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')"""
73
htmp = r"""\$\$F\(k\) = \\int_\{-\\infty\}\^\{\\infty\} f\(x\) e\^\{2\\pi i k\} dx\$\$"""
74
exec2(code, html_pattern = htmp)
75
76
def test_p3_pandas(self, exec2):
77
code = dedent('''
78
%python3
79
import pandas as pd
80
from io import StringIO
81
82
df_csv = r"""Item,Category,Quantity,Weight
83
Pack,Pack,1,33.0
84
Tent,Shelter,1,80.0
85
Sleeping Pad,Sleep,0,27.0
86
Sleeping Bag,Sleep,1,20.0
87
Shoes,Clothing,1,12.0
88
Hat,Clothing,1,2.5"""
89
mydata = pd.read_csv(StringIO(df_csv))
90
mydata.shape''').strip()
91
exec2(code,"(6, 4)")
92
93
def test_p3_autocomplete(self, execintrospect):
94
execintrospect('myd', ["ata"], 'myd', '%python3')
95
96
class TestPython3DefaultMode:
97
def test_set_python3_mode(self, exec2):
98
exec2("%default_mode python3")
99
def test_python3_assignment(self, exec2):
100
exec2("xx=[2,5,99]\nsum(xx)", "106")
101
102
def test_capture_p3d_01(self, exec2):
103
exec2("%capture(stdout='output')\nmax(xx)")
104
def test_capture_p3d_02(self, exec2):
105
exec2("%sage\nprint(output)", "99\n")
106
107
class TestShMode:
108
def test_start_sh(self, exec2):
109
code = "%sh\ndate +%Y-%m-%d"
110
patn = r'\d{4}-\d{2}-\d{2}'
111
exec2(code, pattern=patn)
112
113
# examples from sh mode docstring in sage_salvus.py
114
# note jupyter kernel text ouput is displayed as html
115
def test_single_line(self, exec2):
116
exec2("%sh uptime\n", pattern="\d\.\d")
117
118
def test_multiline(self, exec2):
119
exec2("%sh\nFOO=hello\necho $FOO", pattern="hello")
120
121
def test_direct_call(self, exec2):
122
exec2("sh('date +%Y-%m-%d')", pattern = r'\d{4}-\d{2}-\d{2}')
123
124
def test_capture_sh_01(self, exec2):
125
exec2("%capture(stdout='output')\n%sh uptime")
126
def test_capture_sh_02(self, exec2):
127
exec2("output", pattern="up.*user.*load average")
128
129
def test_remember_settings_01(self, exec2):
130
exec2("%sh FOO='testing123'")
131
def test_remember_settings_02(self, exec2):
132
exec2("%sh echo $FOO", pattern=r"^testing123\s+")
133
134
def test_sh_display(self, execblob, image_file):
135
execblob("%sh display < " + str(image_file), want_html=False)
136
137
def test_sh_autocomplete_01(self, exec2):
138
exec2("%sh TESTVAR29=xyz")
139
def test_sh_autocomplete_02(self, execintrospect):
140
execintrospect('echo $TESTV', ["AR29"], '$TESTV', '%sh')
141
142
def test_bad_command(self, exec2):
143
exec2("%sh xyz", pattern="command not found")
144
145
class TestShDefaultMode:
146
def test_start_sh_dflt(self, exec2):
147
exec2("%default_mode sh")
148
149
def test_multiline_dflt(self, exec2):
150
exec2("FOO=hello\necho $FOO", pattern="^hello")
151
152
def test_date(self, exec2):
153
exec2("date +%Y-%m-%d", pattern = r'^\d{4}-\d{2}-\d{2}')
154
155
def test_capture_sh_01_dflt(self, exec2):
156
exec2("%capture(stdout='output')\nuptime")
157
def test_capture_sh_02_dflt(self, exec2):
158
exec2("%sage\noutput", pattern="up.*user.*load average")
159
160
def test_remember_settings_01_dflt(self, exec2):
161
exec2("FOO='testing123'")
162
def test_remember_settings_02_dflt(self, exec2):
163
exec2("echo $FOO", pattern=r"^testing123\s+")
164
165
def test_sh_display_dflt(self, execblob, image_file):
166
execblob("display < " + str(image_file), want_html=False)
167
168
def test_sh_autocomplete_01_dflt(self, exec2):
169
exec2("TESTVAR29=xyz")
170
def test_sh_autocomplete_02_dflt(self, execintrospect):
171
execintrospect('echo $TESTV', ["AR29"], '$TESTV')
172
173
class TestRMode:
174
def test_r_assignment(self, exec2):
175
exec2("%r\nxx <- c(4,7,13)\nmean(xx)", html_pattern="^8$")
176
177
def test_r_version(self, exec2):
178
exec2("%r\nR.version.string", html_pattern=r"\d+\.\d+\.\d+")
179
180
def test_capture_r_01(self, exec2):
181
exec2("%capture(stdout='output')\n%r\nsum(xx)")
182
def test_capture_r_02(self, exec2):
183
exec2("print(output)", "24\n")
184
185
class TestRDefaultMode:
186
def test_set_r_mode(self, exec2):
187
exec2("%default_mode r")
188
def test_rdflt_assignment(self, exec2):
189
exec2("xx <- c(4,7,13)\nmean(xx)", html_pattern="^8$")
190
191
def test_dflt_capture_r_01(self, exec2):
192
exec2("%capture(stdout='output')\nsum(xx)")
193
def test_dflt_capture_r_02(self, exec2):
194
exec2("%sage\nprint(output)", "24\n")
195
196
class TestRWD:
197
"issue 240"
198
def test_wd0(self, exec2, data_path):
199
dp = data_path.strpath
200
code = "os.chdir('%s')"%dp
201
exec2(code)
202
203
def test_wd(self, exec2, data_path):
204
dp = data_path.strpath
205
exec2("%r\ngetwd()", html_pattern=dp)
206
207
class TestOctaveMode:
208
def test_start_octave(self, exec2):
209
exec2("%octave")
210
211
def test_octave_calc(self, exec2):
212
code = "%octave\nformat short\nbesselh(0,2)"
213
outp = r"ans = 0.22389\s+\+\s+0.51038i"
214
exec2(code, pattern = outp)
215
216
def test_octave_fibonacci(self, exec2):
217
code = dedent('''%octave
218
fib = ones (1, 10);
219
for i = 3:10
220
fib(i) = fib(i-1) + fib(i-2);
221
printf('%d,', fib(i))
222
endfor
223
''')
224
outp = '2,3,5,8,13,21,34,55,'
225
exec2(code, pattern = outp)
226
227
def test_octave_insync(self, exec2):
228
# this just confirms, that input/output is still in sync after the for loop above
229
exec2('%octave\n1+1', pattern = 'ans = 2')
230
231
class TestOctaveDefaultMode:
232
def test_octave_capture1(self, exec2):
233
exec2("%default_mode octave")
234
def test_octave_capture2(self, exec2):
235
exec2("%capture(stdout='output')\nx = [1,2]")
236
def test_octave_capture3(self, exec2):
237
exec2("%sage\nprint(output)", pattern = " 1 2")
238
def test_octave_version(self, exec2):
239
exec2("version()", pattern="4.2.1")
240
241
class TestAnaconda3Mode:
242
def test_start_a3(self, exec2):
243
exec2('a3 = jupyter("anaconda3")')
244
245
def test_issue_862(self, exec2):
246
exec2('%a3\nx=1\nprint("x = %s" % x)\nx','x = 1\n')
247
248
def test_a3_error(self, exec2):
249
exec2('%a3\nxyz*', html_pattern = 'span style.*color')
250
251
class TestJuliaMode:
252
def test_julia1(self, exec2):
253
# julia kernel takes 8-12 sec to load
254
exec2('jlk=jupyter("julia")')
255
256
def test_julia2(self, exec2):
257
exec2('%jlk\nquadratic(a, sqr_term, b) = (-b + sqr_term) / 2a\nquadratic(2.0, -2.0, -12.0)', '2.5')
258
259
def test_julia_version(self, exec2):
260
exec2("%jlk\nVERSION", pattern='"0.6.0"')
261
262
263
264