def test_single_value(self): # Check for MatcherObject wrapping. def isodd(x): # <- Helper function. return x % 2 == 1 matcher = get_matcher(isodd) self.assertIsInstance(matcher, MatcherObject) # When original is adequate, it should be returned unchanged. original = object() matcher = get_matcher(original) self.assertIs(matcher, original)
def test_tuple_of_values(self): # Check for MatcherTuple wrapping. def isodd(x): # <- Helper function. return x % 2 == 1 matcher = get_matcher((1, isodd)) self.assertIsInstance(matcher, MatcherTuple) # When tuple contains no MatcherObject objects, # the original should be returned unchanged. original = ('abc', 123) matcher = get_matcher(original) self.assertIs(matcher, original)
def test_integration(self): """A small integration test that checks a tuple containing all of the different special handling cases. """ def mycallable(x): # <- Helper function. return x == '_' myregex = re.compile('_') myset = set(['_']) matcher = get_matcher((mycallable, myregex, myset, '_', Ellipsis)) self.assertTrue(matcher == ('_', '_', '_', '_', '_')) # <- Passes all conditions. self.assertFalse(matcher == ('X', '_', '_', '_', '_')) # <- Callable returns False. self.assertFalse(matcher == ('_', 'X', '_', '_', '_')) # <- Regex has no match. self.assertFalse(matcher == ('_', '_', 'X', '_', '_')) # <- Not in set. self.assertFalse(matcher == ('_', '_', '_', 'X', '_')) # <- Does not equal string. self.assertTrue( matcher == ('_', '_', '_', '_', 'X')) # <- Passes all conditions (wildcard). expected = "(mycallable, re.compile('_'), {0!r}, '_', ...)".format( myset) self.assertEqual(repr(matcher), expected)
def test_get_matcher_from_predicate(self): predicate = Predicate('abc') matcher = get_matcher(predicate) self.assertIs(matcher, predicate.matcher)
def test_get_matcher_from_matcher(self): original = get_matcher((1, 'abc')) matcher = get_matcher(original) self.assertIs(matcher, original)