Skip to content

Commit

Permalink
Show preview changes for tests with options (#9223)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Dec 21, 2023
1 parent 1e7bc1d commit d835b28
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 0 deletions.
33 changes: 33 additions & 0 deletions crates/ruff_python_formatter/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,39 @@ fn format() {
CodeFrame::new("python", &formatted_code)
)
.unwrap();

if options.preview().is_enabled() {
continue;
}

// We want to capture the differences in the preview style in our fixtures
let options_preview = options.with_preview(PreviewMode::Enabled);
let printed_preview = format_module_source(&content, options_preview.clone())
.expect("Formatting to succeed");
let formatted_preview = printed_preview.as_code();

ensure_unchanged_ast(&content, formatted_preview, &options_preview, input_path);
ensure_stability_when_formatting_twice(
formatted_preview,
&options_preview,
input_path,
);

if formatted_code != formatted_preview {
// Having both snapshots makes it hard to see the difference, so we're keeping only
// diff.
writeln!(
snapshot,
"#### Preview changes\n{}",
CodeFrame::new(
"diff",
TextDiff::from_lines(formatted_code, formatted_preview)
.unified_diff()
.header("Stable", "Preview")
)
)
.unwrap();
}
}
} else {
// We want to capture the differences in the preview style in our fixtures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,28 @@ def doctest_extra_indent3():
```


#### Preview changes
```diff
--- Stable
+++ Preview
@@ -270,9 +270,11 @@
Examples
--------
- >>> df = pl.DataFrame(
- ... {"foo": [1, 2, 3], "bar": [6, 7, 8], "ham": ["a", "b", "c"]}
- ... )
+ >>> df = pl.DataFrame({
+ ... "foo": [1, 2, 3],
+ ... "bar": [6, 7, 8],
+ ... "ham": ["a", "b", "c"],
+ ... })
"""
```


### Output 2
```
indent-style = space
Expand Down Expand Up @@ -1139,6 +1161,28 @@ def doctest_extra_indent3():
```


#### Preview changes
```diff
--- Stable
+++ Preview
@@ -270,9 +270,11 @@
Examples
--------
- >>> df = pl.DataFrame(
- ... {"foo": [1, 2, 3], "bar": [6, 7, 8], "ham": ["a", "b", "c"]}
- ... )
+ >>> df = pl.DataFrame({
+ ... "foo": [1, 2, 3],
+ ... "bar": [6, 7, 8],
+ ... "ham": ["a", "b", "c"],
+ ... })
"""
```


### Output 4
```
indent-style = tab
Expand Down Expand Up @@ -1876,4 +1920,26 @@ def doctest_extra_indent3():
```


#### Preview changes
```diff
--- Stable
+++ Preview
@@ -700,9 +700,11 @@
Examples
--------
- >>> df = pl.DataFrame(
- ... {"foo": [1, 2, 3], "bar": [6, 7, 8], "ham": ["a", "b", "c"]}
- ... )
+ >>> df = pl.DataFrame({
+ ... "foo": [1, 2, 3],
+ ... "bar": [6, 7, 8],
+ ... "ham": ["a", "b", "c"],
+ ... })
"""
```



Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,19 @@ trailing_preferred_quote_texts = [''' "''', ''' ""''', ''' """''', ''' """"''']
```


#### Preview changes
```diff
--- Stable
+++ Preview
@@ -1,4 +1,5 @@
"' test"
+
'" test'
'" test'
```


### Output 2
```
indent-style = space
Expand Down Expand Up @@ -496,4 +509,17 @@ trailing_preferred_quote_texts = [''' "''', ''' ""''', ''' """''', ''' """"''']
```


#### Preview changes
```diff
--- Stable
+++ Preview
@@ -1,4 +1,5 @@
"' test"
+
'" test'
'" test'
```



Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,78 @@ def f():
```


#### Preview changes
```diff
--- Stable
+++ Preview
@@ -1,13 +1,13 @@
"""
Black's `Preview.module_docstring_newlines`
"""
+
first_stmt_after_module_level_docstring = 1
class CachedRepository:
# Black's `Preview.dummy_implementations`
- def get_release_info(self):
- ...
+ def get_release_info(self): ...
def raw_docstring():
@@ -27,23 +27,22 @@
class RemoveNewlineBeforeClassDocstring:
-
"""Black's `Preview.no_blank_line_before_class_docstring`"""
def f():
"""Black's `Preview.prefer_splitting_right_hand_side_of_assignments`"""
- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
- bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
- ] = cccccccc.ccccccccccccc.cccccccc
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
+ cccccccc.ccccccccccccc.cccccccc
+ )
- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
- bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
- ] = cccccccc.ccccccccccccc().cccccccc
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
+ cccccccc.ccccccccccccc().cccccccc
+ )
- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
- bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
- ] = cccccccc.ccccccccccccc(d).cccccccc
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
+ cccccccc.ccccccccccccc(d).cccccccc
+ )
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
cccccccc.ccccccccccccc(d).cccccccc + e
@@ -57,9 +56,9 @@
+ eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
)
- self._cache: dict[
- DependencyCacheKey, list[list[DependencyPackage]]
- ] = collections.defaultdict(list)
- self._cached_dependencies_by_level: dict[
- int, list[DependencyCacheKey]
- ] = collections.defaultdict(list)
+ self._cache: dict[DependencyCacheKey, list[list[DependencyPackage]]] = (
+ collections.defaultdict(list)
+ )
+ self._cached_dependencies_by_level: dict[int, list[DependencyCacheKey]] = (
+ collections.defaultdict(list)
+ )
```


### Output 2
```
indent-style = space
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ def docstring_single():
```


#### Preview changes
```diff
--- Stable
+++ Preview
@@ -1,4 +1,5 @@
'single'
+
'double'
r'r single'
r'r double'
```


### Output 2
```
indent-style = space
Expand Down Expand Up @@ -201,6 +214,19 @@ def docstring_single():
```


#### Preview changes
```diff
--- Stable
+++ Preview
@@ -1,4 +1,5 @@
"single"
+
"double"
r"r single"
r"r double"
```


### Output 3
```
indent-style = space
Expand Down Expand Up @@ -273,4 +299,17 @@ def docstring_single():
```


#### Preview changes
```diff
--- Stable
+++ Preview
@@ -1,4 +1,5 @@
'single'
+
"double"
r'r single'
r"r double"
```



0 comments on commit d835b28

Please sign in to comment.