Blame tests/test_pagure_lib_encoding_utils.py
|
Jeremy Cline |
e62ce0 |
# -*- coding: utf-8 -*-
|
|
Jeremy Cline |
e62ce0 |
"""
|
|
Jeremy Cline |
e62ce0 |
Tests for :module:`pagure.lib.encoding_utils`.
|
|
Jeremy Cline |
e62ce0 |
"""
|
|
Jeremy Cline |
e62ce0 |
|
|
Jeremy Cline |
e62ce0 |
import chardet
|
|
Jeremy Cline |
e62ce0 |
import unittest
|
|
Jeremy Cline |
e62ce0 |
|
|
Jeremy Cline |
e62ce0 |
from pagure.lib import encoding_utils
|
|
Jeremy Cline |
e62ce0 |
|
|
Jeremy Cline |
e62ce0 |
|
|
Jeremy Cline |
e62ce0 |
class TestGuessEncoding(unittest.TestCase):
|
|
Jeremy Cline |
e62ce0 |
|
|
Jeremy Cline |
e62ce0 |
def test_guess_encoding_ascii(self):
|
|
Jeremy Cline |
e62ce0 |
"""
|
|
Jeremy Cline |
e62ce0 |
Assert when ascii-only data is provided ascii is the guessed encoding.
|
|
Jeremy Cline |
e62ce0 |
"""
|
|
Jeremy Cline |
e62ce0 |
data = u'Twas bryllyg, and the slythy toves did gyre and gymble'
|
|
Jeremy Cline |
e62ce0 |
result = encoding_utils.guess_encoding(data.encode('ascii'))
|
|
Jeremy Cline |
e62ce0 |
self.assertEqual(result, 'ascii')
|
|
Jeremy Cline |
e62ce0 |
|
|
Jeremy Cline |
e62ce0 |
def test_guess_encoding_favor_utf_8(self):
|
|
Jeremy Cline |
e62ce0 |
"""
|
|
Jeremy Cline |
e62ce0 |
Test that strings that could be UTF-8 or ISO-8859-2 result in UTF-8.
|
|
Jeremy Cline |
e62ce0 |
"""
|
|
Jeremy Cline |
e62ce0 |
data = u'Ĺ abata'.encode('utf-8')
|
|
Jeremy Cline |
e62ce0 |
result = encoding_utils.guess_encoding(data)
|
|
Jeremy Cline |
e62ce0 |
chardet_result = chardet.detect(data)
|
|
Jeremy Cline |
e62ce0 |
self.assertEqual(result, 'utf-8')
|
|
Jeremy Cline |
e62ce0 |
self.assertEqual(chardet_result['encoding'], 'ISO-8859-2')
|
|
Jeremy Cline |
e62ce0 |
|
|
Jeremy Cline |
4bcf65 |
def test_guess_encoding_no_data(self):
|
|
Jeremy Cline |
4bcf65 |
result = encoding_utils.guess_encoding(u''.encode('utf-8'))
|
|
Jeremy Cline |
4bcf65 |
self.assertEqual(result, 'ascii')
|
|
Jeremy Cline |
4bcf65 |
|
|
Jeremy Cline |
4bcf65 |
|
|
Jeremy Cline |
4bcf65 |
class TestDecode(unittest.TestCase):
|
|
Jeremy Cline |
4bcf65 |
|
|
Jeremy Cline |
e62ce0 |
def test_decode(self):
|
|
Jeremy Cline |
e62ce0 |
data = u'Ĺ abata'
|
|
Jeremy Cline |
e62ce0 |
self.assertEqual(data, encoding_utils.decode(data.encode('utf-8')))
|
|
Jeremy Cline |
e62ce0 |
|
|
Jeremy Cline |
e62ce0 |
|
|
Jeremy Cline |
e62ce0 |
if __name__ == '__main__':
|
|
Jeremy Cline |
e62ce0 |
unittest.main()
|