templates.test_private_lambda
1import smartpy as sp 2 3 4@sp.module 5def main(): 6 class Lib(sp.Contract): 7 @sp.private() 8 def f(self, x): 9 return x * 2 10 11 @sp.private() 12 def g(self, x): 13 return x * 2 14 15 @sp.private() 16 def factorial(self, x): 17 r = 1 18 for y in range(2, x + 1): 19 r *= y 20 return r 21 22 # @sp.private(recursive=True) 23 # def factorial_rec(self, n, factorial_rec): 24 # sp.if n <= 1: 25 # return n 26 # sp.else: 27 # return n * factorial_rec(n-1) 28 29 @sp.private() 30 def factorial_five(self, params): 31 sp.cast(params, sp.unit) 32 r = 1 33 for y in range(2, 6): 34 r *= y 35 return r 36 37 @sp.entrypoint 38 def test_factorial(self): 39 assert self.factorial(5) == 120 40 # assert self.factorial_rec(5) == 120 41 assert self.factorial_five() == 120 42 43 # @sp.private() 44 # def failing(self, x): 45 # with sp.set_result_type(sp.int): 46 # raise "Aaa" + x 47 48 # @sp.entrypoint 49 # def test_failing(self): 50 # aaa = self.failing("") 51 52 class Tester(sp.Contract): 53 def __init__(self, f): 54 sp.cast(f, sp.lambda_[sp.nat, sp.nat]) 55 self.private.f = f 56 self.data.x = None 57 58 @sp.entrypoint 59 def update(self, params): 60 self.data.x = sp.Some(self.private.f(params)) 61 62 @sp.entrypoint 63 def check(self, params, result): 64 assert self.private.f(params) == result 65 66 67def f(x): 68 sp.result(x * 2) 69 70 71@sp.add_test() 72def test(): 73 s = sp.test_scenario("Bind", main) 74 l = main.Lib() 75 s += l 76 l.test_factorial() 77 # TODO activate this line when private lambda access is supported. 78 # tester = main.Tester(l.f) 79 tester = main.Tester(f) 80 s += tester 81 tester.update(12) 82 s.verify(tester.data.x.unwrap_some() == 24) 83 tester.check(params=12, result=24) 84 tester.check(params=11, result=22)
def
f(x):