1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
# Copyright (c) 2014 Matthias C. M. Troffaes
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import unicode_literals
import re
import pytest
import pybtex.database.input.bibtex
import pybtex.plugin
import pybtex.style.formatting.plain
def test_plugin_loader():
"""Check that all enumerated plugins can be imported."""
for group in pybtex.plugin._DEFAULT_PLUGINS:
for name in pybtex.plugin.enumerate_plugin_names(group):
pybtex.plugin.find_plugin(group, name)
class TestPlugin1(pybtex.plugin.Plugin):
pass
class TestPlugin2(pybtex.plugin.Plugin):
pass
class TestPlugin3(pybtex.plugin.Plugin):
pass
class TestPlugin4(pybtex.plugin.Plugin):
pass
def test_register_plugin_1():
assert pybtex.plugin.register_plugin(
'pybtex.style.formatting', 'yippikayee', TestPlugin1
)
assert pybtex.plugin.find_plugin(
'pybtex.style.formatting', 'yippikayee'
) is TestPlugin1
assert not pybtex.plugin.register_plugin(
'pybtex.style.formatting', 'yippikayee', TestPlugin2
)
assert pybtex.plugin.find_plugin(
'pybtex.style.formatting', 'yippikayee'
) is TestPlugin1
assert pybtex.plugin.register_plugin(
'pybtex.style.formatting', 'yippikayee', TestPlugin2, force=True
)
assert pybtex.plugin.find_plugin(
'pybtex.style.formatting', 'yippikayee'
), TestPlugin2
def test_register_plugin_2():
assert not pybtex.plugin.register_plugin(
'pybtex.style.formatting', 'plain', TestPlugin2
)
plugin = pybtex.plugin.find_plugin('pybtex.style.formatting', 'plain')
assert plugin is not TestPlugin2
assert plugin is pybtex.style.formatting.plain.Style
def test_register_plugin_3():
assert pybtex.plugin.register_plugin(
'pybtex.style.formatting.suffixes', '.woo', TestPlugin3
)
plugin = pybtex.plugin.find_plugin(
'pybtex.style.formatting', filename='test.woo')
assert plugin is TestPlugin3
def test_bad_find_plugin():
with pytest.raises(pybtex.plugin.PluginGroupNotFound):
pybtex.plugin.find_plugin("pybtex.invalid.group", "__oops")
with pytest.raises(pybtex.plugin.PluginNotFound) as excinfo:
pybtex.plugin.find_plugin("pybtex.style.formatting", "__oops")
assert 'plugin pybtex.style.formatting.__oops not found' in str(excinfo.value)
with pytest.raises(pybtex.plugin.PluginNotFound):
pybtex.plugin.find_plugin("pybtex.style.formatting", filename="oh.__oops")
def test_bad_register_plugin():
with pytest.raises(pybtex.plugin.PluginGroupNotFound):
pybtex.plugin.register_plugin( "pybtex.invalid.group", "__oops", TestPlugin1)
with pytest.raises(pybtex.plugin.PluginGroupNotFound):
pybtex.plugin.register_plugin( "pybtex.invalid.group.suffixes", ".__oops", TestPlugin1)
# suffixes must start with a dot
with pytest.raises(ValueError):
pybtex.plugin.register_plugin( "pybtex.style.formatting.suffixes", "notasuffix", TestPlugin1)
def test_plugin_suffix():
plugin = pybtex.plugin.find_plugin(
"pybtex.database.input", filename="test.bib")
assert plugin is pybtex.database.input.bibtex.Parser
def test_plugin_alias():
pybtex.plugin._DEFAULT_PLUGINS['pybtex.legacy.input'] = 'punchcard'
assert pybtex.plugin.register_plugin('pybtex.legacy.input', 'punchcard', TestPlugin4)
assert pybtex.plugin.register_plugin(
'pybtex.legacy.input.aliases', 'punchedcard', TestPlugin4
)
assert list(pybtex.plugin.enumerate_plugin_names('pybtex.legacy.input')) == ['punchcard']
plugin = pybtex.plugin.find_plugin("pybtex.legacy.input", 'punchedcard')
assert plugin is TestPlugin4
del pybtex.plugin._DEFAULT_PLUGINS['pybtex.legacy.input']
def test_plugin_class():
"""If a plugin class is passed to find_plugin(), it shoud be returned back."""
plugin = pybtex.plugin.find_plugin("pybtex.database.input", 'bibtex')
plugin2 = pybtex.plugin.find_plugin("pybtex.database.input", plugin)
assert plugin == plugin2
|