Merge trunk for LP
[m6w6/libmemcached] / libtest / framework.h
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * uTest, libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37
38 #pragma once
39
40 /**
41 Framework is the structure which is passed to the test implementation to be filled.
42 This must be implemented in order for the test framework to load the tests. We call
43 get_world() in order to fill this structure.
44 */
45
46 struct Framework {
47 collection_st *collections;
48
49 /* These methods are called outside of any collection call. */
50 test_callback_create_fn *_create;
51 test_callback_fn *_destroy;
52
53 void* create(test_return_t& arg);
54
55 /* This is called a the beginning of any collection run. */
56 test_callback_fn *collection_startup;
57
58 test_return_t startup(void*);
59
60 /* This is called a the end of any collection run. */
61 test_callback_fn *collection_shutdown;
62
63 test_return_t shutdown(void* arg)
64 {
65 if (collection_shutdown)
66 {
67 return collection_shutdown(arg);
68 }
69
70 return TEST_SUCCESS;
71 }
72
73 /**
74 These are run before/after the test. If implemented. Their execution is not controlled
75 by the test.
76 */
77 struct Item {
78 /* This is called a the beginning of any run. */
79 test_callback_fn *_startup;
80
81 test_return_t startup(void*);
82
83 /*
84 This called on a test if the test requires a flush call (the bool is
85 from test_st)
86 */
87 test_callback_fn *_flush;
88
89 /*
90 Run before and after the runnner is executed.
91 */
92 test_callback_fn *pre_run;
93 test_callback_fn *post_run;
94
95 Item() :
96 _startup(NULL),
97 _flush(NULL),
98 pre_run(NULL),
99 post_run(NULL)
100 { }
101
102 test_return_t flush(void* arg, test_st* run);
103
104 void set_pre(test_callback_fn *arg)
105 {
106 pre_run= arg;
107 }
108
109 void set_post(test_callback_fn *arg)
110 {
111 pre_run= arg;
112 }
113
114 test_return_t pre(void *arg)
115 {
116 if (pre_run)
117 {
118 return pre_run(arg);
119 }
120
121 return TEST_SUCCESS;
122 }
123
124 test_return_t post(void *arg)
125 {
126 if (post_run)
127 {
128 return post_run(arg);
129 }
130
131 return TEST_SUCCESS;
132 }
133
134 } item;
135
136 /**
137 If an error occurs during the test, this is called.
138 */
139 test_callback_error_fn *_on_error;
140
141 test_return_t on_error(const enum test_return_t, void *);
142
143 /**
144 Runner represents the callers for the tests. If not implemented we will use
145 a set of default implementations.
146 */
147 Runner *runner;
148
149 Framework();
150
151 virtual ~Framework();
152
153 Framework(const Framework&);
154
155 private:
156 Framework& operator=(const Framework&);
157 void *_creators_ptr;
158 };