refactor: tests and format the world (#314)

This commit is contained in:
ykiko
2025-11-30 15:21:27 +08:00
committed by GitHub
parent 2214d53ea5
commit cec13ec29b
201 changed files with 8302 additions and 8277 deletions

View File

@@ -1,95 +0,0 @@
#include "Test/Tester.h"
#include "Feature/CodeCompletion.h"
namespace clice::testing {
namespace {
suite<"CodeCompletion"> code_completion = [] {
std::vector<feature::CompletionItem> items;
auto code_complete = [&](llvm::StringRef code) {
CompilationParams params;
auto annotation = AnnotatedSource::from(code);
params.arguments = {"clang++", "-std=c++20", "main.cpp"};
params.completion = {"main.cpp", annotation.offsets["pos"]};
params.add_remapped_file("main.cpp", annotation.content);
config::CodeCompletionOption options = {};
items = feature::code_complete(params, options);
};
using enum feature::CompletionItemKind;
test("Score") = [&] {
code_complete(R"cpp(
int foooo(int x);
int x = fo$(pos)
)cpp");
expect(that % items.size() == 1);
expect(items.front().label == "foooo");
expect(items.front().kind == Function);
};
test("Snippet") = [&] {
code_complete(R"cpp(
int x = tru$(pos)
)cpp");
};
test("Overload") = [&] {
code_complete(R"cpp(
int foooo(int x);
int foooo(int x, int y);
int x = fooo$(pos)
)cpp");
};
test("Unqualified") = [&] {
code_complete(R"cpp(
namespace A {
void fooooo();
}
void bar() {
fo$(pos)
}
)cpp");
/// EXPECT: "A::fooooo"
/// To implement this we need to search code completion result from index
/// or traverse AST to collect interesting names.
};
test("Functor") = [&] {
code_complete(R"cpp(
struct X {
void operator() () {}
};
void bar() {
X foo;
fo$(pos);
}
)cpp");
/// TODO:
/// complete lambda as it is a variable.
};
test("Lambda") = [&] {
code_complete(R"cpp(
void bar() {
auto foo = [](int x){ };
fo$(pos);
}
)cpp");
/// TODO:
/// complete lambda as it is a function.
};
};
} // namespace
} // namespace clice::testing

View File

@@ -0,0 +1,94 @@
#include "Test/Test.h"
#include "Test/Tester.h"
#include "Feature/CodeCompletion.h"
namespace clice::testing {
namespace {
TEST_SUITE(CodeCompletion) {
std::vector<feature::CompletionItem> items;
void code_complete(llvm::StringRef code) {
CompilationParams params;
auto annotation = AnnotatedSource::from(code);
params.arguments = {"clang++", "-std=c++20", "main.cpp"};
params.completion = {"main.cpp", annotation.offsets["pos"]};
params.add_remapped_file("main.cpp", annotation.content);
config::CodeCompletionOption options = {};
items = feature::code_complete(params, options);
};
using enum feature::CompletionItemKind;
TEST_CASE(Score) {
code_complete(R"cpp(
int foooo(int x);
int x = fo$(pos)
)cpp");
ASSERT_EQ(items.size(), 1U);
ASSERT_EQ(items.front().label, "foooo");
ASSERT_EQ(items.front().kind, Function);
}
TEST_CASE(Snippet) {
code_complete(R"cpp(
int x = tru$(pos)
)cpp");
}
TEST_CASE(Overload) {
code_complete(R"cpp(
int foooo(int x);
int foooo(int x, int y);
int x = fooo$(pos)
)cpp");
}
TEST_CASE(Unqualified) {
code_complete(R"cpp(
namespace A {
void fooooo();
}
void bar() {
fo$(pos)
}
)cpp");
/// EXPECT: "A::fooooo"
/// To implement this we need to search code completion result from index
/// or traverse AST to collect interesting names.
}
TEST_CASE(Functor) {
code_complete(R"cpp(
struct X {
void operator() () {}
};
void bar() {
X foo;
fo$(pos);
}
)cpp");
/// TODO:
/// complete lambda as it is a variable.
}
TEST_CASE(Lambda) {
code_complete(R"cpp(
void bar() {
auto foo = [](int x){ };
fo$(pos);
}
)cpp");
/// TODO:
/// complete lambda as it is a function.
}
}; // TEST_SUITE(CodeCompletion)
} // namespace
} // namespace clice::testing

View File

@@ -1,83 +0,0 @@
#include "Test/Tester.h"
#include "Feature/DocumentLink.h"
namespace clice::testing {
namespace {
suite<"DocumentLink"> document_link = [] {
Tester tester;
std::vector<feature::DocumentLink> links;
auto run = [&](llvm::StringRef source) {
tester.clear();
tester.add_files("main.cpp", source);
tester.compile();
links = feature::document_links(*tester.unit);
};
auto expect_link = [&](std::size_t index, llvm::StringRef name, llvm::StringRef path) {
auto& link = links[index];
auto range = tester.range(name, "main.cpp");
expect(that % link.range.begin == range.begin);
expect(that % link.range.end == range.end);
/// FIXME: workaround to make `./file` to `file`
llvm::SmallString<64> file = {link.file};
path::remove_dots(file);
expect(that % file == path);
};
test("Include") = [&] {
run(R"cpp(
#[test.h]
#[pragma_once.h]
#pragma once
#[guard_macro.h]
#ifndef TEST3_H
#define TEST3_H
#endif
#[main.cpp]
#include @0["test.h"$]
#include @1["test.h"$]
#include @2["pragma_once.h"$]
#include @3["pragma_once.h"$]
#include @4["guard_macro.h"$]
#include @5["guard_macro.h"$]
)cpp");
expect(that % links.size() == 6);
expect_link(0, "0", "test.h");
expect_link(1, "1", "test.h");
expect_link(2, "2", "pragma_once.h");
expect_link(3, "3", "pragma_once.h");
expect_link(4, "4", "guard_macro.h");
expect_link(5, "5", "guard_macro.h");
};
test("HasInclude") = [&] {
run(R"cpp(
#[test.h]
#[main.cpp]
#include @0["test.h"]
#if __has_include(@1["test.h"])
#endif
#if __has_include("test2.h")
#endif
)cpp");
expect(that % links.size() == 2);
expect_link(0, "0", "test.h");
expect_link(1, "1", "test.h");
};
};
} // namespace
} // namespace clice::testing

View File

@@ -0,0 +1,82 @@
#include "Test/Test.h"
#include "Test/Tester.h"
#include "Feature/DocumentLink.h"
namespace clice::testing {
namespace {
TEST_SUITE(DocumentLink) {
Tester tester;
std::vector<feature::DocumentLink> links;
void run(llvm::StringRef source) {
tester.clear();
tester.add_files("main.cpp", source);
ASSERT_TRUE(tester.compile());
links = feature::document_links(*tester.unit);
};
void expect_link(std::size_t index, llvm::StringRef name, llvm::StringRef path) {
auto& link = links[index];
auto range = tester.range(name, "main.cpp");
ASSERT_EQ(link.range.begin, range.begin);
ASSERT_EQ(link.range.end, range.end);
/// FIXME: workaround to make `./file` to `file`
llvm::SmallString<64> file = {link.file};
path::remove_dots(file);
ASSERT_EQ(file, path);
};
TEST_CASE(Include) {
run(R"cpp(
#[test.h]
#[pragma_once.h]
#pragma once
#[guard_macro.h]
#ifndef TEST3_H
#define TEST3_H
#endif
#[main.cpp]
#include @0["test.h"$]
#include @1["test.h"$]
#include @2["pragma_once.h"$]
#include @3["pragma_once.h"$]
#include @4["guard_macro.h"$]
#include @5["guard_macro.h"$]
)cpp");
ASSERT_EQ(links.size(), 6U);
expect_link(0, "0", "test.h");
expect_link(1, "1", "test.h");
expect_link(2, "2", "pragma_once.h");
expect_link(3, "3", "pragma_once.h");
expect_link(4, "4", "guard_macro.h");
expect_link(5, "5", "guard_macro.h");
}
TEST_CASE(HasInclude) {
run(R"cpp(
#[test.h]
#[main.cpp]
#include @0["test.h"]
#if __has_include(@1["test.h"])
#endif
#if __has_include("test2.h")
#endif
)cpp");
ASSERT_EQ(links.size(), 2U);
expect_link(0, "0", "test.h");
expect_link(1, "1", "test.h");
}
}; // TEST_SUITE(DocumentLink)
} // namespace
} // namespace clice::testing

View File

@@ -1,175 +0,0 @@
#include "Test/Tester.h"
#include "Feature/DocumentSymbol.h"
#include "AST/Utility.h"
namespace clice::testing {
namespace {
suite<"DocumentSymbol"> document_symbol = [] {
Tester tester;
std::vector<feature::DocumentSymbol> symbols;
auto run = [&](llvm::StringRef code) {
tester.clear();
tester.add_main("main.cpp", code);
tester.compile_with_pch();
expect(that % tester.unit.has_value());
symbols = feature::document_symbols(*tester.unit);
};
auto total_size_wrapper = [](const std::vector<feature::DocumentSymbol>& result) -> size_t {
size_t size = 0;
std::function<void(const std::vector<feature::DocumentSymbol>&, size_t&)> total_size =
[&total_size](const std::vector<feature::DocumentSymbol>& result, size_t& size) {
for(auto& item: result) {
++size;
total_size(item.children, size);
}
};
total_size(result, size);
return size;
};
test("Namespace") = [&] {
const char* main = R"cpp(
namespace _1 {
namespace _2 {
}
}
namespace _1 {
namespace _2 {
namespace _3 {
}
}
}
namespace {}
namespace _1::_2{
}
)cpp";
run(main);
expect(that % total_size_wrapper(symbols) == 8);
};
test("Struct") = [&] {
const char* main = R"cpp(
struct _1 {};
struct _2 {};
struct _3 {
struct _4 {};
struct _5 {};
};
)cpp";
run(main);
expect(that % total_size_wrapper(symbols) == 5);
};
test("Field") = [&] {
const char* main = R"cpp(
struct x {
int x1;
int x2;
struct y {
int y1;
int y2;
};
static int x3;
};
)cpp";
run(main);
expect(that % total_size_wrapper(symbols) == 7);
};
test("Constructor") = [&] {
const char* main = R"cpp(
struct S {
int x;
S(): x(0) {}
S(int x) : x(x) {}
S(const S& s) : x(s.x) {}
~S() {}
};
)cpp";
run(main);
expect(that % total_size_wrapper(symbols) == 6);
};
test("Method") = [&] {
const char* main = R"cpp(
struct _0 {
void f(int x) {}
void f(int* x) {}
void f1(int& x) {}
void f2(const int& x) {}
void f2(const _0& x) {}
void f2(_0 x) {}
};
)cpp";
run(main);
expect(that % total_size_wrapper(symbols) == 7);
};
test("Enum") = [&] {
const char* main = R"cpp(
enum class A {
_1,
_2,
_3,
};
enum B {
_a,
_b,
_c,
};
)cpp";
run(main);
expect(that % total_size_wrapper(symbols) == 8);
};
test("TopLevelVariable") = [&] {
const char* main = R"cpp(
constexpr auto x = 1;
int y = 2;
)cpp";
run(main);
expect(that % total_size_wrapper(symbols) == 2);
};
test("Macro") = [&] {
const char* main = R"cpp(
#define CLASS(X) class X
CLASS(test) {
int x = 1;
};
#define VAR(X) int X = 1;
VAR(test)
)cpp";
run(main);
/// expect(that % total_size_wrapper(symbols) == 3);
};
};
} // namespace
} // namespace clice::testing

View File

@@ -0,0 +1,174 @@
#include "Test/Test.h"
#include "Test/Tester.h"
#include "AST/Utility.h"
#include "Feature/DocumentSymbol.h"
namespace clice::testing {
namespace {
TEST_SUITE(DocumentSymbol) {
Tester tester;
std::vector<feature::DocumentSymbol> symbols;
void run(llvm::StringRef code) {
tester.clear();
tester.add_main("main.cpp", code);
ASSERT_TRUE(tester.compile_with_pch());
ASSERT_TRUE(tester.unit.has_value());
symbols = feature::document_symbols(*tester.unit);
}
auto total_size_wrapper(const std::vector<feature::DocumentSymbol>& result) -> size_t {
size_t size = 0;
std::function<void(const std::vector<feature::DocumentSymbol>&, size_t&)> total_size =
[&total_size](const std::vector<feature::DocumentSymbol>& result, size_t& size) {
for(auto& item: result) {
++size;
total_size(item.children, size);
}
};
total_size(result, size);
return size;
};
TEST_CASE(Namespace) {
const char* main = R"cpp(
namespace _1 {
namespace _2 {
}
}
namespace _1 {
namespace _2 {
namespace _3 {
}
}
}
namespace {}
namespace _1::_2{
}
)cpp";
run(main);
ASSERT_EQ(total_size_wrapper(symbols), 8U);
}
TEST_CASE(Struct) {
const char* main = R"cpp(
struct _1 {};
struct _2 {};
struct _3 {
struct _4 {};
struct _5 {};
};
)cpp";
run(main);
ASSERT_EQ(total_size_wrapper(symbols), 5U);
}
TEST_CASE(Field) {
const char* main = R"cpp(
struct x {
int x1;
int x2;
struct y {
int y1;
int y2;
};
static int x3;
};
)cpp";
run(main);
ASSERT_EQ(total_size_wrapper(symbols), 7U);
}
TEST_CASE(Constructor) {
const char* main = R"cpp(
struct S {
int x;
S(): x(0) {}
S(int x) : x(x) {}
S(const S& s) : x(s.x) {}
~S() {}
};
)cpp";
run(main);
ASSERT_EQ(total_size_wrapper(symbols), 6U);
}
TEST_CASE(Method) {
const char* main = R"cpp(
struct _0 {
void f(int x) {}
void f(int* x) {}
void f1(int& x) {}
void f2(const int& x) {}
void f2(const _0& x) {}
void f2(_0 x) {}
};
)cpp";
run(main);
ASSERT_EQ(total_size_wrapper(symbols), 7U);
}
TEST_CASE(Enum) {
const char* main = R"cpp(
enum class A {
_1,
_2,
_3,
};
enum B {
_a,
_b,
_c,
};
)cpp";
run(main);
ASSERT_EQ(total_size_wrapper(symbols), 8U);
}
TEST_CASE(TopLevelVariable) {
const char* main = R"cpp(
constexpr auto x = 1;
int y = 2;
)cpp";
run(main);
ASSERT_EQ(total_size_wrapper(symbols), 2U);
}
TEST_CASE(Macro) {
const char* main = R"cpp(
#define CLASS(X) class X
CLASS(test) {
int x = 1;
};
#define VAR(X) int X = 1;
VAR(test)
)cpp";
run(main);
/// expect(that % total_size_wrapper(symbols) == 3);
}
}; // TEST_SUITE(DocumentSymbol)
} // namespace
} // namespace clice::testing

View File

@@ -1,407 +0,0 @@
#include "Test/Tester.h"
#include "Feature/FoldingRange.h"
namespace clice::testing {
namespace {
suite<"FoldingRange"> folding_range = [] {
Tester tester;
feature::FoldingRanges ranges;
auto run = [&](llvm::StringRef code) {
tester.clear();
tester.add_main("main.cpp", code);
tester.compile_with_pch();
ranges = feature::folding_ranges(*tester.unit);
};
auto expect_folding = [&](std::uint32_t index,
llvm::StringRef begin,
llvm::StringRef end,
feature::FoldingRangeKind kind,
std::source_location loc = std::source_location::current()) {
auto& folding = ranges[index];
auto begin_point = tester.point(begin, "main.cpp");
auto end_point = tester.point(end, "main.cpp");
expect(that % folding.range.begin == begin_point, loc);
expect(that % folding.range.end == end_point, loc);
/// FIXME: folding.kind is not implemented
/// expect(that % folding.kind.value() == kind.value(), loc);
};
using enum feature::FoldingRangeKind::Kind;
test("Namespace") = [&] {
run(R"cpp(
namespace single_line { }
namespace with_nodes $(1){
struct inner $(3){
int x;
}$(4);
}$(2)
namespace strange
$(5){
}$(6)
#define NS_BEGIN namespace ns {
#define NS_END }
$(7)NS_BEGIN
NS_END$(8)
)cpp");
expect(that % ranges.size() == 4);
expect_folding(0, "1", "2", Namespace);
expect_folding(1, "3", "4", Namespace);
expect_folding(2, "5", "6", Namespace);
expect_folding(3, "7", "8", Namespace);
};
test("Enum") = [&] {
run(R"cpp(
enum e1 $(1){
A,
B,
C
}$(2);
enum class e2 $(3){
A,
B,
C
}$(4);
enum e3 { D };
)cpp");
expect(that % ranges.size() == 2);
expect_folding(0, "1", "2", Enum);
expect_folding(1, "3", "4", Enum);
};
test("Record") = [&] {
run(R"cpp(
struct s1 $(1){
int x;
float y;
}$(2);
struct s2 {};
struct s3;
union u1 $(3){
int x;
float y;
}$(4);
struct u2 $(5){
struct s4 $(7){
}$(8);
}$(6);
void foo() $(9){
struct s5 $(11){
}$(12);
}$(10)
)cpp");
expect(that % ranges.size() == 6);
expect_folding(0, "1", "2", Struct);
expect_folding(1, "3", "4", Union);
expect_folding(2, "5", "6", Struct);
expect_folding(3, "7", "8", Struct);
expect_folding(4, "9", "10", FunctionBody);
expect_folding(5, "11", "12", Struct);
};
test("Method") = [&] {
run(R"cpp(
struct s2 $(1){
int x;
float y;
s2() = default;
}$(2);
struct s3;
struct s3 $(3){
void method() $(5){
int x = 0;
}$(6)
void parameter() $(7){
}$(8)
void skip() {};
}$(4);
)cpp");
expect(that % ranges.size() == 4);
expect_folding(0, "1", "2", Struct);
expect_folding(1, "3", "4", Struct);
expect_folding(2, "5", "6", FunctionBody);
expect_folding(3, "7", "8", FunctionBody);
};
test("Lambda") = [&] {
run(R"cpp(
auto z = $(1)[
x = 0, y = 1
]$(2) () $(3){
}$(4);
static int array[4];
auto s = $(5)[
x=0,
y = 1,
z = array[
0],
k = -1
]$(6) () $(7){
return;
}$(8);
auto l1 = [] () {};
auto l2 = [] () $(9){
}$(10);
auto l3 = [] () $(11){
return 0;
}$(12);
auto l4 = [] $(13)(
int x1,
int x2
)$(14) {};
)cpp");
expect(that % ranges.size() == 7);
expect_folding(0, "1", "2", LambdaCapture);
expect_folding(1, "3", "4", FunctionBody);
expect_folding(2, "5", "6", LambdaCapture);
expect_folding(3, "7", "8", FunctionBody);
expect_folding(4, "9", "10", FunctionBody);
expect_folding(5, "11", "12", FunctionBody);
expect_folding(6, "13", "14", FunctionBody);
};
test("Function") = [&] {
run(R"cpp(
void e() {};
void f $(1)(
)$(2) $(3){
}$(4)
void g $(5)(
int x,
int y = 2
)$(6) $(7){
int z;
}$(8)
void h() $(9){
int x = 0;
}$(10)
void i( ) { };
void j $(11)(
int p1,
int p2,
...
)$(12);
void k() $(13){
}$(14)
)cpp");
expect(that % ranges.size() == 7);
expect_folding(0, "1", "2", FunctionParams);
expect_folding(1, "3", "4", FunctionBody);
expect_folding(2, "5", "6", FunctionParams);
expect_folding(3, "7", "8", FunctionBody);
expect_folding(4, "9", "10", FunctionBody);
expect_folding(5, "11", "12", FunctionParams);
expect_folding(6, "13", "14", FunctionBody);
};
test("FunctionCall") = [&] {
run(R"cpp(
int f(int p1, int p2, int p3, int p4, int p5, int p6) { return p1 + p2; }
int main() $(1){
int x = f(1, 2, 3, 4, 5, 6);
int y = f $(2)(
1, 2, 3,
4, 5, 6
)$(3);
return f $(4)(
1, 2, 3,
4, 5, 6
)$(5);
}$(6)
)cpp");
expect(that % ranges.size() == 3);
expect_folding(0, "1", "6", FunctionBody);
expect_folding(1, "2", "3", FunctionCall);
expect_folding(2, "4", "5", FunctionCall);
};
test("CompoundStmt") = [&] {
run(R"cpp(
int main() $(1){
$(3){
$(5){
//
}$(6)
$(7){
//
}$(8)
//
}$(4)
return 0;
}$(2)
)cpp");
};
test("InitializeList") = [&] {
run(R"cpp(
struct L { int xs[4]; };
L l1 = $(1){
1, 2, 3, 4
}$(2);
L l2 = $(3){
//
//
}$(4);
)cpp");
expect(that % ranges.size() == 2);
expect_folding(0, "1", "2", Initializer);
expect_folding(1, "3", "4", Initializer);
};
test("AccessSpecifier") = [&] {
run(R"cpp(
class c1 $(1){
public$(3):
private$(4):
protected$(5):
}$(2);
class c2 $(6){
public$(8):
int x;
private$(9):
float y;
protected$(10):
double z;
}$(7);
#define PUBLIC public:
#define PRIVATE private:
#define PROTECTED protected:
class c3 $(11){
$(13)PUBLIC
int a;
$(15)PRIVATE$(14)
int b;
$(17)PROTECTED$(16)
int c;
}$(12);
)cpp");
expect_folding(0, "1", "2", Class);
expect_folding(1, "3", "4", AccessSpecifier);
expect_folding(2, "4", "5", AccessSpecifier);
expect_folding(3, "5", "2", AccessSpecifier);
expect_folding(4, "6", "7", Class);
expect_folding(5, "8", "9", AccessSpecifier);
expect_folding(6, "9", "10", AccessSpecifier);
expect_folding(7, "10", "7", AccessSpecifier);
expect_folding(8, "11", "12", Class);
expect_folding(9, "13", "14", AccessSpecifier);
expect_folding(10, "15", "16", AccessSpecifier);
expect_folding(11, "17", "12", AccessSpecifier);
};
test("Directive") = [&] {
run(R"cpp(
#ifdef M1
#else
#ifdef M2
#endif
#endif
)cpp");
};
test("PragmaRegion") = [&] {
run(R"cpp(
$(1)#pragma region level1
$(2)#pragma region level2
$(3)#pragma region level3
#$(4)pragma endregion level3
#$(5)pragma endregion level2
#$(6)pragma endregion level1
#pragma endregion // mismatch region, skipped
#pragma region // mismatch region, skipped
)cpp");
/// FIXME: PCH make pragma region not work
/// expect(that % ranges.size() == 3);
/// expect_folding(0, "1", "6", Region);
/// expect_folding(1, "2", "5", Region);
/// expect_folding(2, "3", "4", Region);
};
};
} // namespace
} // namespace clice::testing

View File

@@ -0,0 +1,406 @@
#include "Test/Test.h"
#include "Test/Tester.h"
#include "Feature/FoldingRange.h"
namespace clice::testing {
namespace {
TEST_SUITE(FoldingRange) {
Tester tester;
feature::FoldingRanges ranges;
void run(llvm::StringRef code) {
tester.clear();
tester.add_main("main.cpp", code);
ASSERT_TRUE(tester.compile_with_pch());
ranges = feature::folding_ranges(*tester.unit);
};
void expect_folding(std::uint32_t index,
llvm::StringRef begin,
llvm::StringRef end,
feature::FoldingRangeKind kind,
std::source_location loc = std::source_location::current()) {
auto& folding = ranges[index];
auto begin_point = tester.point(begin, "main.cpp");
auto end_point = tester.point(end, "main.cpp");
ASSERT_EQ(folding.range.begin, begin_point);
ASSERT_EQ(folding.range.end, end_point);
/// FIXME: folding.kind is not implemented
/// expect(that % folding.kind.value() == kind.value(), loc);
};
using enum feature::FoldingRangeKind::Kind;
TEST_CASE(Namespace) {
run(R"cpp(
namespace single_line { }
namespace with_nodes $(1){
struct inner $(3){
int x;
}$(4);
}$(2)
namespace strange
$(5){
}$(6)
#define NS_BEGIN namespace ns {
#define NS_END }
$(7)NS_BEGIN
NS_END$(8)
)cpp");
ASSERT_EQ(ranges.size(), 4U);
expect_folding(0, "1", "2", Namespace);
expect_folding(1, "3", "4", Namespace);
expect_folding(2, "5", "6", Namespace);
expect_folding(3, "7", "8", Namespace);
}
TEST_CASE(Enum) {
run(R"cpp(
enum e1 $(1){
A,
B,
C
}$(2);
enum class e2 $(3){
A,
B,
C
}$(4);
enum e3 { D };
)cpp");
ASSERT_EQ(ranges.size(), 2U);
expect_folding(0, "1", "2", Enum);
expect_folding(1, "3", "4", Enum);
}
TEST_CASE(Record) {
run(R"cpp(
struct s1 $(1){
int x;
float y;
}$(2);
struct s2 {};
struct s3;
union u1 $(3){
int x;
float y;
}$(4);
struct u2 $(5){
struct s4 $(7){
}$(8);
}$(6);
void foo() $(9){
struct s5 $(11){
}$(12);
}$(10)
)cpp");
ASSERT_EQ(ranges.size(), 6U);
expect_folding(0, "1", "2", Struct);
expect_folding(1, "3", "4", Union);
expect_folding(2, "5", "6", Struct);
expect_folding(3, "7", "8", Struct);
expect_folding(4, "9", "10", FunctionBody);
expect_folding(5, "11", "12", Struct);
};
TEST_CASE(Method) {
run(R"cpp(
struct s2 $(1){
int x;
float y;
s2() = default;
}$(2);
struct s3;
struct s3 $(3){
void method() $(5){
int x = 0;
}$(6)
void parameter() $(7){
}$(8)
void skip() {};
}$(4);
)cpp");
ASSERT_EQ(ranges.size(), 4U);
expect_folding(0, "1", "2", Struct);
expect_folding(1, "3", "4", Struct);
expect_folding(2, "5", "6", FunctionBody);
expect_folding(3, "7", "8", FunctionBody);
};
TEST_CASE(Lambda) {
run(R"cpp(
auto z = $(1)[
x = 0, y = 1
]$(2) () $(3){
}$(4);
static int array[4];
auto s = $(5)[
x=0,
y = 1,
z = array[
0],
k = -1
]$(6) () $(7){
return;
}$(8);
auto l1 = [] () {};
auto l2 = [] () $(9){
}$(10);
auto l3 = [] () $(11){
return 0;
}$(12);
auto l4 = [] $(13)(
int x1,
int x2
)$(14) {};
)cpp");
ASSERT_EQ(ranges.size(), 7U);
expect_folding(0, "1", "2", LambdaCapture);
expect_folding(1, "3", "4", FunctionBody);
expect_folding(2, "5", "6", LambdaCapture);
expect_folding(3, "7", "8", FunctionBody);
expect_folding(4, "9", "10", FunctionBody);
expect_folding(5, "11", "12", FunctionBody);
expect_folding(6, "13", "14", FunctionBody);
};
TEST_CASE(Function) {
run(R"cpp(
void e() {};
void f $(1)(
)$(2) $(3){
}$(4)
void g $(5)(
int x,
int y = 2
)$(6) $(7){
int z;
}$(8)
void h() $(9){
int x = 0;
}$(10)
void i( ) { };
void j $(11)(
int p1,
int p2,
...
)$(12);
void k() $(13){
}$(14)
)cpp");
ASSERT_EQ(ranges.size(), 7U);
expect_folding(0, "1", "2", FunctionParams);
expect_folding(1, "3", "4", FunctionBody);
expect_folding(2, "5", "6", FunctionParams);
expect_folding(3, "7", "8", FunctionBody);
expect_folding(4, "9", "10", FunctionBody);
expect_folding(5, "11", "12", FunctionParams);
expect_folding(6, "13", "14", FunctionBody);
}
TEST_CASE(FunctionCall) {
run(R"cpp(
int f(int p1, int p2, int p3, int p4, int p5, int p6) { return p1 + p2; }
int main() $(1){
int x = f(1, 2, 3, 4, 5, 6);
int y = f $(2)(
1, 2, 3,
4, 5, 6
)$(3);
return f $(4)(
1, 2, 3,
4, 5, 6
)$(5);
}$(6)
)cpp");
ASSERT_EQ(ranges.size(), 3U);
expect_folding(0, "1", "6", FunctionBody);
expect_folding(1, "2", "3", FunctionCall);
expect_folding(2, "4", "5", FunctionCall);
}
TEST_CASE(CompoundStmt) {
run(R"cpp(
int main() $(1){
$(3){
$(5){
//
}$(6)
$(7){
//
}$(8)
//
}$(4)
return 0;
}$(2)
)cpp");
}
TEST_CASE(InitializeList) {
run(R"cpp(
struct L { int xs[4]; };
L l1 = $(1){
1, 2, 3, 4
}$(2);
L l2 = $(3){
//
//
}$(4);
)cpp");
ASSERT_EQ(ranges.size(), 2U);
expect_folding(0, "1", "2", Initializer);
expect_folding(1, "3", "4", Initializer);
}
TEST_CASE(AccessSpecifier) {
run(R"cpp(
class c1 $(1){
public$(3):
private$(4):
protected$(5):
}$(2);
class c2 $(6){
public$(8):
int x;
private$(9):
float y;
protected$(10):
double z;
}$(7);
#define PUBLIC public:
#define PRIVATE private:
#define PROTECTED protected:
class c3 $(11){
$(13)PUBLIC
int a;
$(15)PRIVATE$(14)
int b;
$(17)PROTECTED$(16)
int c;
}$(12);
)cpp");
expect_folding(0, "1", "2", Class);
expect_folding(1, "3", "4", AccessSpecifier);
expect_folding(2, "4", "5", AccessSpecifier);
expect_folding(3, "5", "2", AccessSpecifier);
expect_folding(4, "6", "7", Class);
expect_folding(5, "8", "9", AccessSpecifier);
expect_folding(6, "9", "10", AccessSpecifier);
expect_folding(7, "10", "7", AccessSpecifier);
expect_folding(8, "11", "12", Class);
expect_folding(9, "13", "14", AccessSpecifier);
expect_folding(10, "15", "16", AccessSpecifier);
expect_folding(11, "17", "12", AccessSpecifier);
}
TEST_CASE(Directive) {
run(R"cpp(
#ifdef M1
#else
#ifdef M2
#endif
#endif
)cpp");
}
TEST_CASE(PragmaRegion) {
run(R"cpp(
$(1)#pragma region level1
$(2)#pragma region level2
$(3)#pragma region level3
#$(4)pragma endregion level3
#$(5)pragma endregion level2
#$(6)pragma endregion level1
#pragma endregion // mismatch region, skipped
#pragma region // mismatch region, skipped
)cpp");
/// FIXME: PCH make pragma region not work
/// expect(that % ranges.size() == 3);
/// expect_folding(0, "1", "6", Region);
/// expect_folding(1, "2", "5", Region);
/// expect_folding(2, "3", "4", Region);
}
}; // TEST_SUITE(FoldingRange)
} // namespace
} // namespace clice::testing

View File

@@ -1,17 +0,0 @@
#include "Test/Test.h"
#include "Feature/Formatting.h"
namespace clice::testing {
namespace {
suite<"Formatting"> suite = [] {
test("Simple") = [] {
auto edits = feature::document_format("main.cpp", "int main() { return 0; }", std::nullopt);
expect(ne(edits.size(), 0));
};
};
} // namespace
} // namespace clice::testing

View File

@@ -0,0 +1,15 @@
#include "Test/Test.h"
#include "Feature/Formatting.h"
namespace clice::testing {
namespace {
TEST_SUITE(Formatting) {
TEST_CASE(Simple) {
auto edits = feature::document_format("main.cpp", "int main() { return 0; }", std::nullopt);
ASSERT_NE(edits.size(), 0U);
}
}; // TEST_SUITE(Formatting)
} // namespace
} // namespace clice::testing

View File

@@ -1,408 +0,0 @@
#include "Test/Tester.h"
#include "Feature/Hover.h"
#include "clang/AST/RecursiveASTVisitor.h"
namespace clice::testing {
namespace {
using namespace feature;
struct DeclCollector : public clang::RecursiveASTVisitor<DeclCollector> {
llvm::StringMap<const clang::Decl*> decls;
bool VisitNamedDecl(const clang::NamedDecl* decl) {
assert(decl && "Decl must be a valid pointer");
decls[decl->getNameAsString()] = decl;
return true;
}
};
suite<"Hover"> hover = [] {
Tester tester;
llvm::StringMap<const clang::Decl*> decls;
auto run = [&](llvm::StringRef code, LocalSourceRange range = {}) {
tester.clear();
tester.add_main("main.cpp", code);
tester.compile();
DeclCollector collector;
collector.TraverseTranslationUnitDecl(tester.unit->tu());
decls = std::move(collector.decls);
};
test("Namespace") = [&] {
run(R"cpp(
namespace A {
namespace B {
namespace C {}
/// anonymous namespace
namespace {
namespace D {}
}
}
inline namespace E {}
}
/// std namespace
namespace std {
namespace F {}
}
)cpp");
// EXPECT_HOVER("", "");
// EXPECT_HOVER("", "");
// EXPECT_HOVER("", "");
// EXPECT_HOVER("", "");
// EXPECT_HOVER("", "");
// EXPECT_HOVER("", "");
// EXPECT_HOVER("", "");
/// FIXME: inline ?
// EXPECT_HOVER("E", "### namespace A::(inline)E");
};
test("RecordScope") = [&] {
run(R"cpp(
typedef struct A {
struct B {
struct C {};
};
struct {
struct D {};
} _;
} T;
/// forward declaration
struct FORWARD_STRUCT;
struct FORWARD_CLASS;
/// in function body
void f() {
struct X {};
class Y {};
struct {
struct Z {};
} _;
}
namespace n1 {
namespace n2 {
struct NA {
struct NB {};
};
}
namespace {
struct NC {};
}
}
namespace out {
namespace in {
struct M {
int x;
double y;
char z;
T a, b;
};
}
}
)cpp");
// EXPECT_HOVER("A", "");
// EXPECT_HOVER("B", "");
// EXPECT_HOVER("C", "");
// EXPECT_HOVER("X", "");
// EXPECT_HOVER("Y", "");
// EXPECT_HOVER("Z", "");
// EXPECT_HOVER("NA", "");
// EXPECT_HOVER("NB", "");
// EXPECT_HOVER("NC", "");
auto M_TEXT = R"md(### Struct `M`
In namespace: `out::in`
___
<TODO: document>
___
size: 24 (0x18) bytes, align: 8 (0x8) bytes,
___
5 fields:
+ x: `int`
+ y: `double`
+ z: `char`
+ a: `T` (aka `struct A`)
+ b: `T` (aka `struct A`)
___
<TODO: source code>
)md";
};
test("EnumStyle") = [&] {
run(R"cpp(
enum Free {
A = 1,
B = 2,
C = 999,
};
enum class Scope: long {
A = -8,
B = 2,
C = 100,
};
)cpp");
auto FREE_STYLE = R"md(### Enum `Free` `(unsigned int)`
In namespace: `(global)`, (unscoped)
___
<TODO: document>
___
3 items:
+ A = `1 (0x1)`
+ B = `2 (0x2)`
+ C = `999 (0x3E7)`
___
<TODO: source code>
)md";
// EXPECT_HOVER("Scope", "");
};
test("FunctionStyle") = [&] {
run(R"cpp(
typedef long long ll;
ll f(int x, int y, ll z = 1) { return 0; }
template<typename T, typename S>
T t(T a, T b, int c, ll d, S s) { return a; }
namespace {
constexpr static const char* g() { return "hello"; }
}
namespace test {
namespace {
[[deprecated("test deprecate message")]] consteval int h() { return 1; }
}
}
struct A {
constexpr static A m(int left, double right) { return A(); }
};
)cpp");
auto FUNC_STYLE = R"md(### Method `m`
In namespace: `(global)`, scope: `A`
___
`constexpr` `inline` `static`
___
-> `A` (aka `struct A`)
___
2 parameters:
+ left: `int`
+ right: `double`
___
<TODO: document>
___
<TODO: source code>
)md";
// EXPECT_HOVER("f", FREE_STYLE);
// EXPECT_HOVER("t", FREE_STYLE);
// EXPECT_HOVER("g", FREE_STYLE);
// EXPECT_HOVER("h", FREE_STYLE);
};
test("VariableStyle") = [&] {
run(R"cpp(
void f() {
constexpr static auto x1 = 1;
}
)cpp");
auto FREE_STYLE = R"md(### Variable `x1`
In namespace: `(global)`, scope: `f`
___
`constexpr` `static` `(local variable)`
Type: `const int`
size = 4 bytes, align = 4 bytes
___
<TODO: document>
___
<TODO: source code>
)md";
};
/// TEST_F(Hover, HeaderAndNamespace) {
/// auto header = R"cpp()cpp";
///
/// auto code = R"cpp(
/// #in$(h1)clude "head$(h3)er.h"$(h4)
/// #in$(h2)clude <stddef.h$(h5)>
///
/// $(n1)names$(n2)pace$(n3) outt$(n4)er {
///
/// namespac$(n5)e $(n6){
///
/// nam$(n7)espace inne$(n8)r {
///
/// }$(n9)
///
/// }
///
/// }$(n10)
///
/// )cpp";
/// }
// TEST_F(Hover, VariableAndLiteral) {
// auto code = R"cpp(
// // introduce size_t
// #include <cstddef>
//
// long operator ""_w(const char*, size_t) {
// return 1;
// };
//
// aut$(v1)o$(v2) i$(v3)1$(v4) = $(n1)1$(n2);
// auto$(v5) i2 = $(n3)-$(n4)1$(n5);
//
// auto l1$(v6) = $(l1)"test$(l2)_string_li$(l3)t";
// auto l2 = R$(l4)"tes$(l5)t(raw_string_lit)test"$(l6);
// auto l3 = u8$(l7)"test$(l8)_string_li$(l9)t";
// auto l$(v7)4 = $(l10)"$(l11)udf_string$(l12)"_w;
// )cpp";
// run(code);
// }
/// TEST_F(Hover, FunctionDeclAndParameter) {
/// auto code = R"cpp(
/// // introduce size_t
/// #include <bits/c++config.h>
///
/// i$(f1)nt$(f2) f() {
/// return 0;
/// }
///
/// lo$(f3)ng oper$(f4)ator ""_w(const char* str, std::si$(p1)ze_t$(p2) leng$(p3)th) {$(f5)
/// return 1;
/// };
///
///
/// struct A {
/// int f$(f6)n(i$(p4)nt par$(p5)am) {
/// return param;
/// }
///
/// voi$(f7)d ope$(f8)rator()$(f9)(int par$(p6)am) {}
/// };
///
///
/// templ$(f10)ate<typenam$(p7)e T1$(p8), typename T$(p9)2>
/// void templ$(f11)ate_func1(T1 le$(p10)ft, T2 righ$(p11)t)$(f12) {}
///
/// template<in$(p12)t NonTy$(p13)peParam = 1>
/// void templ$(f13)ate_func2() {}
///
/// template<templa$(p14)te<typen$(p15)ame Inn$(p16)er> typenam$(p17)e Outt$(p18)er>
/// void templ$(f14)ate_func3() {}
///
/// )cpp";
/// run(code);
/// }
test("AutoAndDecltype") = [&] {
auto code = R"cpp(
$(a1)aut$(a2)o$(a3) i = -1;
$(d1)dec$(d2)ltype$(d3)(i) j = 2;
struct A { int x; };
aut$(a4)o va$(a5)r = A{};
a$(fa)uto f1() { return 1; }
de$(fn_decltype)cltype(au$(fn_decltype_auto)to) f2() {}
int f3(au$(fn_para_auto)to x) {}
)cpp";
run(code);
/// FIXME: It seems a bug of SelectionTree, which cannot select any node of `f3`;
/// EXPECT_HOVER_TYPE("fn_para_auto", is<Var>);
};
test("Expr") = [&] {
auto code = R"cpp(
int xxxx = 1;
int yyyy = xx$(e1)xx;
struct A {
int function(int param) {
return thi$(e2)s$(e3)->$(e4)funct$(e5)ion(para$(e6)m);
}
int fn(int param) {
return static$(e7)_cast<A*>(nul$(e8)lptr)->function(par$(e9)am);
}
};
)cpp";
run(code);
};
};
} // namespace
} // namespace clice::testing

View File

@@ -0,0 +1,408 @@
#include "Test/Test.h"
#include "Test/Tester.h"
#include "Feature/Hover.h"
#include "clang/AST/RecursiveASTVisitor.h"
namespace clice::testing {
namespace {
using namespace feature;
struct DeclCollector : public clang::RecursiveASTVisitor<DeclCollector> {
llvm::StringMap<const clang::Decl*> decls;
bool VisitNamedDecl(const clang::NamedDecl* decl) {
assert(decl && "Decl must be a valid pointer");
decls[decl->getNameAsString()] = decl;
return true;
}
};
TEST_SUITE(Hover) {
Tester tester;
llvm::StringMap<const clang::Decl*> decls;
void run(llvm::StringRef code, LocalSourceRange range = {}) {
tester.clear();
tester.add_main("main.cpp", code);
ASSERT_TRUE(tester.compile());
DeclCollector collector;
collector.TraverseTranslationUnitDecl(tester.unit->tu());
decls = std::move(collector.decls);
}
TEST_CASE(Namespace) {
run(R"cpp(
namespace A {
namespace B {
namespace C {}
/// anonymous namespace
namespace {
namespace D {}
}
}
inline namespace E {}
}
/// std namespace
namespace std {
namespace F {}
}
)cpp");
// EXPECT_HOVER("", "");
// EXPECT_HOVER("", "");
// EXPECT_HOVER("", "");
// EXPECT_HOVER("", "");
// EXPECT_HOVER("", "");
// EXPECT_HOVER("", "");
// EXPECT_HOVER("", "");
/// FIXME: inline ?
// EXPECT_HOVER("E", "### namespace A::(inline)E");
}
TEST_CASE(RecordScope) {
run(R"cpp(
typedef struct A {
struct B {
struct C {};
};
struct {
struct D {};
} _;
} T;
/// forward declaration
struct FORWARD_STRUCT;
struct FORWARD_CLASS;
/// in function body
void f() {
struct X {};
class Y {};
struct {
struct Z {};
} _;
}
namespace n1 {
namespace n2 {
struct NA {
struct NB {};
};
}
namespace {
struct NC {};
}
}
namespace out {
namespace in {
struct M {
int x;
double y;
char z;
T a, b;
};
}
}
)cpp");
// EXPECT_HOVER("A", "");
// EXPECT_HOVER("B", "");
// EXPECT_HOVER("C", "");
// EXPECT_HOVER("X", "");
// EXPECT_HOVER("Y", "");
// EXPECT_HOVER("Z", "");
// EXPECT_HOVER("NA", "");
// EXPECT_HOVER("NB", "");
// EXPECT_HOVER("NC", "");
auto M_TEXT = R"md(### Struct `M`
In namespace: `out::in`
___
<TODO: document>
___
size: 24 (0x18) bytes, align: 8 (0x8) bytes,
___
5 fields:
+ x: `int`
+ y: `double`
+ z: `char`
+ a: `T` (aka `struct A`)
+ b: `T` (aka `struct A`)
___
<TODO: source code>
)md";
}
TEST_CASE(EnumStyle) {
run(R"cpp(
enum Free {
A = 1,
B = 2,
C = 999,
};
enum class Scope: long {
A = -8,
B = 2,
C = 100,
};
)cpp");
auto FREE_STYLE = R"md(### Enum `Free` `(unsigned int)`
In namespace: `(global)`, (unscoped)
___
<TODO: document>
___
3 items:
+ A = `1 (0x1)`
+ B = `2 (0x2)`
+ C = `999 (0x3E7)`
___
<TODO: source code>
)md";
// EXPECT_HOVER("Scope", "");
}
TEST_CASE(FunctionStyle) {
run(R"cpp(
typedef long long ll;
ll f(int x, int y, ll z = 1) { return 0; }
template<typename T, typename S>
T t(T a, T b, int c, ll d, S s) { return a; }
namespace {
constexpr static const char* g() { return "hello"; }
}
namespace test {
namespace {
[[deprecated("test deprecate message")]] consteval int h() { return 1; }
}
}
struct A {
constexpr static A m(int left, double right) { return A(); }
};
)cpp");
auto FUNC_STYLE = R"md(### Method `m`
In namespace: `(global)`, scope: `A`
___
`constexpr` `inline` `static`
___
-> `A` (aka `struct A`)
___
2 parameters:
+ left: `int`
+ right: `double`
___
<TODO: document>
___
<TODO: source code>
)md";
// EXPECT_HOVER("f", FREE_STYLE);
// EXPECT_HOVER("t", FREE_STYLE);
// EXPECT_HOVER("g", FREE_STYLE);
// EXPECT_HOVER("h", FREE_STYLE);
}
TEST_CASE(VariableStyle) {
run(R"cpp(
void f() {
constexpr static auto x1 = 1;
}
)cpp");
auto FREE_STYLE = R"md(### Variable `x1`
In namespace: `(global)`, scope: `f`
___
`constexpr` `static` `(local variable)`
Type: `const int`
size = 4 bytes, align = 4 bytes
___
<TODO: document>
___
<TODO: source code>
)md";
}
/// TEST_F(Hover, HeaderAndNamespace) {
/// auto header = R"cpp()cpp";
///
/// auto code = R"cpp(
/// #in$(h1)clude "head$(h3)er.h"$(h4)
/// #in$(h2)clude <stddef.h$(h5)>
///
/// $(n1)names$(n2)pace$(n3) outt$(n4)er {
///
/// namespac$(n5)e $(n6){
///
/// nam$(n7)espace inne$(n8)r {
///
/// }$(n9)
///
/// }
///
/// }$(n10)
///
/// )cpp";
/// }
// TEST_F(Hover, VariableAndLiteral) {
// auto code = R"cpp(
// // introduce size_t
// #include <cstddef>
//
// long operator ""_w(const char*, size_t) {
// return 1;
// };
//
// aut$(v1)o$(v2) i$(v3)1$(v4) = $(n1)1$(n2);
// auto$(v5) i2 = $(n3)-$(n4)1$(n5);
//
// auto l1$(v6) = $(l1)"test$(l2)_string_li$(l3)t";
// auto l2 = R$(l4)"tes$(l5)t(raw_string_lit)test"$(l6);
// auto l3 = u8$(l7)"test$(l8)_string_li$(l9)t";
// auto l$(v7)4 = $(l10)"$(l11)udf_string$(l12)"_w;
// )cpp";
// run(code);
// }
/// TEST_F(Hover, FunctionDeclAndParameter) {
/// auto code = R"cpp(
/// // introduce size_t
/// #include <bits/c++config.h>
///
/// i$(f1)nt$(f2) f() {
/// return 0;
/// }
///
/// lo$(f3)ng oper$(f4)ator ""_w(const char* str, std::si$(p1)ze_t$(p2) leng$(p3)th) {$(f5)
/// return 1;
/// };
///
///
/// struct A {
/// int f$(f6)n(i$(p4)nt par$(p5)am) {
/// return param;
/// }
///
/// voi$(f7)d ope$(f8)rator()$(f9)(int par$(p6)am) {}
/// };
///
///
/// templ$(f10)ate<typenam$(p7)e T1$(p8), typename T$(p9)2>
/// void templ$(f11)ate_func1(T1 le$(p10)ft, T2 righ$(p11)t)$(f12) {}
///
/// template<in$(p12)t NonTy$(p13)peParam = 1>
/// void templ$(f13)ate_func2() {}
///
/// template<templa$(p14)te<typen$(p15)ame Inn$(p16)er> typenam$(p17)e Outt$(p18)er>
/// void templ$(f14)ate_func3() {}
///
/// )cpp";
/// run(code);
/// }
TEST_CASE(AutoAndDecltype) {
auto code = R"cpp(
$(a1)aut$(a2)o$(a3) i = -1;
$(d1)dec$(d2)ltype$(d3)(i) j = 2;
struct A { int x; };
aut$(a4)o va$(a5)r = A{};
a$(fa)uto f1() { return 1; }
de$(fn_decltype)cltype(au$(fn_decltype_auto)to) f2() {}
int f3(au$(fn_para_auto)to x) {}
)cpp";
run(code);
/// FIXME: It seems a bug of SelectionTree, which cannot select any node of `f3`;
/// EXPECT_HOVER_TYPE("fn_para_auto", is<Var>);
}
TEST_CASE(Expr) {
auto code = R"cpp(
int xxxx = 1;
int yyyy = xx$(e1)xx;
struct A {
int function(int param) {
return thi$(e2)s$(e3)->$(e4)funct$(e5)ion(para$(e6)m);
}
int fn(int param) {
return static$(e7)_cast<A*>(nul$(e8)lptr)->function(par$(e9)am);
}
};
)cpp";
run(code);
}
}; // TEST_SUITE(Hover)
} // namespace
} // namespace clice::testing

View File

@@ -1,194 +0,0 @@
#include "Test/Tester.h"
#include "Feature/SemanticToken.h"
namespace clice::testing {
namespace {
using enum SymbolKind::Kind;
using enum SymbolModifiers::Kind;
suite<"SemanticToken"> semantic_token = [] {
Tester tester;
feature::SemanticTokens tokens;
auto run = [&](llvm::StringRef code) {
tester.clear();
tester.add_main("main.cpp", code);
tester.compile_with_pch();
tokens = feature::semantic_tokens(*tester.unit);
};
auto expect_token = [&](llvm::StringRef pos,
SymbolKind kind,
SymbolModifiers modifiers = SymbolModifiers(),
std::source_location loc = std::source_location::current()) {
auto range = tester.range(pos);
auto found = false;
for(auto& token: tokens) {
if(token.range == range) {
expect(token.kind == kind, loc);
expect(token.modifiers == modifiers, loc);
found = true;
break;
}
}
expect(found, loc);
};
test("Include") = [&] {
run(R"cpp(
@0[#include] @1[<stddef.h>]
@2[#include] @3["stddef.h"]
@4[#] @5[include] @6["stddef.h"]
)cpp");
expect_token("0", Directive);
expect_token("1", Header);
expect_token("2", Directive);
expect_token("3", Header);
expect_token("4", Directive);
expect_token("5", Directive);
expect_token("6", Header);
};
test("Comment") = [&] {
run(R"cpp(
@line[/// line comment]
int x = 1;
)cpp");
expect_token("line", Comment);
};
test("Keyword") = [&] {
run(R"cpp(
@int[int] main() {
@return[return] 0;
}
)cpp");
expect_token("int", Keyword);
expect_token("return", Keyword);
};
test("Macro") = [&] {
run(R"cpp(
@directive[#define] @macro[FOO]
)cpp");
expect_token("directive", Directive);
expect_token("macro", Macro);
};
test("FinalAndOverride") = [&] {
run(R"cpp(
struct A @final[final] {};
struct B {
virtual void foo();
};
struct C : B {
void foo() @override[override];
};
struct D : C {
void foo() @final2[final];
};
)cpp");
expect_token("final", Keyword);
expect_token("override", Keyword);
expect_token("final2", Keyword);
};
test("VarDecl") = [&] {
run(R"cpp(
extern int @x1[x];
int @x2[x] = 1;
template <typename T, typename U>
extern int @y1[y];
template <typename T, typename U>
int @y2[y] = 2;
template<typename T>
extern int @y3[y]<T, int>;
template<typename T>
int @y4[y]<T, int> = 4;
template<>
int @y5[y]<int, int> = 5;
int main() {
@x3[x] = 6;
}
)cpp");
expect_token("x1", Variable, Declaration);
expect_token("x2", Variable, Definition);
expect_token("y1", Variable, SymbolModifiers(Declaration, Templated));
expect_token("y2", Variable, SymbolModifiers(Definition, Templated));
expect_token("y3", Variable, SymbolModifiers(Declaration, Templated));
expect_token("y4", Variable, SymbolModifiers(Definition, Templated));
expect_token("y5", Variable, Definition);
expect_token("x3", Variable, SymbolModifiers());
};
test("FunctionDecl") = [&] {
run(R"cpp(
extern int @foo1[foo]();
int @foo2[foo]() {
return 0;
}
template <typename T>
extern int @bar1[bar]();
template <typename T>
int @bar2[bar]() {
return 1;
}
)cpp");
expect_token("foo1", Function, Declaration);
expect_token("foo2", Function, Definition);
expect_token("bar1", Function, SymbolModifiers(Declaration, Templated));
expect_token("bar2", Function, SymbolModifiers(Definition, Templated));
};
test("RecordDecl") = [&] {
run(R"cpp(
class @A1[A];
class @A2[A] {};
struct @B1[B];
struct @B2[B] {};
union @C1[C];
union @C2[C] {};
)cpp");
expect_token("A1", Class, Declaration);
expect_token("A2", Class, Definition);
expect_token("B1", Struct, Declaration);
expect_token("B2", Struct, Definition);
expect_token("C1", Union, Declaration);
expect_token("C2", Union, Definition);
};
};
} // namespace
} // namespace clice::testing

View File

@@ -0,0 +1,197 @@
#include "Test/Test.h"
#include "Test/Tester.h"
#include "Feature/SemanticToken.h"
namespace clice::testing {
namespace {
using enum SymbolKind::Kind;
using enum SymbolModifiers::Kind;
TEST_SUITE(SemanticToken) {
Tester tester;
feature::SemanticTokens tokens;
void run(llvm::StringRef code) {
tester.clear();
tester.add_main("main.cpp", code);
ASSERT_TRUE(tester.compile_with_pch());
tokens = feature::semantic_tokens(*tester.unit);
};
void expect_token(llvm::StringRef pos,
SymbolKind kind,
SymbolModifiers modifiers = SymbolModifiers(),
std::source_location loc = std::source_location::current()) {
auto range = tester.range(pos);
auto found = false;
for(auto& token: tokens) {
if(token.range == range) {
ASSERT_EQ(token.kind, kind);
ASSERT_EQ(token.modifiers, modifiers);
found = true;
break;
}
}
ASSERT_TRUE(found);
};
TEST_CASE(Include) {
run(R"cpp(
@0[#include] @1[<stddef.h>]
@2[#include] @3["stddef.h"]
@4[#] @5[include] @6["stddef.h"]
)cpp");
expect_token("0", Directive);
expect_token("1", Header);
expect_token("2", Directive);
expect_token("3", Header);
expect_token("4", Directive);
expect_token("5", Directive);
expect_token("6", Header);
}
TEST_CASE(Comment) {
run(R"cpp(
@line[/// line comment]
int x = 1;
)cpp");
expect_token("line", Comment);
}
TEST_CASE(Keyword) {
run(R"cpp(
@int[int] main() {
@return[return] 0;
}
)cpp");
expect_token("int", Keyword);
expect_token("return", Keyword);
}
TEST_CASE(Macro) {
run(R"cpp(
@directive[#define] @macro[FOO]
)cpp");
expect_token("directive", Directive);
expect_token("macro", Macro);
}
TEST_CASE(FinalAndOverride) {
run(R"cpp(
struct A @final[final] {};
struct B {
virtual void foo();
};
struct C : B {
void foo() @override[override];
};
struct D : C {
void foo() @final2[final];
};
)cpp");
expect_token("final", Keyword);
expect_token("override", Keyword);
expect_token("final2", Keyword);
}
TEST_CASE(VarDecl) {
run(R"cpp(
extern int @x1[x];
int @x2[x] = 1;
template <typename T, typename U>
extern int @y1[y];
template <typename T, typename U>
int @y2[y] = 2;
template<typename T>
extern int @y3[y]<T, int>;
template<typename T>
int @y4[y]<T, int> = 4;
template<>
int @y5[y]<int, int> = 5;
int main() {
@x3[x] = 6;
}
)cpp");
expect_token("x1", Variable, Declaration);
expect_token("x2", Variable, Definition);
expect_token("y1", Variable, SymbolModifiers(Declaration, Templated));
expect_token("y2", Variable, SymbolModifiers(Definition, Templated));
expect_token("y3", Variable, SymbolModifiers(Declaration, Templated));
expect_token("y4", Variable, SymbolModifiers(Definition, Templated));
expect_token("y5", Variable, Definition);
expect_token("x3", Variable, SymbolModifiers());
}
TEST_CASE(FunctionDecl) {
run(R"cpp(
extern int @foo1[foo]();
int @foo2[foo]() {
return 0;
}
template <typename T>
extern int @bar1[bar]();
template <typename T>
int @bar2[bar]() {
return 1;
}
)cpp");
expect_token("foo1", Function, Declaration);
expect_token("foo2", Function, Definition);
expect_token("bar1", Function, SymbolModifiers(Declaration, Templated));
expect_token("bar2", Function, SymbolModifiers(Definition, Templated));
}
TEST_CASE(RecordDecl) {
run(R"cpp(
class @A1[A];
class @A2[A] {};
struct @B1[B];
struct @B2[B] {};
union @C1[C];
union @C2[C] {};
)cpp");
expect_token("A1", Class, Declaration);
expect_token("A2", Class, Definition);
expect_token("B1", Struct, Declaration);
expect_token("B2", Struct, Definition);
expect_token("C1", Union, Declaration);
expect_token("C2", Union, Definition);
}
}; // TEST_SUITE(SemanticToken)
} // namespace
} // namespace clice::testing

View File

@@ -1,43 +0,0 @@
#include "Test/Tester.h"
#include "Feature/SignatureHelp.h"
namespace clice::testing {
namespace {
suite<"SignatureHelp"> signature_help = [] {
Tester tester;
proto::SignatureHelp help;
auto run = [&](llvm::StringRef code) {
tester.clear();
tester.add_main("main.cpp", code);
tester.prepare();
tester.params.completion = {"main.cpp", tester.nameless_points()[0]};
help = feature::signature_help(tester.params, {});
};
test("Simple") = [&] {
run(R"cpp(
void foo();
void foo(int x);
void foo(int x, int y);
int main() {
foo($);
}
)cpp");
expect(eq(help.signatures.size(), 3));
};
/// FIXME: Add more tests.
};
} // namespace
} // namespace clice::testing

View File

@@ -0,0 +1,42 @@
#include "Test/Test.h"
#include "Test/Tester.h"
#include "Feature/SignatureHelp.h"
namespace clice::testing {
namespace {
TEST_SUITE(SignatureHelp) {
Tester tester;
proto::SignatureHelp help;
void run(llvm::StringRef code) {
tester.clear();
tester.add_main("main.cpp", code);
tester.prepare();
tester.params.completion = {"main.cpp", tester.nameless_points()[0]};
help = feature::signature_help(tester.params, {});
};
TEST_CASE(Simple) {
run(R"cpp(
void foo();
void foo(int x);
void foo(int x, int y);
int main() {
foo($);
}
)cpp");
ASSERT_EQ(help.signatures.size(), 3U);
}
/// FIXME: Add more tests.
}; // TEST_SUITE(SignatureHelp)
} // namespace
} // namespace clice::testing