Skip to content

Hegel and GTest stateful testing, invariants are run after rule assertion failure #138

Closed

Description

@pedrolamarao

Consider the following program.
I am building it with clang 22 and gtest 1.18.
I am running it in CentOS Stream 10 in a Proxmox VE machine.
This program runs the invariant after a rule assertion failure.
In this case, the assertion is verifying structural soudness -- it fails because the structure is unsound.
Is this behaviour intended?

#include <hegel/gtest.h>

namespace
{
    struct foo_state {};

    struct foo_machine : hegel::stateful::StateMachine<foo_machine,foo_state>
    {
        foo_machine* self = this;

        size_t state = 42;

        foo_machine () :
            hegel::stateful::StateMachine<foo_machine,foo_state>({
                .initial_state = {}
            })
        {}

        auto rules () -> std::vector<hegel::stateful::Rule<foo_machine>> {
            using Rule = hegel::stateful::Rule<foo_machine>;
            return {
                Rule("copy",[] (auto& t, auto& m) {
                    // oops, copy is broken
                    m.self = nullptr;
                    ASSERT_TRUE( testing::AssertionFailure() << "copy is broken" );
                })
            };
        }

        auto invariants () -> std::vector<hegel::stateful::Invariant<foo_machine>> {
            using Invariant = hegel::stateful::Invariant<foo_machine>;
            return {
                Invariant("state",[] (auto& m) {
                    ASSERT_EQ( m.self->state, 42 );
                })
            };
        }
    };
}

TEST(sandbox,foo) {
    hegel::test([] (auto& t) {
        foo_machine machine {};
        hegel::stateful::run(machine, t);
    });
}

Activity

  1. echoumcp1 commented on Sep 25, 2026

    @echoumcp1
    Member

    Hi Pedro, could you post the printed output?

  2. added
    question
    on Sep 25, 2026
  3. pedrolamarao commented on Sep 25, 2026

    @pedrolamarao
    Author

    This test program terminates with a SIGSEGV raise by dereferencing nullptr in line 34.

    This is a debug build stack trace.

    testing::internal::CmpHelperEQ<…> gtest.h:1399
    (anonymous namespace)::foo_machine::invariants()::{lambda(auto:1&)#1}::operator()<(anonymous namespace)::foo_machine const>((anonymous namespace)::foo_machine const&) const sandbox.cxx:34
    hegel::stateful::check_invariants<…> stateful.h:817
    hegel::stateful::run<…> stateful.h:1208
    $_0::operator()<…>(hegel::TestCase &) const sandbox.cxx:44
    $_0::operator()() const hegel.cpp:295
    hegel::internal::gtest_hooks::run_case gtest.h:128
    hegel::run_case hegel.cpp:170
    hegel::run_body hegel.cpp:295
    hegel::run_from_engine hegel.cpp:644
    hegel::run_test hegel.cpp:816
    hegel::test hegel.cpp:839
    sandbox_foo_Test::TestBody sandbox.cxx:42
    

    My understanding is that, currently, the stateful machinery does consume GTest failures but does not consume fatal failures as such and continues execution of further rules and invariants.

    In practice, I am now causing Hegel to properly stop by raising an std::runtime_error. Unfortunately, my handwritten assertions have poor reporting on the values involved, compared to GTest.

  4. echoumcp1 commented on Sep 25, 2026

    @echoumcp1
    Member

    Yep, this is a bug. Thanks!

  5. added
    bug
    and removed
    question
    on Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions