示例#1
0
    def test_matches(self):
        """
        Ensure Matcher.matches returns a boolean depending on re.match's value
        """
        testMatcher = Matcher('pattern')
        testMatcher._pattern = unittest.mock.MagicMock()
        testMatcher._pattern.match.return_value = None
        self.assertEquals(testMatcher.matches('string'), False)
        testMatcher._pattern.match.assert_called_with('string')

        testMatcher._pattern.match.return_value = '123'
        self.assertEquals(testMatcher.matches('string'), True)
        testMatcher._pattern.match.assert_called_with('string')
示例#2
0
    def test_get_variable_value(self):
        """
        Ensure Matcher.get_variable_value logic flows as intended
        """
        testMatcher = Matcher('pattern')
        testMatcher._pattern = unittest.mock.MagicMock()
        testMatcher._pattern.match.return_value = None
        self.assertEquals(testMatcher.get_variable_value('string', 'var'), None)
        testMatcher._pattern.match.assert_called_with('string')

        matchReturn = unittest.mock.MagicMock()
        testMatcher._pattern.match.return_value = matchReturn
        matchReturn.groupdict.return_value = {'var' : 'lol'}
        self.assertEquals(testMatcher.get_variable_value('string', 'var'), 'lol')
        testMatcher._pattern.match.assert_called_with('string')

        matchReturn.groupdict.return_value = {'var2' : 'lol'}
        self.assertEquals(testMatcher.get_variable_value('string', 'var'), None)
        testMatcher._pattern.match.assert_called_with('string')
示例#3
0
 def get_value_matcher(cls, key):
     """
     Gets a matcher object for the definition value given by key
     """
     return Matcher.from_variable_string(cls.get_value(key))