Commit 7a4dbe22 by Chris Jerdonek

Added new Reader class and tests.

parent b2f59598
# coding: utf-8
"""
This module provides a Reader class to read a template given a path.
"""
from __future__ import with_statement
import os
import sys
DEFAULT_DECODE_ERRORS = 'strict'
class Reader(object):
def __init__(self, encoding=None, decode_errors=None):
"""
Construct a template reader.
Arguments:
encoding: the file encoding. This is the name of the encoding to
use when converting file contents to unicode. This name is
passed as the encoding argument to Python's built-in function
unicode(). Defaults to the encoding name returned by
sys.getdefaultencoding().
decode_errors: the string to pass as the errors argument to the
built-in function unicode() when converting file contents to
unicode. Defaults to "strict".
"""
if decode_errors is None:
decode_errors = DEFAULT_DECODE_ERRORS
if encoding is None:
encoding = sys.getdefaultencoding()
self.decode_errors = decode_errors
self.encoding = encoding
def read(self, path):
"""
Read the template at the given path, and return it as a unicode string.
"""
with open(path, 'r') as f:
text = f.read()
text = unicode(text, self.encoding, self.decode_errors)
return text
# encoding: utf-8
"""
Unit tests of reader.py.
"""
import os
import sys
import unittest
from pystache.reader import Reader
DATA_DIR = 'tests/data'
class ReaderTestCase(unittest.TestCase):
def _get_path(self, filename):
return os.path.join(DATA_DIR, filename)
def test_init__decode_errors(self):
# Test the default value.
reader = Reader()
self.assertEquals(reader.decode_errors, 'strict')
reader = Reader(decode_errors='replace')
self.assertEquals(reader.decode_errors, 'replace')
def test_init__encoding(self):
# Test the default value.
reader = Reader()
self.assertEquals(reader.encoding, sys.getdefaultencoding())
reader = Reader(encoding='foo')
self.assertEquals(reader.encoding, 'foo')
def test_read(self):
"""
Test read().
"""
reader = Reader()
path = self._get_path('ascii.mustache')
self.assertEquals(reader.read(path), 'ascii: abc')
def test_read__returns_unicode(self):
"""
Test that read() returns unicode strings.
"""
reader = Reader()
path = self._get_path('ascii.mustache')
contents = reader.read(path)
self.assertEqual(type(contents), unicode)
def test_read__encoding(self):
"""
Test read(): encoding attribute respected.
"""
reader = Reader()
path = self._get_path('nonascii.mustache')
self.assertRaises(UnicodeDecodeError, reader.read, path)
reader.encoding = 'utf-8'
self.assertEquals(reader.read(path), u'non-ascii: é')
def test_get__decode_errors(self):
"""
Test get(): decode_errors attribute.
"""
reader = Reader()
path = self._get_path('nonascii.mustache')
self.assertRaises(UnicodeDecodeError, reader.read, path)
reader.decode_errors = 'replace'
self.assertEquals(reader.read(path), u'non-ascii: \ufffd\ufffd')
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