Hegel 0.18.3
Property-based testing for C++
Loading...
Searching...
No Matches
hegel.h
1#pragma once
2
386
387#include "core.h"
388#include "errors.h"
389#include "explicit.h"
390#include "settings.h"
391#include "test_case.h"
392
393#include "generators/builds.h"
394#include "generators/collections.h"
395#include "generators/combinators.h"
396#include "generators/default.h"
397#include "generators/formats.h"
398#include "generators/numeric.h"
399#include "generators/primitives.h"
400#include "generators/random.h"
401#include "generators/stateful.h"
402#include "generators/strings.h"
403#include "generators/temporal.h"
404
405#include <exception>
406#include <functional>
407
408#if defined(__GNUC__) || defined(__clang__) || \
409 (defined(_MSC_VER) && _MSC_VER >= 1927)
410#define HEGEL_CALLER_FILE __builtin_FILE()
411#define HEGEL_CALLER_FUNCTION __builtin_FUNCTION()
412#define HEGEL_CALLER_LINE __builtin_LINE()
413#else
414#define HEGEL_CALLER_FILE ""
415#define HEGEL_CALLER_FUNCTION ""
416#define HEGEL_CALLER_LINE 0
417#endif
418
422namespace hegel {
423
450 class FailureOrigin {
451 public:
452 FailureOrigin() = default;
453 virtual ~FailureOrigin() = default;
455 FailureOrigin(const FailureOrigin&) = default;
458 FailureOrigin& operator=(const FailureOrigin&) = default;
459
464 virtual std::string failure_origin() const = 0;
465 };
466
478 std::string name;
480 std::string file;
482 int line = 0;
483 };
484
544 void test(const std::function<void(TestCase&)>& test_fn,
545 const Settings& settings = {},
546 const std::vector<std::string>& failure_blobs = {},
547 const std::vector<ExplicitTestCase>& explicit_cases = {},
548 const char* caller_file = HEGEL_CALLER_FILE,
549 const char* caller_function = HEGEL_CALLER_FUNCTION,
550 int caller_line = HEGEL_CALLER_LINE);
551
576 void test(const std::function<void(TestCase&)>& test_fn,
577 const TestLocation& location, const Settings& settings = {},
578 const std::vector<std::string>& failure_blobs = {},
579 const std::vector<ExplicitTestCase>& explicit_cases = {});
580
582 namespace internal {
583 // How a test framework passes information to Hegel
584 struct FrameworkHooks {
585 // Names the framework test that runs now, or "" outside one.
586 std::string (*current_test_name)() = nullptr;
587 // Runs one test-case body, raising the failed assertions the
588 // framework recorded.
589 void (*run_case)(const std::function<void()>&) = nullptr;
590 // Whether an exception is the framework's report of failed
591 // assertions, whose message names where each one failed.
592 bool (*is_assertion_failure)(const std::exception&) = nullptr;
593 };
594
595 // Makes `hooks` the integration every later run uses. Returns true, so
596 // an integration header can install from a variable initializer and
597 // have it happen before main().
598 bool install_framework_hooks(const FrameworkHooks& hooks);
599
600 // add a blob for a test to run
601 bool register_blob(const char* name, std::vector<const char*> blobs);
602 // look up the failure blobs associated with a test
603 std::vector<std::string> reproduce_blobs_for(const char* name);
604
605 // add an explicit test case for a test
606 bool register_explicit_case(const char* name,
607 ExplicitTestCase test_case);
608 // look up the explicit test cases of a test, in registration order
609 std::vector<ExplicitTestCase> explicit_cases_for(const char* name);
610
611 // What HEGEL_TEST expands to.
612 void
613 test_from_macro(const std::function<void(TestCase&)>& test_fn,
614 const TestLocation& location, const Settings& settings,
615 const std::vector<std::string>& failure_blobs,
616 const std::vector<ExplicitTestCase>& explicit_cases);
617
618 // What the main() that HEGEL_MAIN defines calls. It creates the
619 // Settings once the command line's --profile is in force. Returns
620 // the exit status.
621 int
622 main_from_macro(int argc, char** argv,
623 const std::function<void(TestCase&)>& test_fn,
624 const TestLocation& location,
625 const std::function<Settings()>& make_settings,
626 const std::vector<std::string>& failure_blobs,
627 const std::vector<ExplicitTestCase>& explicit_cases);
628 } // namespace internal
630} // namespace hegel
631
637
674#define HEGEL_TEST(name, ...) \
675 static void hegel_test_body_##name(::hegel::TestCase&); \
676 static void name(const ::hegel::Settings& settings = \
677 ::hegel::Settings(__VA_ARGS__)) { \
678 ::hegel::internal::test_from_macro( \
679 hegel_test_body_##name, \
680 ::hegel::TestLocation{#name, __FILE__, __LINE__}, settings, \
681 ::hegel::internal::reproduce_blobs_for(__FILE__ "::" #name), \
682 ::hegel::internal::explicit_cases_for(__FILE__ "::" #name)); \
683 } \
684 static void hegel_test_body_##name
685
722#define HEGEL_MAIN(name, ...) \
723 static void hegel_test_body_##name(::hegel::TestCase&); \
724 int main(int argc, char** argv) { \
725 return ::hegel::internal::main_from_macro( \
726 argc, argv, hegel_test_body_##name, \
727 ::hegel::TestLocation{#name, __FILE__, __LINE__}, \
728 [] { return ::hegel::Settings(__VA_ARGS__); }, \
729 ::hegel::internal::reproduce_blobs_for(__FILE__ "::" #name), \
730 ::hegel::internal::explicit_cases_for(__FILE__ "::" #name)); \
731 } \
732 static void hegel_test_body_##name
733
760#define HEGEL_REPRODUCE_FAILURE(name, blob, ...) \
761 [[maybe_unused]] static const bool hegel_reproduce_registered_##name = \
762 ::hegel::internal::register_blob(__FILE__ "::" #name, \
763 {blob, __VA_ARGS__});
764
766#define HEGEL_CONCAT_IMPL(a, b) a##b
767#define HEGEL_CONCAT(a, b) HEGEL_CONCAT_IMPL(a, b)
769
791#define HEGEL_EXPLICIT_TEST_CASE(name, ...) \
792 [[maybe_unused]] static const bool HEGEL_CONCAT( \
793 hegel_explicit_registered_##name##_, __LINE__) = \
794 ::hegel::internal::register_explicit_case( \
795 __FILE__ "::" #name, ::hegel::ExplicitTestCase(__VA_ARGS__));
796
797
798// GoogleTest integration, picked up when <gtest/gtest.h> is already visible.
799// Include <hegel/gtest.h> yourself if this header comes first.
800#if defined(GTEST_TEST)
801#include "gtest.h"
802#endif
FailureOrigin(const FailureOrigin &)=default
Copy-constructs. An origin holds no state of its own.
virtual std::string failure_origin() const =0
The origin grouping this failure.
FailureOrigin & operator=(const FailureOrigin &)=default
Handle to the currently-executing test case.
Definition test_case.h:46
Main namespace.
Definition config.h:29
void test(const std::function< void(TestCase &)> &test_fn, const Settings &settings={}, const std::vector< std::string > &failure_blobs={}, const std::vector< ExplicitTestCase > &explicit_cases={}, const char *caller_file=HEGEL_CALLER_FILE, const char *caller_function=HEGEL_CALLER_FUNCTION, int caller_line=HEGEL_CALLER_LINE)
Run a Hegel test.
Configuration options for hegel::test().
Definition settings.h:180
Where a test is defined.
Definition hegel.h:476
int line
Line the test is defined on.
Definition hegel.h:482
std::string name
Test name.
Definition hegel.h:478
std::string file
Source file the test is defined in.
Definition hegel.h:480