mirror of
https://github.com/Instagram/LibCST.git
synced 2025-12-23 10:35:53 +00:00
- Make INDENT/DEDENT dummy tokens more readable instead of outputting an empty string for them. - Sort the "expected" list to make testing easier. - Don't show the list of expected values if there are > 10 possibilities. The output becomes impossible to read at that point. - Try to avoid using an empty line for the displayed contextual line of source code. Instead, point at the line above it, or just don't output any context.
94 lines
2.9 KiB
Python
94 lines
2.9 KiB
Python
# Copyright (c) Facebook, Inc. and its affiliates.
|
|
#
|
|
# This source code is licensed under the MIT license found in the
|
|
# LICENSE file in the root directory of this source tree.
|
|
|
|
# pyre-strict
|
|
|
|
import pickle
|
|
from textwrap import dedent
|
|
|
|
import libcst as cst
|
|
from libcst.testing.utils import UnitTest, data_provider
|
|
|
|
|
|
class ExceptionsTest(UnitTest):
|
|
@data_provider(
|
|
{
|
|
"simple": (
|
|
cst.ParserSyntaxError(
|
|
"some message", lines=["abcd"], raw_line=1, raw_column=0
|
|
),
|
|
dedent(
|
|
"""
|
|
Syntax Error @ 1:1.
|
|
some message
|
|
|
|
abcd
|
|
^
|
|
"""
|
|
).strip(),
|
|
),
|
|
"tab_expansion": (
|
|
cst.ParserSyntaxError(
|
|
"some message", lines=["\tabcd\r\n"], raw_line=1, raw_column=2
|
|
),
|
|
dedent(
|
|
"""
|
|
Syntax Error @ 1:10.
|
|
some message
|
|
|
|
abcd
|
|
^
|
|
"""
|
|
).strip(),
|
|
),
|
|
"shows_last_line_with_text": (
|
|
cst.ParserSyntaxError(
|
|
"some message",
|
|
lines=["abcd\n", "efgh\n", "\n", "\n", "\n", "\n", "\n"],
|
|
raw_line=5,
|
|
raw_column=0,
|
|
),
|
|
dedent(
|
|
"""
|
|
Syntax Error @ 5:1.
|
|
some message
|
|
|
|
efgh
|
|
^
|
|
"""
|
|
).strip(),
|
|
),
|
|
"empty_file": (
|
|
cst.ParserSyntaxError(
|
|
"some message", lines=[""], raw_line=1, raw_column=0
|
|
),
|
|
dedent(
|
|
"""
|
|
Syntax Error @ 1:1.
|
|
some message
|
|
"""
|
|
# There's no code snippet here because the input file was empty.
|
|
).strip(),
|
|
),
|
|
}
|
|
)
|
|
def test_parser_syntax_error_str(
|
|
self, err: cst.ParserSyntaxError, expected: str
|
|
) -> None:
|
|
self.assertEqual(str(err), expected)
|
|
|
|
def test_pickle(self) -> None:
|
|
"""
|
|
It's common to use LibCST with multiprocessing to process files in parallel.
|
|
Multiprocessing uses pickle by default, so we should make sure our errors can be
|
|
pickled/unpickled.
|
|
"""
|
|
orig_exception = cst.ParserSyntaxError(
|
|
"some message", lines=["abcd"], raw_line=1, raw_column=0
|
|
)
|
|
pickled_blob = pickle.dumps(orig_exception)
|
|
new_exception = pickle.loads(pickled_blob)
|
|
self.assertEqual(repr(orig_exception), repr(new_exception))
|
|
self.assertEqual(str(orig_exception), str(new_exception))
|