Skip to content
Snippets Groups Projects
Commit 5aa69d5e authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

2.10.0_b7

parent 03d37419
No related branches found
Tags v2.10.0_b7
No related merge requests found
......@@ -18,7 +18,15 @@ Predicates
.. autofunction:: satella.coding.predicates.equals
.. autofunction:: satella.coding.predicates.length_less_than
.. autofunction:: satella.coding.predicates.shorter_than
.. autofunction:: satella.coding.predicates.longer_than
.. autofunction:: satella.coding.predicates.is_not_none
.. autofunction:: satella.coding.predicates.not_equal
Decorators
----------
......
from .number import between
from .generic import one_of, equals
from .sequences import length_is, length_multiple_of, length_less_than
from .generic import one_of, equals, is_not_none, not_equal, has_attr
from .sequences import length_is, length_multiple_of, shorter_than, longer_than
from .decorators import attribute, item
__all__ = ['between', 'one_of', 'length_is', 'length_less_than', 'length_multiple_of',
'equals', 'attribute', 'item']
__all__ = ['between', 'one_of', 'length_is', 'shorter_than', 'length_multiple_of',
'equals', 'attribute', 'item', 'is_not_none', 'not_equal', 'longer_than',
'has_attr']
......@@ -13,6 +13,35 @@ def one_of(*args) -> tp.Callable[[tp.Any], bool]:
return predicate
def _is_not_none(v):
return v is not None
def is_not_none():
"""
Return a predicate that will return True if passed element is not None
"""
return _is_not_none
def has_attr(x):
"""
Build a predicate that returns True if passed element has attribute x
"""
def predicate(v):
return hasattr(v, x)
return predicate
def not_equal(x):
"""
Build a predicate that returns True only if value passed to it does not equal x
"""
def predicate(v):
return v != x
return predicate
def equals(x):
"""
Build a predicate that returns True only if value passed to it equals x
......
import typing as tp
def length_less_than(x) -> tp.Callable[[tp.Sequence], bool]:
def shorter_than(x) -> tp.Callable[[tp.Sequence], bool]:
"""
Return a predicate that will return True if length of sequence is less than x
......@@ -12,6 +12,15 @@ def length_less_than(x) -> tp.Callable[[tp.Sequence], bool]:
return predicate
def longer_than(x):
"""
Return a predicate that will return True if length of sequence is greater than x
"""
def predicate(v):
return len(v) > x
return predicate
def length_is(x) -> tp.Callable[[tp.Sequence], bool]:
"""
Return a predicate that will return True if length of sequence is x
......
import unittest
from satella.coding.predicates import between, one_of, length_is, length_less_than, \
length_multiple_of, attribute, equals, item
from satella.coding.predicates import between, one_of, length_is, shorter_than, \
length_multiple_of, attribute, equals, item, longer_than, is_not_none, not_equal, \
has_attr
class TestPredicates(unittest.TestCase):
def test_has_attr(self):
class A:
def __init__(self):
self.b = 2
a = A()
self.assertTrue(has_attr('b')(a))
self.assertFalse(has_attr('c')(a))
def test_not_equal(self):
self.assertTrue(not_equal(5)(6))
self.assertFalse(not_equal(5)(5))
def test_is_not_none(self):
self.assertTrue(is_not_none()(6))
self.assertFalse(is_not_none()(None))
def test_longer_than(self):
a = 'ala'
self.assertTrue(longer_than(2)(a))
self.assertFalse(longer_than(3)(a))
def test_length_is_attribute(self):
class Attr:
def __init__(self, b):
......@@ -26,10 +49,10 @@ class TestPredicates(unittest.TestCase):
self.assertTrue(length_is(3)(a))
self.assertFalse(length_is(4)(a))
def test_length_less_than(self):
def test_shorter_than(self):
a = 'ala'
self.assertTrue(length_less_than(4)(a))
self.assertFalse(length_less_than(3)(a))
self.assertTrue(shorter_than(4)(a))
self.assertFalse(shorter_than(3)(a))
def test_length_multiple_of(self):
a = 'ala '
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment