-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix off-by-one issue in pretty-formatter (#1665)
The pretty-format methods have a `max_line_length` that is used to keep the formatted output within the given number of columns when possible. The argument is supposed to be >0, but in some cases, `max(0, ...)` was used, resulting in errors when the limit was hit. This PR updates those calls to `max(1, ...)`. This PR includes some commits to better organize the tests - please view by commit.
- Loading branch information
Showing
5 changed files
with
100 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
metricflow-semantics/tests_metricflow_semantics/mf_logging/test_pformat_dict.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
import textwrap | ||
|
||
from metricflow_semantics.formatting.formatting_helpers import mf_dedent | ||
from metricflow_semantics.mf_logging.pretty_print import mf_pformat_dict | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def test_pformat_many() -> None: # noqa: D103 | ||
result = mf_pformat_dict("Example description:", obj_dict={"object_0": (1, 2, 3), "object_1": {4: 5}}) | ||
|
||
assert ( | ||
textwrap.dedent( | ||
"""\ | ||
Example description: | ||
object_0: (1, 2, 3) | ||
object_1: {4: 5} | ||
""" | ||
).rstrip() | ||
== result | ||
) | ||
|
||
|
||
def test_pformat_many_with_raw_strings() -> None: # noqa: D103 | ||
result = mf_pformat_dict("Example description:", obj_dict={"object_0": "foo\nbar"}, preserve_raw_strings=True) | ||
|
||
assert ( | ||
textwrap.dedent( | ||
"""\ | ||
Example description: | ||
object_0: | ||
foo | ||
bar | ||
""" | ||
).rstrip() | ||
== result | ||
) | ||
|
||
|
||
def test_pformat_dict_with_empty_message() -> None: | ||
"""Test `mf_pformat_dict` without a description.""" | ||
result = mf_pformat_dict(obj_dict={"object_0": (1, 2, 3), "object_1": {4: 5}}) | ||
|
||
assert ( | ||
mf_dedent( | ||
""" | ||
object_0: (1, 2, 3) | ||
object_1: {4: 5} | ||
""" | ||
) | ||
== result | ||
) | ||
|
||
|
||
def test_pformat_dict_with_pad_sections_with_newline() -> None: | ||
"""Test `mf_pformat_dict` with new lines between sections.""" | ||
result = mf_pformat_dict(obj_dict={"object_0": (1, 2, 3), "object_1": {4: 5}}, pad_items_with_newlines=True) | ||
|
||
assert ( | ||
mf_dedent( | ||
""" | ||
object_0: (1, 2, 3) | ||
object_1: {4: 5} | ||
""" | ||
) | ||
== result | ||
) | ||
|
||
|
||
def test_pformat_many_with_strings() -> None: # noqa: D103 | ||
result = mf_pformat_dict("Example description:", obj_dict={"object_0": "foo\nbar"}) | ||
assert ( | ||
textwrap.dedent( | ||
"""\ | ||
Example description: | ||
object_0: 'foo\\nbar' | ||
""" | ||
).rstrip() | ||
== result | ||
) | ||
|
||
|
||
def test_minimal_length() -> None: | ||
"""Test where the max_line_length is the minimal length.""" | ||
assert mf_pformat_dict("Example output", {"foo": "bar"}, max_line_length=1) == mf_dedent( | ||
""" | ||
Example output | ||
foo: 'bar' | ||
""" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters