|
1
|
|
|
# SPDX-FileCopyrightText: Copyright 2020-2023, Contributors to pocketutils |
|
2
|
|
|
# SPDX-PackageHomePage: https://github.com/dmyersturnbull/pocketutils |
|
3
|
|
|
# SPDX-License-Identifier: Apache-2.0 |
|
4
|
|
|
from io import StringIO |
|
5
|
|
|
from typing import Self |
|
6
|
|
|
|
|
7
|
|
|
import pytest |
|
8
|
|
|
from pocketutils.core.input_output import Capture, DelegatingWriter, OpenMode |
|
9
|
|
|
from pocketutils.core.mocks import MockWritable |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class TestIo: |
|
13
|
|
|
def test_delegating(self: Self) -> None: |
|
14
|
|
|
a, b = MockWritable(), MockWritable() |
|
15
|
|
|
d = DelegatingWriter(a, b) |
|
16
|
|
|
d.write("abc") |
|
17
|
|
|
assert a.data == "write:abc" |
|
18
|
|
|
assert b.data == "write:abc" |
|
19
|
|
|
d.flush() |
|
20
|
|
|
assert a.data == "write:abcflush" |
|
21
|
|
|
assert b.data == "write:abcflush" |
|
22
|
|
|
d.close() |
|
23
|
|
|
assert a.data == "write:abcflushclose" |
|
24
|
|
|
assert b.data == "write:abcflushclose" |
|
25
|
|
|
a.write("00") |
|
26
|
|
|
assert a.data == "write:00" |
|
27
|
|
|
assert b.data == "write:abcflushclose" |
|
28
|
|
|
|
|
29
|
|
|
def test_capture(self: Self) -> None: |
|
30
|
|
|
w = StringIO("abc") |
|
31
|
|
|
c = Capture(w) |
|
32
|
|
|
assert c.value == "abc" |
|
33
|
|
|
|
|
34
|
|
|
def test_open_mode_normalize(self: Self) -> None: |
|
35
|
|
|
o = OpenMode |
|
36
|
|
|
assert str(o("").normalize()) == "rt" |
|
37
|
|
|
assert str(o("r").normalize()) == "rt" |
|
38
|
|
|
assert str(o("wU").normalize()) == "wt" |
|
39
|
|
|
assert str(o("a").normalize()) == "at" |
|
40
|
|
|
assert str(o("w+").normalize()) == "wt+" |
|
41
|
|
|
assert str(o("wt+").normalize()) == "wt+" |
|
42
|
|
|
assert str(o("wb+").normalize()) == "wb+" |
|
43
|
|
|
assert str(o("Ux").normalize()) == "xt" |
|
44
|
|
|
assert str(o("Uxb").normalize()) == "xb" |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
if __name__ == "__main__": |
|
48
|
|
|
pytest.main() |
|
49
|
|
|
|