Commit 70b597e8 by Chris Jerdonek

Changed CustomizedTemplate attributes and added custom_template.Loader tests.

Added CustomizedTemplate.template_rel_path and renamed CustomizedTemplate.template_directory
to CustomizedTemplate.template_rel_directory.  Added unit tests to test
the custom_template.Loader constructor.
parent 08b5173c
...@@ -29,10 +29,10 @@ class CustomizedTemplate(object): ...@@ -29,10 +29,10 @@ class CustomizedTemplate(object):
template: the template to use, as a unicode string. template: the template to use, as a unicode string.
template_path: the path to the template file, relative to the template_rel_path: the path to the template file, relative to the
directory containing the module defining the class. directory containing the module defining the class.
template_dir: the directory containing the template file, relative template_rel_directory: the directory containing the template file, relative
to the directory containing the module defining the class. to the directory containing the module defining the class.
template_extension: the template file extension. Defaults to "mustache". template_extension: the template file extension. Defaults to "mustache".
...@@ -41,12 +41,12 @@ class CustomizedTemplate(object): ...@@ -41,12 +41,12 @@ class CustomizedTemplate(object):
""" """
template = None template = None
# TODO: remove template_path.
template_path = None template_path = None
template_rel_path = None
template_directory = None template_rel_directory = None
template_name = None template_name = None
template_extension = None template_extension = None
template_encoding = None template_encoding = None
...@@ -126,7 +126,6 @@ class Loader(object): ...@@ -126,7 +126,6 @@ class Loader(object):
""" """
# TODO: unit test this.
def __init__(self, search_dirs, template_locator=None, reader=None): def __init__(self, search_dirs, template_locator=None, reader=None):
if reader is None: if reader is None:
reader = Reader() reader = Reader()
...@@ -143,10 +142,10 @@ class Loader(object): ...@@ -143,10 +142,10 @@ class Loader(object):
Return the relative template path as a (dir, file_name) pair. Return the relative template path as a (dir, file_name) pair.
""" """
if view.template_path is not None: if view.template_rel_path is not None:
return os.path.split(view.template_path) return os.path.split(view.template_rel_path)
template_dir = view.template_directory template_dir = view.template_rel_directory
# Otherwise, we don't know the directory. # Otherwise, we don't know the directory.
......
...@@ -6,6 +6,7 @@ Unit tests of view.py. ...@@ -6,6 +6,7 @@ Unit tests of view.py.
""" """
import os.path import os.path
import sys
import unittest import unittest
from examples.simple import Simple from examples.simple import Simple
...@@ -14,8 +15,9 @@ from examples.lambdas import Lambdas ...@@ -14,8 +15,9 @@ from examples.lambdas import Lambdas
from examples.inverted import Inverted, InvertedLists from examples.inverted import Inverted, InvertedLists
from pystache import Renderer from pystache import Renderer
from pystache import View from pystache import View
from pystache.reader import Reader
from pystache.custom_template import Loader from pystache.custom_template import Loader
from pystache.locator import Locator
from pystache.reader import Reader
from .common import AssertIsMixin from .common import AssertIsMixin
from .common import DATA_DIR from .common import DATA_DIR
from .data.views import SampleView from .data.views import SampleView
...@@ -56,7 +58,7 @@ class ViewTestCase(unittest.TestCase): ...@@ -56,7 +58,7 @@ class ViewTestCase(unittest.TestCase):
def test_template_path_for_partials(self): def test_template_path_for_partials(self):
""" """
Test that View.template_path is respected for partials. Test that View.template_rel_path is respected for partials.
""" """
class TestView(View): class TestView(View):
...@@ -139,18 +141,41 @@ class LoaderTests(unittest.TestCase, AssertIsMixin): ...@@ -139,18 +141,41 @@ class LoaderTests(unittest.TestCase, AssertIsMixin):
locator = Loader(search_dirs=[DATA_DIR]) locator = Loader(search_dirs=[DATA_DIR])
return locator return locator
# TODO: fully test constructor.
def test_init__reader(self):
reader = "reader" # in practice, this is a reader instance.
locator = Loader(search_dirs=None, template_locator=None, reader=reader)
self.assertIs(locator.reader, reader)
def _assert_template_location(self, view, expected): def _assert_template_location(self, view, expected):
locator = self._make_locator() locator = self._make_locator()
actual = locator.get_relative_template_location(view) actual = locator.get_relative_template_location(view)
self.assertEquals(actual, expected) self.assertEquals(actual, expected)
def test_init__defaults(self):
loader = Loader([])
# Check the reader attribute.
reader = loader.reader
self.assertEquals(reader.decode_errors, 'strict')
self.assertEquals(reader.encoding, sys.getdefaultencoding())
# Check the template_locator attribute.
locator = loader.template_locator
self.assertEquals(locator.template_extension, 'mustache')
def test_init__search_dirs(self):
search_dirs = ['a', 'b']
loader = Loader(search_dirs)
self.assertEquals(loader.search_dirs, ['a', 'b'])
def test_init__reader(self):
reader = Reader()
loader = Loader([], reader=reader)
self.assertIs(loader.reader, reader)
def test_init__locator(self):
locator = Locator()
loader = Loader([], template_locator=locator)
self.assertIs(loader.template_locator, locator)
def test_get_relative_template_location(self): def test_get_relative_template_location(self):
""" """
Test get_relative_template_location(): default behavior (no attributes set). Test get_relative_template_location(): default behavior (no attributes set).
...@@ -159,31 +184,31 @@ class LoaderTests(unittest.TestCase, AssertIsMixin): ...@@ -159,31 +184,31 @@ class LoaderTests(unittest.TestCase, AssertIsMixin):
view = SampleView() view = SampleView()
self._assert_template_location(view, (None, 'sample_view.mustache')) self._assert_template_location(view, (None, 'sample_view.mustache'))
def test_get_relative_template_location__template_path__file_name_only(self): def test_get_relative_template_location__template_rel_path__file_name_only(self):
""" """
Test get_relative_template_location(): template_path attribute. Test get_relative_template_location(): template_rel_path attribute.
""" """
view = SampleView() view = SampleView()
view.template_path = 'template.txt' view.template_rel_path = 'template.txt'
self._assert_template_location(view, ('', 'template.txt')) self._assert_template_location(view, ('', 'template.txt'))
def test_get_relative_template_location__template_path__file_name_with_directory(self): def test_get_relative_template_location__template_rel_path__file_name_with_directory(self):
""" """
Test get_relative_template_location(): template_path attribute. Test get_relative_template_location(): template_rel_path attribute.
""" """
view = SampleView() view = SampleView()
view.template_path = 'foo/bar/template.txt' view.template_rel_path = 'foo/bar/template.txt'
self._assert_template_location(view, ('foo/bar', 'template.txt')) self._assert_template_location(view, ('foo/bar', 'template.txt'))
def test_get_relative_template_location__template_directory(self): def test_get_relative_template_location__template_rel_directory(self):
""" """
Test get_relative_template_location(): template_directory attribute. Test get_relative_template_location(): template_rel_directory attribute.
""" """
view = SampleView() view = SampleView()
view.template_directory = 'foo' view.template_rel_directory = 'foo'
self._assert_template_location(view, ('foo', 'sample_view.mustache')) self._assert_template_location(view, ('foo', 'sample_view.mustache'))
...@@ -213,7 +238,7 @@ class LoaderTests(unittest.TestCase, AssertIsMixin): ...@@ -213,7 +238,7 @@ class LoaderTests(unittest.TestCase, AssertIsMixin):
locator = self._make_locator() locator = self._make_locator()
view = SampleView() view = SampleView()
view.template_path = 'foo/bar.txt' view.template_rel_path = 'foo/bar.txt'
self.assertTrue(locator.get_relative_template_location(view)[0] is not None) self.assertTrue(locator.get_relative_template_location(view)[0] is not None)
actual = locator.get_template_path(view) actual = locator.get_template_path(view)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment