示例#1
0
def test_xget_glob_attrs_some():
    # type: () -> None
    "Should get keys of object"
    assert str(dig.xget(E(foo=1, bar=2), 'f*')) == '[foo=1:int]'
示例#2
0
def test_xget_object():
    # type: () -> None
    "Should obj"
    assert dig.xget(E(foo='bar'), 'foo') == 'bar'
示例#3
0
def test_xget_glob_attrs_all():
    # type: () -> None
    "Should get keys of dict"
    assert str(dig.xget(E(foo=1, bar=2), '*')) == '[bar=2:int, foo=1:int]'
示例#4
0
def test_dig_complex():
    # type: () -> None
    "Should "
    ob = E(foo=1, bar=2, fx=(1, 2, 3))
    assert dig.dig(ob, 'f*.1.0') == 1
示例#5
0
def test_dig_object():
    # type: () -> None
    "Should "
    assert dig.dig(E(foo='bar', bar=E(jox='jox')), 'bar.jox') == 'jox'
示例#6
0
import pytest

from altered import E

from kingston import dig
from kingston.testing import fixture

from hypothesis import given
from hypothesis import strategies as st

import math
from typing import Any


@fixture.params("top, path, value",
  (E(foo=1), "foo", 1),
  (E(foo=E(bar=1)), "foo.bar", 1),
  (E(foo=1), "bar", AttributeError),
  (E(foo=E(baz=1)), "foo.bar", AttributeError),
)  # yapf: disable
def test_subattr(top: E, path: str, value: Any) -> None:
    "Should subattr"
    if type(value) is not int:
        with pytest.raises(value):
            dig.subattr(top, path)
    else:
        assert dig.subattr(top, path) == value


@given(st.deferred(lambda: st.integers() | st.floats() | st.text()))
def test_xget_prim(param: Any) -> None:
示例#7
0
    "Should pubvars"

    class Cls(object):
        def __init__(self, x, y):
            self.x = x
            self.y = y

    assert set(lang.pubvars(Cls('x', 'y'))) == {'x', 'y'}


@pytest.mark.wbox
@fixture.params(
    "obj, name",
    ('Hello', 'str'),
    (None, 'None'),
    (E(a=1), 'Expando'),
)
def test_typename(obj, name) -> None:
    "Should typename"
    assert lang.typename(obj) == name


def test_primbases() -> None:
    """Should return the primitive baseclasses from an object
    deriving from a primitive type.

    """
    class FromPrim(int):
        pass

    assert lang.primbases(FromPrim) == [int]
示例#8
0
    "Should more_filter"
    pass


def test_piping_as_mapping() -> None:
    """Piping objects derived from `ComposePiping` should always support
    the bitwise pipe (`'|'`) operatror as a simple function
    composition.

    """
    incr = lambda x: x + 1
    showr = "It is {}!".format
    assert (lang.ComposePiping(5) >> incr >> incr >> showr)() == "It is 7!"


got_a, got_b, none = E(a=1), E(b=1), E(c=1)
got_ab, got_bc = E(a=1, b=1), E(b=1, c=1)

has = lambda name: lambda obj: hasattr(obj, name)

only_a = lambda: lang.LogicPiping() // has('a')
a_and_b = lambda: lang.LogicPiping() // has('a') & has('b')
either = lambda: lang.LogicPiping() // has('a') | has('b')
a_or_b = lambda: lang.LogicPiping() // has('a') | has('b')


class LogicAndCount(lang.CountPiping, lang.LogicPiping):
    pass


@fixture.params(