'''
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

[((15, 11, 15, 23), 'fppy.try_monad.Try.apply', 'Try.apply', ({(15, 21, 15, 22): '(1)'}, {}), '(1)', False, 'from fppy.try_monad import Try, Success, Fail\n'), ((15, 27, 15, 37), 'fppy.try_monad.Success', 'Success', ({(15, 35, 15, 36): '(1)'}, {}), '(1)', False, 'from fppy.try_monad import Try, Success, Fail\n'), ((16, 11, 16, 17), 'fppy.try_monad.Try', 'Try', ({(16, 15, 16, 16): '(1)'}, {}), '(1)', False, 'from fppy.try_monad import Try, Success, Fail\n'), ((16, 21, 16, 31), 'fppy.try_monad.Success', 'Success', ({(16, 29, 16, 30): '(1)'}, {}), '(1)', False, 'from fppy.try_monad import Try, Success, Fail\n'), ((20, 42, 20, 49), 'fppy.option.Just', 'Just', ({(20, 47, 20, 48): '(1)'}, {}), '(1)', False, 'from fppy.option import Just, Nothing\n'), ((21, 49, 21, 58), 'fppy.option.Nothing', 'Nothing', ({}, {}), '()', False, 'from fppy.option import Just, Nothing\n'), ((23, 9, 23, 33), 'pytest.raises', 'pytest.raises', ({(23, 23, 23, 32): 'TypeError'}, {}), '(TypeError)', False, 'import pytest\n'), ((24, 8, 24, 23), 'fppy.try_monad.Fail.unapply', 'Fail.unapply', ({(24, 21, 24, 22): '(1)'}, {}), '(1)', False, 'from fppy.try_monad import Try, Success, Fail\n'), ((26, 9, 26, 33), 'pytest.raises', 'pytest.raises', ({(26, 23, 26, 32): 'TypeError'}, {}), '(TypeError)', False, 'import pytest\n'), ((29, 9, 29, 33), 'pytest.raises', 'pytest.raises', ({(29, 23, 29, 32): 'TypeError'}, {}), '(TypeError)', False, 'import pytest\n'), ((30, 8, 30, 26), 'fppy.try_monad.Success.unapply', 'Success.unapply', ({(30, 24, 30, 25): '(1)'}, {}), '(1)', False, 'from fppy.try_monad import Try, Success, Fail\n'), ((32, 9, 32, 33), 'pytest.raises', 'pytest.raises', ({(32, 23, 32, 32): 'TypeError'}, {}), '(TypeError)', False, 'import pytest\n'), ((36, 46, 36, 56), 'fppy.try_monad.Success', 'Success', ({(36, 54, 36, 55): '(2)'}, {}), '(2)', False, 'from fppy.try_monad import Try, Success, Fail\n'), ((45, 56, 45, 66), 'fppy.try_monad.Success', 'Success', ({(45, 64, 45, 65): '(2)'}, {}), '(2)', False, 'from fppy.try_monad import Try, Success, Fail\n'), ((50, 9, 50, 33), 'pytest.raises', 'pytest.raises', ({(50, 23, 50, 32): 'TypeError'}, {}), '(TypeError)', False, 'import pytest\n'), ((20, 27, 20, 37), 'fppy.try_monad.Success', 'Success', ({(20, 35, 20, 36): '(1)'}, {}), '(1)', False, 'from fppy.try_monad import Try, Success, Fail\n'), ((27, 21, 27, 31), 'fppy.try_monad.Success', 'Success', ({(27, 29, 27, 30): '(1)'}, {}), '(1)', False, 'from fppy.try_monad import Try, Success, Fail\n'), ((36, 11, 36, 21), 'fppy.try_monad.Success', 'Success', ({(36, 19, 36, 20): '(1)'}, {}), '(1)', False, 'from fppy.try_monad import Try, Success, Fail\n'), ((37, 11, 37, 21), 'fppy.try_monad.Success', 'Success', ({(37, 19, 37, 20): '(1)'}, {}), '(1)', False, 'from fppy.try_monad import Try, Success, Fail\n'), ((45, 11, 45, 21), 'fppy.try_monad.Success', 'Success', ({(45, 19, 45, 20): '(1)'}, {}), '(1)', False, 'from fppy.try_monad import Try, Success, Fail\n'), ((45, 41, 45, 51), 'fppy.try_monad.Success', 'Success', ({(45, 49, 45, 50): '(2)'}, {}), '(2)', False, 'from fppy.try_monad import Try, Success, Fail\n'), ((47, 28, 47, 38), 'fppy.try_monad.Success', 'Success', ({(47, 36, 47, 37): '(1)'}, {}), '(1)', False, 'from fppy.try_monad import Try, Success, Fail\n'), ((51, 8, 51, 18), 'fppy.try_monad.Success', 'Success', ({(51, 16, 51, 17): '(1)'}, {}), '(1)', False, 'from fppy.try_monad import Try, Success, Fail\n'), ((70, 11, 70, 21), 'fppy.try_monad.Success', 'Success', ({(70, 19, 70, 20): '(1)'}, {}), '(1)', False, 'from fppy.try_monad import Try, Success, Fail\n'), ((73, 11, 73, 19), 'fppy.try_monad.Try', 'Try', ({(73, 15, 73, 18): '"""s"""'}, {}), "('s')", False, 'from fppy.try_monad import Try, Success, Fail\n'), ((80, 11, 80, 21), 'fppy.try_monad.Success', 'Success', ({(80, 19, 80, 20): '(1)'}, {}), '(1)', False, 'from fppy.try_monad import Try, Success, Fail\n')]