Пример #1
0
def test_eager_data_wr():
    class Foo:
        x = 123

    foo = Foo()
    rr = RouterEager(foo)
    rr.x = 321
    assert (foo.x == 321)
Пример #2
0
def test_eager_class_meth_call():
    class Foo:
        def m(self):
            return 123

    foo = Foo()
    rr = RouterEager(foo)
    assert (rr.m() == 123)
Пример #3
0
def test_eager_object_meth_call():
    class Foo:
        pass

    foo = Foo()
    foo.m = lambda: 123
    rr = RouterEager(foo)
    assert (rr.m() == 123)
Пример #4
0
def test_eager_prop_wr():
    class Foo:
        def set_x(self, val):
            self.x_val = val

        x = property(lambda obj: None, set_x)

    foo = Foo()
    rr = RouterEager(foo)
    rr.x = 123
    assert (foo.x_val == 123)
Пример #5
0
def test_eager_class_data_rd():
    class Foo:
        x = 123

    foo = Foo()
    rr = RouterEager(foo)
    assert (rr.x == 123)
Пример #6
0
def test_eager_object_prop_rd():
    class Foo:
        x = property(lambda obj: 123)

    foo = Foo()
    rr = RouterEager(foo)
    assert (rr.x == 123)
Пример #7
0
def test_eager_class_data_object_data_rd():
    class Foo:
        x = 123

    foo = Foo()
    foo.x = 321
    rr = RouterEager(foo)
    assert (rr.x == 321)
Пример #8
0
def test_eager_object_data_rd():
    class Foo:
        pass

    foo = Foo()
    foo.x = 123
    rr = RouterEager(foo)
    assert (rr.x == 123)
Пример #9
0
def test_eager_object_prop_object_data_rd():
    class Foo:
        pass

    foo = Foo()
    foo.x = 123
    Foo.x = property(lambda obj: 321)
    rr = RouterEager(foo)
    assert (rr.x == 321)
Пример #10
0
def test_eager_modif_regexp():
    class Foo:
        bar0 = 123
        bar1 = 321

    foo = Foo()
    rr = RouterEager(foo, '^[^_].*0$', '^(.*)[0-9]$', '\g<1>')

    assert (set(dir(rr)) == {'bar'})
    assert (rr.bar == 123)
    with pytest.raises(AttributeError):
        rr.bar0
    with pytest.raises(AttributeError):
        rr.bar1
Пример #11
0
def test_eager_uc_nesting():
    class Foo0:
        bar0 = 123
        bar1 = 321

    class Foo1:
        bar0 = 456
        bar1 = 654

    foo0 = Foo0()
    foo1 = Foo1()

    hub = RouterEager(foo0, '^[^_].*$', '^(.*)$', r'foo0_\1') + RouterEager(
        foo1, '^[^_].*$', '^(.*)$', r'foo1_\1')
    bar0 = RouterEager(hub, '^.*_bar0$', '^(.*)_bar0$', r'\1')
    bar1 = RouterEager(hub, '^.*_bar1$', '^(.*)_bar1$', r'\1')

    # test dir()
    assert (set(dir(bar0)) == {'foo0', 'foo1'})
    assert (set(dir(bar1)) == {'foo0', 'foo1'})

    # test reads
    assert (bar0.foo0 == 123)
    assert (bar0.foo1 == 456)
    assert (bar1.foo0 == 321)
    assert (bar1.foo1 == 654)

    # test writes
    bar0.foo0 = 1234
    bar0.foo1 = 4567
    bar1.foo0 = 3210
    bar1.foo1 = 6543
    assert (foo0.bar0 == 1234)
    assert (foo1.bar0 == 4567)
    assert (foo0.bar1 == 3210)
    assert (foo1.bar1 == 6543)
Пример #12
0
def test_eager_uc_combining():
    class Foo0:
        bar = 123

    class Foo1:
        bar = 321

    foo0 = Foo0()
    foo1 = Foo1()
    foo = RouterEager(foo0, '^[^_].*$', '^(.*)$', r'\g<1>0') + RouterEager(
        foo1, '^[^_].*$', '^(.*)$', r'\g<1>1')

    # test dir()
    assert (set(dir(foo)) == {'bar0', 'bar1'})

    # test reads
    assert (foo.bar0 == 123)
    assert (foo.bar1 == 321)

    # test writes
    foo.bar0 = 345
    foo.bar1 = 456
    assert (foo0.bar == 345)
    assert (foo1.bar == 456)
Пример #13
0
def test_eager_filter_regexp():
    class Foo:
        x0 = 123
        x1 = 234
        x2 = 345
        x3 = 456

    foo = Foo()
    rr = RouterEager(foo, '^x[0-1]$')
    assert (set(dir(rr)) == {'x0', 'x1'})
    assert (rr.x0 == 123)
    assert (rr.x1 == 234)
    with pytest.raises(AttributeError):
        rr.x2
    with pytest.raises(AttributeError):
        rr.x3
Пример #14
0
def test_eager_dir_call():
    class Foo:
        x = 123
        y = property(lambda obj: None)

        def m(self):
            pass

    foo = Foo()
    foo.z = 123

    rr = RouterEager(foo)
    rr_dir = dir(rr)
    assert ('x' in rr_dir)
    assert ('y' in rr_dir)
    assert ('z' in rr_dir)
    assert ('m' in rr_dir)
Пример #15
0
def test_eager_uc_splitting():
    class Foo:
        bar0 = 123
        bar1 = 321

    foo = Foo()
    foo0 = RouterEager(foo, '^.*0$', '^(.*)0$', '\g<1>')
    foo1 = RouterEager(foo, '^.*1$', '^(.*)1$', '\g<1>')

    # test dir()
    assert (dir(foo0) == ['bar'])
    assert (dir(foo1) == ['bar'])

    # test reads
    assert (foo0.bar == 123)
    assert (foo1.bar == 321)

    # test writes
    foo0.bar = 456
    foo1.bar = 567
    assert (foo.bar0 == 456)
    assert (foo.bar1 == 567)
Пример #16
0
def test_lazy_add_eager():
    with pytest.raises(TypeError):
        RouterLazy(None) + RouterEager(None) + RouterEager(None)