def compare_with_expected_output(self, output, basename):
     """ Compiles a single test template to a module, import it, then
     executes the template with the test parameters given checking for
     the expected output
     
     output: The output of the compiled template
     basename: Name of the template file without extension, it is also the module name
     
     """
     # Verify the results
     expected_output_filepath = os.path.join(EXPECTED_OUTPUT_DIR, '%s.expected-output.html' % basename)
     with open(expected_output_filepath, 'rt') as expected_output_file:
         expected_output = expected_output_file.read().decode('utf8')
     if output != expected_output:
         util.print_diff(expected_output, output, 'Expected output', 'Output of the compiled template')
     self.assertEquals(expected_output, output)
    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)