| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include <stdio.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <string.h> |
| 20 |
#include <errno.h> |
| 21 |
|
| 22 |
#include <rlutil/rlutil.h> |
| 23 |
|
| 24 |
#include "cbehave.h" |
| 25 |
|
| 26 |
|
| 27 |
cbehave_scope_e cbehave_scope; |
| 28 |
|
| 29 |
static cbehave_symbol_head_t _symbol_list; |
| 30 |
|
| 31 |
static cbehave_symbol_t* lookup_symbol(const char *symbol_name, int obj_type); |
| 32 |
static void add_value(cbehave_symbol_t *s, void *value, int count); |
| 33 |
static cbehave_value_t* get_value(cbehave_symbol_t *s); |
| 34 |
|
| 35 |
void should_int_equal(int actual, int expected, |
| 36 |
void *state, const char *file, |
| 37 |
int line) { |
| 38 |
int *_scenario_state = (int*)state; |
| 39 |
if ((expected) != (actual)) { |
| 40 |
(*_scenario_state) = 1; |
| 41 |
setColor(RED); |
| 42 |
printf("\t\t\t%s:%d: Failed: expected[%d], but actual[%d].\n", |
| 43 |
file, |
| 44 |
line, |
| 45 |
expected, |
| 46 |
actual); |
| 47 |
resetColor(); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
void should_int_gt(int val1, int val2, |
| 52 |
void *state, |
| 53 |
const char *file, int line) { |
| 54 |
int *_scenario_state = (int*)state; |
| 55 |
if ((val1) <= (val2)) { |
| 56 |
(*_scenario_state) = 1; |
| 57 |
setColor(RED); |
| 58 |
printf("\t\t\t%s:%d: Failed: [%d] not greater than [%d].\n", |
| 59 |
file, |
| 60 |
line, |
| 61 |
val1, |
| 62 |
val2); |
| 63 |
resetColor(); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
void should_int_lt(int val1, int val2, |
| 68 |
void *state, |
| 69 |
const char *file, int line) { |
| 70 |
int *_scenario_state = (int*)state; |
| 71 |
if ((val1) >= (val2)) { |
| 72 |
(*_scenario_state) = 1; |
| 73 |
setColor(RED); |
| 74 |
printf("\t\t\t%s:%d: Failed: [%d] not less than [%d].\n", |
| 75 |
file, |
| 76 |
line, |
| 77 |
val1, |
| 78 |
val2); |
| 79 |
resetColor(); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
void should_int_ge(int val1, int val2, |
| 84 |
void *state, |
| 85 |
const char *file, int line) { |
| 86 |
int *_scenario_state = (int*)state; |
| 87 |
if ((val1) < (val2)) { |
| 88 |
(*_scenario_state) = 1; |
| 89 |
setColor(RED); |
| 90 |
printf("\t\t\t%s:%d: Failed: [%d] not greater than or equal to [%d].\n", |
| 91 |
file, |
| 92 |
line, |
| 93 |
val1, |
| 94 |
val2); |
| 95 |
resetColor(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
void should_int_le(int val1, int val2, |
| 100 |
void *state, |
| 101 |
const char *file, int line) { |
| 102 |
int *_scenario_state = (int*)state; |
| 103 |
if ((val1) > (val2)) { |
| 104 |
(*_scenario_state) = 1; |
| 105 |
setColor(RED); |
| 106 |
printf("\t\t\t%s:%d: Failed: [%d] not less than or equal to [%d].\n", |
| 107 |
file, |
| 108 |
line, |
| 109 |
val1, |
| 110 |
val2); |
| 111 |
resetColor(); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
void should_str_equal(const char *actual, const char *expected, void *state, |
| 116 |
const char *file, int line) { |
| 117 |
|
| 118 |
int *_scenario_state = (int*)state; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
if (expected == actual) return; |
| 123 |
|
| 124 |
if (expected && actual) { |
| 125 |
if (!strcmp(expected, actual)) { |
| 126 |
return; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
(*_scenario_state) = 1; |
| 131 |
setColor(RED); |
| 132 |
printf("\t\t\t%s:%d: Failed: expected[%s], but actual[%s].\n", |
| 133 |
file, line, |
| 134 |
expected ? expected : "NULL", |
| 135 |
actual ? actual : "NULL"); |
| 136 |
resetColor(); |
| 137 |
} |
| 138 |
|
| 139 |
void should_mem_equal(const void *actual, const void *expected, size_t size, void *state, |
| 140 |
const char *file, int line) { |
| 141 |
|
| 142 |
int *_scenario_state = (int*)state; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
if (expected == actual) return; |
| 147 |
|
| 148 |
if (expected && actual) { |
| 149 |
if (!memcmp(expected, actual, size)) { |
| 150 |
return; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
(*_scenario_state) = 1; |
| 155 |
setColor(RED); |
| 156 |
printf("\t\t\t%s:%d: Failed: memory does not equal.\n", file, line); |
| 157 |
resetColor(); |
| 158 |
} |
| 159 |
|
| 160 |
void should_be_bool(bool actual, bool expected, void *state, const char *file, int line) { |
| 161 |
int *_scenario_state = (int*)state; |
| 162 |
if (actual != expected) { |
| 163 |
(*_scenario_state) = 1; |
| 164 |
setColor(RED); |
| 165 |
printf("\t\t\t%s:%d: Failed: actual[%d] is not a %s value.\n", |
| 166 |
file, |
| 167 |
line, |
| 168 |
actual, |
| 169 |
expected ? "true" : "false"); |
| 170 |
resetColor(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void cbehave_given_entry(const char *prompt, const char *str, void *state) { |
| 175 |
(void)(state); |
| 176 |
printf("\t\t%s %s\n", prompt, str); |
| 177 |
} |
| 178 |
|
| 179 |
void cbehave_when_entry(const char *prompt, const char *str, void *state) { |
| 180 |
(void)(state); |
| 181 |
printf("\t\t%s %s\n", prompt, str); |
| 182 |
} |
| 183 |
|
| 184 |
void cbehave_then_entry(const char *prompt, const char *str, void *state) { |
| 185 |
(void)(state); |
| 186 |
printf("\t\t%s %s\n", prompt, str); |
| 187 |
} |
| 188 |
|
| 189 |
void cbehave_scenario_entry(const char *str, void *state) { |
| 190 |
cbehave_state *cur = (cbehave_state*)state; |
| 191 |
cur->total_scenarios++; |
| 192 |
|
| 193 |
printf("\tScenario: %s\n", str); |
| 194 |
} |
| 195 |
|
| 196 |
void cbehave_feature_entry(const char *str, void *old_state, void *state) { |
| 197 |
cbehave_state *cur = (cbehave_state*)state; |
| 198 |
cbehave_state *old = (cbehave_state*)old_state; |
| 199 |
|
| 200 |
cur->total_features++; |
| 201 |
memcpy(old, state, sizeof(*cur)); |
| 202 |
|
| 203 |
printf("\nFeature: %s\n", str); |
| 204 |
} |
| 205 |
|
| 206 |
void cbehave_given_exit(void *state) { |
| 207 |
(void)(state); |
| 208 |
} |
| 209 |
|
| 210 |
void cbehave_when_exit(void *state) { |
| 211 |
(void)(state); |
| 212 |
} |
| 213 |
|
| 214 |
void cbehave_then_exit(void *state) { |
| 215 |
(void)(state); |
| 216 |
} |
| 217 |
|
| 218 |
void cbehave_scenario_exit(void *scenario_state, void *state) { |
| 219 |
int *_scenario_state = (int*)scenario_state; |
| 220 |
cbehave_state *cur = (cbehave_state*)state; |
| 221 |
|
| 222 |
if ((*_scenario_state) == 1) { |
| 223 |
cur->failed_scenarios++; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
void cbehave_feature_exit(void *old_state, void *state) { |
| 228 |
cbehave_state *cur = (cbehave_state*)state; |
| 229 |
cbehave_state *old = (cbehave_state*)old_state; |
| 230 |
|
| 231 |
if (cur->failed_scenarios > old->failed_scenarios) { |
| 232 |
cur->failed_features++; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
void cbehave_feature_return(const char *file, int line, int ret, void *state) { |
| 237 |
cbehave_state *cur = (cbehave_state*)state; |
| 238 |
|
| 239 |
cur->failed_scenarios++; |
| 240 |
|
| 241 |
setColor(RED); |
| 242 |
printf("\t\t\t%s:%d: Exception occurred, error code: %d.\n", |
| 243 |
file, |
| 244 |
line, |
| 245 |
ret); |
| 246 |
resetColor(); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
int _cbehave_runner(const char *description, const cbehave_feature *features, int count) { |
| 251 |
cbehave_state *state = NULL; |
| 252 |
int i; |
| 253 |
int ret; |
| 254 |
|
| 255 |
printf("%s\n", CBEHAVE_LOGO); |
| 256 |
printf("%s\n", description); |
| 257 |
|
| 258 |
state = (cbehave_state*)malloc(sizeof(*state)); |
| 259 |
if (!state) { |
| 260 |
setColor(RED); |
| 261 |
printf("\t%s:%d: Failed to alloc memory, error code: %d.\n", |
| 262 |
__FILE__, __LINE__, errno); |
| 263 |
resetColor(); |
| 264 |
return -1; |
| 265 |
} |
| 266 |
memset(state, 0, sizeof(*state)); |
| 267 |
|
| 268 |
APR_RING_INIT(&_symbol_list, cbehave_symbol_t, link); |
| 269 |
|
| 270 |
for (i = 0; i < count; i++) { |
| 271 |
features[i].func(state); |
| 272 |
} |
| 273 |
|
| 274 |
printf("\nSummary: \n"); |
| 275 |
if (state->failed_features) { |
| 276 |
setColor(RED); |
| 277 |
} else { |
| 278 |
setColor(GREEN); |
| 279 |
} |
| 280 |
printf("\tfeatures: [%d/%d]\n", |
| 281 |
state->total_features - state->failed_features, state->total_features); |
| 282 |
resetColor(); |
| 283 |
|
| 284 |
if (state->failed_scenarios) { |
| 285 |
setColor(RED); |
| 286 |
} else { |
| 287 |
setColor(GREEN); |
| 288 |
} |
| 289 |
printf("\tscenarios: [%d/%d]\n", state->total_scenarios - state->failed_scenarios, state->total_scenarios); |
| 290 |
resetColor(); |
| 291 |
|
| 292 |
ret = (state->failed_features == 0) ? 0 : 1; |
| 293 |
|
| 294 |
if (state) { |
| 295 |
free(state); |
| 296 |
} |
| 297 |
return ret; |
| 298 |
} |
| 299 |
|
| 300 |
void* cbehave_mock_obj(const char *fcname, |
| 301 |
int lineno, |
| 302 |
const char *fname, |
| 303 |
int obj_type) { |
| 304 |
cbehave_symbol_t *s = NULL; |
| 305 |
cbehave_value_t *v = NULL; |
| 306 |
void *p; |
| 307 |
|
| 308 |
s = lookup_symbol(fcname, obj_type); |
| 309 |
if (!s) { |
| 310 |
printf("\t[CBEHAVE]: can't find the symbol: <%s> which is being mocked!, %d line in file %s\n", |
| 311 |
fcname, lineno, fname); |
| 312 |
exit(EXIT_FAILURE); |
| 313 |
} |
| 314 |
|
| 315 |
if (s->always_return_flag) return s->value; |
| 316 |
|
| 317 |
v = get_value(s); |
| 318 |
if (!v) { |
| 319 |
printf("\t[CBEHAVE]: you have not set the value of mock obj <%s>!, %d line in file %s\n", |
| 320 |
fcname, lineno, fname); |
| 321 |
exit(EXIT_FAILURE); |
| 322 |
} |
| 323 |
|
| 324 |
p = v->value; |
| 325 |
|
| 326 |
APR_RING_REMOVE(v, link); |
| 327 |
free(v); |
| 328 |
|
| 329 |
return p; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
void cbehave_mock_obj_return(const char *symbol_name, |
| 334 |
void *value, |
| 335 |
const char *fcname, |
| 336 |
int lineno, |
| 337 |
const char *fname, |
| 338 |
int obj_type, |
| 339 |
int count) { |
| 340 |
|
| 341 |
cbehave_symbol_t *s = lookup_symbol(symbol_name, obj_type); |
| 342 |
(void)(fcname); |
| 343 |
(void)(lineno); |
| 344 |
(void)(fname); |
| 345 |
if (!s) { |
| 346 |
errno = 0; |
| 347 |
s = (cbehave_symbol_t*)malloc(sizeof(*s)); |
| 348 |
if (!s) { |
| 349 |
printf("\t[CBEHAVE]: malloc error!, errcode[%d]\n", errno); |
| 350 |
exit(EXIT_FAILURE); |
| 351 |
} |
| 352 |
memset(s, 0, sizeof(*s)); |
| 353 |
strcpy(s->desc, symbol_name); |
| 354 |
s->obj_type = obj_type; |
| 355 |
APR_RING_INIT(&(s->value_list), cbehave_value_t, link); |
| 356 |
APR_RING_INSERT_TAIL(&_symbol_list, s, cbehave_symbol_t, link); |
| 357 |
} |
| 358 |
|
| 359 |
add_value(s, value, count); |
| 360 |
} |
| 361 |
|
| 362 |
static cbehave_symbol_t* lookup_symbol(const char *symbol_name, int obj_type) { |
| 363 |
cbehave_symbol_t *s = NULL; |
| 364 |
|
| 365 |
APR_RING_FOREACH(s, &_symbol_list, cbehave_symbol_t, link) { |
| 366 |
if (s != NULL) { |
| 367 |
if ((s->obj_type == obj_type) |
| 368 |
&& (!strcmp(s->desc, symbol_name))) { |
| 369 |
return s; |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
return NULL; |
| 375 |
} |
| 376 |
|
| 377 |
static void add_value(cbehave_symbol_t *s, void *value, int count) { |
| 378 |
cbehave_value_t *v = NULL; |
| 379 |
int i; |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
if (count == -1) { |
| 386 |
s->always_return_flag = 1; |
| 387 |
s->value = value; |
| 388 |
return; |
| 389 |
} |
| 390 |
|
| 391 |
s->always_return_flag = 0; |
| 392 |
|
| 393 |
for (i = 0; i < count; i++) { |
| 394 |
errno = 0; |
| 395 |
v = (cbehave_value_t*)malloc(sizeof(*v)); |
| 396 |
if (!v) { |
| 397 |
printf("\t[CBEHAVE]: malloc error!, errcode[%d]\n", errno); |
| 398 |
exit(EXIT_FAILURE); |
| 399 |
} |
| 400 |
memset(v, 0, sizeof(*v)); |
| 401 |
v->value = value; |
| 402 |
|
| 403 |
APR_RING_INSERT_TAIL(&(s->value_list), v, cbehave_value_t, link); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
static cbehave_value_t* get_value(cbehave_symbol_t *s) { |
| 408 |
cbehave_value_t *v = NULL; |
| 409 |
|
| 410 |
v = APR_RING_FIRST(&(s->value_list)); |
| 411 |
return v; |
| 412 |
} |
| 413 |
|