Skip to content

Commit

Permalink
Enable more non-panicking formatter tests (#3262)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Feb 27, 2023
1 parent 2700158 commit 16be691
Show file tree
Hide file tree
Showing 9 changed files with 3,282 additions and 7 deletions.
8 changes: 1 addition & 7 deletions crates/ruff_python_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,7 @@ mod tests {
pattern = "resources/test/fixtures/black/**/*.py",
// Excluded tests because they reach unreachable when attaching tokens
exclude = [
"*comments.py",
"*comments[3,5,8].py",
"*comments_non_breaking_space.py",
"*docstring_preview.py",
"*docstring.py",
"*fmtonoff.py",
"*fmtskip8.py",
"*comments8.py",
])
]
#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: snapshot
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/comments3.py
---
## Input

```py
# The percent-percent comments are Spyder IDE cells.
# %%
def func():
x = """
a really long string
"""
lcomp3 = [
# This one is actually too long to fit in a single line.
element.split("\n", 1)[0]
# yup
for element in collection.select_elements()
# right
if element is not None
]
# Capture each of the exceptions in the MultiError along with each of their causes and contexts
if isinstance(exc_value, MultiError):
embedded = []
for exc in exc_value.exceptions:
if exc not in _seen:
embedded.append(
# This should be left alone (before)
traceback.TracebackException.from_exception(
exc,
limit=limit,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
# copy the set of _seen exceptions so that duplicates
# shared between sub-exceptions are not omitted
_seen=set(_seen),
)
# This should be left alone (after)
)
# everything is fine if the expression isn't nested
traceback.TracebackException.from_exception(
exc,
limit=limit,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
# copy the set of _seen exceptions so that duplicates
# shared between sub-exceptions are not omitted
_seen=set(_seen),
)
# %%
```

## Black Differences

```diff
--- Black
+++ Ruff
@@ -9,10 +9,11 @@
lcomp3 = [
# This one is actually too long to fit in a single line.
element.split("\n", 1)[0]
- # yup
- for element in collection.select_elements()
- # right
- if element is not None
+ for # yup
+ element in collection.select_elements()
+ if # right
+ element
+ is not None
]
# Capture each of the exceptions in the MultiError along with each of their causes and contexts
if isinstance(exc_value, MultiError):
@@ -26,9 +27,9 @@
limit=limit,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
- # copy the set of _seen exceptions so that duplicates
+ _seen=# copy the set of _seen exceptions so that duplicates
# shared between sub-exceptions are not omitted
- _seen=set(_seen),
+ set(_seen),
)
# This should be left alone (after)
)
@@ -39,9 +40,9 @@
limit=limit,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
- # copy the set of _seen exceptions so that duplicates
+ _seen=# copy the set of _seen exceptions so that duplicates
# shared between sub-exceptions are not omitted
- _seen=set(_seen),
+ set(_seen),
)
```

## Ruff Output

```py
# The percent-percent comments are Spyder IDE cells.
# %%
def func():
x = """
a really long string
"""
lcomp3 = [
# This one is actually too long to fit in a single line.
element.split("\n", 1)[0]
for # yup
element in collection.select_elements()
if # right
element
is not None
]
# Capture each of the exceptions in the MultiError along with each of their causes and contexts
if isinstance(exc_value, MultiError):
embedded = []
for exc in exc_value.exceptions:
if exc not in _seen:
embedded.append(
# This should be left alone (before)
traceback.TracebackException.from_exception(
exc,
limit=limit,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
_seen=# copy the set of _seen exceptions so that duplicates
# shared between sub-exceptions are not omitted
set(_seen),
)
# This should be left alone (after)
)
# everything is fine if the expression isn't nested
traceback.TracebackException.from_exception(
exc,
limit=limit,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
_seen=# copy the set of _seen exceptions so that duplicates
# shared between sub-exceptions are not omitted
set(_seen),
)
# %%
```

## Black Output

```py
# The percent-percent comments are Spyder IDE cells.
# %%
def func():
x = """
a really long string
"""
lcomp3 = [
# This one is actually too long to fit in a single line.
element.split("\n", 1)[0]
# yup
for element in collection.select_elements()
# right
if element is not None
]
# Capture each of the exceptions in the MultiError along with each of their causes and contexts
if isinstance(exc_value, MultiError):
embedded = []
for exc in exc_value.exceptions:
if exc not in _seen:
embedded.append(
# This should be left alone (before)
traceback.TracebackException.from_exception(
exc,
limit=limit,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
# copy the set of _seen exceptions so that duplicates
# shared between sub-exceptions are not omitted
_seen=set(_seen),
)
# This should be left alone (after)
)
# everything is fine if the expression isn't nested
traceback.TracebackException.from_exception(
exc,
limit=limit,
lookup_lines=lookup_lines,
capture_locals=capture_locals,
# copy the set of _seen exceptions so that duplicates
# shared between sub-exceptions are not omitted
_seen=set(_seen),
)
# %%
```


Loading

0 comments on commit 16be691

Please sign in to comment.