class TestMatch(object): def setup(self): self.match = Match('foo') self.client = Mock() def test_init_does_not_overwrite_things(self): class MyMatch(Match): pattern = 'foo' m = MyMatch() assert m.pattern == 'foo' def test_match_using_callable(self): self.match.pattern = lambda m: 'foobar' assert 'foobar' == self.match.match('this is a foo message') def test_match_using_simple_pattern(self): self.match.pattern = r'foo-(\d+)' assert ['123'] == self.match.match('this is about foo-123') def test_match_returns_none_on_typeerror(self): self.match.pattern = Mock(side_effect=TypeError) assert self.match.match('this is a foo message') is None def test_simple_decorator(self): @match('foo-(\d+)') def foo(client, chan, nick, msg, matches): return matches[0] assert '123' == foo._plugins[0](self.client, '#bots', 'me', 'this is about foo-123') def test_callable_decorator(self): @match(lambda x: x.startswith('foo')) def foo(client, chan, nick, msg, matches): return 'bar' assert 'bar' == foo._plugins[0](self.client, '#bots', 'me', 'foo at the start') assert foo._plugins[0](self.client, '#bots', 'me', 'not at the start foo') is None def test_match_with_unicode(self): @match(u'☃') def snowman_match(client, chan, nick, msg, matches): return 'snowman' assert 'snowman' == snowman_match._plugins[0](self.client, '#bots', 'me', u'☃')
def setup(self): self.match = Match('foo') self.client = Mock()