Hegel 0.11.4
Property-based testing for C++
Loading...
Searching...
No Matches
hegel.h
1#pragma once
2
329
330#include "core.h"
331#include "settings.h"
332#include "test_case.h"
333
334#include "generators/builds.h"
335#include "generators/collections.h"
336#include "generators/combinators.h"
337#include "generators/default.h"
338#include "generators/formats.h"
339#include "generators/numeric.h"
340#include "generators/primitives.h"
341#include "generators/random.h"
342#include "generators/stateful.h"
343#include "generators/strings.h"
344
345#include <functional>
346
347#if defined(__GNUC__) || defined(__clang__) || \
348 (defined(_MSC_VER) && _MSC_VER >= 1927)
349#define HEGEL_CALLER_FILE __builtin_FILE()
350#define HEGEL_CALLER_FUNCTION __builtin_FUNCTION()
351#define HEGEL_CALLER_LINE __builtin_LINE()
352#else
353#define HEGEL_CALLER_FILE ""
354#define HEGEL_CALLER_FUNCTION ""
355#define HEGEL_CALLER_LINE 0
356#endif
357
361namespace hegel {
362
389 class FailureOrigin {
390 public:
391 FailureOrigin() = default;
392 virtual ~FailureOrigin() = default;
394 FailureOrigin(const FailureOrigin&) = default;
397 FailureOrigin& operator=(const FailureOrigin&) = default;
398
403 virtual std::string failure_origin() const = 0;
404 };
405
417 std::string name;
419 std::string file;
421 int line = 0;
422 };
423
472 void test(const std::function<void(TestCase&)>& test_fn,
473 const Settings& settings = {},
474 const std::vector<std::string>& failure_blobs = {},
475 const char* caller_file = HEGEL_CALLER_FILE,
476 const char* caller_function = HEGEL_CALLER_FUNCTION,
477 int caller_line = HEGEL_CALLER_LINE);
478
499 void test(const std::function<void(TestCase&)>& test_fn,
500 const TestLocation& location, const Settings& settings = {},
501 const std::vector<std::string>& failure_blobs = {});
502
504 namespace internal {
505 // How a test framework passes information to Hegel
506 struct FrameworkHooks {
507 // Names the framework test that runs now, or "" outside one.
508 std::string (*current_test_name)() = nullptr;
509 // Runs one test-case body, raising the failed assertions the
510 // framework recorded.
511 void (*run_case)(const std::function<void()>&) = nullptr;
512 };
513
514 // Makes `hooks` the integration every later run uses. Returns true, so
515 // an integration header can install from a variable initializer and
516 // have it happen before main().
517 bool install_framework_hooks(const FrameworkHooks& hooks);
518
519 // add a blob for a test to run
520 bool register_blob(const char* name, std::vector<const char*> blobs);
521 // look up the failure blobs associated with a test
522 std::vector<std::string> reproduce_blobs_for(const char* name);
523
524 // What HEGEL_TEST expands to.
525 void test_from_macro(const std::function<void(TestCase&)>& test_fn,
526 const TestLocation& location,
527 const Settings& settings,
528 const std::vector<std::string>& failure_blobs);
529 } // namespace internal
531} // namespace hegel
532
538
575#define HEGEL_TEST(name, ...) \
576 static void hegel_test_body_##name(::hegel::TestCase&); \
577 static void name(::hegel::Settings settings = \
578 ::hegel::Settings(__VA_ARGS__)) { \
579 if (!settings.database_key.has_value()) { \
580 settings.database_key = __FILE__ "::" #name; \
581 } \
582 ::hegel::internal::test_from_macro( \
583 hegel_test_body_##name, \
584 ::hegel::TestLocation{#name, __FILE__, __LINE__}, settings, \
585 ::hegel::internal::reproduce_blobs_for(__FILE__ "::" #name)); \
586 } \
587 static void hegel_test_body_##name
588
613#define HEGEL_REPRODUCE_FAILURE(name, blob, ...) \
614 [[maybe_unused]] static const bool hegel_reproduce_registered_##name = \
615 ::hegel::internal::register_blob(__FILE__ "::" #name, \
616 {blob, __VA_ARGS__});
617
618
619// GoogleTest integration, picked up when <gtest/gtest.h> is already visible.
620// Include <hegel/gtest.h> yourself if this header comes first.
621#if defined(GTEST_TEST)
622#include "gtest.h"
623#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:40
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 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:198
Where a test is defined.
Definition hegel.h:415
int line
Line the test is defined on.
Definition hegel.h:421
std::string name
Test name, as it appears in the report header.
Definition hegel.h:417
std::string file
Source file the test is defined in.
Definition hegel.h:419