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

Add support for expressions in the Text generator #32

Merged
merged 2 commits into from
Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 13 additions & 3 deletions populous/generators/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from string import punctuation

from populous.compat import ascii_letters
from populous.vars import parse_vars

from .base import Generator

Expand All @@ -19,8 +20,14 @@ def get_arguments(self, min_length=0, max_length=None, chars='<a-Z><0-9> ',
**kwargs):
super(Text, self).get_arguments(**kwargs)

self.min_length = min_length or 0
self.max_length = max_length or 10000
if min_length is None:
min_length = 0

if max_length is None:
max_length = 10000

self.min_length = parse_vars(min_length)
self.max_length = parse_vars(max_length)
self.chars = self.get_chars(chars)

def get_chars(self, description):
Expand All @@ -45,7 +52,10 @@ def generate(self):

while True:
# get a random length for the string
length = random.randint(self.min_length, self.max_length)
length = random.randint(
self.evaluate(self.min_length),
self.evaluate(self.max_length)
)
# get a random array of short integers of the same length than
# the final string
rand_shorts = array.array('H', os.urandom(length * 2))
Expand Down
23 changes: 23 additions & 0 deletions tests/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,29 @@ def test_text(item):
assert len(sample) == 10
assert all((c in 'abc' for c in e) for e in sample)

# Expressions
generator = generators.Text(
item, 'foo',
chars='abc',
min_length='$([5, 10]|random)',
max_length=10
)
sample = take(generator, 10)

assert len(sample) == 10
assert all((5 <= len(e) <= 10) for e in sample)

generator = generators.Text(
item, 'foo',
chars='abc',
min_length='$([1, 2]|random)',
max_length='$([3, 4]|random)'
)
sample = take(generator, 10)

assert len(sample) == 10
assert all((1 <= len(e) <= 4) for e in sample)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should also add a test with max_length=0 as there was apparently a bug with it :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :-)


def test_datetime(blueprint, item):
from datetime import datetime
Expand Down