Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ SpacesInParensOptions:

SpacesInSquareBrackets: false

WrapNamespaceBodyWithEmptyLines: Always
# Order
QualifierAlignment: Custom
QualifierOrder: ["constexpr", "const", "inline", "static", "type"]
Expand All @@ -101,3 +102,4 @@ SortUsingDeclarations: Never
IncludeBlocks: Merge

ForEachMacros: ["REFLECTABLE_RECORD"]
NamespaceMacros: [TEST_SUITE]
1 change: 1 addition & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ jobs:
shell: bash
run: |
if [[ "${{ runner.os }}" == "macOS" ]]; then
export SDKROOT=$(xcrun --show-sdk-path)
export PATH="/opt/homebrew/opt/llvm@20/bin:/opt/homebrew/opt/lld@20/bin:$PATH"
fi

Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,5 @@ if(CLICE_ENABLE_TEST)
"${PROJECT_SOURCE_DIR}/bin/unit_tests.cc"
)
target_include_directories(unit_tests PUBLIC "${PROJECT_SOURCE_DIR}")
target_link_libraries(unit_tests PRIVATE clice-core)
target_link_libraries(unit_tests PRIVATE clice-core cpptrace::cpptrace)
endif()
151 changes: 2 additions & 149 deletions bin/unit_tests.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Test/Test.h"
#include "Test/Test.h"
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
#include "Support/Logging.h"
#include "Support/GlobPattern.h"

Expand All @@ -9,11 +10,6 @@
using namespace clice;
using namespace clice::testing;

constexpr static std::string_view GREEN = "\033[32m";
constexpr static std::string_view YELLOW = "\033[33m";
constexpr static std::string_view RED = "\033[31m";
constexpr static std::string_view CLEAR = "\033[0m";

namespace {

namespace cl = llvm::cl;
Expand All @@ -40,163 +36,20 @@ cl::opt<bool> enable_example{
cl::cat(unittest_category),
};

std::optional<GlobPattern> pattern;

} // namespace

namespace clice::testing {

Runner& Runner::instance() {
static Runner runner;
return runner;
}

void Runner::add_suite(std::string_view name, Suite suite) {
suites[name].emplace_back(suite);
}

void Runner::on_test(std::string_view name, Test test, bool skipped) {
std::string full_name = std::format("{}.{}", curr_suite_name, name);

/// If this test if filter, directly return.
if(pattern && !pattern->match(full_name)) {
return;
}

/// If there is any test in the suite, we print the suite start info.
if(all_skipped) {
std::println("{}[----------] tests from {}{}", GREEN, curr_suite_name, CLEAR);
all_skipped = false;
}

if(skipped) {
/// If this test is marked as skipped, only print skip information.
std::println("{}[ SKIPPED ] {}{}", YELLOW, full_name, CLEAR);
return;
}

/// Reset whether this test is failed or fatal.
curr_failed = false;
curr_fatal = false;

using namespace std::chrono;

std::println("{}[ RUN ] {}.{}{}", GREEN, curr_suite_name, name, CLEAR);
auto begin = system_clock::now();

test();

auto duration = duration_cast<milliseconds>(system_clock::now() - begin);
std::println("{}[ {} ] {} ({} ms){}",
curr_failed ? RED : GREEN,
curr_failed ? "FAILED" : " OK",
full_name,
duration.count(),
CLEAR);

/// Update test information.
curr_tests_count += 1;
total_tests_count += 1;

curr_test_duration += duration;
total_test_duration += duration;

if(curr_failed) {
curr_failed_tests_count += 1;
total_failed_tests_count += 1;
}
}

void Runner::fail(const may_failure& failure) {
if(failure.failed) {
curr_failed = true;
std::println("{}Failure at {}:{}:{}! [{}]{}",
RED,
failure.location.file_name(),
failure.location.line(),
failure.location.column(),
failure.expression,
CLEAR);
std::println("{}", failure.message);
}

if(failure.fatal) {
curr_fatal = true;
std::println("{}--> Test stopped due to fatal error.{}", RED, CLEAR);
std::exit(1);
}
}

int Runner::run_tests() {
/// Register all tests.
std::println("{}[----------] Global test environment set-up.{}", GREEN, CLEAR);

for(auto& [suite_name, suite]: suites) {
if(!enable_example && suite_name == "TEST.Example") {
continue;
}

if(!test_filter.empty()) {
auto pos = test_filter.find_first_of('.');
if(pos != std::string::npos && test_filter.substr(0, pos) != suite_name) {
continue;
}
}

curr_fatal = false;
all_skipped = true;
curr_suite_name = suite_name;
curr_tests_count = 0;
curr_failed_tests_count = 0;
curr_test_duration = std::chrono::milliseconds();

for(auto& callback: suite) {
callback();
}

/// If there is any test in the suite, we print the suite info.
if(!all_skipped) {
total_suites_count += 1;
std::println("{}[----------] {} tests from {} ({} ms total)\n{}",
GREEN,
curr_tests_count,
suite_name,
total_test_duration.count(),
CLEAR);
}
}

std::println("{}[----------] Global test environment tear-down{}", GREEN, CLEAR);
std::println("{}[==========] {} tests from {} test suites ran. ({} ms total){}",
GREEN,
total_tests_count,
total_suites_count,
total_test_duration.count(),
CLEAR);

return total_failed_tests_count != 0;
}

} // namespace clice::testing

int main(int argc, const char* argv[]) {
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
llvm::cl::HideUnrelatedOptions(unittest_category);
llvm::cl::ParseCommandLineOptions(argc, argv, "clice test\n");

logging::stderr_logger("clice", logging::options);

if(!test_filter.empty()) {
if(auto result = GlobPattern::create(test_filter)) {
pattern.emplace(std::move(*result));
}
}

if(auto result = fs::init_resource_dir(argv[0]); !result) {
std::println("Failed to get resource directory, because {}", result.error());
return 1;
}

using namespace clice::testing;
return Runner::instance().run_tests();
return Runner2::instance().run_tests(test_filter);
}
10 changes: 9 additions & 1 deletion cmake/package.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ FetchContent_Declare(
GIT_TAG v4.4.2
)
set(ENABLE_ROARING_TESTS OFF CACHE INTERNAL "" FORCE)
set(ENABLE_ROARING_MICROBENCHMARKS OFF CACHE INTERNAL "" FORCE)

# flatbuffers
FetchContent_Declare(
Expand All @@ -128,7 +129,14 @@ set(FLATBUFFERS_BUILD_GRPC OFF CACHE BOOL "" FORCE)
set(FLATBUFFERS_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(FLATBUFFERS_BUILD_FLATHASH OFF CACHE BOOL "" FORCE)

FetchContent_MakeAvailable(libuv spdlog tomlplusplus croaring flatbuffers)
# cpptrace
FetchContent_Declare(
cpptrace
GIT_REPOSITORY https://github.com/jeremy-rifkin/cpptrace.git
GIT_TAG v1.0.4
)

FetchContent_MakeAvailable(libuv spdlog tomlplusplus croaring flatbuffers cpptrace)

if(WIN32)
target_compile_definitions(uv_a PRIVATE _CRT_SECURE_NO_WARNINGS)
Expand Down
1 change: 1 addition & 0 deletions docs/semantic-tokens-example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ struct Widget {

template <typename T>
Widget(T) -> Widget<typename T::value_type>;

} // namespace foo
2 changes: 2 additions & 0 deletions include/Compiler/Compilation.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
#include "Support/FileSystem.h"

namespace clang {

class CodeCompleteConsumer;

}

namespace clice {
Expand Down
2 changes: 2 additions & 0 deletions include/Compiler/Diagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include "clang/Basic/Diagnostic.h"

namespace clang {

class DiagnosticConsumer;

}

namespace clice {
Expand Down
2 changes: 2 additions & 0 deletions include/Compiler/Tidy.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include <memory>

namespace clang {

class CompilerInstance;

}

namespace clice::tidy {
Expand Down
2 changes: 2 additions & 0 deletions include/Index/IncludeGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include "llvm/ADT/DenseMap.h"

namespace clice {

class CompilationUnit;

}

namespace clice::index {
Expand Down
Loading