diff options
Diffstat (limited to 'test/test_util.py')
-rw-r--r-- | test/test_util.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/test/test_util.py b/test/test_util.py new file mode 100644 index 0000000..c83ac98 --- /dev/null +++ b/test/test_util.py @@ -0,0 +1,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 [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 [split_path(p)[-1] for p in find(maildir, type='d')] \ + == [b'mail', b'cur', 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")' |