|
| 1 | +import copy |
| 2 | +import sys |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +import pytest |
| 6 | +from hypothesis import example, given |
| 7 | +from hypothesis import strategies as st |
| 8 | + |
| 9 | +from returns.io import IO |
| 10 | +from returns.maybe import Maybe, Nothing, Some |
| 11 | +from returns.primitives.container import BaseContainer |
| 12 | +from returns.result import Failure, Result, Success |
| 13 | + |
| 14 | + |
| 15 | +@given(st.integers(), st.integers()) |
| 16 | +def test_base_container_replace_inner_value(initial: int, new_val: int): |
| 17 | + """Ensures that __replace__ updates inner_value properly.""" |
| 18 | + container = BaseContainer(initial) |
| 19 | + replaced = container.__replace__(inner_value=new_val) |
| 20 | + |
| 21 | + assert replaced != container or initial == new_val |
| 22 | + assert replaced._inner_value == new_val |
| 23 | + assert container._inner_value == initial |
| 24 | + assert isinstance(replaced, BaseContainer) |
| 25 | + |
| 26 | + |
| 27 | +@given(st.text(), st.text()) |
| 28 | +def test_base_container_replace_underscore_inner_value(initial: str, new_val: str): |
| 29 | + """Ensures that __replace__ accepts _inner_value keyword argument.""" |
| 30 | + container = BaseContainer(initial) |
| 31 | + replaced = container.__replace__(_inner_value=new_val) |
| 32 | + |
| 33 | + assert replaced._inner_value == new_val |
| 34 | + assert container._inner_value == initial |
| 35 | + |
| 36 | + |
| 37 | +def test_base_container_replace_no_args(): |
| 38 | + """Ensures that __replace__ without arguments returns an equal copy.""" |
| 39 | + container = BaseContainer(42) |
| 40 | + replaced = container.__replace__() |
| 41 | + |
| 42 | + assert replaced == container |
| 43 | + assert replaced is not container |
| 44 | + |
| 45 | + |
| 46 | +def test_base_container_replace_unexpected_kwarg(): |
| 47 | + """Ensures that unexpected kwargs raise TypeError.""" |
| 48 | + container = BaseContainer(42) |
| 49 | + with pytest.raises(TypeError, match="unexpected keyword argument 'invalid'"): |
| 50 | + container.__replace__(invalid=True) |
| 51 | + |
| 52 | + |
| 53 | +def test_base_container_replace_multiple_values(): |
| 54 | + """Ensures providing both inner_value and _inner_value raises TypeError.""" |
| 55 | + container = BaseContainer(42) |
| 56 | + with pytest.raises(TypeError, match="multiple values for inner value"): |
| 57 | + container.__replace__(inner_value=1, _inner_value=2) |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize( |
| 61 | + ('container', 'new_value', 'expected'), |
| 62 | + [ |
| 63 | + (Success(1), 2, Success(2)), |
| 64 | + (Failure('err1'), 'err2', Failure('err2')), |
| 65 | + (Some('a'), 'b', Some('b')), |
| 66 | + (IO(100), 200, IO(200)), |
| 67 | + ], |
| 68 | +) |
| 69 | +def test_concrete_containers_replace(container: Any, new_value: Any, expected: Any): |
| 70 | + """Ensures concrete containers support __replace__.""" |
| 71 | + replaced = container.__replace__(inner_value=new_value) |
| 72 | + assert replaced == expected |
| 73 | + assert isinstance(replaced, type(container)) |
| 74 | + |
| 75 | + |
| 76 | +def test_nothing_replace(): |
| 77 | + """Ensures Nothing supports __replace__ when value remains None.""" |
| 78 | + assert Nothing.__replace__() is Nothing |
| 79 | + assert Nothing.__replace__(inner_value=None) is Nothing |
| 80 | + assert Nothing.__replace__(_inner_value=None) is Nothing |
| 81 | + |
| 82 | + with pytest.raises(TypeError, match="Cannot replace inner_value on Nothing"): |
| 83 | + Nothing.__replace__(inner_value=42) |
| 84 | + |
| 85 | + with pytest.raises(TypeError, match="Cannot replace _inner_value on Nothing"): |
| 86 | + Nothing.__replace__(_inner_value=42) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.skipif(sys.version_info < (3, 13), reason="copy.replace was added in Python 3.13") |
| 90 | +def test_copy_replace_standard_library(): |
| 91 | + """Ensures stdlib copy.replace works seamlessly on Python 3.13+.""" |
| 92 | + replace = getattr(copy, "replace") |
| 93 | + s = Success(1) |
| 94 | + replaced = replace(s, inner_value=2) |
| 95 | + assert replaced == Success(2) |
0 commit comments