Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factoring out String length check. #23

Merged
merged 4 commits into from
May 18, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions fauxfactory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ class FauxFactory(object):
Generate random data for your tests.
"""

@classmethod
def _is_positive_int(cls, length):
"""
Check that ``length`` argument is a valid integer, greater than zero.

@type length: int
@param length: The desired length of the string

@rtype: Exception
@return: A ``ValueError`` exception
"""

# Validate length argument
if not isinstance(length, int) or length <= 0:
raise ValueError("%s is an invalid \'length\'." % length)

@classmethod
def generate_string(cls, str_type, length):
"""
Expand Down Expand Up @@ -99,8 +115,7 @@ def generate_alpha(cls, length=5):
"""

# Validate length argument
if not isinstance(length, int) or length <= 0:
raise ValueError("%s is an invalid \'length\'." % length)
cls._is_positive_int(length)

output_string = u''.join(
random.choice(string.ascii_letters) for i in range(length)
Expand All @@ -121,8 +136,7 @@ def generate_alphanumeric(cls, length=5):
"""

# Validate length argument
if not isinstance(length, int) or length <= 0:
raise ValueError("%s is an invalid \'length\'." % length)
cls._is_positive_int(length)

output_string = u''.join(
random.choice(
Expand Down Expand Up @@ -181,8 +195,7 @@ def generate_cjk(cls, length=5):
"""

# Validate length argument
if not isinstance(length, int) or length <= 0:
raise ValueError("%s is an invalid \'length\'." % length)
cls._is_positive_int(length)

# Generate codepoints, then convert the codepoints to a string. The
# valid range of CJK codepoints is 0x4E00 - 0x9FCC, inclusive. Python 2
Expand Down Expand Up @@ -360,10 +373,7 @@ def generate_iplum(cls, words=None, paragraphs=None):
if not isinstance(words, int) or words < 0:
raise ValueError(
"Cannot generate a string with negative number of words.")

if not isinstance(paragraphs, int) or paragraphs <= 0:
raise ValueError(
"Cannot generate a string with negative number of paragraphs.")
cls._is_positive_int(paragraphs)

# Original Lorem Ipsum string
all_words = LOREM_IPSUM_TEXT.split()
Expand Down Expand Up @@ -414,8 +424,7 @@ def generate_latin1(cls, length=5):
"""

# Validate length argument
if not isinstance(length, int) or length <= 0:
raise ValueError("%s is an invalid \'length\'." % length)
cls._is_positive_int(length)

range0 = range1 = range2 = []
range0 = ['00C0', '00D6']
Expand Down Expand Up @@ -518,8 +527,7 @@ def generate_numeric_string(cls, length=5):
"""

# Validate length argument
if not isinstance(length, int) or length <= 0:
raise ValueError("%s is an invalid \'length\'." % length)
cls._is_positive_int(length)

output_string = u''.join(
random.choice(string.digits) for i in range(length)
Expand Down Expand Up @@ -614,8 +622,7 @@ def generate_utf8(cls, length=5):
"""

# Validate length argument
if not isinstance(length, int) or length <= 0:
raise ValueError("%s is an invalid \'length\'." % length)
cls._is_positive_int(length)

# Generate codepoints. The valid range of UTF-8 codepoints is
# 0x0-0x10FFFF, minus the following: 0xC0-0xC1, 0xF5-0xFF and
Expand All @@ -625,9 +632,10 @@ def generate_utf8(cls, length=5):
codepoints = []
while len(codepoints) < length:
codepoint = random.randint(0x0, 0x10FFFF)
if codepoint not in range(0xC0, 0xC1 + 1) \
and codepoint not in range(0xF5, 0xFF + 1) \
and codepoint not in range(0xD800, 0xDFFF + 1):
if (
codepoint not in range(0xC0, 0xC1 + 1)
and codepoint not in range(0xF5, 0xFF + 1)
and codepoint not in range(0xD800, 0xDFFF + 1)):
codepoints.append(codepoint)

# Convert codepoints to characters. Python 2 and 3 support the `unichr`
Expand Down