'''
Author: huangbaochen<huangbaochenwo@live.com>
Date: 2021-12-11 20:04:19
LastEditTime: 2021-12-11 21:46:16
LastEditors: huangbaochen<huangbaochenwo@live.com>
Description: 测试Try单子
No MERCY
'''
import pytest
from fppy.try_monad import Try, Success, Fail
from fppy.option import Just, Nothing

@pytest.mark.try_monad
def test_try_apply():
    assert Try.apply(1) == Success(1)
    assert Try(1) == Success(1)

@pytest.mark.try_monad
def test_try_unapply():
    assert Success.unapply(Success(1)) == Just(1)
    assert Fail.unapply(Fail(TypeError(), 1)) == Nothing()

    with pytest.raises(TypeError):
        Fail.unapply(1)

    with pytest.raises(TypeError):
        Fail.unapply(Success(1))

    with pytest.raises(TypeError):
        Success.unapply(1)

    with pytest.raises(TypeError):
        Success.unapply(Fail(Exception(), 1))

def test_try_monad_map():
    assert Success(1).map(lambda x: x + 1) == Success(2)
    assert Success(1).map(lambda x: x / 0) ==\
        Fail(ZeroDivisionError('division by zero'), 1)
    assert Fail(ZeroDivisionError('division by zero'),  1)\
        .map(lambda x: x + 1) ==\
        Fail(ZeroDivisionError('division by zero'),  1)

@pytest.mark.try_monad
def test_try_monad_flat_map():
    assert Success(1).flat_map(lambda x: Success(2)) == Success(2)
    assert Fail(ZeroDivisionError('division by zero'), 1)\
        .flat_map(lambda x: Success(1)) ==\
        Fail(ZeroDivisionError('division by zero'), 1)

    with pytest.raises(TypeError):
        Success(1).flat_map(lambda x: x + 1)

@pytest.mark.try_monad
def test_try_monad_eq():
    assert Fail(ZeroDivisionError('division by zero'), 1) ==\
        Fail(ZeroDivisionError('division by zero'), 1)

    assert Fail(ZeroDivisionError('division by'), 1) !=\
        Fail(ZeroDivisionError('division by zero'), 1)

    assert Fail(ZeroDivisionError('division by zero'), 0) !=\
        Fail(ZeroDivisionError('division by zero'), 1)

@pytest.mark.try_monad
def test_try_monad_get():
    assert Fail(ZeroDivisionError('division by zero'), 1)\
        .get().args ==\
        ZeroDivisionError('division by zero').args

    assert Success(1).get() == 1

    # pylint: disable=no-member
    assert Try("s").get() == "s"

@pytest.mark.try_monad
def test_try_monad_get_or_else():
    assert Fail(ZeroDivisionError('division by zero'),  1)\
        .get_or_else(2) == 2

    assert Success(1).get_or_else(2) == 1

@pytest.mark.try_monad
def test_try_monad_get_error_input():
    assert Fail(ZeroDivisionError('division by zero'),  1)\
        .get_error_input() == 1

[((350, 362), 'fppy.try_monad.Try.apply', 'Try.apply', (['(1)'], {}), '(1)\n', (359, 362), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((366, 376), 'fppy.try_monad.Success', 'Success', (['(1)'], {}), '(1)\n', (373, 376), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((388, 394), 'fppy.try_monad.Try', 'Try', (['(1)'], {}), '(1)\n', (391, 394), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((398, 408), 'fppy.try_monad.Success', 'Success', (['(1)'], {}), '(1)\n', (405, 408), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((499, 506), 'fppy.option.Just', 'Just', (['(1)'], {}), '(1)\n', (503, 506), False, 'from fppy.option import Just, Nothing\n'), ((556, 565), 'fppy.option.Nothing', 'Nothing', ([], {}), '()\n', (563, 565), False, 'from fppy.option import Just, Nothing\n'), ((576, 600), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (589, 600), False, 'import pytest\n'), ((610, 625), 'fppy.try_monad.Fail.unapply', 'Fail.unapply', (['(1)'], {}), '(1)\n', (622, 625), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((636, 660), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (649, 660), False, 'import pytest\n'), ((705, 729), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (718, 729), False, 'import pytest\n'), ((739, 757), 'fppy.try_monad.Success.unapply', 'Success.unapply', (['(1)'], {}), '(1)\n', (754, 757), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((768, 792), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (781, 792), False, 'import pytest\n'), ((913, 923), 'fppy.try_monad.Success', 'Success', (['(2)'], {}), '(2)\n', (920, 923), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((1287, 1297), 'fppy.try_monad.Success', 'Success', (['(2)'], {}), '(2)\n', (1294, 1297), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((1466, 1490), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (1479, 1490), False, 'import pytest\n'), ((484, 494), 'fppy.try_monad.Success', 'Success', (['(1)'], {}), '(1)\n', (491, 494), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((683, 693), 'fppy.try_monad.Success', 'Success', (['(1)'], {}), '(1)\n', (690, 693), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((878, 888), 'fppy.try_monad.Success', 'Success', (['(1)'], {}), '(1)\n', (885, 888), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((935, 945), 'fppy.try_monad.Success', 'Success', (['(1)'], {}), '(1)\n', (942, 945), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((1242, 1252), 'fppy.try_monad.Success', 'Success', (['(1)'], {}), '(1)\n', (1249, 1252), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((1272, 1282), 'fppy.try_monad.Success', 'Success', (['(2)'], {}), '(2)\n', (1279, 1282), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((1385, 1395), 'fppy.try_monad.Success', 'Success', (['(1)'], {}), '(1)\n', (1392, 1395), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((1500, 1510), 'fppy.try_monad.Success', 'Success', (['(1)'], {}), '(1)\n', (1507, 1510), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((2130, 2140), 'fppy.try_monad.Success', 'Success', (['(1)'], {}), '(1)\n', (2137, 2140), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((2196, 2204), 'fppy.try_monad.Try', 'Try', (['"""s"""'], {}), "('s')\n", (2199, 2204), False, 'from fppy.try_monad import Try, Success, Fail\n'), ((2377, 2387), 'fppy.try_monad.Success', 'Success', (['(1)'], {}), '(1)\n', (2384, 2387), False, 'from fppy.try_monad import Try, Success, Fail\n')]