import importlib
import nose.tools

import pybtex.plugin
import pybtex.plugin.loader
import pybtex.style.formatting.plain


def test_plugin_loader():
    """Check that all enumerated plugins can be imported."""
    for group in pybtex.plugin.loader.PLUGIN_GROUPS:
        for name in pybtex.plugin.enumerate_plugin_names(group):
            pybtex.plugin.find_plugin(group, name)

def test_builtin_plugin_module_class_datas():
    for module_name, class_name, plugin_data in pybtex.plugin.loader.builtin_plugin_module_class_datas():
        plugin = getattr(importlib.import_module(module_name), class_name)
        nose.tools.assert_is(plugin, pybtex.plugin.find_plugin(
            plugin_data.plugin_group, plugin_data.plugin_name))
        nose.tools.assert_equal(plugin_data.plugin_name, plugin.name)
        nose.tools.assert_equal(plugin_data.suffixes, plugin.suffixes)
        nose.tools.assert_equal(plugin_data.aliases, plugin.aliases)

class TestPlugin1(pybtex.plugin.Plugin):
    name = 'yippikayee'

class TestPlugin2(pybtex.plugin.Plugin):
    name = 'plain'

class TestPlugin3(pybtex.plugin.Plugin):
    name = 'yippikayeetoo'
    aliases = ('plain',)

def test_register_plugin_1():
    pybtex.plugin.register_plugin('pybtex.style.formatting', TestPlugin1)
    nose.tools.assert_is(
        TestPlugin1, pybtex.plugin.find_plugin(
            'pybtex.style.formatting', 'yippikayee'))

def test_register_plugin_2():
    pybtex.plugin.register_plugin('pybtex.style.formatting', TestPlugin2)
    plugin = pybtex.plugin.find_plugin('pybtex.style.formatting', 'plain')
    nose.tools.assert_is_not(plugin, TestPlugin2)
    nose.tools.assert_is(plugin, pybtex.style.formatting.plain.Style)

def test_register_plugin_3():
    pybtex.plugin.register_plugin('pybtex.style.formatting', TestPlugin3)
    plugin = pybtex.plugin.find_plugin('pybtex.style.formatting', 'plain')
    nose.tools.assert_is_not(plugin, TestPlugin3)
    nose.tools.assert_is(plugin, pybtex.style.formatting.plain.Style)

def test_bad_plugin():
    nose.tools.assert_raises(
        pybtex.plugin.loader.PluginGroupNotFound,
        lambda: pybtex.plugin.find_plugin("pybtex.invalid.group", "__oops"))
    nose.tools.assert_raises(
        pybtex.plugin.loader.PluginNotFound,
        lambda: pybtex.plugin.find_plugin("pybtex.style.formatting", "__oops"))
    nose.tools.assert_raises(
        pybtex.plugin.loader.PluginNotFound,
        lambda: pybtex.plugin.find_plugin("pybtex.style.formatting",
                                          filename="oh.__oops"))

@@ -29,16 +29,52 @@
 .tools%0A%0A
+import pybtex.database.input.bibtex%0A
 import p
@@ -152,17 +152,16 @@
 .plain%0A%0A
-%0A
 def test
@@ -2565,16 +2565,207 @@
 e=%22oh.__oops%22))%0A
+%0Adef test_plugin_suffix():%0A    plugin = pybtex.plugin.find_plugin(%0A        %22pybtex.database.input%22, filename=%22test.bib%22)%0A    nose.tools.assert_is(plugin, pybtex.database.input.bibtex.Parser)%0A

Test plugin suffix.