Path: blob/main/python/pylang/test/functions.py
1396 views
# globals: assrt123def nothing():4pass567assrt.equal(nothing(), undefined)8910def add(a, b):11return a + b121314def sub(a, b):15return a - b161718mul = None192021def nonlocal_test():22nonlocal mul2324def mul0(a, b):25return a * b2627mul = mul02829def dev(a, b): # noqa:unused-local30return a / b313233nonlocal_test()3435assrt.equal(add(1, 2), 3)36assrt.equal(sub(1, 2), -1)37assrt.equal(mul(2, 2), 4)383940# for some reason input to throws must be of type block, hence the 'def' wrapper41def divtest():42div(6, 3) # noqa: undef434445assrt.throws(divtest, r"%js /div is not defined/")4647arr = [8, 4]48assrt.equal(add(*arr), 12)49assrt.ok(Array.isArray(arr))505152def sum(*args):53ttl = 054for i in args:55ttl += i56return ttl575859assrt.equal(sum(1, 2, 3), 6)60assrt.equal(sum(1, *[2, 3]), 6)6162num = 4636465def nonlocal_num():66nonlocal num67num = 5686970nonlocal_num()71assrt.equal(num, 5)7273x = "foo"74y = 5757677def swap(x, y):78return y, x798081x, y = swap(x, y)82assrt.equal(x, 5)83assrt.equal(y, "foo")8485count = 0868788def inctest():89def fake_increment():90count += 19192def real_increment():93nonlocal count94count += 19596return fake_increment, real_increment979899f, r = inctest()100101f()102assrt.equal(count, 0)103r()104assrt.equal(count, 1)105106st = "this is a string"107assrt.equal(jstype(st), r"%js typeof st")108109# testing inlined functions110inlined = [111def(x): return x+1;, def(x): return x+2;,112def(x): return x+3;,113def(x): return x+4;114]115assrt.equal(inlined[0](1), 2)116assrt.equal(inlined[1](1), 3)117assrt.equal(inlined[2](1), 4)118assrt.equal(inlined[3](1), 5)119120121# decorators122def makebold(fn):123def wrapped(arg):124return "<b>" + fn(arg) + "</b>"125126return wrapped127128129def makeitalic(fn):130def wrapped(arg):131return "<i>" + fn(arg) + "</i>"132133return wrapped134135136@makebold137@makeitalic138def hello(something):139return "hello " + something140141142assrt.equal(hello("world"), "<b><i>hello world</i></b>")143assrt.equal(hello.__module__, '__main__')144assrt.equal(hello.__argnames__.length, 1)145assrt.equal(hello.__argnames__[0], 'arg')146147148def simple_wrapper(f):149f.test_attr = 'test'150return f151152153@simple_wrapper154def fw(x):155pass156157158assrt.equal(fw.__module__, '__main__')159assrt.equal(fw.__argnames__.length, 1)160assrt.equal(fw.__argnames__[0], 'x')161# just because something is a reserved keyword in PyLang, doesn't mean other libraries won't attempt to use it162# let's make sure we parse that correctly163five = {}164r"%js five.is = function(n) { return 5 == n };"165assrt.ok(r"%js five.is(5)")166167# function assignment via conditional168foo = (def(): return 5;) if 0 else (def(): return 6;)169bar = (def(): return 5;) if 0 < 1 else (def(): return 6;)170baz = (def():171return 5172) if 1 else (def():173return 6174)175assrt.equal(foo(), 6)176assrt.equal(bar(), 5)177assrt.equal(baz(), 5)178179180def trailing_comma(181a,182b,183):184return a + b185186187assrt.equal(trailing_comma(1, 2), 3)188assrt.equal(trailing_comma(1891,1902,191), 3)192193194def return_string_with_newline():195return '''a196b'''197198199assrt.equal(return_string_with_newline(), 'a\nb')200201202