Back to index

moin  1.9.0~rc2
test_unicode.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     MoinMoin - Test if MoinMoin.parser.* do write UNICODE objects
00004 
00005     Comment:
00006     The test produces an exception in another place, if some routine encodes unicode to UTF8 too early.
00007     The corresponding bug was found initially in MoinMoin.parser.text_xslt and the test was designed for
00008     this problem, but the test actually examines all available parsers.
00009 
00010     @copyright: 2007,2008 by Raphael Bossek <raphael.bossek@gmail.com>
00011     @license: GNU GPL, see COPYING for details.
00012 """
00013 
00014 import py
00015 import sys, traceback
00016 
00017 import MoinMoin.parser
00018 from MoinMoin.Page import Page
00019 
00020 class TestParserOutput(object):
00021     """ Parser has to generate unicode output. """
00022     def test_ParserOutput(self):
00023         """ This method aims generally at MoinMoin.parser.text_xslt -
00024             this parser should encode Unicode input to UTF8 as late as possible.
00025         """
00026         request = self.request
00027         assert not request.cfg.allow_xslt, u'allow_xslt should be disabled'
00028         errmsg = []
00029 
00030         # Some examples to verify with additional stuff
00031         parser_raw_input = {
00032             u'text_html': u'<html><body><h1>%s</h1></body></html>',
00033             u'text_irssi': u"[12:01] <RaphaelBosek> %s",
00034             u'text_moin_wiki': u'||<#fefefe> %s ||',
00035             u'text_python': u'if True: print "%s"',
00036             u'text_xslt': u'<?xml version="1.0" encoding="ISO-8859-1"?><!-- %s -->',
00037         }
00038 
00039         # Blacklist for parsers that don't work - this list should be empty !
00040         parser_blacklist = []
00041 
00042         # Create a page if it doesn't exist already.
00043         if not u'page' in request.formatter.__dict__ or not request.formatter.page:
00044             request.formatter.page = Page(request, u'test_parser_unicode_page')
00045             # this temporarily fixes an error with page-names, should be fixed at a central place some time
00046             request.page = Page(request, u'test_parser_unicode_page')
00047 
00048         # Check all parsers for UNICODE output.
00049         for parsername in MoinMoin.parser.modules:
00050             if parsername in parser_blacklist:
00051                 continue
00052 
00053             module = __import__(u'MoinMoin.parser', globals(), {}, [parsername])
00054             parsermodule = getattr(module, parsername)
00055             if u'Parser' in parsermodule.__dict__:
00056                 # Get the parser_input or use a simple fallback if the parser is not found in parser_raw_input
00057                 i = parser_raw_input.get(parsername, u'%s') % u'\xC3\x84\xC3\x96\xC3\x9C\xC3\xE2\x82\xAC\x27'
00058                 p = parsermodule.Parser(i, request)
00059 
00060                 # This is the actual request that would produce an exception, which would usually look like the following:
00061                 # >  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
00062                 # usually occurring in python/lib/StringIO.py:270
00063                 r = request.redirectedOutput(p.format, request.formatter)
00064 
00065                 # This assertion will only be triggered, if the parser does not write unicode at all
00066                 assert isinstance(r, unicode), u'MoinMoin.parser.%s does not write UNICODE data but %s' % (parsername, type(r), )
00067 
00068 coverage_modules = ['MoinMoin.parser']
00069