Use inline flags for test cases (#3931)
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
This commit is contained in:
parent
715f60c11b
commit
a69bda3b9b
@ -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`.
|
||||
|
@ -1,3 +1,4 @@
|
||||
# flags: --skip-string-normalization
|
||||
class ALonelyClass:
|
||||
'''
|
||||
A multiline class docstring.
|
@ -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."""
|
||||
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview --minimum-version=3.10
|
||||
# normal, short, function definition
|
||||
def foo(a, b) -> tuple[int, float]: ...
|
||||
|
@ -1,3 +1,4 @@
|
||||
# flags: --pyi
|
||||
def f(): # type: ignore
|
||||
...
|
||||
|
@ -1,3 +1,4 @@
|
||||
# flags: --line-length=6
|
||||
# Regression test for #3427, which reproes only with line length <= 6
|
||||
def f():
|
||||
"""
|
@ -1,3 +1,4 @@
|
||||
# flags: --pyi --preview
|
||||
import sys
|
||||
|
||||
class Outer:
|
@ -1,5 +1,3 @@
|
||||
#!/usr/bin/env python3.6
|
||||
|
||||
x = 123456789
|
||||
x = 123456
|
||||
x = .1
|
||||
@ -21,9 +19,6 @@
|
||||
|
||||
# output
|
||||
|
||||
|
||||
#!/usr/bin/env python3.6
|
||||
|
||||
x = 123456789
|
||||
x = 123456
|
||||
x = 0.1
|
@ -1,5 +1,3 @@
|
||||
#!/usr/bin/env python3.6
|
||||
|
||||
x = 123456789
|
||||
x = 1_2_3_4_5_6_7
|
||||
x = 1E+1
|
||||
@ -11,8 +9,6 @@
|
||||
|
||||
# output
|
||||
|
||||
#!/usr/bin/env python3.6
|
||||
|
||||
x = 123456789
|
||||
x = 1_2_3_4_5_6_7
|
||||
x = 1e1
|
@ -1,3 +1,4 @@
|
||||
# flags: --minimum-version=3.10
|
||||
with (CtxManager() as example):
|
||||
...
|
||||
|
@ -1,3 +1,4 @@
|
||||
# flags: --minimum-version=3.10
|
||||
# Cases sampled from Lib/test/test_patma.py
|
||||
|
||||
# case black_test_patma_098
|
@ -1,3 +1,4 @@
|
||||
# flags: --minimum-version=3.10
|
||||
import match
|
||||
|
||||
match something:
|
@ -1,3 +1,4 @@
|
||||
# flags: --minimum-version=3.10
|
||||
re.match()
|
||||
match = a
|
||||
with match() as match:
|
@ -1,3 +1,4 @@
|
||||
# flags: --minimum-version=3.10
|
||||
# Cases sampled from PEP 636 examples
|
||||
|
||||
match command.split():
|
@ -1,3 +1,4 @@
|
||||
# flags: --minimum-version=3.10
|
||||
match something:
|
||||
case b(): print(1+1)
|
||||
case c(
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview --minimum-version=3.10
|
||||
# This has always worked
|
||||
z= Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong
|
||||
|
@ -1,3 +1,4 @@
|
||||
# flags: --minimum-version=3.8
|
||||
def positional_only_arg(a, /):
|
||||
pass
|
||||
|
@ -1,3 +1,4 @@
|
||||
# flags: --minimum-version=3.8
|
||||
(a := 1)
|
||||
(a := a)
|
||||
if (match := pattern.search(data)) is None:
|
@ -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
|
@ -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]
|
@ -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}
|
@ -1,3 +1,4 @@
|
||||
# flags: --minimum-version=3.8
|
||||
if (foo := 0):
|
||||
pass
|
||||
|
@ -1,3 +1,4 @@
|
||||
# flags: --minimum-version=3.11
|
||||
A[*b]
|
||||
A[*b] = 1
|
||||
A
|
@ -1,3 +1,4 @@
|
||||
# flags: --minimum-version=3.11
|
||||
try:
|
||||
raise OSError("blah")
|
||||
except* ExceptionGroup as e:
|
@ -1,3 +1,4 @@
|
||||
# flags: --minimum-version=3.11
|
||||
try:
|
||||
raise OSError("blah")
|
||||
except * ExceptionGroup as e:
|
@ -1,3 +1,4 @@
|
||||
# flags: --line-length=0
|
||||
importA;()<<0**0#
|
||||
|
||||
# output
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview
|
||||
async def func() -> (int):
|
||||
return 0
|
||||
|
@ -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
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview
|
||||
from .config import (
|
||||
Any,
|
||||
Bool,
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview --minimum-version=3.8
|
||||
with \
|
||||
make_context_manager1() as cm1, \
|
||||
make_context_manager2() as cm2, \
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview --minimum-version=3.9
|
||||
with \
|
||||
make_context_manager1() as cm1, \
|
||||
make_context_manager2() as cm2, \
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview --minimum-version=3.10
|
||||
# This file uses pattern matching introduced in Python 3.10.
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview --minimum-version=3.11
|
||||
# This file uses except* clause in Python 3.11.
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview
|
||||
# This file doesn't use any Python 3.9+ only grammars.
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview --minimum-version=3.9
|
||||
# This file uses parenthesized context managers introduced in Python 3.9.
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview
|
||||
from typing import NoReturn, Protocol, Union, overload
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview
|
||||
x = "\x1F"
|
||||
x = "\\x1B"
|
||||
x = "\\\x1B"
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview
|
||||
my_dict = {
|
||||
"something_something":
|
||||
r"Lorem ipsum dolor sit amet, an sed convenire eloquentiam \t"
|
@ -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."
|
@ -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).
|
@ -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?"
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview
|
||||
class A:
|
||||
def foo():
|
||||
result = type(message)("")
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview
|
||||
def func(
|
||||
arg1,
|
||||
arg2,
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview
|
||||
"""cow
|
||||
say""",
|
||||
call(3, "dogsay", textwrap.dedent("""dove
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview
|
||||
def line_before_docstring():
|
||||
|
||||
"""Please move me up"""
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview
|
||||
x[(a:=0):]
|
||||
x[:(a:=0)]
|
||||
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview
|
||||
("" % a) ** 2
|
||||
("" % a)[0]
|
||||
("" % a)()
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview
|
||||
first_item, second_item = (
|
||||
some_looooooooong_module.some_looooooooooooooong_function_name(
|
||||
first_argument, second_argument, third_argument
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview
|
||||
# Long string example
|
||||
def frobnicate() -> "ThisIsTrulyUnreasonablyExtremelyLongClassName | list[ThisIsTrulyUnreasonablyExtremelyLongClassName]":
|
||||
pass
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview
|
||||
e = {
|
||||
"a": fun(msg, "ts"),
|
||||
"longggggggggggggggid": ...,
|
@ -1,3 +1,4 @@
|
||||
# flags: --preview --minimum-version=3.10
|
||||
x[a:=0]
|
||||
x[a := 0]
|
||||
x[a := 0, b := 1]
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user