summaryrefslogtreecommitdiff
path: root/Userland/DevTools/HackStudio/LanguageServers/Cpp/Tests.cpp
blob: 14dbd87944c7675c0e6b5fe367c875498002036a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "Tests.h"
#include "../FileDB.h"
#include "CppComprehensionEngine.h"
#include <AK/LexicalPath.h>
#include <LibCore/File.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)

constexpr char TESTS_ROOT_DIR[] = "/home/anon/Tests/cpp-tests/comprehension";

static void test_complete_local_args();
static void test_complete_local_vars();
static void test_complete_type();
static void test_find_variable_definition();
static void test_complete_includes();
static void test_parameters_hint();

int run_tests()
{
    test_complete_local_args();
    test_complete_local_vars();
    test_complete_type();
    test_find_variable_definition();
    test_complete_includes();
    test_parameters_hint();
    return s_some_test_failed ? 1 : 0;
}

static void add_file(FileDB& filedb, String const& name)
{
    auto file = Core::File::open(LexicalPath::join(TESTS_ROOT_DIR, name).string(), Core::OpenMode::ReadOnly);
    VERIFY(!file.is_error());
    filedb.add(name, file.value()->fd());
}

void test_complete_local_args()
{
    I_TEST(Complete Local Args)
    FileDB filedb;
    add_file(filedb, "complete_local_args.cpp");
    CppComprehensionEngine engine(filedb);
    auto suggestions = engine.get_suggestions("complete_local_args.cpp", { 2, 6 });
    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;
    add_file(filedb, "complete_local_vars.cpp");
    CppComprehensionEngine autocomplete(filedb);
    auto suggestions = autocomplete.get_suggestions("complete_local_vars.cpp", { 3, 7 });
    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;
    add_file(filedb, "complete_type.cpp");
    CppComprehensionEngine autocomplete(filedb);
    auto suggestions = autocomplete.get_suggestions("complete_type.cpp", { 5, 7 });
    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;
    add_file(filedb, "find_variable_declaration.cpp");
    CppComprehensionEngine engine(filedb);
    auto position = engine.find_declaration_of("find_variable_declaration.cpp", { 2, 5 });
    if (!position.has_value())
        FAIL("declaration not found");

    if (position.value().file == "find_variable_declaration.cpp" && position.value().line == 0 && position.value().column >= 19)
        PASS;
    FAIL("wrong declaration location");
}

void test_complete_includes()
{
    I_TEST(Complete Type)
    FileDB filedb;
    filedb.set_project_root(TESTS_ROOT_DIR);
    add_file(filedb, "complete_includes.cpp");
    add_file(filedb, "sample_header.h");
    CppComprehensionEngine autocomplete(filedb);

    auto suggestions = autocomplete.get_suggestions("complete_includes.cpp", { 0, 22 });
    if (suggestions.size() != 1)
        FAIL(project include - bad size);

    if (suggestions[0].completion != "\"sample_header.h\"")
        FAIL("project include - wrong results");

    suggestions = autocomplete.get_suggestions("complete_includes.cpp", { 1, 18 });
    if (suggestions.size() != 1)
        FAIL(global include - bad size);

    if (suggestions[0].completion != "<sys/cdefs.h>")
        FAIL("global include - wrong results");

    PASS;
}

void test_parameters_hint()
{
    I_TEST(Function Parameters hint)
    FileDB filedb;
    filedb.set_project_root(TESTS_ROOT_DIR);
    add_file(filedb, "parameters_hint1.cpp");
    CppComprehensionEngine engine(filedb);

    auto result = engine.get_function_params_hint("parameters_hint1.cpp", { 4, 9 });
    if (!result.has_value())
        FAIL("failed to get parameters hint (1)");
    if (result->params != Vector<String> { "int x", "char y" } || result->current_index != 0)
        FAIL("bad result (1)");

    result = engine.get_function_params_hint("parameters_hint1.cpp", { 5, 15 });
    if (!result.has_value())
        FAIL("failed to get parameters hint (2)");
    if (result->params != Vector<String> { "int x", "char y" } || result->current_index != 1)
        FAIL("bad result (2)");

    result = engine.get_function_params_hint("parameters_hint1.cpp", { 6, 8 });
    if (!result.has_value())
        FAIL("failed to get parameters hint (3)");
    if (result->params != Vector<String> { "int x", "char y" } || result->current_index != 0)
        FAIL("bad result (3)");

    PASS;
}