diff --git a/fimfarchive/utils.py b/fimfarchive/utils.py new file mode 100644 index 0000000..9aac2e1 --- /dev/null +++ b/fimfarchive/utils.py @@ -0,0 +1,45 @@ +""" +Various utilities. +""" + + +# +# 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 . +# + + +__all__ = ( + 'Empty', +) + + +class EmptyMeta(type): + """ + Meta-class for Empty. + """ + + def __bool__(cls): + return False + + +class Empty(metaclass=EmptyMeta): + """ + Unique placeholder similar to `None`. + """ + + def __bool__(self): + return False diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..00609d5 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,71 @@ +""" +Utility 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 . +# + + +from fimfarchive.utils import Empty + + +class TestEmpty: + """ + Empty tests. + """ + + def test_empty_class_is_not_none(self): + """ + Tests `Empty` class is different from `None`. + """ + assert Empty is not None + assert Empty != None # noqa + + def test_empty_class_evaluates_to_false(self): + """ + Tests `Empty` class evaluates to `False`. + """ + assert not Empty + + def test_empty_class_is_empty(self): + """ + Tests `Empty` class can be identified. + """ + assert Empty is Empty + assert Empty == Empty + + def test_empty_instance_evaluates_to_false(self): + """ + Tests `Empty` instance evaulates to `False` + """ + assert not Empty() + + def test_empty_instance_is_unique(self): + """ + Tests `Empty` instances are unique. + """ + empty = Empty() + + assert isinstance(empty, Empty) + assert empty is not Empty + assert empty != Empty + assert empty is not Empty() + assert empty != Empty() + assert empty is empty + assert empty == empty