File: test_result_bind.py

package info (click to toggle)
python-returns 0.26.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: python: 11,000; makefile: 18
file content (76 lines) | stat: -rw-r--r-- 2,189 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from returns.result import Failure, Result, Success


def test_bind():
    """Ensures that bind works."""

    def factory(inner_value: int) -> Result[int, str]:
        if inner_value > 0:
            return Success(inner_value * 2)
        return Failure(str(inner_value))

    input_value = 5
    bound: Result[int, str] = Success(input_value)

    assert bound.bind(factory) == factory(input_value)
    assert Success(input_value).bind(factory) == factory(input_value)
    assert str(bound.bind(factory)) == '<Success: 10>'

    input_value = 0
    bound2: Result[int, str] = Success(input_value)

    assert bound2.bind(factory) == factory(input_value)
    assert str(bound2.bind(factory)) == '<Failure: 0>'


def test_left_identity_success():
    """Ensures that left identity works for Success container."""

    def factory(inner_value: int) -> Result[int, str]:
        return Success(inner_value * 2)

    input_value = 5
    bound: Result[int, str] = Success(input_value)

    assert bound.bind(factory) == factory(input_value)


def test_left_identity_failure():
    """Ensures that left identity works for Failure container."""

    def factory(inner_value: int) -> Result[int, int]:
        return Failure(6)

    input_value = 5
    bound: Result[int, int] = Failure(input_value)

    assert bound.bind(factory) == Failure(input_value)
    assert Failure(input_value).bind(factory) == Failure(5)
    assert str(bound) == '<Failure: 5>'


def test_lash_success():
    """Ensures that lash works for Success container."""

    def factory(inner_value) -> Result[int, str]:
        return Success(inner_value * 2)

    bound = Success(5).lash(factory)

    assert bound == Success(5)
    assert Success(5).lash(factory) == Success(5)
    assert str(bound) == '<Success: 5>'


def test_lash_failure():
    """Ensures that lash works for Failure container."""

    def factory(inner_value: int) -> Result[str, int]:
        return Failure(inner_value + 1)

    expected = 6
    bound: Result[str, int] = Failure(5)

    assert bound.lash(factory) == Failure(expected)
    assert Failure(5).lash(factory) == Failure(expected)
    assert str(bound.lash(factory)) == '<Failure: 6>'