aboutsummaryrefslogtreecommitdiff
path: root/test/common/Event2Test.cpp
blob: 0c67f28e9fcc8c5737254897a457e13b595e706a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "cru/base/Event2.h"

#include <catch2/catch_test_macros.hpp>

using cru::Event2;

TEST_CASE("Event2", "[event2]") {
  Event2 event;

  int counter = 0;

  auto handler = [&counter] { counter++; };
  auto token = event.AddHandler(handler);

  auto handler2 = [&counter](decltype(event)::Context* context) { counter++; };

  SECTION("two handlers should work.") {
    event.Raise();
    REQUIRE(counter == 1);
    event.Raise();
    REQUIRE(counter == 2);

    event.AddHandler(handler2);
    event.Raise();
    REQUIRE(counter == 4);
  }

  SECTION("handler revoker should work.") {
    token.RevokeHandler();
    event.Raise();
    REQUIRE(counter == 0);

    token = event.AddHandler(handler);
    event.AddHandler(handler2);
    event.Raise();
    REQUIRE(counter == 2);

    token.RevokeHandler();
    event.Raise();
    REQUIRE(counter == 3);
  }

  SECTION("stop handling should work.") {
    auto short_circuit_handler = [&counter](decltype(event)::Context* context) {
      context->SetStopHandling();
    };

    event.AddHandler(short_circuit_handler);
    event.AddHandler(handler2);

    event.Raise();
    REQUIRE(counter == 1);
  }
}