summaryrefslogtreecommitdiff
path: root/vendor/bandit/bandit/context.h
blob: 711942531b3810a3e2f8961db5bb2290c0a5dc1d (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
#ifndef BANDIT_CONTEXT_H
#define BANDIT_CONTEXT_H

namespace bandit {
  namespace detail {

    class context
    {
      public:
        virtual ~context() {}
        virtual const std::string& name() = 0;
        virtual void execution_is_starting() = 0;
        virtual void register_before_each(voidfunc_t func) = 0;
        virtual void register_after_each(voidfunc_t func) = 0;
        virtual void run_before_eaches() = 0;
        virtual void run_after_eaches() = 0;
        virtual bool hard_skip() = 0;
    };

    class bandit_context : public context
    {
      public:
        bandit_context(const char* desc, bool hard_skip_a)
          : desc_(desc), hard_skip_(hard_skip_a), is_executing_(false)
        {}

        const std::string& name() 
        {
          return desc_;
        }
        
        void execution_is_starting()
        {
          is_executing_ = true;
        }

        void register_before_each(voidfunc_t func)
        {
          if(is_executing_)
          {
            throw test_run_error("before_each was called after 'describe' or 'it'");
          }

          before_eaches_.push_back(func);
        }

        void register_after_each(voidfunc_t func)
        {
          if(is_executing_)
          {
            throw test_run_error("after_each was called after 'describe' or 'it'");
          }

          after_eaches_.push_back(func);
        }

        void run_before_eaches()
        {
          run_all(before_eaches_);
        }

        void run_after_eaches()
        {
          run_all(after_eaches_);
        }

        bool hard_skip() 
        {
          return hard_skip_;
        }

      private:
        void run_all(const std::list<voidfunc_t>& funcs)
        {
          auto call_func = [](voidfunc_t f){ f(); };

          for_each(funcs.begin(), funcs.end(), call_func);
        }

      private:
        std::string desc_;
        bool hard_skip_;
        bool is_executing_;
        std::list<voidfunc_t> before_eaches_;
        std::list<voidfunc_t> after_eaches_;
    };
    typedef std::deque<context*> contextstack_t;

    inline contextstack_t& context_stack()
    {
      static contextstack_t contexts;
      return contexts;
    }
  }
}

#endif