#include #include #include #include #include namespace { using namespace vls::features::outline; using Void::Testing::expect; using Void::Testing::require; std::vector names(const std::vector &symbols) { std::vector result; for (const auto &symbol : symbols) result.push_back(symbol.name); return result; } } // namespace VOID_TEST(Outline, recognizes_namespaces_classes_and_enums) { const std::string text = "namespace demo {\n" "struct Point {\n" " int x;\n" "};\n" "enum class Color { red };\n" "class Widget;\n" "}\n"; const auto symbols = outline(text); expect(names(symbols) == std::vector{"demo", "Point", "Color", "Widget"}); expect(symbols[0].kind == Kind::namespace_); expect(symbols[1].kind == Kind::class_); expect(symbols[2].kind == Kind::enum_); expect(symbols[3].kind == Kind::class_); } VOID_TEST(Outline, distinguishes_functions_from_call_statements) { const std::string text = "int compute(int value) { return value; }\n" "void run() {\n" " compute(1);\n" " if (compute(2)) {}\n" " auto x = compute(3);\n" "}\n"; const auto symbols = outline(text); expect(names(symbols) == std::vector{"compute", "run"}); expect(symbols[0].kind == Kind::function); } VOID_TEST(Outline, skips_preprocessor_and_comments) { const std::string text = "#define fake(x) x\n" "// int commented();\n" "/* void blocked();\n" " int still_blocked(); */\n" "int real();\n"; expect(names(outline(text)) == std::vector{"real"}); } VOID_TEST(Outline, ranges_cover_the_line_and_the_name) { const std::string text = " int value(int a) { return a; }\n"; const auto symbols = outline(text); require(symbols.size() == 1); expect(symbols[0].range.start == Position{0, 0}); expect(symbols[0].range.end.character == text.size() - 1); expect(symbols[0].selection_range.start == Position{0, 6}); expect(symbols[0].selection_range.end == Position{0, 11}); } VOID_TEST(Outline, is_bounded) { std::string text; for (int line = 0; line != 20; ++line) text += "int f" + std::to_string(line) + "();\n"; expect(outline(text).size() == 20); expect(outline(text, Limits{.symbols = 3}).size() == 3); expect(outline(" int " + std::string(300, 'x') + "();\n", Limits{.nameBytes = 8}).empty()); expect(outline(text, Limits{.scanBytes = 10}).size() == 1); }