def test_string_quotes(self) -> None: source, expected = read_data("string_quotes") actual = fs(source) self.assertFormatEqual(expected, actual) assert_equivalent(source, actual) assert_stable(source, actual, FileMode()) mode = FileMode(string_normalization=False) not_normalized = fs(source, mode=mode) self.assertFormatEqual(source, not_normalized) assert_equivalent(source, not_normalized) assert_stable(source, not_normalized, mode=mode)
def test_numeric_literals_ignoring_underscores(self) -> None: source, expected = read_data("numeric_literals_skip_underscores") mode = FileMode(target_versions=PY36_VERSIONS) actual = fs(source, mode=mode) self.assertFormatEqual(expected, actual) assert_equivalent(source, actual) assert_stable(source, actual, mode)
def test_pep_570(self) -> None: source, expected = read_data("pep_570") actual = fs(source) self.assertFormatEqual(expected, actual) assert_stable(source, actual, FileMode()) if sys.version_info >= (3, 8): assert_equivalent(source, actual)
def test_python2_print_function(self) -> None: source, expected = read_data("python2_print_function") mode = FileMode(target_versions={TargetVersion.PY27}) actual = fs(source, mode=mode) self.assertFormatEqual(expected, actual) assert_equivalent(source, actual) assert_stable(source, actual, mode)
def test_expression_ff(self) -> None: source, expected = read_data("expression") with TempFile() as tmp_file: self.assertTrue(ff(tmp_file)) actual = tmp_file.read() self.assertFormatEqual(expected, actual) with patch("dump_to_file", dump_to_stderr): assert_equivalent(source, actual) assert_stable(source, actual, FileMode())
def test_format_file_contents(self) -> None: empty = "" mode = FileMode() with self.assertRaises(NothingChanged): format_file_contents(empty, mode=mode, fast=False) just_nl = "\n" with self.assertRaises(NothingChanged): format_file_contents(just_nl, mode=mode, fast=False) same = "j = [1, 2, 3]\n" with self.assertRaises(NothingChanged): format_file_contents(same, mode=mode, fast=False) different = "j = [1,2,3]" expected = same actual = format_file_contents(different, mode=mode, fast=False) self.assertEqual(expected, actual) invalid = "return if you can" with self.assertRaises(InvalidInput) as e: format_file_contents(invalid, mode=mode, fast=False) self.assertEqual(str(e.exception), "Cannot parse: 1:7: return if you can")
def test_bracket_match(self) -> None: source, expected = read_data("bracketmatch") actual = fs(source) self.assertFormatEqual(expected, actual) assert_equivalent(source, actual) assert_stable(source, actual, FileMode())
def test_new_line_between_class_and_code(self) -> None: source, expected = read_data("class_methods_new_line") actual = fs(source) self.assertFormatEqual(expected, actual) assert_equivalent(source, actual) assert_stable(source, actual, FileMode())
def test_remove_empty_parentheses_after_class(self) -> None: source, expected = read_data("class_blank_parentheses") actual = fs(source) self.assertFormatEqual(expected, actual) assert_equivalent(source, actual) assert_stable(source, actual, FileMode())
def test_stub(self) -> None: mode = FileMode(is_pyi=True) source, expected = read_data("stub.pyi") actual = fs(source, mode=mode) self.assertFormatEqual(expected, actual) assert_stable(source, actual, mode)
def test_python2_unicode_literals(self) -> None: source, expected = read_data("python2_unicode_literals") actual = fs(source) self.assertFormatEqual(expected, actual) assert_equivalent(source, actual) assert_stable(source, actual, FileMode())
def test_empty(self) -> None: source = expected = "" actual = fs(source) self.assertFormatEqual(expected, actual) assert_equivalent(source, actual) assert_stable(source, actual, FileMode())
def test_comment_after_escaped_newline(self) -> None: source, expected = read_data("comment_after_escaped_newline") actual = fs(source) self.assertFormatEqual(expected, actual) assert_equivalent(source, actual) assert_stable(source, actual, FileMode())
def test_function_trailing_comma(self) -> None: source, expected = read_data("function_trailing_comma") actual = fs(source) self.assertFormatEqual(expected, actual) assert_equivalent(source, actual) assert_stable(source, actual, FileMode())
def test_beginning_backslash(self) -> None: source, expected = read_data("beginning_backslash") actual = fs(source) self.assertFormatEqual(expected, actual) assert_equivalent(source, actual) assert_stable(source, actual, FileMode())
def test_deep(self) -> None: source, expected = read_data("deep") actual = fs(source) self.assertFormatEqual(expected, actual) assert_equivalent(source, actual) assert_stable(source, actual, FileMode())
def test_numeric_literals_py2(self) -> None: source, expected = read_data("numeric_literals_py2") actual = fs(source) self.assertFormatEqual(expected, actual) assert_stable(source, actual, FileMode())
def test_tricky_unicode_symbols(self) -> None: source, expected = read_data("tricky_unicode_symbols") actual = fs(source) self.assertFormatEqual(expected, actual) assert_equivalent(source, actual) assert_stable(source, actual, FileMode())
dump_to_file, NothingChanged, format_file_contents, InvalidInput, WriteBack, main, ) from click.testing import CliRunner from mo_files import TempFile from pcf import format_file_in_place, format_str from tests.utils import assert_equivalent, assert_stable, FileMode, assert_close_enough CLOSE_ENOUGH = True ff = partial(format_file_in_place, mode=FileMode(), fast=True) fs = partial(format_str, mode=FileMode()) THIS_FILE = Path(__file__) THIS_DIR = THIS_FILE.parent EMPTY_LINE = "# EMPTY LINE WITH WHITESPACE" + " (this comment will be removed)" def dump_to_stderr(*output: str) -> str: return "\n" + "\n".join(output) + "\n" def read_data(name: str, data: bool = True) -> Tuple[str, str]: """read_data('test_name') -> 'input', 'output'""" if not name.endswith((".py", ".pyi", ".out", ".diff")): name += ".py" _input: List[str] = []