Completed
Push — master ( 7b504c...b6f606 )
by Ionel Cristian
01:16
created

tests.LocalsSimpleProxy   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 9
Duplicated Lines 0 %
Metric Value
dl 0
loc 9
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A LocalsSimpleProxy.__init__() 0 3 1
A LocalsSimpleProxy.__str__() 0 4 2
1
from functools import partial
2
3
import pytest
4
5
6
empty = object()
7
8
9
class cached_property(object):
10
    def __init__(self, func):
11
        self.func = func
12
13
    def __get__(self, obj, cls):
14
        value = obj.__dict__[self.func.__name__] = self.func(obj)
15
        return value
16
17
18
class SimpleProxy(object):
19
    def __init__(self, factory):
20
        self.factory = factory
21
        self.object = empty
22
23
    def __str__(self):
24
        if self.object is empty:
25
            self.object = self.factory()
26
        return str(self.object)
27
28
29
class CachedPropertyProxy(object):
30
    def __init__(self, factory):
31
        self.factory = factory
32
33
    @cached_property
34
    def object(self):
35
        return self.factory()
36
37
    def __str__(self):
38
        return str(self.object)
39
40
41
class LocalsSimpleProxy(object):
42
    def __init__(self, factory):
43
        self.factory = factory
44
        self.object = empty
45
46
    def __str__(self, func=str):
47
        if self.object is empty:
48
            self.object = self.factory()
49
        return func(self.object)
50
51
52
class LocalsCachedPropertyProxy(object):
53
    def __init__(self, factory):
54
        self.factory = factory
55
56
    @cached_property
57
    def object(self):
58
        return self.factory()
59
60
    def __str__(self, func=str):
61
        return func(self.object)
62
63
64
@pytest.fixture(scope="module", params=["SimpleProxy", "CachedPropertyProxy", "LocalsSimpleProxy", "LocalsCachedPropertyProxy"])
65
def impl(request):
66
    return globals()[request.param]
67
68
69
def test_proto(benchmark, impl):
70
    obj = "foobar"
71
    proxied = impl(lambda: obj)
72
    result = benchmark(partial(str, proxied))
73
    assert result == obj
74