Commit 8782cc27 by Chris Jerdonek

The view.Locator class now supports view.template_encoding.

parent af2b04dc
......@@ -9,6 +9,7 @@ import os.path
from .context import Context
from .locator import Locator as TemplateLocator
from .reader import Reader
from .renderer import Renderer
......@@ -137,7 +138,14 @@ class Locator(object):
"""
def __init__(self, reader, search_dirs, template_locator):
# TODO: unit test this.
def __init__(self, search_dirs, template_locator=None, reader=None):
if reader is None:
reader = Reader()
if template_locator is None:
template_locator = TemplateLocator()
self.reader = reader
self.search_dirs = search_dirs
self.template_locator = template_locator
......@@ -180,10 +188,8 @@ class Locator(object):
"""
if view.template is not None:
# TODO: unit test rendering with a non-unicode value for this attribute.
return view.template
return self.reader.unicode(view.template, view.template_encoding)
path = self.get_template_path(view)
# TODO: add support for encoding.
return self.reader.read(path)
return self.reader.read(path, view.template_encoding)
Sample view...
\ No newline at end of file
ascii: abc
\ No newline at end of file
......@@ -10,3 +10,7 @@ class SayHello(object):
class SampleView(View):
pass
class NonAscii(View):
pass
......@@ -100,7 +100,7 @@ class ReaderTestCase(unittest.TestCase):
"""
reader = Reader()
path = self._get_path('nonascii.mustache')
path = self._get_path('non_ascii.mustache')
self.assertRaises(UnicodeDecodeError, reader.read, path)
reader.encoding = 'utf-8'
......@@ -112,7 +112,7 @@ class ReaderTestCase(unittest.TestCase):
"""
reader = Reader()
path = self._get_path('nonascii.mustache')
path = self._get_path('non_ascii.mustache')
self.assertRaises(UnicodeDecodeError, reader.read, path)
self.assertEquals(reader.read(path, encoding='utf-8'), u'non-ascii: é')
......@@ -123,7 +123,7 @@ class ReaderTestCase(unittest.TestCase):
"""
reader = Reader()
path = self._get_path('nonascii.mustache')
path = self._get_path('non_ascii.mustache')
self.assertRaises(UnicodeDecodeError, reader.read, path)
reader.decode_errors = 'replace'
......
......@@ -200,7 +200,7 @@ class RendererTestCase(unittest.TestCase):
self.assertEquals(type(actual), unicode)
def test_read__file_encoding(self):
filename = 'nonascii.mustache'
filename = 'non_ascii.mustache'
renderer = Renderer()
renderer.file_encoding = 'ascii'
......@@ -211,7 +211,7 @@ class RendererTestCase(unittest.TestCase):
self.assertEquals(actual, u'non-ascii: é')
def test_read__decode_errors(self):
filename = 'nonascii.mustache'
filename = 'non_ascii.mustache'
renderer = Renderer()
self.assertRaises(UnicodeDecodeError, self._read, renderer, filename)
......
# coding: utf-8
"""
Unit tests of view.py.
"""
import os.path
import unittest
......@@ -5,12 +12,13 @@ from examples.simple import Simple
from examples.complex_view import ComplexView
from examples.lambdas import Lambdas
from examples.inverted import Inverted, InvertedLists
from pystache.locator import Locator as TemplateLocator
from pystache.reader import Reader
from pystache.view import View
from pystache.view import Locator as ViewLocator
from .common import AssertIsMixin
from .common import DATA_DIR
from .data.views import SampleView
from .data.views import NonAscii
class Thing(object):
......@@ -177,19 +185,13 @@ class ViewTestCase(unittest.TestCase):
class LocatorTests(unittest.TestCase, AssertIsMixin):
def _make_locator(self):
class MockReader(object):
def read(self, path):
return "read: %s" % repr(path)
reader = MockReader()
template_locator = TemplateLocator()
locator = ViewLocator(reader=reader, search_dirs=[DATA_DIR], template_locator=template_locator)
locator = ViewLocator(search_dirs=[DATA_DIR])
return locator
# TODO: fully test constructor.
def test_init__reader(self):
reader = "reader" # in practice, this is a reader instance.
locator = ViewLocator(reader, search_dirs=None, template_locator=None)
locator = ViewLocator(search_dirs=None, template_locator=None, reader=reader)
self.assertIs(locator.reader, reader)
......@@ -273,26 +275,53 @@ class LocatorTests(unittest.TestCase, AssertIsMixin):
self.assertEquals(actual, expected)
def test_get_template__template_attribute_set(self):
def _assert_get_template(self, view, expected):
locator = self._make_locator()
actual = locator.get_template(view)
self.assertEquals(type(actual), unicode)
self.assertEquals(actual, expected)
def test_get_template(self):
"""
Test get_template() with view.template set to a non-None value.
Test get_template(): default behavior (no attributes set).
"""
locator = self._make_locator()
view = View()
view = SampleView()
self._assert_get_template(view, u"ascii: abc")
def test_get_template__template(self):
"""
Test get_template(): template attribute.
"""
view = SampleView()
view.template = 'foo'
self.assertEquals(locator.get_template(view), 'foo')
self._assert_get_template(view, 'foo')
def test_get_template__template_attribute_not_set(self):
def test_get_template__template__template_encoding(self):
"""
Test get_template() with view.template set to None.
Test get_template(): template attribute with template encoding attribute.
"""
locator = self._make_locator()
locator.get_template_path = lambda view: "path"
view = SampleView()
view.template = u'é'.encode('utf-8')
self.assertRaises(UnicodeDecodeError, self._assert_get_template, view, 'foo')
view.template_encoding = 'utf-8'
self._assert_get_template(view, u'é')
def test_get_template__template_encoding(self):
"""
Test get_template(): template_encoding attribute.
"""
view = NonAscii()
view = View()
view.template = None
self.assertRaises(UnicodeDecodeError, self._assert_get_template, view, 'foo')
self.assertEquals(locator.get_template(view), "read: 'path'")
view.template_encoding = 'utf-8'
self._assert_get_template(view, u"non-ascii: é")
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