Skip to content

Commit

Permalink
Use inline flags for test cases (#3931)
Browse files Browse the repository at this point in the history
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
  • Loading branch information
JelleZijlstra and hauntsaninja committed Oct 10, 2023
1 parent 715f60c commit a69bda3
Show file tree
Hide file tree
Showing 132 changed files with 206 additions and 220 deletions.
21 changes: 20 additions & 1 deletion docs/contributing/the_basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,26 @@ Further examples of invoking the tests
(.venv)$ tox -e py -- --print-tree-diff=False
```

`Black` has two pytest command-line options affecting test files in `tests/data/` that
### Testing

All aspects of the _Black_ style should be tested. Normally, tests should be created as
files in the `tests/data/cases` directory. These files consist of up to three parts:

- A line that starts with `# flags: ` followed by a set of command-line options. For
example, if the line is `# flags: --preview --skip-magic-trailing-comma`, the test
case will be run with preview mode on and the magic trailing comma off. The options
accepted are mostly a subset of those of _Black_ itself, except for the
`--minimum-version=` flag, which should be used when testing a grammar feature that
works only in newer versions of Python. This flag ensures that we don't try to
validate the AST on older versions and tests that we autodetect the Python version
correctly when the feature is used. For the exact flags accepted, see the function
`get_flags_parser` in `tests/util.py`. If this line is omitted, the default options
are used.
- A block of Python code used as input for the formatter.
- The line `# output`, followed by the output of _Black_ when run on the previous block.
If this is omitted, the test asserts that _Black_ will leave the input code unchanged.

_Black_ has two pytest command-line options affecting test files in `tests/data/` that
are split into an input part, and an output part, separated by a line with`# output`.
These can be passed to `pytest` through `tox`, or directly into pytest if not using
`tox`.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --skip-string-normalization
class ALonelyClass:
'''
A multiline class docstring.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview --skip-string-normalization
def do_not_touch_this_prefix():
R"""There was a bug where docstring prefixes would be normalized even with -S."""

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview --minimum-version=3.10
# normal, short, function definition
def foo(a, b) -> tuple[int, float]: ...

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --pyi
def f(): # type: ignore
...

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --line-length=6
# Regression test for #3427, which reproes only with line length <= 6
def f():
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --pyi --preview
import sys

class Outer:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python3.6

x = 123456789
x = 123456
x = .1
Expand All @@ -21,9 +19,6 @@

# output


#!/usr/bin/env python3.6

x = 123456789
x = 123456
x = 0.1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python3.6

x = 123456789
x = 1_2_3_4_5_6_7
x = 1E+1
Expand All @@ -11,8 +9,6 @@

# output

#!/usr/bin/env python3.6

x = 123456789
x = 1_2_3_4_5_6_7
x = 1e1
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.10
with (CtxManager() as example):
...

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.10
# Cases sampled from Lib/test/test_patma.py

# case black_test_patma_098
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.10
import match

match something:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.10
re.match()
match = a
with match() as match:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.10
# Cases sampled from PEP 636 examples

match command.split():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.10
match something:
case b(): print(1+1)
case c(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview --minimum-version=3.10
# This has always worked
z= Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.8
def positional_only_arg(a, /):
pass

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.8
(a := 1)
(a := a)
if (match := pattern.search(data)) is None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --fast
# Most of the following examples are really dumb, some of them aren't even accepted by Python,
# we're fixing them only so fuzzers (which follow the grammar which actually allows these
# examples matter of fact!) don't yell at us :p
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.10
# Unparenthesized walruses are now allowed in indices since Python 3.10.
x[a:=0]
x[a:=0, b:=1]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.9
# Unparenthesized walruses are now allowed in set literals & set comprehensions
# since Python 3.9
{x := 1, 2, 3}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.8
if (foo := 0):
pass

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.11
A[*b]
A[*b] = 1
A
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.11
try:
raise OSError("blah")
except* ExceptionGroup as e:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.11
try:
raise OSError("blah")
except * ExceptionGroup as e:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --line-length=0
importA;()<<0**0#

# output
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
async def func() -> (int):
return 0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
# long variable name
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = 0
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = 1 # with a comment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
from .config import (
Any,
Bool,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview --minimum-version=3.8
with \
make_context_manager1() as cm1, \
make_context_manager2() as cm2, \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview --minimum-version=3.9
with \
make_context_manager1() as cm1, \
make_context_manager2() as cm2, \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview --minimum-version=3.10
# This file uses pattern matching introduced in Python 3.10.


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview --minimum-version=3.11
# This file uses except* clause in Python 3.11.


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
# This file doesn't use any Python 3.9+ only grammars.


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview --minimum-version=3.9
# This file uses parenthesized context managers introduced in Python 3.9.


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
from typing import NoReturn, Protocol, Union, overload


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
x = "\x1F"
x = "\\x1B"
x = "\\\x1B"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
my_dict = {
"something_something":
r"Lorem ipsum dolor sit amet, an sed convenire eloquentiam \t"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
x = "This is a really long string that can't possibly be expected to fit all together on one line. In fact it may even take up three or more lines... like four or five... but probably just three."

x += "This is a really long string that can't possibly be expected to fit all together on one line. In fact it may even take up three or more lines... like four or five... but probably just three."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
# The following strings do not have not-so-many chars, but are long enough
# when these are rendered in a monospace font (if the renderer respects
# Unicode East Asian Width properties).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
some_variable = "This string is long but not so long that it needs to be split just yet"
some_variable = 'This string is long but not so long that it needs to be split just yet'
some_variable = "This string is long, just long enough that it needs to be split, u get?"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
class A:
def foo():
result = type(message)("")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
def func(
arg1,
arg2,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
"""cow
say""",
call(3, "dogsay", textwrap.dedent("""dove
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
def line_before_docstring():

"""Please move me up"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
x[(a:=0):]
x[:(a:=0)]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
("" % a) ** 2
("" % a)[0]
("" % a)()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
first_item, second_item = (
some_looooooooong_module.some_looooooooooooooong_function_name(
first_argument, second_argument, third_argument
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
# Long string example
def frobnicate() -> "ThisIsTrulyUnreasonablyExtremelyLongClassName | list[ThisIsTrulyUnreasonablyExtremelyLongClassName]":
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview
e = {
"a": fun(msg, "ts"),
"longggggggggggggggid": ...,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --preview --minimum-version=3.10
x[a:=0]
x[a := 0]
x[a := 0, b := 1]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python3.7
# flags: --minimum-version=3.7


def f():
Expand Down Expand Up @@ -33,9 +33,6 @@ def make_arange(n):
# output


#!/usr/bin/env python3.7


def f():
return (i * 2 async for i in arange(42))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python3.8
# flags: --minimum-version=3.8


def starred_return():
Expand All @@ -22,9 +22,6 @@ def t():
# output


#!/usr/bin/env python3.8


def starred_return():
my_list = ["value2", "value3"]
return "value1", *my_list
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python3.9
# flags: --minimum-version=3.9

@relaxed_decorator[0]
def f():
Expand All @@ -14,10 +14,6 @@ def f():

# output


#!/usr/bin/env python3.9


@relaxed_decorator[0]
def f():
...
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.10
def http_status(status):

match status:
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.9
with (open("bla.txt")):
pass

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --skip-magic-trailing-comma
# We should not remove the trailing comma in a single-element subscript.
a: tuple[int,]
b = tuple[int,]
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.10
for x in *a, *b:
print(x)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --pyi
X: int

def f(): ...
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.12
type A=int
type Gen[T]=list[T]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --minimum-version=3.12
def func [T ](): pass
async def func [ T ] (): pass
class C[ T ] : pass
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions tests/data/miscellaneous/force_pyi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flags: --pyi
from typing import Union

@bird
Expand Down

0 comments on commit a69bda3

Please sign in to comment.