Permalink
Browse files

Added Empty Source Block option to macros.

  • Loading branch information...
1 parent de1e195 commit b6e0f64db96a62e65550a2e7819576f5f5784011 @asottile committed Jul 27, 2012
Showing with 52 additions and 0 deletions.
  1. +7 −0 cheetah/Parser.py
  2. +45 −0 cheetah/Tests/SyntaxAndOutput.py
View
@@ -2325,13 +2325,20 @@ def eatMacroCall(self):
args = self.getExpression(useNameMapper=False,
pyTokensToBreakAt=[':']).strip()
+ # define the property emptySourceBlock on your macro in order to have it parsed as a macro without contents
+ # In this case, the macro will only pick up arguments just as before
if self.matchColonForSingleLineShortFormDirective():
isShortForm = True
self.advance() # skip over :
self.getWhiteSpace(max=1)
srcBlock = self.readToEOL(gobble=False)
EOLCharsInShortForm = self.readToEOL(gobble=True)
#self.readToEOL(gobble=False)
+ elif hasattr(macro, 'emptySourceBlock'):
+ isShortForm = True
+ srcBlock = ''
+ self.readToEOL(gobble=False)
+ EOLCharsInShortForm = self.readToEOL(gobble=True)
else:
isShortForm = False
if self.peek()==':':
@@ -3107,6 +3107,51 @@ def test1(self):
"0123456789")
+class MacroEmptySourceBlock(OutputTest):
+ """Tests that emptySourceBlock property on macro is parsed correctly."""
+
+ class TestMacroNoArgs(object):
+ """
+ Define a macro which does not take parameters but has an empty source block
+ """
+
+ emptySourceBlock = True
+
+ def __init__(self, parser): pass
+ def __call__(self, src, **kwargs): return 'testMacroNoArgs'
+
+ class TestMacroTakesArgs(object):
+ """
+ Define a macro which takes parameters and spits them back out and has an empty source block
+ """
+
+ emptySourceBlock = True
+
+ def __init__(self, parser): pass
+ def __call__(self, src, customParam=None, **kwargs): return customParam
+ def convertArgStrToDict(self, argumentString, **kwargs):
+ return {'customParam': argumentString}
+
+ def _getCompilerSettings(self):
+ return {'macroDirectives': {
+ 'testMacroNoArgs': self.TestMacroNoArgs ,
+ 'testMacroTakesArgs': self.TestMacroTakesArgs
+ }}
+
+ def test1(self):
+ """Test that a macro with no args is processed correctly."""
+
+ self.verify("""\
+#testMacroNoArgs
+""",
+ 'testMacroNoArgs')
+
+ def test2(self):
+ """Test that a macro with no args is processed correctly."""
+ self.verify("""\
+#testMacroTakesArgs arguments, passed
+""",
+ 'arguments, passed')
class DefmacroDirective(OutputTest):
def _getCompilerSettings(self):

0 comments on commit b6e0f64

Please sign in to comment.