Initial commit

This commit is contained in:
Regen
2019-11-12 01:57:28 +09:00
commit b6916f1082
20 changed files with 1392 additions and 0 deletions

0
tests/__init__.py Normal file
View File

18
tests/conftest.py Normal file
View File

@@ -0,0 +1,18 @@
import logging
import pytest
@pytest.fixture()
def cmake_build(shared_datadir):
from subprocess import run
source = shared_datadir / 'cmake'
build = source / 'build'
build.mkdir()
p = run(['cmake', '-S', source, '-B', build],
check=True,
capture_output=True,
universal_newlines=True)
logging.debug(p.stdout)
logging.debug(p.stderr)
yield build

View File

@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.10)
project(testproject CXX)
add_executable(test_app main.cpp)
add_library(test_lib lib.cpp)

0
tests/data/cmake/lib.cpp Normal file
View File

View File

@@ -0,0 +1 @@
int main(int argc, char *argv[]) { return 0; }

73
tests/test_api.py Normal file
View File

@@ -0,0 +1,73 @@
import subprocess
from cmake_language_server.api import API
def test_query_with_cache(cmake_build):
api = API('cmake', cmake_build)
assert api.query()
query = cmake_build / '.cmake' / 'api' / 'v1' / 'query'
assert query.exists()
reply = cmake_build / '.cmake' / 'api' / 'v1' / 'reply'
assert reply.exists()
def test_query_without_cache(cmake_build):
api = API('cmake', cmake_build)
(cmake_build / 'CMakeCache.txt').unlink()
assert not api.query()
def test_read_variable(cmake_build):
api = API('cmake', cmake_build)
assert api.query()
assert api.read_reply()
assert api.get_variable_doc('testproject_BINARY_DIR')
def test_read_cmake_files(cmake_build):
api = API('cmake', cmake_build)
api.parse_doc()
assert api.query()
api.read_reply()
assert 'GNU' in api.get_variable_doc('CMAKE_CXX_COMPILER_ID')
def test_parse_commands(cmake_build):
api = API('cmake', cmake_build)
api.parse_doc()
p = subprocess.run(['cmake', '--help-command-list'],
capture_output=True,
universal_newlines=True)
commands = p.stdout.strip().split('\n')
for command in commands:
assert api.get_command_doc(command) is not None, f'{command} not found'
assert 'break()' in api.get_command_doc('break')
assert api.get_command_doc('not_existing_command') is None
def test_parse_variables(cmake_build):
api = API('cmake', cmake_build)
api.parse_doc()
p = subprocess.run(['cmake', '--help-variable-list'],
capture_output=True,
universal_newlines=True)
variables = p.stdout.strip().split('\n')
for variable in variables:
if '<' in variable:
continue
assert api.get_variable_doc(
variable) is not None, f'{variable} not found'
assert api.get_variable_doc('BUILD_SHARED_LIBS') is not None
assert api.get_variable_doc('not_existing_variable') is None

74
tests/test_fomatter.py Normal file
View File

@@ -0,0 +1,74 @@
from cmake_language_server.formatter import Formatter
from cmake_language_server.parser import ListParser
def make_formatter_test(liststr: str, expect: str):
def test():
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()
''')

64
tests/test_parser.py Normal file
View File

@@ -0,0 +1,64 @@
from typing import List
from cmake_language_server.parser import ListParser, TokenType
def make_parser_test(liststr: str,
expect_token: List[TokenType],
expect_remain: str = ''):
def test():
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')