Esempio n. 1
0
def test_equal():
    opt1 = option.some(10)
    opt2 = option.some(10)
    opt3 = option.some(55)
    opt4 = option.none()
    compare: Callable[[int, int], bool] = lambda x, y: x == y
    assert option.equal(compare, opt1, opt2)
    assert option.equal(compare, opt1, opt3) == False
    assert option.equal(compare, opt1, opt4) == False
    assert option.equal(compare, opt4, None)
Esempio n. 2
0
def test_compare():
    opt1 = option.some(10)
    opt2 = option.some(10)
    opt3 = option.some(55)
    opt4 = option.none()
    compare: Callable[[int, int], int] = lambda x, y: x - y
    assert option.compare(compare, opt1, opt2) == 0
    assert option.compare(compare, opt1, opt3) < 0
    assert option.compare(compare, opt3, opt1) > 0
    assert option.compare(compare, opt1, opt4) > 0
    assert option.compare(compare, opt1, opt4) > 0
    assert option.compare(compare, opt4, None) == 0
Esempio n. 3
0
def test_iter(capsys):
    opt: Optional[int] = option.some(55)
    option.iter(print, opt)
    captured = capsys.readouterr()
    assert captured.out == "55\n"
    option.iter(print, option.none())
    captured = capsys.readouterr()
    assert captured.out == ""
Esempio n. 4
0
def test_is_some():
    assert option.is_some(option.some(55)) == True
    assert option.is_some(option.none()) == False
Esempio n. 5
0
def test_fold():
    opt: Optional[int] = option.some(55)
    nopt: Optional[int] = option.none()
    assert option.fold(-1, lambda n: n * 2, opt) == 110
    assert option.fold(-1, lambda n: n * 2, nopt) == -1
Esempio n. 6
0
def test_map():
    opt: Optional[int] = option.some(55)
    assert option.map(str, opt) == "55"
    assert option.map(lambda n: n / 2, opt) == 27.5
    assert option.map(str, option.none()) == None
Esempio n. 7
0
def test_some():
    opt = option.some(5)
    assert opt == 5
    assert isinstance(opt, int)
Esempio n. 8
0
def test_bind():
    opt = option.some("Dave")
    nopt = option.none()
    assert option.bind(opt, str.upper) == "DAVE"
    assert option.bind(nopt, str.upper) == None
Esempio n. 9
0
def test_join():
    optopt: Optional[Optional[str]] = option.some(option.some("Dave"))
    assert option.join(optopt) == "Dave"
    noption = None
    assert option.join(noption) == None
Esempio n. 10
0
def test_get():
    opt = option.some("Dave")
    nopt = option.none()
    assert option.get(opt) == "Dave"
    with pytest.raises(option.InvalidArgument):
        assert option.get(nopt)
Esempio n. 11
0
def test_value():
    opt = option.some(5)
    nopt = option.none()
    assert option.value(1, opt) == 5
    assert option.value(1, nopt) == 1
Esempio n. 12
0
def test_to_sequence():
    opt = option.some(55)
    assert option.to_seq(opt) == [55]
    assert option.to_seq(option.none()) == []
Esempio n. 13
0
def test_some_exception():
    with pytest.raises(option.InvalidArgument):
        option.some(None)
Esempio n. 14
0
def test_to_list():
    opt = option.some(55)
    assert option.to_list(opt) == [55]
    assert option.to_list(option.none()) == []