Use cmakelang formatter (#52)
This commit is contained in:
@@ -1,168 +0,0 @@
|
||||
import sys
|
||||
from contextlib import contextmanager
|
||||
from io import StringIO
|
||||
from pathlib import Path
|
||||
from typing import Callable, Iterator
|
||||
|
||||
from cmake_language_server.formatter import Formatter, main
|
||||
from cmake_language_server.parser import ListParser
|
||||
from pytest import CaptureFixture
|
||||
|
||||
|
||||
def make_formatter_test(liststr: str, expect: str) -> Callable[[], None]:
|
||||
def test() -> None:
|
||||
tokens, remain = ListParser().parse(liststr)
|
||||
actual = Formatter().format(tokens)
|
||||
assert actual == expect
|
||||
|
||||
return test
|
||||
|
||||
|
||||
test_command = make_formatter_test("a()", "a()\n")
|
||||
test_command_tolower = make_formatter_test("A()", "a()\n")
|
||||
test_remove_space = make_formatter_test(
|
||||
"""
|
||||
#a
|
||||
b ( c ) # d
|
||||
""",
|
||||
"""\
|
||||
#a
|
||||
b(c) # d
|
||||
""",
|
||||
)
|
||||
test_indent_if = make_formatter_test(
|
||||
"""
|
||||
if()
|
||||
a() # a
|
||||
else()
|
||||
# b
|
||||
b()
|
||||
endif()
|
||||
""",
|
||||
"""\
|
||||
if()
|
||||
a() # a
|
||||
else()
|
||||
# b
|
||||
b()
|
||||
endif()
|
||||
""",
|
||||
)
|
||||
test_indent_if_nested = make_formatter_test(
|
||||
"""
|
||||
if()
|
||||
if()
|
||||
a()
|
||||
b()
|
||||
endif()
|
||||
endif()
|
||||
""",
|
||||
"""\
|
||||
if()
|
||||
if()
|
||||
a()
|
||||
b()
|
||||
endif()
|
||||
endif()
|
||||
""",
|
||||
)
|
||||
test_argument = make_formatter_test("a( b c d)", "a(b c d)\n")
|
||||
test_argument_multiline = make_formatter_test(
|
||||
"""
|
||||
if()
|
||||
a(b c
|
||||
d # e
|
||||
f
|
||||
# g
|
||||
) # h
|
||||
endif()
|
||||
""",
|
||||
"""\
|
||||
if()
|
||||
a(
|
||||
b c
|
||||
d # e
|
||||
f
|
||||
# g
|
||||
) # h
|
||||
endif()
|
||||
""",
|
||||
)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def mock_stdin(buf: str) -> Iterator[None]:
|
||||
stdin = sys.stdin
|
||||
sys.stdin = StringIO(buf)
|
||||
yield
|
||||
sys.stdin = stdin
|
||||
|
||||
|
||||
def test_main_stdin(capsys: CaptureFixture[str]) -> None:
|
||||
with mock_stdin(" a()"):
|
||||
main([])
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == "a()\n"
|
||||
assert captured.err == ""
|
||||
|
||||
|
||||
def test_main_stdin_diff(capsys: CaptureFixture[str]) -> None:
|
||||
with mock_stdin(" a()"):
|
||||
main(["-d"])
|
||||
captured = capsys.readouterr()
|
||||
assert "- a()" in captured.out
|
||||
assert "+a()" in captured.out
|
||||
assert captured.err == ""
|
||||
|
||||
|
||||
def test_main_file_1(capsys: CaptureFixture[str], tmp_path: Path) -> None:
|
||||
testfile1 = tmp_path / "list1.cmake"
|
||||
with testfile1.open("w") as fp:
|
||||
fp.write(" a()")
|
||||
|
||||
main([str(testfile1)])
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == "a()\n"
|
||||
assert captured.err == ""
|
||||
|
||||
|
||||
def test_main_file_2(capsys: CaptureFixture[str], tmp_path: Path) -> None:
|
||||
testfile1 = tmp_path / "list1.cmake"
|
||||
with testfile1.open("w") as fp:
|
||||
fp.write(" a()")
|
||||
testfile2 = tmp_path / "list2.cmake"
|
||||
with testfile2.open("w") as fp:
|
||||
fp.write(" b()")
|
||||
|
||||
main([str(testfile1), str(testfile2)])
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == "a()\nb()\n"
|
||||
assert captured.err == ""
|
||||
|
||||
|
||||
def test_main_inplace(capsys: CaptureFixture[str], tmp_path: Path) -> None:
|
||||
testfile1 = tmp_path / "list1.cmake"
|
||||
with testfile1.open("w") as fp:
|
||||
fp.write(" a()")
|
||||
|
||||
main(["-i", str(testfile1)])
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == ""
|
||||
assert captured.err == ""
|
||||
|
||||
with testfile1.open() as fp:
|
||||
content = fp.read()
|
||||
assert content == "a()\n"
|
||||
|
||||
|
||||
def test_main_diff(capsys: CaptureFixture[str], tmp_path: Path) -> None:
|
||||
testfile1 = tmp_path / "list1.cmake"
|
||||
with testfile1.open("w") as fp:
|
||||
fp.write(" a()")
|
||||
|
||||
main(["-d", str(testfile1)])
|
||||
captured = capsys.readouterr()
|
||||
assert str(testfile1) in captured.out
|
||||
assert "- a()" in captured.out
|
||||
assert "+a()" in captured.out
|
||||
assert captured.err == ""
|
||||
@@ -1,83 +0,0 @@
|
||||
from typing import Callable, List
|
||||
|
||||
from cmake_language_server.parser import ListParser, TokenType
|
||||
|
||||
|
||||
def make_parser_test(
|
||||
liststr: str, expect_token: List[TokenType], expect_remain: str = ""
|
||||
) -> Callable[[], None]:
|
||||
def test() -> None:
|
||||
actual_token, actual_remain = ListParser().parse(liststr)
|
||||
assert actual_token == expect_token
|
||||
assert actual_remain == expect_remain
|
||||
|
||||
return test
|
||||
|
||||
|
||||
test_command_no_args = make_parser_test("a()", [("a", [])])
|
||||
test_command_space = make_parser_test(" a ()", [" ", ("a", [])])
|
||||
test_command_arg = make_parser_test("a(b)", [("a", ["b"])])
|
||||
test_command_arg_space = make_parser_test("a ( b )", [("a", ["b"])])
|
||||
test_command_arg_escape = make_parser_test(r"a(\n\")", [("a", [r"\n\""])])
|
||||
test_command_arg_paren = make_parser_test("a((b))", [("a", ["(", "b", ")"])])
|
||||
test_command_arg_paren_paren = make_parser_test(
|
||||
"a(((b)))", [("a", ["(", "(", "b", ")", ")"])]
|
||||
)
|
||||
test_command_arg_quote = make_parser_test(r'a("b\"")', [("a", [r'"b\""'])])
|
||||
test_command_arg_quote_cont = make_parser_test('a("\\\n")', [("a", ['"\\\n"'])])
|
||||
test_command_arg_quo_multiline = make_parser_test(
|
||||
"""a("b
|
||||
c
|
||||
")""",
|
||||
[("a", ['"b\nc\n"'])],
|
||||
)
|
||||
test_command_arg_bracket_0 = make_parser_test("a([[b]])", [("a", ["[[b]]"])])
|
||||
test_command_arg_bracket_1 = make_parser_test("a([=[b]=])", [("a", ["[=[b]=]"])])
|
||||
test_command_arg_space = make_parser_test("a ( b )", [("a", [" ", "b", " "])])
|
||||
test_command_arg_multi = make_parser_test("a(b c)", [("a", ["b", " ", "c"])])
|
||||
test_command_multielement = make_parser_test(
|
||||
"""a(
|
||||
b
|
||||
c # c
|
||||
)""",
|
||||
[("a", ["\n", " ", "b", "\n", " ", "c", " ", "# c", "\n"])],
|
||||
)
|
||||
test_line_comment = make_parser_test("a() # b # c", [("a", []), " ", "# b # c"])
|
||||
test_bracket_comment = make_parser_test("#[[a]]#[[b]]", ["#[[a]]", "#[[b]]"])
|
||||
test_bracket_comment_nested = make_parser_test("#[=[[[a]]]=]", ["#[=[[[a]]]=]"])
|
||||
test_bracket_comment_multiline = make_parser_test(
|
||||
"#[[\na\nb\nc\n]]", ["#[[\na\nb\nc\n]]"]
|
||||
)
|
||||
test_if_block = make_parser_test(
|
||||
"""if()
|
||||
a()
|
||||
else()
|
||||
b()
|
||||
endif()""",
|
||||
[
|
||||
("if", []),
|
||||
"\n",
|
||||
" ",
|
||||
("a", []),
|
||||
"\n",
|
||||
("else", []),
|
||||
"\n",
|
||||
" ",
|
||||
("b", []),
|
||||
"\n",
|
||||
("endif", []),
|
||||
],
|
||||
)
|
||||
test_comment_multi_linecomment = make_parser_test(
|
||||
"""a()# a
|
||||
b() # b
|
||||
c() # c""",
|
||||
[("a", []), "# a", "\n", ("b", []), " ", "# b", "\n", ("c", []), " ", "# c"],
|
||||
)
|
||||
|
||||
test_incomplete_id = make_parser_test("a", [], "a")
|
||||
test_incomplete_command = make_parser_test("a(", [], "a(")
|
||||
test_incomplete_id_after_command = make_parser_test("a()\nb", [("a", []), "\n"], "b")
|
||||
test_incomplete_command_after_command = make_parser_test(
|
||||
"a()\nb(c", [("a", []), "\n"], "b(c"
|
||||
)
|
||||
Reference in New Issue
Block a user