Repository: joke2k/faker
Pull request #213 (2015-03-18 20:32:39)

Fix gender-specific prefix/suffix handling

coveralls: 
[![Coverage Status](https://coveralls.io/builds/2144030/badge)](https://coveralls.io/builds/2144030)

Coverage decreased (-0.17%) to 88.97% when pulling **0d53e898a6a1dfd51c32bb5efcd4c6a4b9891a01 on akx:suffix** into **edc57342b01593203e09577c2860cb61a35db5a9 on joke2k:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/2144133/badge)](https://coveralls.io/builds/2144133)

Coverage decreased (-0.18%) to 88.96% when pulling **0d53e898a6a1dfd51c32bb5efcd4c6a4b9891a01 on akx:suffix** into **edc57342b01593203e09577c2860cb61a35db5a9 on joke2k:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/2144252/badge)](https://coveralls.io/builds/2144252)

Coverage decreased (-0.13%) to 89.01% when pulling **0d53e898a6a1dfd51c32bb5efcd4c6a4b9891a01 on akx:suffix** into **edc57342b01593203e09577c2860cb61a35db5a9 on joke2k:master**.


Patch:
diff --git a/faker/providers/person/__init__.py b/faker/providers/person/__init__.py
index 1cb10f56..65959246 100644
--- a/faker/providers/person/__init__.py
+++ b/faker/providers/person/__init__.py
@@ -71,7 +71,8 @@ class Provider(BaseProvider):
         if hasattr(cls, 'prefixes'):
             return cls.random_element(cls.prefixes)
         if hasattr(cls, 'prefixes_male') and hasattr(cls, 'prefixes_female'):
-            return cls.random_element((cls.prefixes_male, cls.prefixes_female))
+            prefixes = cls.random_element((cls.prefixes_male, cls.prefixes_female))
+            return cls.random_element(prefixes)
         return ''
 
     @classmethod
@@ -91,7 +92,8 @@ class Provider(BaseProvider):
         if hasattr(cls, 'suffixes'):
             return cls.random_element(cls.suffixes)
         if hasattr(cls, 'suffixes_male') and hasattr(cls, 'suffixes_female'):
-            return cls.random_element((cls.suffixes_male, cls.suffixes_female))
+            suffixes = cls.random_element((cls.suffixes_male, cls.suffixes_female))
+            return cls.random_element(suffixes)
         return ''
 
     @classmethod


Test patch:
diff --git a/faker/tests/__init__.py b/faker/tests/__init__.py
index c6a84097..85556b63 100644
--- a/faker/tests/__init__.py
+++ b/faker/tests/__init__.py
@@ -6,10 +6,21 @@ import json
 import os
 import random
 import unittest
+import sys
 
-from faker import Generator
+try:
+    from StringIO import StringIO
+except ImportError:
+    from io import StringIO
+
+from faker import Generator, Factory
 from faker.utils import text, decorators
 
+try:
+    string_types = (basestring,)
+except NameError:
+    string_types = (str,)
+
 
 TEST_DIR = os.path.dirname(__file__)
 
@@ -153,16 +164,25 @@ class FactoryTestCase(unittest.TestCase):
 
     def test_documentor(self):
         from faker.cli import print_doc
-        print_doc()
-        print_doc('address')
-        print_doc('faker.providers.it_IT.person')
+        output = StringIO()
+        print_doc(output=output)
+        print_doc('address', output=output)
+        print_doc('faker.providers.it_IT.person', output=output)
+        assert output.getvalue()
         self.assertRaises(AttributeError,
                           self.generator.get_formatter,
                           'barFormatter')
 
     def test_command(self):
-        from faker.cli import execute_from_command_line
-        execute_from_command_line(['faker', 'address'])
+        from faker.cli import Command
+        orig_stdout = sys.stdout
+        try:
+            sys.stdout = StringIO()
+            command = Command(['faker', 'address'])
+            command.execute()
+            assert sys.stdout.getvalue()
+        finally:
+            sys.stdout = orig_stdout
 
     def test_slugify(self):
         slug = text.slugify("a'b/c")
@@ -231,6 +251,14 @@ class FactoryTestCase(unittest.TestCase):
         result = datetime_safe.date(2008, 2, 29).strftime('%y')
         self.assertEqual(result, r'08')
 
+    def test_prefix_suffix_always_string(self):
+        # Locales known to contain `*_male` and `*_female`.
+        for locale in ("bg_BG", "dk_DK", "en", "ru_RU", "tr_TR"):
+            f = Factory.create(locale=locale)
+            for x in range(20):  # Probabilistic testing.
+                assert isinstance(f.prefix(), string_types)
+                assert isinstance(f.suffix(), string_types)
+
 
 if __name__ == '__main__':
     unittest.main()