Skip to content

Commit

Permalink
- add the final patch for #146
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzeek committed Apr 6, 2012
1 parent 1b4d1fb commit 8cce610
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 8 deletions.
19 changes: 11 additions & 8 deletions mako/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,15 +778,18 @@ def visitControlLine(self, node):
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):
Expand Down
98 changes: 98 additions & 0 deletions test/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,104 @@ def test_blank_control_8(self):
"",
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("""
Expand Down

0 comments on commit 8cce610

Please sign in to comment.