def test_html_minimizer(self): self.assertEquals(html_minimizer.minimize('<html />'), '<html/>') self.assertEquals(html_minimizer.minimize(' <html /> '), '<html/>') self.assertEquals(html_minimizer.minimize(' \n <html /> \n '), '<html/>') self.assertEquals(html_minimizer.minimize('<html></html>'), '<html/>') self.assertEquals(html_minimizer.minimize('<html>x</html>'), '<html>x</html>') self.assertEquals(html_minimizer.minimize('<html> x </html>'), '<html> x </html>') self.assertEquals(html_minimizer.minimize('<html> x </html>'), '<html> x </html>') self.assertEquals(html_minimizer.minimize('<html> \n x \n </html>'), '<html> x </html>') self.assertEquals(html_minimizer.minimize('<html> \n </html>'), '<html>\n</html>') self.assertEquals(html_minimizer.minimize('<html> \n\n \n \n \n </html>'), '<html>\n</html>')
def compare_with_genshi(self, output, basename, arguments, template_parameters=None, translator=None): """ Compares the output of a single compiled template with the output generated by Genshi itself output: The output of the compiled template basename: Name of the template file without extension, it is also the module name arguments: The arguments of the template like in a Python function definition template_parameters: Keyword parameters to pass to the template """ # Render the same template using Genshi try: import genshi except ImportError: print 'Genshi is not installed, testing against Genshi skipped.' return if template_parameters is None: template_parameters = {} import genshi.template template_filename = '%s.html' % basename template_pathname = os.path.join(DATA_DIR, template_filename) with open(template_pathname, 'rt') as template_file: source = template_file.read() assert source.decode('utf8') genshi_template = genshi.template.MarkupTemplate( source, filepath=template_pathname, filename=template_filename) if translator: from genshi import filters translator_filter = filters.Translator(translator) translator_filter.setup(genshi_template) kws = eval('dict(%s)' % arguments) kws.update(template_parameters) token_stream = genshi_template.generate(**kws) genshi_output = token_stream.render(method='xml', encoding=None) assert isinstance(genshi_output, unicode) if constants.DEBUGGING: with open('data/output/%s.output.html' % basename, 'wt') as output_file: output_file.write(output.encode('utf-8')) with open('data/output/%s.genshi.output.html' % basename, 'wt') as output_file: output_file.write(genshi_output.encode('utf-8')) # Normalize output to make them comparable minimized_output = util.remove_duplicate_whitespace(html_minimizer.minimize(output)) minimized_genshi_output = util.remove_duplicate_whitespace(html_minimizer.minimize(genshi_output)) # Removing all the whitespace between and around elements minimized_output = minimized_output.replace('>\n', '>', ).replace('> ', '>').replace('\n<', '<', ).replace(' <', '<') minimized_genshi_output = minimized_genshi_output.replace('>\n', '>', ).replace('> ', '>').replace('\n<', '<', ).replace(' <', '<') # Add back newlines after each tag to allow printing unified difference minimized_output = minimized_output.replace('>', '>\n', ) minimized_genshi_output = minimized_genshi_output.replace('>', '>\n', ) if constants.DEBUGGING: with open('data/output/%s.minimized.output.html' % basename, 'wt') as output_file: output_file.write(minimized_output.encode('utf-8')) with open('data/output/%s.minimized.genshi.output.html' % basename, 'wt') as output_file: output_file.write(minimized_genshi_output.encode('utf-8')) # Compare the results if minimized_output != minimized_genshi_output: util.print_diff(minimized_genshi_output, minimized_output, 'Genshi output', 'Output of the compiled template') self.assertEquals(minimized_genshi_output, minimized_output)