mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 07:27:45 +00:00
LanguageServer/Cpp: Add tests
The Cpp LanguageServer tests can be run with: CppLanguageServer -t The tests now only cover some very simple autocomplete and "find declaration" use cases, but it's a start :)
This commit is contained in:
parent
e9fc5d6cc3
commit
6329e9fce6
4 changed files with 165 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
LexerAutoComplete.cpp
|
LexerAutoComplete.cpp
|
||||||
ParserAutoComplete.cpp
|
ParserAutoComplete.cpp
|
||||||
|
Tests.cpp
|
||||||
main.cpp
|
main.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
135
Userland/DevTools/HackStudio/LanguageServers/Cpp/Tests.cpp
Normal file
135
Userland/DevTools/HackStudio/LanguageServers/Cpp/Tests.cpp
Normal file
|
@ -0,0 +1,135 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "Tests.h"
|
||||||
|
#include "../FileDB.h"
|
||||||
|
#include "ParserAutoComplete.h"
|
||||||
|
|
||||||
|
using namespace LanguageServers;
|
||||||
|
using namespace LanguageServers::Cpp;
|
||||||
|
|
||||||
|
static bool s_some_test_failed = false;
|
||||||
|
|
||||||
|
#define I_TEST(name) \
|
||||||
|
{ \
|
||||||
|
printf("Testing " #name "... "); \
|
||||||
|
fflush(stdout); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define PASS \
|
||||||
|
do { \
|
||||||
|
printf("PASS\n"); \
|
||||||
|
fflush(stdout); \
|
||||||
|
return; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define FAIL(reason) \
|
||||||
|
do { \
|
||||||
|
printf("FAIL: " #reason "\n"); \
|
||||||
|
s_some_test_failed = true; \
|
||||||
|
return; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
static void test_complete_local_args();
|
||||||
|
static void test_complete_local_vars();
|
||||||
|
static void test_complete_type();
|
||||||
|
static void test_find_variable_definition();
|
||||||
|
|
||||||
|
int run_tests()
|
||||||
|
{
|
||||||
|
test_complete_local_args();
|
||||||
|
test_complete_local_vars();
|
||||||
|
test_complete_type();
|
||||||
|
test_find_variable_definition();
|
||||||
|
return s_some_test_failed ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_complete_local_args()
|
||||||
|
{
|
||||||
|
I_TEST(Complete Local Args)
|
||||||
|
FileDB filedb;
|
||||||
|
String content = R"(
|
||||||
|
int main(int argc, char** argv){
|
||||||
|
ar
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
filedb.add("a.cpp", content);
|
||||||
|
ParserAutoComplete autocomplete(filedb);
|
||||||
|
auto suggestions = autocomplete.get_suggestions("a.cpp", { 2, 2 });
|
||||||
|
if (suggestions.size() != 2)
|
||||||
|
FAIL(bad size);
|
||||||
|
|
||||||
|
if (suggestions[0].completion == "argc" && suggestions[1].completion == "argv")
|
||||||
|
PASS;
|
||||||
|
|
||||||
|
FAIL("wrong results");
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_complete_local_vars()
|
||||||
|
{
|
||||||
|
I_TEST(Complete Local Vars)
|
||||||
|
FileDB filedb;
|
||||||
|
String content = R"(
|
||||||
|
int main(int argc, char** argv){
|
||||||
|
int myvar1 = 3;
|
||||||
|
myv
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
filedb.add("a.cpp", content);
|
||||||
|
ParserAutoComplete autocomplete(filedb);
|
||||||
|
auto suggestions = autocomplete.get_suggestions("a.cpp", { 3, 3 });
|
||||||
|
if (suggestions.size() != 1)
|
||||||
|
FAIL(bad size);
|
||||||
|
|
||||||
|
if (suggestions[0].completion == "myvar1")
|
||||||
|
PASS;
|
||||||
|
|
||||||
|
FAIL("wrong results");
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_complete_type()
|
||||||
|
{
|
||||||
|
I_TEST(Complete Type)
|
||||||
|
FileDB filedb;
|
||||||
|
String content = R"(
|
||||||
|
struct MyStruct {
|
||||||
|
int x;
|
||||||
|
}
|
||||||
|
void foo(){
|
||||||
|
MyS
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
filedb.add("a.cpp", content);
|
||||||
|
ParserAutoComplete autocomplete(filedb);
|
||||||
|
auto suggestions = autocomplete.get_suggestions("a.cpp", { 5, 3 });
|
||||||
|
if (suggestions.size() != 1)
|
||||||
|
FAIL(bad size);
|
||||||
|
|
||||||
|
if (suggestions[0].completion == "MyStruct")
|
||||||
|
PASS;
|
||||||
|
|
||||||
|
FAIL("wrong results");
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_find_variable_definition()
|
||||||
|
{
|
||||||
|
I_TEST(Find Variable Declaration)
|
||||||
|
FileDB filedb;
|
||||||
|
String content = R"(
|
||||||
|
int main(int argc, char** argv){
|
||||||
|
argv = nullptr;
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
filedb.add("a.cpp", content);
|
||||||
|
ParserAutoComplete autocomplete(filedb);
|
||||||
|
auto position = autocomplete.find_declaration_of("a.cpp", { 2, 1 });
|
||||||
|
if (!position.has_value())
|
||||||
|
FAIL("declaration not found");
|
||||||
|
|
||||||
|
if (position.value().file == "a.cpp" && position.value().line == 1 && position.value().column >= 19)
|
||||||
|
PASS;
|
||||||
|
FAIL("wrong declaration location");
|
||||||
|
}
|
9
Userland/DevTools/HackStudio/LanguageServers/Cpp/Tests.h
Normal file
9
Userland/DevTools/HackStudio/LanguageServers/Cpp/Tests.h
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
int run_tests();
|
|
@ -5,7 +5,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "ClientConnection.h"
|
#include "ClientConnection.h"
|
||||||
|
#include "Tests.h"
|
||||||
#include <AK/LexicalPath.h>
|
#include <AK/LexicalPath.h>
|
||||||
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/EventLoop.h>
|
#include <LibCore/EventLoop.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <LibCore/LocalServer.h>
|
#include <LibCore/LocalServer.h>
|
||||||
|
@ -13,7 +15,24 @@
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(int, char**)
|
static int mode_server();
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
bool tests = false;
|
||||||
|
|
||||||
|
Core::ArgsParser parser;
|
||||||
|
parser.add_option(tests, "Run tests", "tests", 't');
|
||||||
|
parser.parse(argc, argv);
|
||||||
|
|
||||||
|
if (tests) {
|
||||||
|
return run_tests();
|
||||||
|
}
|
||||||
|
|
||||||
|
return mode_server();
|
||||||
|
}
|
||||||
|
|
||||||
|
int mode_server()
|
||||||
{
|
{
|
||||||
Core::EventLoop event_loop;
|
Core::EventLoop event_loop;
|
||||||
if (pledge("stdio unix recvfd rpath ", nullptr) < 0) {
|
if (pledge("stdio unix recvfd rpath ", nullptr) < 0) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue