@@ -105,3 +105,76 @@ def test_blank_trailing_line(self):
105105
106106 def test_tabs (self ):
107107 self .assertIndented (original .tabs )
108+
109+
110+ class TestSeparateBlocks (unittest .TestCase ):
111+ """Pasted code has no blank line after a block, so one has to be added
112+ for the statement that follows it to be run separately (#1057)"""
113+
114+ def test_statement_after_block (self ):
115+ self .assertEqual (
116+ preproc ("for i in r:\n f(i)\n print(1)\n " ),
117+ "for i in r:\n f(i)\n \n print(1)\n " ,
118+ )
119+
120+ def test_statement_after_nested_blocks (self ):
121+ self .assertEqual (
122+ preproc ("for i in r:\n if i:\n f(i)\n print(1)\n " ),
123+ "for i in r:\n if i:\n f(i)\n \n print(1)\n " ,
124+ )
125+
126+ def test_consecutive_blocks (self ):
127+ self .assertEqual (
128+ preproc ("if a:\n f()\n while b:\n g()\n " ),
129+ "if a:\n f()\n \n while b:\n g()\n " ,
130+ )
131+
132+ def test_clauses_continuing_a_block_are_left_alone (self ):
133+ for source in (
134+ "if a:\n f()\n elif b:\n g()\n else:\n h()\n " ,
135+ "try:\n f()\n except E:\n g()\n finally:\n h()\n " ,
136+ "for i in r:\n f(i)\n else:\n g()\n " ,
137+ ):
138+ with self .subTest (source = source ):
139+ self .assertEqual (preproc (source ), source )
140+
141+ def test_unindented_lines_inside_brackets_are_left_alone (self ):
142+ # an unclosed bracket keeps the statement going, so these lines are
143+ # continuations rather than new statements
144+ for source in (
145+ "total = (\n 1 +\n 2\n )\n " ,
146+ "items = [\n 'a',\n 'b',\n ]\n " ,
147+ "f(\n x,\n )\n " ,
148+ ):
149+ with self .subTest (source = source ):
150+ self .assertEqual (preproc (source ), source )
151+
152+ def test_consecutive_simple_statements_are_left_alone (self ):
153+ source = "a = 1\n b = 2\n print(a + b)\n "
154+ self .assertEqual (preproc (source ), source )
155+
156+ def test_existing_blank_line_is_not_doubled (self ):
157+ source = "for i in r:\n f(i)\n \n print(1)\n "
158+ self .assertEqual (preproc (source ), source )
159+
160+ def test_block_after_statement (self ):
161+ source = "a = 1\n for i in r:\n f(i)\n "
162+ self .assertEqual (preproc (source ), source )
163+
164+ def test_comment_above_a_continuing_clause_is_left_alone (self ):
165+ source = "if a:\n f()\n # why not\n else:\n g()\n "
166+ self .assertEqual (preproc (source ), source )
167+
168+ def test_decorator_is_not_separated_from_its_definition (self ):
169+ self .assertEqual (
170+ preproc ("@deco\n def f():\n pass\n print(1)\n " ),
171+ "@deco\n def f():\n pass\n \n print(1)\n " ,
172+ )
173+
174+ def test_lines_inside_a_string_are_left_alone (self ):
175+ source = 's = """\n abc\n def\n """\n print(s)\n '
176+ self .assertEqual (preproc (source ), source )
177+
178+ def test_backslash_continuation_is_left_alone (self ):
179+ source = "a = 1 + \\ \n 2\n print(a)\n "
180+ self .assertEqual (preproc (source ), source )
0 commit comments