Permalink
Browse files

Add TemplateSpec.template_path for issue #41.

  • Loading branch information...
cjerdonek committed Oct 17, 2012
1 parent e255b92 commit 950e511b8c77e69f973884fc2ae9ebfdae42e388
Showing with 27 additions and 11 deletions.
  1. +3 −0 pystache/specloader.py
  2. +3 −0 pystache/template_spec.py
  3. +21 −11 pystache/tests/test_specloader.py
View
@@ -55,6 +55,9 @@ def _find(self, spec):
Find and return the path to the template associated to the instance.
"""
+ if spec.template_path is not None:
+ return spec.template_path
+
dir_path, file_name = self._find_relative(spec)
locator = self.loader._make_locator()
@@ -34,6 +34,8 @@ class TemplateSpec(object):
template_name: the name of the template.
+ template_path: absolute path to the template.
+
template_rel_directory: the directory containing the template file,
relative to the directory containing the module defining the class.
@@ -46,5 +48,6 @@ class TemplateSpec(object):
template_encoding = None
template_extension = None
template_name = None
+ template_path = None
template_rel_directory = None
template_rel_path = None
@@ -30,6 +30,14 @@ class Thing(object):
pass
+class AssertPathsMixin:
+
+ """A unittest.TestCase mixin to check path equality."""
+
+ def assertPaths(self, actual, expected):
+ self.assertEqual(actual, expected)
+
+
class ViewTestCase(unittest.TestCase, AssertStringMixin):
def test_template_rel_directory(self):
@@ -174,7 +182,8 @@ def to_unicode(s, encoding=None):
return SpecLoader(loader=loader)
-class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
+class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin,
+ AssertPathsMixin):
"""
Tests template_spec.SpecLoader.
@@ -288,13 +297,21 @@ def unicode(self, s, encoding=None):
self.assertEqual(loader.s, "template-foo")
self.assertEqual(loader.encoding, "encoding-foo")
+ def test_find__template_path(self):
+ """Test _find() with TemplateSpec.template_path."""
+ loader = self._make_specloader()
+ custom = TemplateSpec()
+ custom.template_path = "path/foo"
+ actual = loader._find(custom)
+ self.assertPaths(actual, "path/foo")
+
# TODO: migrate these tests into the SpecLoaderTests class.
# TODO: rename the get_template() tests to test load().
# TODO: condense, reorganize, and rename the tests so that it is
# clear whether we have full test coverage (e.g. organized by
# TemplateSpec attributes or something).
-class TemplateSpecTests(unittest.TestCase):
+class TemplateSpecTests(unittest.TestCase, AssertPathsMixin):
def _make_loader(self):
return _make_specloader()
@@ -358,13 +375,6 @@ def test_find_relative__template_extension(self):
view.template_extension = 'txt'
self._assert_template_location(view, (None, 'sample_view.txt'))
- def _assert_paths(self, actual, expected):
- """
- Assert that two paths are the same.
-
- """
- self.assertEqual(actual, expected)
-
def test_find__with_directory(self):
"""
Test _find() with a view that has a directory specified.
@@ -379,7 +389,7 @@ def test_find__with_directory(self):
actual = loader._find(view)
expected = os.path.join(DATA_DIR, 'foo/bar.txt')
- self._assert_paths(actual, expected)
+ self.assertPaths(actual, expected)
def test_find__without_directory(self):
"""
@@ -394,7 +404,7 @@ def test_find__without_directory(self):
actual = loader._find(view)
expected = os.path.join(DATA_DIR, 'sample_view.mustache')
- self._assert_paths(actual, expected)
+ self.assertPaths(actual, expected)
def _assert_get_template(self, custom, expected):
loader = self._make_loader()

0 comments on commit 950e511

Please sign in to comment.