Commit 10bba6f1 by Chris Jerdonek

Finished view.Locator.get_template() (though still need auxiliary methods).

parent 2976f997
...@@ -5,6 +5,8 @@ This module provides a View class. ...@@ -5,6 +5,8 @@ This module provides a View class.
""" """
import os.path
from .context import Context from .context import Context
from .locator import Locator from .locator import Locator
from .renderer import Renderer from .renderer import Renderer
...@@ -112,12 +114,37 @@ class Locator(object): ...@@ -112,12 +114,37 @@ class Locator(object):
""" """
def __init__(self): def __init__(self, reader):
pass self.reader = reader
# TODO: unit test
def get_relative_template_location(self, view):
"""
Return the relative template path as a (dir, file_name) pair.
"""
if view.template_path is not None:
return os.path.split(view.template_path)
# TODO: finish this
return None
# TODO: unit test
def get_template_path(self, view):
"""
Return the path to the view's associated template.
"""
if view.template_path is not None:
return os.path.split(view.template_path)
# TODO: finish this
return None
def get_template(self, view): def get_template(self, view):
if view.template is not None: if view.template is not None:
return view.template return view.template
# TODO: locate template path = self.get_template_path(view)
return None
return self.reader.read(path)
...@@ -27,3 +27,11 @@ def assert_strings(test_case, actual, expected): ...@@ -27,3 +27,11 @@ def assert_strings(test_case, actual, expected):
test_case.assertEquals(actual, expected, message) test_case.assertEquals(actual, expected, message)
class AssertIsMixin:
"""A mixin for adding assertIs() to a unittest.TestCase."""
# unittest.assertIs() is not available until Python 2.7:
# http://docs.python.org/library/unittest.html#unittest.TestCase.assertIsNone
def assertIs(self, first, second):
self.assertTrue(first is second, msg="%s is not %s" % (repr(first), repr(second)))
...@@ -11,7 +11,7 @@ import unittest ...@@ -11,7 +11,7 @@ import unittest
from pystache.context import _NOT_FOUND from pystache.context import _NOT_FOUND
from pystache.context import _get_value from pystache.context import _get_value
from pystache.context import Context from pystache.context import Context
from tests.common import AssertIsMixin
class SimpleObject(object): class SimpleObject(object):
...@@ -39,16 +39,6 @@ class DictLike(object): ...@@ -39,16 +39,6 @@ class DictLike(object):
return self._dict[key] return self._dict[key]
class AssertIsMixin:
"""A mixin for adding assertIs() to a unittest.TestCase."""
# unittest.assertIs() is not available until Python 2.7:
# http://docs.python.org/library/unittest.html#unittest.TestCase.assertIsNone
def assertIs(self, first, second):
self.assertTrue(first is second, msg="%s is not %s" % (repr(first), repr(second)))
class GetValueTests(unittest.TestCase, AssertIsMixin): class GetValueTests(unittest.TestCase, AssertIsMixin):
"""Test context._get_value().""" """Test context._get_value()."""
......
...@@ -7,6 +7,7 @@ from examples.lambdas import Lambdas ...@@ -7,6 +7,7 @@ from examples.lambdas import Lambdas
from examples.inverted import Inverted, InvertedLists from examples.inverted import Inverted, InvertedLists
from pystache.view import View from pystache.view import View
from pystache.view import Locator from pystache.view import Locator
from tests.common import AssertIsMixin
class Thing(object): class Thing(object):
...@@ -170,15 +171,59 @@ class ViewTestCase(unittest.TestCase): ...@@ -170,15 +171,59 @@ class ViewTestCase(unittest.TestCase):
self.assertEquals(view.render(), """one, two, three, empty list""") self.assertEquals(view.render(), """one, two, three, empty list""")
class LocatorTests(unittest.TestCase): class LocatorTests(unittest.TestCase, AssertIsMixin):
def _make_locator(self): def _make_locator(self):
locator = Locator() class MockReader(object):
def read(self, path):
return "read: %s" % repr(path)
reader = MockReader()
locator = Locator(reader=reader)
return locator return locator
def test_get_template(self): def test_init__reader(self):
reader = "reader" # in practice, this is a reader instance.
locator = Locator(reader)
self.assertIs(locator.reader, reader)
# TODO: make this test real
def test_get_relative_template_location__template_path__file_name(self):
locator = self._make_locator()
view = View()
view.template_path = 'foo.txt'
self.assertEquals(locator.get_relative_template_location(view), ('', 'foo.txt'))
# TODO: make this test real
def test_get_relative_template_location__template_path__full_path(self):
locator = self._make_locator()
view = View()
view.template_path = 'foo.txt'
self.assertEquals(locator.get_relative_template_location(view), ('', 'foo.txt'))
def test_get_template__template_attribute_set(self):
"""
Test get_template() with view.template set to a non-None value.
"""
locator = self._make_locator() locator = self._make_locator()
view = View() view = View()
view.template = 'foo' view.template = 'foo'
self.assertEquals(locator.get_template(view), 'foo') self.assertEquals(locator.get_template(view), 'foo')
def test_get_template__template_attribute_not_set(self):
"""
Test get_template() with view.template set to None.
"""
locator = self._make_locator()
locator.get_template_path = lambda view: "path"
view = View()
view.template = None
self.assertEquals(locator.get_template(view), "read: 'path'")
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