Exemplo n.º 1
0
    def test_read(self):
        """
        Test read().

        """
        loader = Loader()
        path = self._get_path('ascii.mustache')
        actual = loader.read(path)
        self.assertString(actual, u'ascii: abc')
Exemplo n.º 2
0
    def test_unicode__basic__input_unicode(self):
        """
        Test unicode(): default arguments with unicode input.

        """
        loader = Loader()
        actual = loader.unicode(u"foo")

        self.assertString(actual, u"foo")
Exemplo n.º 3
0
    def test_read__encoding__argument(self):
        """
        Test read(): encoding argument respected.

        """
        loader = Loader()
        path = self._get_path('non_ascii.mustache')

        self.assertRaises(UnicodeDecodeError, loader.read, path)

        actual = loader.read(path, encoding='utf-8')
        self.assertString(actual, u'non-ascii: é')
Exemplo n.º 4
0
    def test_read__file_encoding__attribute(self):
        """
        Test read(): file_encoding attribute respected.

        """
        loader = Loader()
        path = self._get_path('non_ascii.mustache')

        self.assertRaises(UnicodeDecodeError, loader.read, path)

        loader.file_encoding = 'utf-8'
        actual = loader.read(path)
        self.assertString(actual, u'non-ascii: é')
Exemplo n.º 5
0
    def test_unicode__encoding_argument(self):
        """
        Test unicode(): encoding argument.

        """
        loader = Loader()

        non_ascii = u'abcdé'.encode('utf-8')

        self.assertRaises(UnicodeDecodeError, loader.unicode, non_ascii)

        actual = loader.unicode(non_ascii, encoding='utf-8')
        self.assertString(actual, u'abcdé')
Exemplo n.º 6
0
    def test_unicode__basic__input_unicode_subclass(self):
        """
        Test unicode(): default arguments with unicode-subclass input.

        """
        class UnicodeSubclass(unicode):
            pass

        s = UnicodeSubclass(u"foo")

        loader = Loader()
        actual = loader.unicode(s)

        self.assertString(actual, u"foo")
Exemplo n.º 7
0
    def test_unicode__to_unicode__attribute(self):
        """
        Test unicode(): encoding attribute.

        """
        loader = Loader()

        non_ascii = u'abcdé'.encode('utf-8')
        self.assertRaises(UnicodeDecodeError, loader.unicode, non_ascii)

        def to_unicode(s, encoding=None):
            if encoding is None:
                encoding = 'utf-8'
            return unicode(s, encoding)

        loader.to_unicode = to_unicode
        self.assertString(loader.unicode(non_ascii), u"abcdé")
Exemplo n.º 8
0
 def test_init__file_encoding__default(self):
     file_encoding = defaults.FILE_ENCODING
     try:
         defaults.FILE_ENCODING = 'foo'
         loader = Loader()
         self.assertEqual(loader.file_encoding, 'foo')
     finally:
         defaults.FILE_ENCODING = file_encoding
Exemplo n.º 9
0
    def test_loader__to_unicode__attribute(self):
        """
        Test read(): to_unicode attribute respected.

        """
        loader = Loader()
        path = self._get_path('non_ascii.mustache')

        self.assertRaises(UnicodeDecodeError, loader.read, path)
Exemplo n.º 10
0
    def test_init__to_unicode__default(self):
        loader = Loader()
        self.assertRaises(TypeError, loader.to_unicode, u"abc")

        decode_errors = defaults.DECODE_ERRORS
        string_encoding = defaults.STRING_ENCODING

        nonascii = u'abcdé'.encode('utf-8')

        loader = Loader()
        self.assertRaises(UnicodeDecodeError, loader.to_unicode, nonascii)

        defaults.DECODE_ERRORS = 'ignore'
        loader = Loader()
        self.assertString(loader.to_unicode(nonascii), u'abcd')

        defaults.STRING_ENCODING = 'utf-8'
        loader = Loader()
        self.assertString(loader.to_unicode(nonascii), u'abcdé')
Exemplo n.º 11
0
def _make_specloader():
    """
    Return a default SpecLoader instance for testing purposes.

    """

    # Python 2 and 3 have different default encodings.  Thus, to have
    # consistent test results across both versions, we need to specify
    # the string and file encodings explicitly rather than relying on
    # the defaults.
    def to_unicode(s, encoding=None):
        """
        Raises a TypeError exception if the given string is already unicode.

        """
        if encoding is None:
            encoding = 'ascii'
        return unicode(s, encoding, 'strict')

    loader = Loader(file_encoding='ascii', to_unicode=to_unicode)
    return SpecLoader(loader=loader)
Exemplo n.º 12
0
    def __init__(self, loader=None):
        if loader is None:
            loader = Loader()

        self.loader = loader
Exemplo n.º 13
0
 def test_init__to_unicode(self):
     to_unicode = lambda x: x
     loader = Loader(to_unicode=to_unicode)
     self.assertEqual(loader.to_unicode, to_unicode)
Exemplo n.º 14
0
    def test_init__loader(self):
        loader = Loader()
        custom = SpecLoader(loader=loader)

        self.assertIs(custom.loader, loader)
Exemplo n.º 15
0
 def test_init__file_encoding(self):
     loader = Loader(file_encoding='bar')
     self.assertEqual(loader.file_encoding, 'bar')
Exemplo n.º 16
0
 def test_init__extension__default(self):
     # Test the default value.
     loader = Loader()
     self.assertEqual(loader.extension, 'mustache')
Exemplo n.º 17
0
 def test_init__extension(self):
     loader = Loader(extension='foo')
     self.assertEqual(loader.extension, 'foo')