Files
deb-python-django-compressor/compressor/tests/test_parsers.py
2013-04-16 10:39:27 +02:00

87 lines
3.0 KiB
Python

from __future__ import with_statement
import os
try:
import lxml
except ImportError:
lxml = None
try:
import html5lib
except ImportError:
html5lib = None
try:
from BeautifulSoup import BeautifulSoup
except ImportError:
BeautifulSoup = None
from compressor.base import SOURCE_HUNK, SOURCE_FILE
from compressor.conf import settings
from compressor.css import CssCompressor
from compressor.tests.test_base import CompressorTestCase
try:
from django.utils import unittest as ut2
except ImportError:
import unittest2 as ut2
class ParserTestCase(object):
def setUp(self):
self.old_parser = settings.COMPRESS_PARSER
settings.COMPRESS_PARSER = self.parser_cls
super(ParserTestCase, self).setUp()
def tearDown(self):
settings.COMPRESS_PARSER = self.old_parser
@ut2.skipIf(lxml is None, 'lxml not found')
class LxmlParserTests(ParserTestCase, CompressorTestCase):
parser_cls = 'compressor.parser.LxmlParser'
@ut2.skipIf(html5lib is None, 'html5lib not found')
class Html5LibParserTests(ParserTestCase, CompressorTestCase):
parser_cls = 'compressor.parser.Html5LibParser'
def setUp(self):
super(Html5LibParserTests, self).setUp()
# special version of the css since the parser sucks
self.css = """\
<link href="/static/css/one.css" rel="stylesheet" type="text/css">
<style type="text/css">p { border:5px solid green;}</style>
<link href="/static/css/two.css" rel="stylesheet" type="text/css">"""
self.css_node = CssCompressor(self.css)
def test_css_split(self):
out = [
(SOURCE_FILE, os.path.join(settings.COMPRESS_ROOT, 'css', 'one.css'), 'css/one.css', '<link href="/static/css/one.css" rel="stylesheet" type="text/css">'),
(SOURCE_HUNK, 'p { border:5px solid green;}', None, '<style type="text/css">p { border:5px solid green;}</style>'),
(SOURCE_FILE, os.path.join(settings.COMPRESS_ROOT, 'css', 'two.css'), 'css/two.css', '<link href="/static/css/two.css" rel="stylesheet" type="text/css">'),
]
split = self.css_node.split_contents()
split = [(x[0], x[1], x[2], self.css_node.parser.elem_str(x[3])) for x in split]
self.assertEqual(out, split)
def test_js_split(self):
out = [
(SOURCE_FILE, os.path.join(settings.COMPRESS_ROOT, 'js', 'one.js'), 'js/one.js', '<script src="/static/js/one.js" type="text/javascript"></script>'),
(SOURCE_HUNK, 'obj.value = "value";', None, '<script type="text/javascript">obj.value = "value";</script>'),
]
split = self.js_node.split_contents()
split = [(x[0], x[1], x[2], self.js_node.parser.elem_str(x[3])) for x in split]
self.assertEqual(out, split)
@ut2.skipIf(BeautifulSoup is None, 'BeautifulSoup not found')
class BeautifulSoupParserTests(ParserTestCase, CompressorTestCase):
parser_cls = 'compressor.parser.BeautifulSoupParser'
class HtmlParserTests(ParserTestCase, CompressorTestCase):
parser_cls = 'compressor.parser.HtmlParser'