| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
|
| 4 |
|
| 5 |
__license__ = 'GPL v3' |
| 6 |
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>' |
| 7 |
|
| 8 |
from tinycss.decoding import decode |
| 9 |
from tinycss.tests import BaseTest |
| 10 |
|
| 11 |
|
| 12 |
def params(css, encoding, use_bom=False, expect_error=False, **kwargs): |
| 13 |
"""Nicer syntax to make a tuple.""" |
| 14 |
return css, encoding, use_bom, expect_error, kwargs |
| 15 |
|
| 16 |
class TestDecoding(BaseTest): |
| 17 |
|
| 18 |
def test_decoding(self): |
| 19 |
for (css, encoding, use_bom, expect_error, kwargs) in [ |
| 20 |
params('', 'utf8'), # default to utf8 |
| 21 |
params('𐂃', 'utf8'), |
| 22 |
params('é', 'latin1'), # utf8 fails, fall back on ShiftJIS |
| 23 |
params('£', 'ShiftJIS', expect_error=True), |
| 24 |
params('£', 'ShiftJIS', protocol_encoding='Shift-JIS'), |
| 25 |
params('£', 'ShiftJIS', linking_encoding='Shift-JIS'), |
| 26 |
params('£', 'ShiftJIS', document_encoding='Shift-JIS'), |
| 27 |
params('£', 'ShiftJIS', protocol_encoding='utf8', |
| 28 |
document_encoding='ShiftJIS'), |
| 29 |
params('@charset "utf8"; £', 'ShiftJIS', expect_error=True), |
| 30 |
params('@charset "utf£8"; £', 'ShiftJIS', expect_error=True), |
| 31 |
params('@charset "unknown-encoding"; £', 'ShiftJIS', expect_error=True), |
| 32 |
params('@charset "utf8"; £', 'ShiftJIS', document_encoding='ShiftJIS'), |
| 33 |
params('£', 'ShiftJIS', linking_encoding='utf8', |
| 34 |
document_encoding='ShiftJIS'), |
| 35 |
params('@charset "utf-32"; 𐂃', 'utf-32-be'), |
| 36 |
params('@charset "Shift-JIS"; £', 'ShiftJIS'), |
| 37 |
params('@charset "ISO-8859-8"; £', 'ShiftJIS', expect_error=True), |
| 38 |
params('𐂃', 'utf-16-le', expect_error=True), # no BOM |
| 39 |
params('𐂃', 'utf-16-le', use_bom=True), |
| 40 |
params('𐂃', 'utf-32-be', expect_error=True), |
| 41 |
params('𐂃', 'utf-32-be', use_bom=True), |
| 42 |
params('𐂃', 'utf-32-be', document_encoding='utf-32-be'), |
| 43 |
params('𐂃', 'utf-32-be', linking_encoding='utf-32-be'), |
| 44 |
params('@charset "utf-32-le"; 𐂃', 'utf-32-be', |
| 45 |
use_bom=True, expect_error=True), |
| 46 |
# protocol_encoding takes precedence over @charset |
| 47 |
params('@charset "ISO-8859-8"; £', 'ShiftJIS', |
| 48 |
protocol_encoding='Shift-JIS'), |
| 49 |
params('@charset "unknown-encoding"; £', 'ShiftJIS', |
| 50 |
protocol_encoding='Shift-JIS'), |
| 51 |
params('@charset "Shift-JIS"; £', 'ShiftJIS', |
| 52 |
protocol_encoding='utf8'), |
| 53 |
# @charset takes precedence over document_encoding |
| 54 |
params('@charset "Shift-JIS"; £', 'ShiftJIS', |
| 55 |
document_encoding='ISO-8859-8'), |
| 56 |
# @charset takes precedence over linking_encoding |
| 57 |
params('@charset "Shift-JIS"; £', 'ShiftJIS', |
| 58 |
linking_encoding='ISO-8859-8'), |
| 59 |
# linking_encoding takes precedence over document_encoding |
| 60 |
params('£', 'ShiftJIS', |
| 61 |
linking_encoding='Shift-JIS', document_encoding='ISO-8859-8'), |
| 62 |
]: |
| 63 |
if use_bom: |
| 64 |
source = '\ufeff' + css |
| 65 |
else: |
| 66 |
source = css |
| 67 |
css_bytes = source.encode(encoding) |
| 68 |
result, result_encoding = decode(css_bytes, **kwargs) |
| 69 |
if expect_error: |
| 70 |
self.assertNotEqual(result, css) |
| 71 |
else: |
| 72 |
self.ae(result, css) |