Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/python/pylang/test/functions.py
1396 views
1
# globals: assrt
2
3
4
def nothing():
5
pass
6
7
8
assrt.equal(nothing(), undefined)
9
10
11
def add(a, b):
12
return a + b
13
14
15
def sub(a, b):
16
return a - b
17
18
19
mul = None
20
21
22
def nonlocal_test():
23
nonlocal mul
24
25
def mul0(a, b):
26
return a * b
27
28
mul = mul0
29
30
def dev(a, b): # noqa:unused-local
31
return a / b
32
33
34
nonlocal_test()
35
36
assrt.equal(add(1, 2), 3)
37
assrt.equal(sub(1, 2), -1)
38
assrt.equal(mul(2, 2), 4)
39
40
41
# for some reason input to throws must be of type block, hence the 'def' wrapper
42
def divtest():
43
div(6, 3) # noqa: undef
44
45
46
assrt.throws(divtest, r"%js /div is not defined/")
47
48
arr = [8, 4]
49
assrt.equal(add(*arr), 12)
50
assrt.ok(Array.isArray(arr))
51
52
53
def sum(*args):
54
ttl = 0
55
for i in args:
56
ttl += i
57
return ttl
58
59
60
assrt.equal(sum(1, 2, 3), 6)
61
assrt.equal(sum(1, *[2, 3]), 6)
62
63
num = 4
64
65
66
def nonlocal_num():
67
nonlocal num
68
num = 5
69
70
71
nonlocal_num()
72
assrt.equal(num, 5)
73
74
x = "foo"
75
y = 5
76
77
78
def swap(x, y):
79
return y, x
80
81
82
x, y = swap(x, y)
83
assrt.equal(x, 5)
84
assrt.equal(y, "foo")
85
86
count = 0
87
88
89
def inctest():
90
def fake_increment():
91
count += 1
92
93
def real_increment():
94
nonlocal count
95
count += 1
96
97
return fake_increment, real_increment
98
99
100
f, r = inctest()
101
102
f()
103
assrt.equal(count, 0)
104
r()
105
assrt.equal(count, 1)
106
107
st = "this is a string"
108
assrt.equal(jstype(st), r"%js typeof st")
109
110
# testing inlined functions
111
inlined = [
112
def(x): return x+1;, def(x): return x+2;,
113
def(x): return x+3;,
114
def(x): return x+4;
115
]
116
assrt.equal(inlined[0](1), 2)
117
assrt.equal(inlined[1](1), 3)
118
assrt.equal(inlined[2](1), 4)
119
assrt.equal(inlined[3](1), 5)
120
121
122
# decorators
123
def makebold(fn):
124
def wrapped(arg):
125
return "<b>" + fn(arg) + "</b>"
126
127
return wrapped
128
129
130
def makeitalic(fn):
131
def wrapped(arg):
132
return "<i>" + fn(arg) + "</i>"
133
134
return wrapped
135
136
137
@makebold
138
@makeitalic
139
def hello(something):
140
return "hello " + something
141
142
143
assrt.equal(hello("world"), "<b><i>hello world</i></b>")
144
assrt.equal(hello.__module__, '__main__')
145
assrt.equal(hello.__argnames__.length, 1)
146
assrt.equal(hello.__argnames__[0], 'arg')
147
148
149
def simple_wrapper(f):
150
f.test_attr = 'test'
151
return f
152
153
154
@simple_wrapper
155
def fw(x):
156
pass
157
158
159
assrt.equal(fw.__module__, '__main__')
160
assrt.equal(fw.__argnames__.length, 1)
161
assrt.equal(fw.__argnames__[0], 'x')
162
# just because something is a reserved keyword in PyLang, doesn't mean other libraries won't attempt to use it
163
# let's make sure we parse that correctly
164
five = {}
165
r"%js five.is = function(n) { return 5 == n };"
166
assrt.ok(r"%js five.is(5)")
167
168
# function assignment via conditional
169
foo = (def(): return 5;) if 0 else (def(): return 6;)
170
bar = (def(): return 5;) if 0 < 1 else (def(): return 6;)
171
baz = (def():
172
return 5
173
) if 1 else (def():
174
return 6
175
)
176
assrt.equal(foo(), 6)
177
assrt.equal(bar(), 5)
178
assrt.equal(baz(), 5)
179
180
181
def trailing_comma(
182
a,
183
b,
184
):
185
return a + b
186
187
188
assrt.equal(trailing_comma(1, 2), 3)
189
assrt.equal(trailing_comma(
190
1,
191
2,
192
), 3)
193
194
195
def return_string_with_newline():
196
return '''a
197
b'''
198
199
200
assrt.equal(return_string_with_newline(), 'a\nb')
201
202