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 = """\ """ 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', ''), (SOURCE_HUNK, 'p { border:5px solid green;}', None, ''), (SOURCE_FILE, os.path.join(settings.COMPRESS_ROOT, 'css', 'two.css'), 'css/two.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', ''), (SOURCE_HUNK, 'obj.value = "value";', None, ''), ] 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'