Echo Writes Code

suite.cpp

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "crucible/test/suite.hpp"

#include "crucible/core/iterators.hpp"

namespace crucible::test
{
  // -----------------------------------------------------------------------------------------------
  // Suite -----------------------------------------------------------------------------------------
  // -----------------------------------------------------------------------------------------------

  struct Suite_Data {};

  Suite::Suite(std::unique_ptr<Suite_Data> data) :
    m_data { std::move(data) }
  {}

  auto Suite::add_before_all(Step const step) -> void
  {
    m_before_all_list.push(step);
  }

  auto Suite::add_after_all(Step const step) -> void
  {
    m_after_all_list.push(step);
  }

  auto Suite::add_before_each(Step const step) -> void
  {
    m_before_each_list.push(step);
  }

  auto Suite::add_after_each(Step const step) -> void
  {
    m_after_each_list.push(step);
  }

  auto Suite::add_scenario(Step const step) -> void
  {
    m_scenario_list.push(step);
  }

  auto Suite::run() const -> void
  {
    // Run the Before_All fixtures
    core::iterate(m_before_all_list)
    .examine([](Step const before_all) { before_all(); })
    .consume();

    core::iterate(m_scenario_list)
    .examine([this](Step const scenario) {
      // Run the Before_Each fixtures
      core::iterate(m_before_each_list)
      .examine([](Step const before_each) { before_each(); })
      .consume();

      // Run the actual test scenario
      scenario();

      // Run the After_Each fixtures
      core::iterate(m_after_each_list)
      .examine([](Step const after_each) { after_each(); })
      .consume();
    })
    .consume();

    // Run the After_All fixtures
    core::iterate(m_after_all_list)
    .examine([](Step const after_all) { after_all(); })
    .consume();
  }

  auto suite() -> Suite &
  {
    static Suite instance { std::make_unique<Suite_Data>() };
    return instance;
  }

  auto run_suite(core::Immutable_View<core::String_View> const /* arguments */) -> core::Exit_Status
  {
    suite().run();
    return core::Exit_Status::Success;
  }

  // -----------------------------------------------------------------------------------------------
  // Registrars ------------------------------------------------------------------------------------
  // -----------------------------------------------------------------------------------------------

  Before_All_Registrar::Before_All_Registrar(Step const step)
  {
    suite().add_before_all(step);
  }

  After_All_Registrar::After_All_Registrar(Step const step)
  {
    suite().add_after_all(step);
  }

  Before_Each_Registrar::Before_Each_Registrar(Step const step)
  {
    suite().add_before_each(step);
  }

  After_Each_Registrar::After_Each_Registrar(Step const step)
  {
    suite().add_after_each(step);
  }

  Scenario_Registrar::Scenario_Registrar(Step const step)
  {
    suite().add_scenario(step);
  }
}