aboutsummaryrefslogtreecommitdiff
path: root/test/test_util.py
blob: a86bf072e077cca38d4cd0f52fa3b401050af551 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from mu4web.util import (
    MutableString,
    chain,
    cwd,
    find,
    force,
    split_path,
)
import os
from tempfile import TemporaryDirectory
from math import sqrt
import pytest


def test_split_path() -> None:
    assert split_path("/home/hugo/test/something") == ['home', 'hugo', 'test', 'something']


def test_find(maildir: str) -> None:
    assert sorted([split_path(p)[-3:] for p in find(maildir, type='f')]) == [
        [b'mail', b'cur', b'1691189956.3013485_5.gandalf:2,RS'],
        [b'mail', b'cur', b'1691189958.3013485_7.gandalf:2,S'],
        [b'mail', b'cur', b'1691314589.3192599_3.gandalf:2,S']]
    assert sorted([split_path(p)[-1] for p in find(maildir, type='d')]) \
        == [b'cur', b'mail', b'new', b'tmp']


def test_cwd() -> None:
    outer = os.getcwd()
    with TemporaryDirectory() as dir:
        with cwd(dir):
            assert os.getcwd() == dir
    assert outer == os.getcwd()


def test_chain() -> None:
    assert (chain(range(9))
            @ sum  # type: ignore
            @ sqrt).value == 6.0  # type: ignore


def test_force() -> None:
    assert force(1) == 1

    with pytest.raises(AssertionError):
        force(None)


def test_mutable_string() -> None:

    def inner(s: str | MutableString, msg: str) -> None:
        s += msg

    s1: str = ''
    s2: MutableString = MutableString()

    inner(s1, "Hello")
    inner(s1, "World")

    inner(s2, "Hello")
    inner(s2, "World")

    # Control with regular string, to ensure that we actually mutate
    # the variable value, and not just the variable.
    assert s1 == ''

    assert str(s2) == "HelloWorld"
    assert repr(s2) == 'MutableString("HelloWorld")'