view MoinMoin/util/_tests/ @ 4:497a65401dcb

fix moin2 repo url, add some other urls to the docs
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 21 Feb 2011 03:08:19 +0100
parents 5568cf133caf
children fe47d0977895
line wrap: on
line source
# -*- coding: iso-8859-1 -*-
    MoinMoin - MoinMoin.util.interwiki Tests

    @copyright: 2010 MoinMoin:ThomasWaldmann,
                2010 MoinMoin:MicheleOrru
    @license: GNU GPL, see COPYING for details.

from __future__ import with_statement
import py
import tempfile
import os.path
import shutil

from MoinMoin.util.interwiki import resolve_interwiki, split_interwiki, join_wiki, InterWikiMap
from MoinMoin._tests import wikiconfig

class TestInterWiki(object):
    class Config(wikiconfig.Config):
        interwiki_map = dict(MoinMoin='', )

    def testSplitWiki(self):
        tests = [('SomePage', ('Self', 'SomePage')),
                 ('OtherWiki:OtherPage', ('OtherWiki', 'OtherPage')),
                 (':OtherPage', ('', 'OtherPage')),
                 # broken ('/OtherPage', ('Self', '/OtherPage')),
                 # wrong interpretation ('MainPage/OtherPage', ('Self', 'MainPage/OtherPage')),
        for markup, (wikiname, pagename) in tests:
            assert split_interwiki(markup) == (wikiname, pagename)

    def testJoinWiki(self):
        tests = [(('', u'SomePage'), ''),
                 (('$PAGE&action=show', u'SomePage'), ''),
                 (('', u'Aktuelle\xc4nderungen'), ''),
                 (('$PAGE/show', u'Aktuelle\xc4nderungen'), ''),
        for (baseurl, pagename), url in tests:
            assert join_wiki(baseurl, pagename) == url

    def testResolveInterWiki(self):
        result = resolve_interwiki('MoinMoin', 'SomePage')
        assert result == ('MoinMoin', u'', 'SomePage', False)
        result = resolve_interwiki('Self', 'SomePage')
        assert result == ('Self', u'/', 'SomePage', False)

class TestInterWikiMapBackend(object):
    tests for interwiki map

    def test_load_file(self):
        Test that InterWikiMap.from_file correctly loads file objects.
        tmpdir = tempfile.mkdtemp()

        # test an invalid file
        with py.test.raises(IOError):
            InterWikiMap.from_file(os.path.join(tmpdir, 'void'))

        # test a consistent valid file
        testfile = os.path.join(tmpdir, 'foo.iwm')
        with open(testfile, 'w') as f:
            f.write('foo bar\n'
                    'baz spam\n'
                    'ham end end # this is really the end.')
        testiwm = InterWikiMap.from_file(testfile)
        assert testiwm.iwmap == dict(foo='bar', baz='spam', ham='end end')

        # test a malformed file
        testfile = os.path.join(tmpdir, 'bar.iwm')
        with open(testfile, 'w') as f:
            f.write('# This is a malformed interwiki file\n'
                    'fails # ever')
        with py.test.raises(ValueError):

        # finally destroy everything

    def test_load_string(self):
        Test that InterWikiMap.from_unicode correctly loads unicode objects.
        # test for void wiki maps
        assert InterWikiMap.from_string(u'').iwmap == dict()
        assert InterWikiMap.from_string(u'#spam\r\n').iwmap == dict()
        # test for comments
        s = ('# foo bar\n'
             '#       space     space\n'
             'foo bar\r\n'
             'ham spam # this is a valid description')
        assert InterWikiMap.from_string(s).iwmap == dict(foo='bar',
        # test for valid strings
        s = ('link1\r\n'
        assert (InterWikiMap.from_string(s).iwmap ==
        # test invalid strings
        with py.test.raises(ValueError):

    def test_real_interwiki_map(self):
        Test a 'real' interwiki file.
        abspath = __file__.rsplit('MoinMoin')[0]
        testfile = os.path.join(abspath, 'contrib', 'interwiki', 'intermap.txt')
        testiwm = InterWikiMap.from_file(testfile)

        assert 'MoinSrc' in testiwm.iwmap
        assert testiwm.iwmap['MoinMaster'] == ''
        assert 'PythonInfo' in testiwm.iwmap
        assert 'this' not in testiwm.iwmap
        assert testiwm.iwmap['MoinCVS'] == ';file='

coverage_modules = ['MoinMoin.util.interwiki']