Skip to content

S24.02: Adopt CALLED_FUNCTION test macro and standardise *CallCount naming #321

Closed

Description

@DavidCozens

Overview

Replace ad-hoc call-count assertions across the test base with a small set
of named macros, and standardise the <name>CallCount variable/getter
naming so the macros work uniformly. Pure test refactor; no production
code touched.

Why

  • The test base mixes several call-count assertion shapes —
    LONGS_EQUAL(N, fooCallCount), LONGS_EQUAL(N, FakeFoo_BarCount()),
    CHECK_TRUE(barCalled) — with inconsistent naming
    (SenderFake_SendCount, StoreFake_WriteCount, g_sleepCallCount,
    storeFullCallbackCount).
  • A single intent-named macro family makes test bodies read as English
    and centralises the naming contract via token paste, so future drift
    is caught at compile time rather than in review.
  • Partial implementations already exist:
    • The bespoke CHECK_HANDLER_INVOKED_ONCE /
      CHECK_HANDLER_NOT_INVOKED macros in Tests/SolidSyslogErrorTest.cpp.
    • A more substantial expression-form CALLED_FUNCTION(f, n) in
      Tests/TestUtils.h plus a CososoTesting::NEVER/ONCE/... namespace,
      used today only in Tests/SolidSyslogSwitchingSenderTest.cpp.
    • This story generalises both into one consistent token-paste design
      so the naming rule is enforced at compile time.

In scope

Extend Tests/TestUtils.h (move into Tests/Support/TestUtils.h so
non-Tests/-root test executables — Tests/Example, Tests/FreeRtos —
can include it via the established Support include path), keeping the
existing CososoTesting namespace + NEVER/ONCE/TWICE/THRICE enum.
Replace the single existing CALLED_FUNCTION(f, n) macro with three
token-paste macros that enforce the <name>CallCount naming rule:

#define CALLED_FUNCTION(name, count)            LONGS_EQUAL((count), name##CallCount)
#define CALLED_FAKE(getter, count)              LONGS_EQUAL((count), getter##CallCount())
#define CALLED_FAKE_ON(getter, instance, count) LONGS_EQUAL((count), getter##CallCount(instance))

Migrate the pre-existing CALLED_FUNCTION(SenderFake_…(inner), N) call
sites in Tests/SolidSyslogSwitchingSenderTest.cpp to CALLED_FAKE_ON.

Rename non-conforming counters/getters to the <name>CallCount rule:

  • SenderFake_SendCount → SenderFake_SendCallCount
  • SenderFake_DisconnectCount → SenderFake_DisconnectCallCount
  • StoreFake_WriteCount → StoreFake_WriteCallCount
  • g_sleepCallCount → NoOpSleepCallCount (SolidSyslogTlsStreamTest.cpp)
  • getPortCallCount / getHostCallCount → SpyGetPortCallCount /
    SpyGetHostCallCount (UDP and Stream sender tests)
  • sleepCallCount → SleepFakeCallCount
    (Tests/Example/ExampleServiceThreadTest.cpp)
  • storeFullCallbackCount → CountStoreFullInvocationsCallCount
  • thresholdCallbackCount → CountThresholdCrossingsCallCount

Convert boolean call-flags to int counters so they route through
CALLED_FUNCTION:

  • storeFullCallbackInvoked → StoreFullCallbackCallCount
  • computeIntegrityCalled → SpyComputeIntegrityCallCount
  • verifyIntegrityCalled → SpyVerifyIntegrityCallCount

Replace the bespoke macros in Tests/SolidSyslogErrorTest.cpp with the
generic form (CALLED_FUNCTION(handler, ONCE) /
CALLED_FUNCTION(handler, NEVER)).

Sweep every existing call-count assertion site (LONGS_EQUAL with a
CallCount operand; CHECK_TRUE/CHECK_FALSE on the converted bool flags)
to use the new macros across all test executables — main test exe,
ExampleTests, FreeRtos host tests, and the Winsock-build variants.

Out of scope

  • Production code (Tier 1/2) — no production changes at all.
  • lastCount in Tests/SolidSyslogBlockDeviceTest.cpp (byte count, not
    call count).
  • spy.callCount struct member in
    Tests/Example/ExampleInteractiveTest.cpp (different shape, low
    payoff).
  • Adding new tests or coverage — pure refactor of existing assertions.

Acceptance criteria

  • Tests/Support/TestUtils.h defines the three macros and is reachable
    from every test executable that asserts on call counts.
  • All *Count / call-flag variables and getters follow the
    <name>CallCount rule (only lastCount exception noted above
    remains).
  • All call-count assertions use CALLED_FUNCTION, CALLED_FAKE, or
    CALLED_FAKE_ON — no LONGS_EQUAL / CHECK_TRUE against a counter
    or call-flag remains.
  • Bespoke macros in SolidSyslogErrorTest.cpp removed.
  • All CI checks pass; coverage unchanged within noise.

Notes

  • Single PR, pure test refactor — large diff but mechanical.
  • Pre-PR check list (per CLAUDE.md): build-linux-gcc, build-linux-clang,
    sanitize-linux-gcc, coverage-linux-gcc, analyze-tidy, analyze-cppcheck,
    analyze-format.

Parent epic: #254

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

    storyStory issue

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions