Commit 8782cc27 by Chris Jerdonek

The view.Locator class now supports view.template_encoding.

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