components/python/decorator/test/results-2.6-32.master
changeset 3840 39959a8e7a76
equal deleted inserted replaced
3839:dece34b8bbeb 3840:39959a8e7a76
       
     1 [?1034hTrying:
       
     2     @memoize_uw
       
     3     def f1(x):
       
     4         time.sleep(1) # simulate some long computation
       
     5         return x
       
     6 Expecting nothing
       
     7 ok
       
     8 Trying:
       
     9     from inspect import getargspec 
       
    10 Expecting nothing
       
    11 ok
       
    12 Trying:
       
    13     print getargspec(f1) # I am using Python 2.6+ here
       
    14 Expecting:
       
    15     ArgSpec(args=[], varargs='args', keywords='kw', defaults=None)
       
    16 ok
       
    17 Trying:
       
    18     f1(0, 1)
       
    19 Expecting:
       
    20     Traceback (most recent call last):
       
    21        ...
       
    22     TypeError: f1() takes exactly 1 argument (2 given)
       
    23 ok
       
    24 Trying:
       
    25     from decorator import decorator
       
    26 Expecting nothing
       
    27 ok
       
    28 Trying:
       
    29     @memoize
       
    30     def heavy_computation():
       
    31         time.sleep(2)
       
    32         return "done"
       
    33 Expecting nothing
       
    34 ok
       
    35 Trying:
       
    36     print heavy_computation() # the first time it will take 2 seconds
       
    37 Expecting:
       
    38     done
       
    39 ok
       
    40 Trying:
       
    41     print heavy_computation() # the second time it will be instantaneous
       
    42 Expecting:
       
    43     done
       
    44 ok
       
    45 Trying:
       
    46     print getargspec(heavy_computation) 
       
    47 Expecting:
       
    48     ArgSpec(args=[], varargs=None, keywords=None, defaults=None)
       
    49 ok
       
    50 Trying:
       
    51     @trace
       
    52     def f1(x):
       
    53         pass
       
    54 Expecting nothing
       
    55 ok
       
    56 Trying:
       
    57     f1(0)
       
    58 Expecting:
       
    59     calling f1 with args (0,), {}
       
    60 ok
       
    61 Trying:
       
    62     print getargspec(f1) 
       
    63 Expecting:
       
    64     ArgSpec(args=['x'], varargs=None, keywords=None, defaults=None)
       
    65 ok
       
    66 Trying:
       
    67     @trace
       
    68     def f(x, y=1, z=2, *args, **kw):
       
    69         pass
       
    70 Expecting nothing
       
    71 ok
       
    72 Trying:
       
    73     f(0, 3)
       
    74 Expecting:
       
    75     calling f with args (0, 3, 2), {}
       
    76 ok
       
    77 Trying:
       
    78     print getargspec(f) 
       
    79 Expecting:
       
    80     ArgSpec(args=['x', 'y', 'z'], varargs='args', keywords='kw', defaults=(1, 2))
       
    81 ok
       
    82 Trying:
       
    83     @trace
       
    84     def exotic_signature((x, y)=(1,2)): return x+y
       
    85 Expecting nothing
       
    86 ok
       
    87 Trying:
       
    88     print getargspec(exotic_signature)
       
    89 Expecting:
       
    90     ArgSpec(args=[['x', 'y']], varargs=None, keywords=None, defaults=((1, 2),))
       
    91 ok
       
    92 Trying:
       
    93     exotic_signature() 
       
    94 Expecting:
       
    95     calling exotic_signature with args ((1, 2),), {}
       
    96     3
       
    97 ok
       
    98 Trying:
       
    99     @decorator
       
   100     def trace(f, *args, **kw):
       
   101         print "calling %s with args %s, %s" % (f.func_name, args, kw)
       
   102         return f(*args, **kw)
       
   103 Expecting nothing
       
   104 ok
       
   105 Trying:
       
   106     trace # doctest: +ELLIPSIS
       
   107 Expecting:
       
   108     <function trace at 0x...>
       
   109 ok
       
   110 Trying:
       
   111     @trace
       
   112     def func(): pass
       
   113 Expecting nothing
       
   114 ok
       
   115 Trying:
       
   116     func()
       
   117 Expecting:
       
   118     calling func with args (), {}
       
   119 ok
       
   120 Trying:
       
   121     @blocking("Please wait ...")
       
   122     def read_data():
       
   123         time.sleep(3) # simulate a blocking resource
       
   124         return "some data"
       
   125 Expecting nothing
       
   126 ok
       
   127 Trying:
       
   128     print read_data() # data is not available yet
       
   129 Expecting:
       
   130     Please wait ...
       
   131 ok
       
   132 Trying:
       
   133     time.sleep(1)  
       
   134 Expecting nothing
       
   135 ok
       
   136 Trying:
       
   137     print read_data() # data is not available yet
       
   138 Expecting:
       
   139     Please wait ...
       
   140 ok
       
   141 Trying:
       
   142     time.sleep(1)
       
   143 Expecting nothing
       
   144 ok
       
   145 Trying:
       
   146     print read_data() # data is not available yet
       
   147 Expecting:
       
   148     Please wait ...
       
   149 ok
       
   150 Trying:
       
   151     time.sleep(1.1) # after 3.1 seconds, data is available
       
   152 Expecting nothing
       
   153 ok
       
   154 Trying:
       
   155     print read_data()
       
   156 Expecting:
       
   157     some data
       
   158 ok
       
   159 Trying:
       
   160     async = decorator(Async(threading.Thread))
       
   161 Expecting nothing
       
   162 ok
       
   163 Trying:
       
   164     datalist = [] # for simplicity the written data are stored into a list.
       
   165 Expecting nothing
       
   166 ok
       
   167 Trying:
       
   168     @async
       
   169     def write(data):
       
   170         # append data to the datalist by locking
       
   171         with threading.Lock():
       
   172             time.sleep(1) # emulate some long running operation
       
   173             datalist.append(data)
       
   174         # other operations not requiring a lock here
       
   175 Expecting nothing
       
   176 ok
       
   177 Trying:
       
   178     write("data1") # doctest: +ELLIPSIS
       
   179 Expecting:
       
   180     <Thread(write-1, started...)>
       
   181 ok
       
   182 Trying:
       
   183     time.sleep(.1) # wait a bit, so we are sure data2 is written after data1
       
   184 Expecting nothing
       
   185 ok
       
   186 Trying:
       
   187     write("data2") # doctest: +ELLIPSIS
       
   188 Expecting:
       
   189     <Thread(write-2, started...)>
       
   190 ok
       
   191 Trying:
       
   192     time.sleep(2) # wait for the writers to complete
       
   193 Expecting nothing
       
   194 ok
       
   195 Trying:
       
   196     print datalist
       
   197 Expecting:
       
   198     ['data1', 'data2']
       
   199 ok
       
   200 Trying:
       
   201     from contextlib import contextmanager
       
   202 Expecting nothing
       
   203 ok
       
   204 Trying:
       
   205     @contextmanager
       
   206     def before_after(before, after):
       
   207         print(before)
       
   208         yield
       
   209         print(after)
       
   210 Expecting nothing
       
   211 ok
       
   212 Trying:
       
   213     ba = before_after('BEFORE', 'AFTER')
       
   214 Expecting nothing
       
   215 ok
       
   216 Trying:
       
   217     type(ba)
       
   218 Expecting:
       
   219     <class 'contextlib.GeneratorContextManager'>
       
   220 ok
       
   221 Trying:
       
   222     with ba:
       
   223         print 'hello'
       
   224 Expecting:
       
   225     BEFORE
       
   226     hello
       
   227     AFTER
       
   228 ok
       
   229 Trying:
       
   230     def f(*args, **kw): # a function with a generic signature
       
   231         print args, kw
       
   232 Expecting nothing
       
   233 ok
       
   234 Trying:
       
   235     f1 = FunctionMaker.create('f1(a, b)', 'f(a, b)', dict(f=f))
       
   236 Expecting nothing
       
   237 ok
       
   238 Trying:
       
   239     f1(1,2)
       
   240 Expecting:
       
   241     (1, 2) {}
       
   242 ok
       
   243 Trying:
       
   244     f1 = FunctionMaker.create(
       
   245         'f1(a, b)', 'f(a, b)', dict(f=f), addsource=True)
       
   246 Expecting nothing
       
   247 ok
       
   248 Trying:
       
   249     print f1.__source__
       
   250 Expecting:
       
   251     def f1(a, b):
       
   252         f(a, b)
       
   253     <BLANKLINE>
       
   254 ok
       
   255 Trying:
       
   256     print inspect.getsource(example)
       
   257 Expecting:
       
   258         def wrapper(*args, **kw):
       
   259             return func(*args, **kw)
       
   260     <BLANKLINE>
       
   261 ok
       
   262 Trying:
       
   263     print inspect.getsource(factorial.__wrapped__)
       
   264 Expecting:
       
   265     @tail_recursive
       
   266     def factorial(n, acc=1):
       
   267         "The good old factorial"
       
   268         if n == 0: return acc
       
   269         return factorial(n-1, n*acc)
       
   270     <BLANKLINE>
       
   271 ok
       
   272 Trying:
       
   273     print factorial(4) 
       
   274 Expecting:
       
   275     24
       
   276 ok
       
   277 Trying:
       
   278     @trace
       
   279     def f():
       
   280         1/0
       
   281 Expecting nothing
       
   282 ok
       
   283 Trying:
       
   284     f()
       
   285 Expecting:
       
   286     Traceback (most recent call last):
       
   287       ...
       
   288          File "<string>", line 2, in f
       
   289          File "<doctest __main__[18]>", line 4, in trace
       
   290            return f(*args, **kw)
       
   291          File "<doctest __main__[47]>", line 3, in f
       
   292            1/0
       
   293     ZeroDivisionError: integer division or modulo by zero
       
   294 ok
       
   295 Trying:
       
   296     class C(object): 
       
   297          @trace
       
   298          def meth(self):
       
   299              pass
       
   300 Expecting nothing
       
   301 ok
       
   302 Trying:
       
   303     class C(object):
       
   304          def meth(self):
       
   305              pass
       
   306 Expecting nothing
       
   307 ok
       
   308 Trying:
       
   309     trace(C.meth)
       
   310 Expecting:
       
   311     Traceback (most recent call last):
       
   312       ...
       
   313     TypeError: You are decorating a non function: <unbound method C.meth>
       
   314 ok
       
   315 Trying:
       
   316     trace(C.meth.im_func) # doctest: +ELLIPSIS
       
   317 Expecting:
       
   318     <function meth at 0x...>
       
   319 ok
       
   320 Trying:
       
   321     @trace
       
   322     def f(_func_): print f
       
   323 Expecting:
       
   324     Traceback (most recent call last):
       
   325       ...
       
   326     NameError: _func_ is overridden in
       
   327     def f(_func_):
       
   328         return _call_(_func_, _func_)
       
   329 ok
       
   330 Trying:
       
   331     def f(): pass # the original function
       
   332 Expecting nothing
       
   333 ok
       
   334 Trying:
       
   335     f.attr1 = "something" # setting an attribute
       
   336 Expecting nothing
       
   337 ok
       
   338 Trying:
       
   339     f.attr2 = "something else" # setting another attribute
       
   340 Expecting nothing
       
   341 ok
       
   342 Trying:
       
   343     traced_f = trace(f) # the decorated function
       
   344 Expecting nothing
       
   345 ok
       
   346 Trying:
       
   347     traced_f.attr1
       
   348 Expecting:
       
   349     'something'
       
   350 ok
       
   351 Trying:
       
   352     traced_f.attr2 = "something different" # setting attr
       
   353 Expecting nothing
       
   354 ok
       
   355 Trying:
       
   356     f.attr2 # the original attribute did not change
       
   357 Expecting:
       
   358     'something else'
       
   359 ok
       
   360 Trying:
       
   361     a = Action()
       
   362 Expecting nothing
       
   363 ok
       
   364 Trying:
       
   365     a.view() # ok
       
   366 Expecting nothing
       
   367 ok
       
   368 Trying:
       
   369     a.insert() # err
       
   370 Expecting:
       
   371     Traceback (most recent call last):
       
   372        ...
       
   373     PermissionError: User does not have the permission to run insert!
       
   374 ok
       
   375 Trying:
       
   376     decorator(_memoize).__name__
       
   377 Expecting:
       
   378     '_memoize'
       
   379 ok
       
   380 Trying:
       
   381     factorial.__doc__
       
   382 Expecting:
       
   383     'The good old factorial'
       
   384 ok
       
   385 Trying:
       
   386     ba.__class__.__name__
       
   387 Expecting:
       
   388     'ContextManager'
       
   389 ok
       
   390 Trying:
       
   391     hello('michele')
       
   392 Expecting:
       
   393     BEFORE
       
   394     hello michele
       
   395     AFTER
       
   396 ok
       
   397 31 items had no tests:
       
   398     __main__.Action.delete
       
   399     __main__.Action.insert
       
   400     __main__.Action.view
       
   401     __main__.Admin
       
   402     __main__.Async
       
   403     __main__.Async.__call__
       
   404     __main__.Async.__init__
       
   405     __main__.PermissionError
       
   406     __main__.PowerUser
       
   407     __main__.TailRecursive
       
   408     __main__.TailRecursive.__call__
       
   409     __main__.TailRecursive.__init__
       
   410     __main__.User
       
   411     __main__._memoize
       
   412     __main__._trace
       
   413     __main__.before_after
       
   414     __main__.blocking
       
   415     __main__.decorator_apply
       
   416     __main__.example
       
   417     __main__.fact
       
   418     __main__.factorial
       
   419     __main__.get_userclass
       
   420     __main__.identity_dec
       
   421     __main__.memoize
       
   422     __main__.memoize_uw
       
   423     __main__.on_closing
       
   424     __main__.on_failure
       
   425     __main__.on_success
       
   426     __main__.restricted
       
   427     __main__.tail_recursive
       
   428     __main__.trace
       
   429 4 items passed all tests:
       
   430   65 tests in __main__
       
   431    3 tests in __main__.Action
       
   432    2 tests in __main__.a_test_for_pylons
       
   433    2 tests in __main__.hello
       
   434 72 tests in 35 items.
       
   435 72 passed and 0 failed.
       
   436 Test passed.