|
| 1 | +from typing import Iterator |
| 2 | +import unittest |
| 3 | + |
| 4 | +from pythoned.edit import edit |
| 5 | + |
| 6 | + |
| 7 | +def lines() -> Iterator[str]: |
| 8 | + return iter(["f00\n", "bar\n", "f00bar"]) |
| 9 | + |
| 10 | + |
| 11 | +class TestStream(unittest.TestCase): |
| 12 | + def test_edit(self) -> None: |
| 13 | + self.assertEqual( |
| 14 | + list(edit(lines(), "_[-1]")), |
| 15 | + ["0\n", "r\n", "r"], |
| 16 | + msg="str expression must edit the lines", |
| 17 | + ) |
| 18 | + self.assertEqual( |
| 19 | + list(edit(lines(), 're.sub(r"\d", "X", _)')), |
| 20 | + ["fXX\n", "bar\n", "fXXbar"], |
| 21 | + msg="re should be supported out-of-the-box", |
| 22 | + ) |
| 23 | + self.assertEqual( |
| 24 | + list(edit(lines(), '"0" in _')), |
| 25 | + ["f00\n", "f00bar"], |
| 26 | + msg="bool expression must filter the lines", |
| 27 | + ) |
| 28 | + self.assertEqual( |
| 29 | + list(edit(lines(), "len(_) == 3")), |
| 30 | + ["f00\n", "bar\n"], |
| 31 | + msg="_ must exclude linesep", |
| 32 | + ) |
| 33 | + self.assertEqual( |
| 34 | + list(edit(lines(), "str(int(math.pow(10, len(_))))")), |
| 35 | + ["1000\n", "1000\n", "1000000"], |
| 36 | + msg="modules should be auto-imported", |
| 37 | + ) |
0 commit comments