Exemple #1
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 == ""
Exemple #2
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)
Exemple #3
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
Exemple #4
0
def test_is_some():
    assert option.is_some(option.some(55)) == True
    assert option.is_some(option.none()) == False
Exemple #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
Exemple #6
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
Exemple #7
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
Exemple #8
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)
Exemple #9
0
def test_value():
    opt = option.some(5)
    nopt = option.none()
    assert option.value(1, opt) == 5
    assert option.value(1, nopt) == 1
Exemple #10
0
def test_none():
    assert option.none() == None
Exemple #11
0
def test_to_sequence():
    opt = option.some(55)
    assert option.to_seq(opt) == [55]
    assert option.to_seq(option.none()) == []
Exemple #12
0
def test_to_list():
    opt = option.some(55)
    assert option.to_list(opt) == [55]
    assert option.to_list(option.none()) == []