-
Notifications
You must be signed in to change notification settings - Fork 353
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
Feature: Return namedtuples for get_format_args #93
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,14 @@ | |
import re | ||
from collections import namedtuple | ||
|
||
import pytest | ||
from boltons.formatutils import (get_format_args, | ||
split_format_str, | ||
tokenize_format_str, | ||
infer_positional_format_args) | ||
infer_positional_format_args, | ||
FormatArgs, | ||
NamedFormatArg, | ||
PositionalFormatArg) | ||
|
||
|
||
PFAT = namedtuple("PositionalFormatArgTest", "fstr arg_vals res") | ||
|
@@ -48,6 +52,40 @@ def test_get_fstr_args(): | |
return results | ||
|
||
|
||
@pytest.mark.parametrize(('sample', 'expected'), zip(_TEST_TMPLS, [ | ||
([], [('hello', str)]), | ||
([], [('hello', str)]), | ||
([], [('hello', str), ('width', str)]), | ||
([], [('hello', str), ('fchar', str), ('width', str)]), | ||
([(0, str), (1, int), (2, float)], []), | ||
# example 6 is skipped | ||
])) | ||
def test_get_format_args(sample, expected): | ||
"""Test `get_format_args` result as tuples.""" | ||
assert get_format_args(sample) == expected | ||
|
||
|
||
@pytest.mark.parametrize(('sample', 'expected'), zip(_TEST_TMPLS, [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hehe this inline parametrize is kind of long, just curious, is this common practice with pytest? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume so; the pytest docs show an example of a multi-line parametrize right at the top. There are also a few instances in the pytest tests themselves (such as in test_conftest.py, test_config.py and test_mark.py). If I have something like this that I intend to reuse exactly as-is, rather than moving the list of samples to a separate variable, I create a new mark:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting, thanks! |
||
FormatArgs([], [NamedFormatArg('hello', str)]), | ||
FormatArgs([], [NamedFormatArg('hello', str)]), | ||
FormatArgs([], [NamedFormatArg('hello', str), | ||
NamedFormatArg('width', str)]), | ||
FormatArgs([], [NamedFormatArg('hello', str), | ||
NamedFormatArg('fchar', str), | ||
NamedFormatArg('width', str)]), | ||
FormatArgs([PositionalFormatArg(0, str), | ||
PositionalFormatArg(1, int), | ||
PositionalFormatArg(2, float)], []), | ||
# example 6 is skipped | ||
])) | ||
def test_get_format_args_namedtuples(sample, expected): | ||
"""Test `get_format_args` result as `namedtuples`.""" | ||
result = get_format_args(sample) | ||
assert result == expected | ||
assert result.positional == expected.positional | ||
assert result.named == expected.named | ||
|
||
|
||
def test_split_fstr(): | ||
results = [] | ||
for t in _TEST_TMPLS: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you change this to:
This will allow the module to continue to be used both inside and outside of the boltons package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. It also occurs to me that I have also neglected to update the documentation.