1+ import tempfile
12from typing import Iterator
23import unittest
34
@@ -10,28 +11,29 @@ def lines() -> Iterator[str]:
1011
1112class TestStream (unittest .TestCase ):
1213 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- )
14+ with tempfile .NamedTemporaryFile (delete = True ) as process_line_file :
15+ self .assertEqual (
16+ list (edit (lines (), "_[-1]" , process_line_file )),
17+ ["0\n " , "r\n " , "r" ],
18+ msg = "str expression must edit the lines" ,
19+ )
20+ self .assertEqual (
21+ list (edit (lines (), 're.sub(r"\d", "X", _)' , process_line_file )),
22+ ["fXX\n " , "bar\n " , "fXXbar" ],
23+ msg = "re should be supported out-of-the-box" ,
24+ )
25+ self .assertEqual (
26+ list (edit (lines (), '"0" in _' , process_line_file )),
27+ ["f00\n " , "f00bar" ],
28+ msg = "bool expression must filter the lines" ,
29+ )
30+ self .assertEqual (
31+ list (edit (lines (), "len(_) == 3" , process_line_file )),
32+ ["f00\n " , "bar\n " ],
33+ msg = "_ must exclude linesep" ,
34+ )
35+ self .assertEqual (
36+ list (edit (lines (), "str(int(math.pow(10, len(_))))" , process_line_file )),
37+ ["1000\n " , "1000\n " , "1000000" ],
38+ msg = "modules should be auto-imported" ,
39+ )
0 commit comments