Skip to content

Commit

Permalink
Add test cases for match statements
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Aug 5, 2023
1 parent 73a6595 commit a755afe
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# leading match comment
match foo: # dangling match comment
case "bar":
pass


# leading match comment
match ( # leading expr comment
# another leading expr comment
foo # trailing expr comment
# another trailing expr comment
): # dangling match comment
case "bar":
pass


# leading match comment
match ( # hello
foo # trailing expr comment
, # another
): # dangling match comment
case "bar":
pass


match [ # comment
first,
second,
third
]: # another comment
case ["a", "b", "c"]:
pass

match ( # comment
"a b c"
).split(): # another comment
case ["a", "b", "c"]:
pass


match ( # comment
# let's go
yield foo
): # another comment
case ["a", "b", "c"]:
pass


match aaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh: # comment
case "sshhhhhhhh":
pass


def foo():
match inside_func: # comment
case "bar":
pass
4 changes: 2 additions & 2 deletions crates/ruff_python_formatter/src/statement/stmt_match.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ruff_formatter::{format_args, write, Buffer, FormatResult};
use ruff_python_ast::StmtMatch;

use crate::comments::trailing_comments;
use crate::comments::dangling_comments;
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::not_yet_implemented_custom_text;
Expand Down Expand Up @@ -29,7 +29,7 @@ impl FormatNodeRule<StmtMatch> for FormatStmtMatch {
space(),
maybe_parenthesize_expression(subject, item, Parenthesize::IfBreaks),
text(":"),
trailing_comments(dangling_item_comments)
dangling_comments(dangling_item_comments)
]
)?;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py
---
## Input
```py
# leading match comment
match foo: # dangling match comment
case "bar":
pass
# leading match comment
match ( # leading expr comment
# another leading expr comment
foo # trailing expr comment
# another trailing expr comment
): # dangling match comment
case "bar":
pass
# leading match comment
match ( # hello
foo # trailing expr comment
, # another
): # dangling match comment
case "bar":
pass
match [ # comment
first,
second,
third
]: # another comment
case ["a", "b", "c"]:
pass
match ( # comment
"a b c"
).split(): # another comment
case ["a", "b", "c"]:
pass
match ( # comment
# let's go
yield foo
): # another comment
case ["a", "b", "c"]:
pass
match aaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh: # comment
case "sshhhhhhhh":
pass
def foo():
match inside_func: # comment
case "bar":
pass
```

## Output
```py
# leading match comment
match foo: # dangling match comment
case NOT_YET_IMPLEMENTED_MatchCase:
pass
# leading match comment
match (
# leading expr comment
# another leading expr comment
foo # trailing expr comment
# another trailing expr comment
): # dangling match comment
case NOT_YET_IMPLEMENTED_MatchCase:
pass
# leading match comment
match ( # hello
foo, # trailing expr comment # another
): # dangling match comment
case NOT_YET_IMPLEMENTED_MatchCase:
pass
match [first, second, third]: # another comment # comment
case NOT_YET_IMPLEMENTED_MatchCase:
pass
match (
# comment
"a b c"
).split(): # another comment
case NOT_YET_IMPLEMENTED_MatchCase:
pass
match (
# comment
# let's go
yield foo
): # another comment
case NOT_YET_IMPLEMENTED_MatchCase:
pass
match aaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh: # comment
case NOT_YET_IMPLEMENTED_MatchCase:
pass
def foo():
match inside_func: # comment
case NOT_YET_IMPLEMENTED_MatchCase:
pass
```



0 comments on commit a755afe

Please sign in to comment.