Add mappers module

This commit is contained in:
Joakim Soderlund 2016-12-27 20:24:29 +01:00
parent 0c4d0bd6fb
commit 8209897df5
2 changed files with 111 additions and 0 deletions

43
fimfarchive/mappers.py Normal file
View file

@ -0,0 +1,43 @@
"""
Mappers for Fimfarchive.
"""
#
# Fimfarchive, preserves stories from Fimfiction.
# Copyright (C) 2015 Joakim Soderlund
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
class Mapper:
"""
Callable which maps something to something else.
"""
def __call__(self, *args, **kwargs):
raise NotImplementedError()
class StaticMapper(Mapper):
"""
Returns the supplied value for any call.
"""
def __init__(self, value=None):
self.value = value
def __call__(self, *args, **kwargs):
return self.value

68
tests/test_mappers.py Normal file
View file

@ -0,0 +1,68 @@
"""
Mapper tests.
"""
#
# Fimfarchive, preserves stories from Fimfiction.
# Copyright (C) 2015 Joakim Soderlund
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import pytest
from fimfarchive.mappers import StaticMapper
class TestStaticMapper:
"""
StaticMapper tests.
"""
@pytest.fixture
def value(self):
"""
Returns a unique instance.
"""
return object()
def test_value(self, value):
"""
Tests returns the supplied value.
"""
mapper = StaticMapper(value)
assert mapper() is value
def test_default_value(self):
"""
Tests `None` is returned by default.
"""
mapper = StaticMapper()
assert mapper() is None
def test_args(self, value):
"""
Tests callable ignores args.
"""
mapper = StaticMapper(value)
assert mapper(1, 2, 3) is value
def test_kwargs(self, value):
"""
Tests callable ignores kwargs.
"""
mapper = StaticMapper(value)
assert mapper(a=1, b=2) is value