Commits

Mike Bayer committed eea3618

- add the final patch for #146

Comments (0)

Files changed (2)

                 text = node.text
             self.printer.writeline(text)
             children = node.get_children()
-            # this covers the two situations where we want to insert a pass:
-            #    1) a ternary control line with no children and
+            # this covers the three situations where we want to insert a pass:
+            #    1) a ternary control line with no children,
             #    2) a primary control line with nothing but its own ternary
-            #          and end control lines
-            if (not node.get_children or
-                util.all(isinstance(c, parsetree.ControlLine)
-                         for c in children) and
-                util.all((node.is_ternary(c.keyword) or c.isend)
-                         for c in children)):
+            #          and end control lines, and
+            #    3) any control line with no content other than comments
+            if not children or (
+                    util.all(isinstance(c, (parsetree.Comment,
+                                            parsetree.ControlLine))
+                             for c in children) and
+                    util.all((node.is_ternary(c.keyword) or c.isend)
+                             for c in children
+                             if isinstance(c, parsetree.ControlLine))):
                 self.printer.writeline("pass")
  
     def visitText(self, node):

test/test_template.py

             "",
             filters=lambda s:s.strip()
         )
+
+    def test_commented_blank_control_1(self):
+        self._do_memory_test(
+            """
+            % if True:
+            ## comment
+            % endif
+            """,
+            "",
+            filters=lambda s:s.strip()
+        )
+ 
+    def test_commented_blank_control_2(self):
+        self._do_memory_test(
+            """
+            % if True:
+            ## comment
+            % elif True:
+            ## comment
+            % endif
+            """,
+            "",
+            filters=lambda s:s.strip()
+        )
+ 
+    def test_commented_blank_control_3(self):
+        self._do_memory_test(
+            """
+            % if True:
+            ## comment
+            % else:
+            ## comment
+            % endif
+            """,
+            "",
+            filters=lambda s:s.strip()
+        )
+ 
+    def test_commented_blank_control_4(self):
+        self._do_memory_test(
+            """
+            % if True:
+            ## comment
+            % elif True:
+            ## comment
+            % else:
+            ## comment
+            % endif
+            """,
+            "",
+            filters=lambda s:s.strip()
+        )
+ 
+    def test_commented_blank_control_5(self):
+        self._do_memory_test(
+            """
+            % for x in range(10):
+            ## comment
+            % endfor
+            """,
+            "",
+            filters=lambda s:s.strip()
+        )
+ 
+    def test_commented_blank_control_6(self):
+        self._do_memory_test(
+            """
+            % while False:
+            ## comment
+            % endwhile
+            """,
+            "",
+            filters=lambda s:s.strip()
+        )
+ 
+    def test_commented_blank_control_7(self):
+        self._do_memory_test(
+            """
+            % try:
+            ## comment
+            % except:
+            ## comment
+            % endtry
+            """,
+            "",
+            filters=lambda s:s.strip()
+        )
+ 
+    def test_commented_blank_control_8(self):
+        self._do_memory_test(
+            """
+            % with open('x', 'w') as fp:
+            ## comment
+            % endwith
+            """,
+            "",
+            filters=lambda s:s.strip()
+        )
  
     def test_multiline_control(self):
         t = Template("""