summaryrefslogtreecommitdiff
path: root/vendor/bandit/bandit/grammar.h
blob: 6aaec23dee84839cf0ebb49f3113987ca6b0d89c (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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#ifndef BANDIT_GRAMMAR_H
#define BANDIT_GRAMMAR_H

namespace bandit {

  inline void describe(const char* desc, detail::voidfunc_t func,
      detail::listener& listener, detail::contextstack_t& context_stack,
      bool hard_skip = false)
  {
    listener.context_starting(desc);

    context_stack.back()->execution_is_starting();

    detail::bandit_context ctxt(desc, hard_skip);

    context_stack.push_back(&ctxt);
    try
    {
      func();
    }
    catch(const bandit::detail::test_run_error& error)
    {
      listener.test_run_error(desc, error);
    }

    context_stack.pop_back();

    listener.context_ended(desc);
  }

  inline void describe(const char* desc, detail::voidfunc_t func)
  {
    describe(desc, func, detail::registered_listener(), detail::context_stack());
  }

  inline void describe_skip(const char* desc, detail::voidfunc_t func,
      detail::listener& listener, detail::contextstack_t& context_stack)
  {
    bool skip = true;
    describe(desc, func, listener, context_stack, skip);
  }

  inline void describe_skip(const char* desc, detail::voidfunc_t func)
  {
    describe_skip(desc, func, detail::registered_listener(), 
        detail::context_stack());
  }

  inline void before_each(detail::voidfunc_t func, 
      detail::contextstack_t& context_stack)
  {
    context_stack.back()->register_before_each(func);
  }

  inline void before_each(detail::voidfunc_t func)
  {
    before_each(func, detail::context_stack());
  }

  inline void after_each(detail::voidfunc_t func, 
      detail::contextstack_t& context_stack)
  {
    context_stack.back()->register_after_each(func);
  }

  inline void after_each(detail::voidfunc_t func)
  {
    after_each(func, detail::context_stack());
  }

  inline void it_skip(const char* desc, detail::voidfunc_t, detail::listener& listener)
  {
    listener.it_skip(desc);
  }
  
  inline void it_skip(const char* desc, detail::voidfunc_t func)
  {
    it_skip(desc, func, detail::registered_listener());
  }

  inline void it(const char* desc, detail::voidfunc_t func, detail::listener& listener,
      detail::contextstack_t& context_stack, 
      bandit::adapters::assertion_adapter& assertion_adapter, 
      const detail::run_policy& run_policy)
  {
    if(!run_policy.should_run(desc, context_stack))
    {
      it_skip(desc, func, listener);
      return;
    }

    listener.it_starting(desc);

    context_stack.back()->execution_is_starting();

    auto run_before_eaches = [&](){
      for_each(context_stack.begin(), context_stack.end(), [](detail::context* ctxt){
          ctxt->run_before_eaches();
      });
    };

    auto run_after_eaches = [&](){
      for_each(context_stack.begin(), context_stack.end(), [](detail::context* ctxt){
          ctxt->run_after_eaches();
      });
    };

    try
    {
      assertion_adapter.adapt_exceptions([&](){
          run_before_eaches();

          func();
          listener.it_succeeded(desc);
      });
    }
    catch(const bandit::detail::assertion_exception& ex)
    {
      listener.it_failed(desc, ex);
    }
    catch(...)
    {
      listener.it_unknown_error(desc);
    }

    try
    {
      run_after_eaches();
    }
    catch(...)
    {
      listener.it_unknown_error(desc);
    }
  }

  inline void it(const char* desc, detail::voidfunc_t func)
  {
    it(desc, func, detail::registered_listener(), detail::context_stack(), 
        detail::registered_adapter(), detail::registered_run_policy());
  }


}

#endif