diff --git a/gmailfilter/test.py b/gmailfilter/test.py index 126af74..20cc69a 100644 --- a/gmailfilter/test.py +++ b/gmailfilter/test.py @@ -132,7 +132,7 @@ class SubjectContains(Test): """ - def __init__(self, search_string, case_sensitive=False): + def __init__(self, search_string, case_sensitive=True): self._search_string = search_string self._case_sensitive = case_sensitive @@ -154,7 +154,6 @@ def __init__(self, target_list): self._target_list = target_list def match(self, message): - return get_list_id(message) == self._target_list diff --git a/gmailfilter/tests/factory.py b/gmailfilter/tests/factory.py index c746b8b..842da0b 100644 --- a/gmailfilter/tests/factory.py +++ b/gmailfilter/tests/factory.py @@ -6,7 +6,7 @@ class TestFactoryMixin(object): """A mixin class that generates test fake values.""" - def get_email_message(self, headers=None): + def get_email_message(self, headers=None, subject='Test Subject'): """Get an email message. :param headers: If set, must be a dict or 2-tuple iteratble of key/value @@ -15,6 +15,7 @@ def get_email_message(self, headers=None): message = FakeMessage() if headers: message.headers = dict(headers) + message.headers['Subject'] = subject return message @@ -26,3 +27,6 @@ def __init__(self): def get_headers(self): return self.headers + def subject(self): + return self.get_headers()['Subject'] + diff --git a/gmailfilter/tests/test_tests.py b/gmailfilter/tests/test_tests.py index 0b8305c..88bb507 100644 --- a/gmailfilter/tests/test_tests.py +++ b/gmailfilter/tests/test_tests.py @@ -7,6 +7,7 @@ And, Or, MatchesHeader, + SubjectContains, ) from gmailfilter._message import Message @@ -86,3 +87,39 @@ def test_fails_when_header_value_is_wrong(self): def test_passes_with_correct_value(self): message = self.get_email_message(headers=dict(SomeHeader='123')) self.assertTrue(MatchesHeader('SomeHeader', '123').match(message)) + + +class SubjectContainsTests(TestCase, TestFactoryMixin): + + def test_case_sensitive_contains_matches_whole_string(self): + message = self.get_email_message(subject='Hello World') + self.assertTrue(SubjectContains('Hello World').match(message)) + + def test_case_sensitive_contains_matches_partial_string(self): + message = self.get_email_message(subject='Hello World') + self.assertTrue(SubjectContains('ello Worl').match(message)) + + def test_case_sensitive_is_the_default(self): + message = self.get_email_message(subject='Hello World') + self.assertFalse(SubjectContains('hello world').match(message)) + + def test_case_insensitive_works_with_ascii(self): + message = self.get_email_message(subject='Hello World') + self.assertTrue( + SubjectContains('hello world', case_sensitive=False).match(message) + ) + + def test_case_insensitive_works_with_unicode(self): + message = self.get_email_message(subject='Hello BUẞE') + self.assertTrue( + SubjectContains('hello BUSSE', case_sensitive=False).match(message) + ) + self.assertTrue( + SubjectContains('hello Buße', case_sensitive=False).match(message) + ) + + def test_case_insensitive_works_with_unicode_accents(self): + message = self.get_email_message(subject='Hêllo') + self.assertTrue( + SubjectContains('h\xeallo', case_sensitive=False).match(message) + )