Ejemplo n.º 1
0
    def test_repr(self):
        pred = Predicate('abc')
        self.assertEqual(repr(pred), "Predicate('abc')")

        pred = ~Predicate('abc')
        self.assertEqual(repr(pred), "~Predicate('abc')")

        pred = Predicate('abc', name='custom_name')
        self.assertEqual(repr(pred), "Predicate('abc', name='custom_name')")
Ejemplo n.º 2
0
    def test_predicate_from_predicate(self):
        pred1 = Predicate('abc')
        pred2 = Predicate(pred1)
        self.assertIsNot(pred1, pred2, msg='should be different object')
        self.assertIs(pred1.obj, pred2.obj, msg='should keep original reference')
        self.assertEqual(pred1.matcher, pred2.matcher)
        self.assertEqual(pred1._inverted, pred2._inverted)

        from_inverted = Predicate(~Predicate('abc'))
        self.assertTrue(from_inverted._inverted)
Ejemplo n.º 3
0
    def test_predicate_function(self):
        pred = Predicate('abc')
        self.assertTrue(pred('abc'))
        self.assertFalse(pred('def'))
        self.assertFalse(pred(123))

        pred = Predicate(1)
        self.assertTrue(pred(1))
        self.assertFalse(pred(2))
        self.assertFalse(pred('abc'))

        pred = Predicate(('abc', int))
        self.assertTrue(pred(('abc', 1)))
        self.assertFalse(pred(('abc', 1.0)))
Ejemplo n.º 4
0
    def test_optional_name(self):
        pred1 = Predicate('abc')  # <- No name arg provided.
        self.assertFalse(hasattr(pred1, '__name__'))

        pred2 = Predicate(pred1)  # <- No name arg inherited from pred1.
        self.assertFalse(hasattr(pred1, '__name__'))

        pred3 = Predicate('abc', name='pred3_name')  # <- Provides name.
        self.assertEqual(pred3.__name__, 'pred3_name')

        pred4 = Predicate(pred3)  # <- Inherits name from pred3.
        self.assertEqual(pred4.__name__, 'pred3_name')

        pred5 = Predicate(pred3, name='pred5_name')  # <- Overrides pred3 name.
        self.assertEqual(pred5.__name__, 'pred5_name')
Ejemplo n.º 5
0
    def test_passthrough(self):
        """Callable predicates should return the values provided by
        the given function as-is--the values should not be converted
        to True or False.
        """
        TOKEN = object()

        def divisible_or_token(x):  # <- Helper function.
            if x % 3 == 0:
                return True
            if x % 5 == 0:
                return TOKEN
            return False

        predicate = Predicate(divisible_or_token)
        self.assertEqual(predicate(1), False)
        self.assertEqual(predicate(3), True)
        self.assertIs(predicate(5), TOKEN, msg='TOKEN should be returned, not True.')
Ejemplo n.º 6
0
    def test_str(self):
        pred = Predicate('abc')
        self.assertEqual(str(pred), "'abc'")

        inverted = ~Predicate('abc')
        self.assertEqual(str(inverted), "not 'abc'")
Ejemplo n.º 7
0
 def test_inverted_logic(self):
     pred = ~Predicate('abc')
     self.assertFalse(pred('abc'))
     self.assertTrue(pred('def'))
Ejemplo n.º 8
0
 def test_get_matcher_from_predicate(self):
     predicate = Predicate('abc')
     matcher = get_matcher(predicate)
     self.assertIs(matcher, predicate.matcher)
Ejemplo n.º 9
0
    def test_repr(self):
        pred = Predicate('abc')
        self.assertEqual(repr(pred), "Predicate('abc')")

        pred = ~Predicate('abc')
        self.assertEqual(repr(pred), "~Predicate('abc')")