Skip to content

Truly independent web browser

6.7 KB
Raw Blame
/*
 * Copyright (c) 2018-2020, Andreas Kling <[email protected]>
 * Copyright (c) 2021, Andrew Kaster <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/ByteString.h>
#include <AK/Function.h>
#include <AK/NonnullRefPtr.h>
#include <AK/RefCounted.h>
#include <LibTest/Export.h>
#include <LibTest/Macros.h>
#include <LibTest/Randomized/RandomnessSource.h>
#include <LibTest/Randomized/Shrink.h>

namespace Test {

using TestFunction = Function<void()>;

inline void run_with_randomness_source(Randomized::RandomnessSource source, TestFunction const& test_function)
{
    set_randomness_source(move(source));
    set_current_test_result(TestResult::NotRun);
    test_function();
    if (current_test_result() == TestResult::NotRun) {
        set_current_test_result(TestResult::Passed);
    }
}

class TEST_API TestCase : public RefCounted<TestCase> {
public:
    TestCase(ByteString name, ByteString source, TestFunction&& function)
        : m_name(move(name))
        , m_source(move(source))
        , m_function(move(function))
    {
    }

    ByteString const& name() const { return m_name; }
    ByteString const& source() const { return m_source; }
    TestFunction const& func() const { return m_function; }

    static NonnullRefPtr<TestCase> randomized(ByteString name, ByteString source, TestFunction&& test_function)
    {
        using namespace Randomized;

        constexpr u8 MAX_GEN_ATTEMPTS_PER_VALUE = 30;

        TestFunction test_case_function = [test_function = move(test_function)]() {
            u64 max_randomized_runs = randomized_runs();
            for (u64 i = 0; i < max_randomized_runs; ++i) {
                bool generated_successfully = false;
                u8 gen_attempt;
                for (gen_attempt = 0; gen_attempt < MAX_GEN_ATTEMPTS_PER_VALUE && !generated_successfully; ++gen_attempt) {
                    // We're going to run the test function many times, so let's turn off the reporting until we finish.
                    disable_reporting();

                    set_current_test_result(TestResult::NotRun);
                    run_with_randomness_source(RandomnessSource::live(), test_function);
                    switch (current_test_result()) {
                    case TestResult::NotRun:
                        VERIFY_NOT_REACHED();
                        break;
                    case TestResult::Passed: {
                        generated_successfully = true;
                        break;
                    }
                    case TestResult::Failed: {
                        generated_successfully = true;
                        RandomRun first_failure = randomness_source().run();
                        RandomRun best_failure = shrink(first_failure, test_function);

                        // Run one last time with reporting on, so that the user can see the minimal failure
                        enable_reporting();
                        run_with_randomness_source(RandomnessSource::recorded(best_failure), test_function);
                        return;
                    }
                    case TestResult::Rejected:
                        break;
                    case TestResult::Overrun:
                        break;
                    default:
                        VERIFY_NOT_REACHED();
                        break;
                    }
                }
                enable_reporting();
                if (!generated_successfully) {
                    // The loop above got to the full MAX_GEN_ATTEMPTS_PER_VALUE and gave up.
                    // Run one last time with reporting on, so that the user gets the REJECTED message.
                    RandomRun last_failure = randomness_source().run();
                    run_with_randomness_source(RandomnessSource::recorded(last_failure), test_function);
                    return;
                }
            }
            // All randomized_runs() values generated + passed the test.
        };
        return make_ref_counted<TestCase>(move(name), move(source), move(test_case_function));
    }

private:
    ByteString m_name;
    ByteString m_source;
    TestFunction m_function;
};

// Helper to hide implementation of TestSuite from users
TEST_API void add_test_case_to_suite(NonnullRefPtr<TestCase> const& test_case);
TEST_API void set_suite_setup_function(ByteString const& source, Function<void()> setup);

}

#define TEST_SETUP                                                 \
    static void __setup();                                         \
    [[maybe_unused]] static auto const __setup_registration = [] { \
        ::Test::set_suite_setup_function(__FILE__, __setup);       \
        return 0;                                                  \
    }();                                                           \
    static void __setup()

// Registrations have internal linkage so different source files may use the same case names.
#define TEST_CASE(x)                                                                                \
    static void __test_##x();                                                                       \
    [[maybe_unused]] static auto const __test_registration_##x = [] {                               \
        ::Test::add_test_case_to_suite(adopt_ref(*new ::Test::TestCase(#x, __FILE__, __test_##x))); \
        return 0;                                                                                   \
    }();                                                                                            \
    static void __test_##x()

#define RANDOMIZED_TEST_CASE(x)                                                                            \
    static void __randomized_test_##x();                                                                   \
    [[maybe_unused]] static auto const __randomized_registration_##x = [] {                                \
        ::Test::add_test_case_to_suite(::Test::TestCase::randomized(#x, __FILE__, __randomized_test_##x)); \
        return 0;                                                                                          \
    }();                                                                                                   \
    static void __randomized_test_##x()

// This allows us to print the generated locals in the test after a failure is fully shrunk.
#define GEN(identifier, value)                                        \
    auto identifier = (value);                                        \
    if (::Test::current_test_result() == ::Test::TestResult::Overrun) \
        return;                                                       \
    if (::Test::is_reporting_enabled())                               \
    ::AK::warnln("{} = {}", #identifier, (identifier))