view MoinMoin/util/_tests/test_registry.py @ 2657:080673c3e60a

Now nameless items can also be downloaded and removed unnecessary blank spaces in items/content.py
author Saurabh Kathpalia <saurabh.kathpalia95@gmail.com>
date Sun, 06 Jul 2014 09:25:31 +0530
parents 181a556a0920
children
line wrap: on
line source
# Copyright: 2008,2009 MoinMoin:BastianBlank
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
MoinMoin - Tests for MoinMoin.util.registry
"""


import pytest

from MoinMoin.util.registry import *


def factory_all(arg):
    return 1


def factory_all2(arg):
    return 3


def factory_none(arg):
    pass


def factory_special(arg):
    if arg == 'a':
        return 2


def test_Registry_get():
    r = Registry()

    r.register(factory_none)
    r.register(factory_special)
    assert r.get('a') == 2

    r.register(factory_all)
    assert r.get(None) == 1
    assert r.get('a') == 2

    r.register(factory_all2, r.PRIORITY_FIRST)
    assert r.get(None) == 3
    assert r.get('a') == 3


def test_Registry_lifecycle():
    r = Registry()

    assert len(r._entries) == 0

    r.register(factory_all)
    assert len(r._entries) == 1

    r.register(factory_none)
    assert len(r._entries) == 2

    r.register(factory_none)
    assert len(r._entries) == 2
    assert r._entries[0].factory is factory_all
    assert r._entries[1].factory is factory_none

    r.register(factory_none, r.PRIORITY_FIRST)
    assert len(r._entries) == 3
    assert r._entries[0].factory is factory_none
    assert r._entries[0].priority == r.PRIORITY_FIRST
    assert r._entries[1].factory is factory_all
    assert r._entries[2].factory is factory_none
    assert r._entries[2].priority == r.PRIORITY_MIDDLE

    r.unregister(factory_none)
    assert len(r._entries) == 1
    assert r._entries[0].factory is factory_all

    r.unregister(factory_all)
    assert len(r._entries) == 0

    pytest.raises(ValueError, r.unregister, factory_none)
    assert len(r._entries) == 0