11from typing import Iterator
22import unittest
33
4- from pythoned import edit
4+ from pythoned import edit , generate
55
66
77def lines () -> Iterator [str ]:
@@ -10,28 +10,53 @@ def lines() -> Iterator[str]:
1010
1111class TestStream (unittest .TestCase ):
1212 def test_edit (self ) -> None :
13- self .assertEqual (
14- list (edit (lines (), "_[-1]" )),
13+ self .assertListEqual (
14+ list (edit ("_[-1]" , lines () )),
1515 ["0\n " , "r\n " , "r" ],
1616 msg = "str expression must edit the lines" ,
1717 )
18- self .assertEqual (
19- list (edit (lines (), 're.sub(r"\d", "X", _)' )),
18+ self .assertListEqual (
19+ list (edit ('re.sub(r"\d", "X", _)' , lines () )),
2020 ["fXX\n " , "bar\n " , "fXXbar" ],
2121 msg = "re should be supported out-of-the-box" ,
2222 )
23- self .assertEqual (
24- list (edit (lines (), '"0" in _' )),
23+ self .assertListEqual (
24+ list (edit ('"0" in _' , lines () )),
2525 ["f00\n " , "f00bar" ],
2626 msg = "bool expression must filter the lines" ,
2727 )
28- self .assertEqual (
29- list (edit (lines (), "len(_) == 3" )),
28+ self .assertListEqual (
29+ list (edit ("list(_)" , lines ())),
30+ ["f\n " , "0\n " , "0\n " , "b\n " , "a\n " , "r\n " , "f\n " , "0\n " , "0\n " , "b\n " , "a\n " , "r" ],
31+ msg = "list expression should flatten" ,
32+ )
33+ self .assertListEqual (
34+ list (edit ("len(_) == 3" , lines ())),
3035 ["f00\n " , "bar\n " ],
31- msg = "_ must exclude linesep" ,
36+ msg = "`_` must not include linesep" ,
3237 )
33- self .assertEqual (
34- list (edit (lines (), "re.sub('[0]', 'O', str(int(math.pow(10, len(_)))))" )),
38+ self .assertListEqual (
39+ list (edit ("re.sub('[0]', 'O', str(int(math.pow(10, len(_)))))" , lines () )),
3540 ["1OOO\n " , "1OOO\n " , "1OOOOOO" ],
3641 msg = "modules should be auto-imported" ,
3742 )
43+ self .assertListEqual (
44+ list (generate ("['foo', 'bar']" )),
45+ ["foo\n " , "bar\n " ],
46+ msg = "generator when `_` not used" ,
47+ )
48+ self .assertListEqual (
49+ list (generate ("[0, 1]" )),
50+ ["0\n " , "1\n " ],
51+ msg = "generator when `_` not used, ok with non str elements" ,
52+ )
53+ with self .assertRaisesRegex (
54+ TypeError ,
55+ "the generating expression must be an iterable but got a <class 'bool'>" ,
56+ ):
57+ list (generate ("True" ))
58+ with self .assertRaisesRegex (
59+ TypeError ,
60+ r"the editing expression must be an str \(editing\) or a bool \(filtering\) or a iterable \(flattening\) but got a <class 'int'>" ,
61+ ):
62+ list (edit ("0 if _ else 1" , lines ()))
0 commit comments