I have the following strings:
I submit the following values: username 'foo', password 'bar'
I submit the following values: username 'foo', password 'bar','foo', profile 'bar', extra 'something'
I am trying to match the value pairs but I am not sure how I can repeat a pattern.
So the result I want is:
username 'foo'
password 'bar'
...
My regex so far:
I submit the following values: (\w+\s[^,]+),
I need to find a way to repeat the pattern and I also need to take care of the missing comma at the end. I am using the result in a Cucumber like testing framework for Python (freshen).
The end result will be something like:
@When(r'I submit the following values: (\w+\s[^,]+), ...')
def post_values_to_url(*args):
post_dict = {}
for pairs in args:
#add values to dict
response = client.get('this/is/a/url', post_dict)
If you need to be able to parse an arbitrary number of pairs, then I’d suggest using a table, e.g.:
There’s a rows_hash method on Cucumber::Ast::Table that will give you what you need.
However, I suspect that using a ‘one-size-fits-all’ step definition like this is going make your scenarios difficult to read. Instead, how about doing something like this:
Edit: Just noticed you’re not actually Cucumber. But I suspect Freshen would also support tables.