Skip to content

Commit

Permalink
Merge 0c6dd93 into 8eeb0b6
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Jun 13, 2016
2 parents 8eeb0b6 + 0c6dd93 commit 13702fd
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Cheetah/legacy_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ def __init__(self, source, settings=None):
if source == '':
warnings.warn('You supplied an empty string for the source!')

self._original_source = source
self._parser = self.parserClass(source, compiler=self)
self._class_compiler = None
self._base_import = 'from Cheetah.Template import {} as {}'.format(
Expand Down Expand Up @@ -512,6 +513,14 @@ def set_extends(self, extends_name):
extends_name, CLASS_NAME, BASE_CLASS_NAME,
)

# TODO(#183): stop using the metaclass and just generate functions
# Partial templates expose their functions as globals, find all the
# defined functions and add them to known global vars.
if extends_name == 'Cheetah.partial_template':
self._global_vars.update(get_defined_method_names(
self._original_source,
))

def add_compiler_settings(self):
settings_str = self.getStrConst()
self.clearStrConst()
Expand Down Expand Up @@ -564,3 +573,21 @@ def getModuleCode(self):
) + '\n'

return moduleDef


def get_defined_method_names(original_source):
class CollectsMethodNamesCompiler(object):
def __init__(self):
self.method_names = set()

# Trivially allow anything outside of startMethodDef
def __getattr__(self, name):
return lambda *args, **kwargs: None

# Collect our function names
def startMethodDef(self, method_name, *args):
self.method_names.add(method_name)

compiler = CollectsMethodNamesCompiler()
LegacyParser(original_source, compiler=compiler).parse()
return compiler.method_names
10 changes: 10 additions & 0 deletions testing/templates/src/optimize_name.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#extends Cheetah.partial_template

#def foo():
$bar(5)
#end def


#def bar(x)
#return x * x
#end def
8 changes: 8 additions & 0 deletions tests/Compiler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from Cheetah.compile import _create_module_from_source
from Cheetah.compile import compile_source
from Cheetah.compile import compile_to_class
from Cheetah.Template import Template
from testing.util import run_python


Expand Down Expand Up @@ -221,3 +222,10 @@ def test_optimization_kwargs():
'#end def\n'
)
assert ' _v = kwargs #' in src


def test_optimization_partial_template_functions():
from testing.templates.src.optimize_name import foo
assert foo(Template()).strip() == '25'
src = io.open('testing/templates/src/optimize_name.py').read()
assert ' _v = bar(5) #' in src
12 changes: 12 additions & 0 deletions tests/legacy_compiler_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from __future__ import absolute_import
from __future__ import unicode_literals

from Cheetah.legacy_compiler import get_defined_method_names


def test_get_method_names_trivial():
assert get_defined_method_names('') == set()


def test_get_method_names_a_method():
assert get_defined_method_names('#def foo(): 1') == {'foo'}
11 changes: 11 additions & 0 deletions tests/testing/all_partials_tested_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def test_get_partial_methods():
from testing.templates import src
ret = get_partial_methods((src,))
assert ret == {
'testing.templates.src.optimize_name': {'foo', 'bar'},
'testing.templates.src.partial_template_no_arguments': {'render'},
'testing.templates.src.partial_with_same_name':
{'partial_with_same_name'},
Expand Down Expand Up @@ -129,6 +130,16 @@ def test_get_partial_tests():
'testing.templates.src.context_partial_template',
'render',
),
(
P.OptimizeNamePartialTemplateFooTest,
'testing.templates.src.optimize_name',
'foo',
),
(
P.OptimizeNamePartialTemplateBarTest,
'testing.templates.src.optimize_name',
'bar',
),
}


Expand Down
19 changes: 19 additions & 0 deletions tests/testing/partial_template_test_case_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ def assert_partial_rendering(self, pq, partial_args, partial_kwargs):
assert pq.text() == 'Hello world'


class OptimizeNamePartialTemplateFooTest(PartialTemplateTestCase):
partial = 'testing.templates.src.optimize_name'
method = 'foo'

def assert_partial_rendering(self, pq, *_):
assert pq.text() == '25'


class OptimizeNamePartialTemplateBarTest(PartialTemplateTestCase):
partial = 'testing.templates.src.optimize_name'
method = 'bar'

def get_partial_arguments(self):
return (3,), {}

def assert_partial_rendering(self, pq, *_):
assert pq.text() == '9'


def test_it_can_fail_wrong_args():
class Failure(PartialTemplateTestCase):
partial = 'testing.templates.src.partial_template'
Expand Down

0 comments on commit 13702fd

Please sign in to comment.