Updating for 1.0.2 release
[awesomized/libmemcached] / libtest / framework.h
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23
24 #pragma once
25
26 /**
27 Framework is the structure which is passed to the test implementation to be filled.
28 This must be implemented in order for the test framework to load the tests. We call
29 get_world() in order to fill this structure.
30 */
31
32 class Framework {
33 public:
34 collection_st *collections;
35
36 /* These methods are called outside of any collection call. */
37 test_callback_create_fn *_create;
38 test_callback_destroy_fn *_destroy;
39
40 /* This is called a the beginning of any collection run. */
41 test_callback_fn *collection_startup;
42
43 /* This is called a the end of any collection run. */
44 test_callback_fn *collection_shutdown;
45
46 void set_collection_shutdown(test_callback_error_fn *arg)
47 {
48 _on_error= arg;
49 }
50
51 public:
52 void* create(test_return_t& arg);
53
54 test_return_t startup(void*);
55
56 test_return_t shutdown(void* arg)
57 {
58 if (collection_shutdown)
59 {
60 return collection_shutdown(arg);
61 }
62
63 return TEST_SUCCESS;
64 }
65
66 /**
67 These are run before/after the test. If implemented. Their execution is not controlled
68 by the test.
69 */
70 class Item {
71 public:
72 /* This is called a the beginning of any run. */
73 test_callback_fn *_startup;
74
75 test_return_t startup(void*);
76
77 /*
78 This called on a test if the test requires a flush call (the bool is
79 from test_st)
80 */
81 test_callback_fn *_flush;
82
83 private:
84 /*
85 Run before and after the runnner is executed.
86 */
87 test_callback_fn *pre_run;
88 test_callback_fn *post_run;
89
90 public:
91
92 Item() :
93 _startup(NULL),
94 _flush(NULL),
95 pre_run(NULL),
96 post_run(NULL)
97 { }
98
99 void set_startup(test_callback_fn *arg)
100 {
101 _startup= arg;
102 }
103
104 void set_collection(test_callback_fn *arg)
105 {
106 _flush= arg;
107 }
108
109 void set_flush(test_callback_fn *arg)
110 {
111 _flush= arg;
112 }
113
114 void set_pre(test_callback_fn *arg)
115 {
116 pre_run= arg;
117 }
118
119 void set_post(test_callback_fn *arg)
120 {
121 pre_run= arg;
122 }
123
124 test_return_t pre(void *arg);
125 test_return_t flush(void* arg, test_st* run);
126 test_return_t post(void *arg);
127
128 } item;
129
130 /**
131 If an error occurs during the test, this is called.
132 */
133 test_callback_error_fn *_on_error;
134
135 void set_on_error(test_callback_error_fn *arg)
136 {
137 _on_error= arg;
138 }
139
140 test_return_t on_error(const enum test_return_t, void *);
141
142 void set_socket()
143 {
144 _servers.set_socket();
145 }
146
147 void set_sasl(const std::string& username_arg, const std::string& password_arg)
148 {
149 _servers.set_sasl(username_arg, password_arg);
150 }
151
152 libtest::server_startup_st& servers()
153 {
154 return _servers;
155 }
156
157 /**
158 Runner represents the callers for the tests. If not implemented we will use
159 a set of default implementations.
160 */
161 libtest::Runner *_runner;
162
163 void set_runner(libtest::Runner *arg)
164 {
165 _runner= arg;
166 }
167
168 libtest::Runner *runner();
169
170
171 Framework();
172
173 virtual ~Framework();
174
175 Framework(const Framework&);
176
177 private:
178 Framework& operator=(const Framework&);
179 libtest::server_startup_st _servers;
180 bool _socket;
181 void *_creators_ptr;
182 };