Implemented emails converter.
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Tue, 2 Feb 2016 21:09:24 +0000 (21:09 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Tue, 2 Feb 2016 21:09:24 +0000 (21:09 +0000)
git-svn-id: http://www.winterrodeln.org/svn/wrpylib/trunk@2436 7aebc617-e5e2-0310-91dc-80fb5f6d2477

tests/test_wrvalidators.py
wrpylib/wrvalidators.py

index a61e8475eeb7a2ed491d89175c113752bcb35425..8f1fb7523e692ada8424e8b881d48f4ddb8202bb 100644 (file)
@@ -398,6 +398,27 @@ class TestMaskedEmail(unittest.TestCase):
         self.assertEqual('office()example.com', masked_email_to_str(('office@example.com', True), '()'))
 
 
+class TestEmails(unittest.TestCase):
+    def test_from_str(self):
+        self.assertEqual(None, emails_from_str(''))
+        self.assertEqual([], emails_from_str('Nein'))
+        self.assertEqual([(('info@example.com', False), None)], emails_from_str('info@example.com'))
+        self.assertEqual([(('info@example.com', True), None)], emails_from_str('info(at)example.com'))
+        self.assertEqual([(('info@example.com', False), 'Office')], emails_from_str('info@example.com (Office)'))
+        self.assertEqual([(('info@example.com', False), None), ('home@example.com', 'Privat')], emails_from_str('info@example.com; home@example.com (Privat)'))
+        with self.assertRaises(ValueError):
+            emails_from_str('nein')
+        with self.assertRaises(ValueError):
+            emails_from_str('info@example.com; ho me@example.com (Privat)')
+
+    def test_to_str(self):
+        self.assertEqual('', emails_to_str(None))
+        self.assertEqual('Nein', emails_to_str([]))
+        self.assertEqual('info@example.com', emails_to_str([(('info@example.com', False), None)]))
+        self.assertEqual('info@example.com (Office)', emails_to_str([(('info@example.com', False), 'Office')]))
+        self.assertEqual('info@example.com; home@example.com (Privat)', emails_to_str([(('info@example.com', False), None), (('home@example.com', False), 'Privat')]))
+
+
 class TestBox(unittest.TestCase):
     def test_from_str(self):
         value = '{{MyTemplate|apple=2|banana=5}}'
index 14509f55d3b27250857dcf59c143f167fe1df52f..d3a11a694378f87f09aab49c47da58946ee9aa3e 100644 (file)
@@ -630,6 +630,12 @@ def masked_email_to_str(value, mask='(at)'):
     return email
 
 
+def emails_from_str(value):
+    return opt_no_german_from_str(value, lambda val: enum_from_str(val, lambda v: value_comment_from_str(v, masked_email_from_str, opt_str_from_str, True)), False, [], None)
+
+
+def emails_to_str(value):
+    return opt_no_german_to_str(value, lambda val: enum_to_str(val, lambda v: value_comment_to_str(v, masked_email_to_str, opt_str_to_str, True)), False, [], None)
 
 
 class MaskedEmail(formencode.FancyValidator):