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