コード例 #1
0
ファイル: test.py プロジェクト: roganov/pybind
def test_variable_length_tuple():
    xs = bind(Tuple[int, ...], ['1', '2', '3'])
    assert xs == (1, 2, 3)

    xs = bind(Tuple[str, ...], [])
    assert xs == ()

    with pytest.raises(PybindError):
        bind(Tuple[str, ...], 'not a list or tuple')
コード例 #2
0
ファイル: test.py プロジェクト: roganov/pybind
def test_newtype():
    UserId = NewType('UserId', int)

    class X:
        user_id: UserId

    assert bind(X, {'user_id': '1'}).user_id == 1
コード例 #3
0
ファイル: test.py プロジェクト: roganov/pybind
    def test(self):
        class X:
            x: int
            y: bool

        x = bind(X, {'x': 1, 'y': False})

        assert isinstance(x, X)
        assert x.x == 1
        assert x.y is False
コード例 #4
0
ファイル: test.py プロジェクト: roganov/pybind
    def test_subclass(self):
        class X:
            x: int

        class XX(X):
            xx: str

        xx = bind(XX, {'x': '1', 'xx': 'xx'})

        assert isinstance(xx, XX)
        assert xx.x == 1
        assert xx.xx == 'xx'
コード例 #5
0
ファイル: test.py プロジェクト: roganov/pybind
    def test_nested(self):
        class Y:
            a: int
            b: str

        class X:
            y: Y

        x = bind(X, {'y': {'a': 1, 'b': '123'}})

        assert isinstance(x.y, Y)
        assert x.y.a == 1
        assert x.y.b == '123'
コード例 #6
0
ファイル: test.py プロジェクト: roganov/pybind
def test_date():
    assert bind(date, '2016-01-01') == date(year=2016, month=1, day=1)
コード例 #7
0
ファイル: test.py プロジェクト: roganov/pybind
    def test_optional(self):
        class X:
            x: Optional[int]

        assert bind(X, {}).x is None
        assert bind(X, {'x': 1}).x == 1
コード例 #8
0
ファイル: test.py プロジェクト: roganov/pybind
 def test_whitespaces_invalid(self):
     with invalid_as('required'):
         bind(str, ' \n \t')
コード例 #9
0
ファイル: test.py プロジェクト: roganov/pybind
def test_bind_raw_dict():
    d = bind(dict, {1: 1})
    assert d == {1: 1}
コード例 #10
0
ファイル: test.py プロジェクト: roganov/pybind
    def test_named(self):
        x = bind(self.X, {'a': '1', 'b': 'b'})

        assert x == self.X(a=1, b='b')
コード例 #11
0
ファイル: test.py プロジェクト: roganov/pybind
 def test_None_invalid(self):
     with invalid_as('required'):
         bind(str, None)
コード例 #12
0
ファイル: test.py プロジェクト: roganov/pybind
def test_list():
    xs = bind(List[int], ['1', 2, '3'])
    assert xs == [1, 2, 3]
コード例 #13
0
ファイル: test.py プロジェクト: roganov/pybind
 def test_positional_with_optional(self):
     x = bind(self.XOpt, [1])
     assert x == self.XOpt(a=1, b=None)
コード例 #14
0
ファイル: test.py プロジェクト: roganov/pybind
def test_datetime():
    expected = datetime(year=2016, month=1, day=1, hour=0, minute=0)
    assert bind(datetime, '2016-01-01T00:00:00') == expected
コード例 #15
0
ファイル: test.py プロジェクト: roganov/pybind
def test_tuples():
    Point = Tuple[float, float, Tuple[str, int]]

    p = bind(Point, ['0', '1', ['abc', 3]])

    assert p == (0., 1., ('abc', 3))
コード例 #16
0
ファイル: test.py プロジェクト: roganov/pybind
def test_decimal():
    assert bind(Decimal, '1.1') == Decimal('1.1')
コード例 #17
0
ファイル: test.py プロジェクト: roganov/pybind
def test_any():
    assert bind(Any, {'foo': 'bar'}) == {'foo': 'bar'}
コード例 #18
0
ファイル: test.py プロジェクト: roganov/pybind
def test_union():
    U = Union[int, str]

    assert bind(U, '1') == 1
    assert bind(U, 'abc') == 'abc'
コード例 #19
0
ファイル: test.py プロジェクト: roganov/pybind
    def test_subclass(self):
        xx = bind(self.XX, {'a': '1', 'b': 'b', 'c': 'c'})

        assert xx == self.XX(a=1, b='b', c='c')
コード例 #20
0
ファイル: test.py プロジェクト: roganov/pybind
 def test_named_with_optional(self):
     x = bind(self.XOpt, {'a': '1'})
     assert x == self.XOpt(a=1, b=None)
コード例 #21
0
ファイル: test.py プロジェクト: roganov/pybind
def test_tuple_with_optional():
    Point = Tuple[float, Optional[float]]

    assert bind(Point, ['1.1']) == (1.1, None)
コード例 #22
0
ファイル: test.py プロジェクト: roganov/pybind
def test_enum():
    class X(Enum):
        a = '1'

    assert bind(X, '1') == X.a
コード例 #23
0
ファイル: test.py プロジェクト: roganov/pybind
def test_int_enum():
    class X(IntEnum):
        a = 1
        b = 2

    assert bind(X, '1') == X.a
コード例 #24
0
ファイル: test.py プロジェクト: roganov/pybind
    def test_positional(self):
        x = bind(self.X, [1, 'b'])

        assert x == self.X(a=1, b='b')