-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert syntax from EXPECT_EQUAL_INT(a, b) to EXPECT_INT(a, ==, b). This is much cleaner. Also, implements support for other equalities, and makes defining custom assertions much easier.
- Loading branch information
1 parent
06ab933
commit 6430355
Showing
13 changed files
with
330 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include "evaluators.h" | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
// General purpose converter from string (==) to operator enum (CAUGHT_OP_EQUAL) | ||
enum caught_operator | ||
caught_str_to_operator(char *str) | ||
{ | ||
int len = sizeof(CAUGHT_OPERATOR_STRS) / sizeof(CAUGHT_OPERATOR_STRS[0]); | ||
int i; | ||
for (i = 0; i < len; ++i) | ||
{ | ||
if (strcmp(str, CAUGHT_OPERATOR_STRS[i]) == 0) | ||
{ | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
// General purpose converter from enum (CAUGHT_OP_EQUAL) to operator string (==) | ||
const char *caught_operator_to_str(enum caught_operator operator) | ||
{ | ||
return CAUGHT_OPERATOR_STRS[operator]; | ||
} | ||
|
||
// General purpose converter from enum (CAUGHT_OP_EQUAL) to a to be statement (to be, to not be, etc.) | ||
const char *caught_operator_to_to_be_statement(enum caught_operator operator) | ||
{ | ||
return CAUGHT_OPERATOR_TO_BES[operator]; | ||
} | ||
|
||
// Evaluators take in a left hand size, operator, and right hand side | ||
// they then evaluate the result of that expression | ||
// CAUGHT_GENERATE_GENERIC_EVALUATOR just uses the default operators (==, <=, >=, ...) | ||
// but more advanced definitions are needed for things like strings | ||
|
||
bool caught_internal_evaluator_ptr(void *lhs, enum caught_operator operator, void * rhs) | ||
{ | ||
CAUGHT_GENERATE_GENERIC_EVALUATOR | ||
} | ||
bool caught_internal_evaluator_bool(bool lhs, enum caught_operator operator, bool rhs) | ||
{ | ||
CAUGHT_GENERATE_GENERIC_EVALUATOR | ||
} | ||
|
||
bool caught_internal_evaluator_int(int lhs, enum caught_operator operator, int rhs) | ||
{ | ||
CAUGHT_GENERATE_GENERIC_EVALUATOR | ||
} | ||
bool caught_internal_evaluator_char(char lhs, enum caught_operator operator, char rhs) | ||
{ | ||
CAUGHT_GENERATE_GENERIC_EVALUATOR | ||
} | ||
bool caught_internal_evaluator_str(char *lhs, enum caught_operator operator, char * rhs) | ||
{ | ||
bool null_exists = (lhs == NULL) || (rhs == NULL); | ||
switch (operator) | ||
{ | ||
case CAUGHT_OP_EQUAL: | ||
return (null_exists && lhs == rhs) || (!null_exists && strcmp(lhs, rhs) == 0); | ||
case CAUGHT_OP_NOT_EQUAL: | ||
return (null_exists && lhs != rhs) || (!null_exists && strcmp(lhs, rhs) != 0); | ||
default: | ||
fprintf(stderr, "Cannot compare strings with %s, only == and != are supported!", caught_operator_to_str(operator)); | ||
exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <stdbool.h> | ||
#include <string.h> | ||
|
||
#ifndef CAUGHT_EVALUATORS | ||
#define CAUGHT_EVALUATORS | ||
|
||
enum caught_operator | ||
{ | ||
CAUGHT_OP_EQUAL, | ||
CAUGHT_OP_NOT_EQUAL, | ||
CAUGHT_OP_LESS_THAN, | ||
CAUGHT_OP_GREATER_THAN, | ||
CAUGHT_OP_LESS_THAN_EQ, | ||
CAUGHT_OP_GREATER_THAN_EQ, | ||
}; | ||
|
||
static char *CAUGHT_OPERATOR_STRS[] = { | ||
"==", | ||
"!=", | ||
"<", | ||
">", | ||
"<=", | ||
">=", | ||
}; | ||
|
||
static char *CAUGHT_OPERATOR_TO_BES[] = { | ||
"to be", | ||
"to not be", | ||
"to be less than", | ||
"to be greater than", | ||
"to be <= to", | ||
"to be >= to", | ||
}; | ||
|
||
enum caught_operator | ||
caught_str_to_operator(char *str); | ||
const char *caught_operator_to_str(enum caught_operator operator); | ||
const char *caught_operator_to_to_be_statement(enum caught_operator operator); | ||
|
||
bool caught_internal_evaluator_ptr(void *lhs, enum caught_operator operator, void * rhs); | ||
bool caught_internal_evaluator_bool(bool lhs, enum caught_operator operator, bool rhs); | ||
bool caught_internal_evaluator_int(int lhs, enum caught_operator operator, int rhs); | ||
bool caught_internal_evaluator_char(char lhs, enum caught_operator operator, char rhs); | ||
bool caught_internal_evaluator_str(char *lhs, enum caught_operator operator, char * rhs); | ||
|
||
// Uses default operators (==, <=, >=, ...) to compare lhs to rhs | ||
#define CAUGHT_GENERATE_GENERIC_EVALUATOR \ | ||
switch (operator) \ | ||
{ \ | ||
case CAUGHT_OP_EQUAL: \ | ||
return lhs == rhs; \ | ||
case CAUGHT_OP_NOT_EQUAL: \ | ||
return lhs != rhs; \ | ||
case CAUGHT_OP_LESS_THAN: \ | ||
return lhs < rhs; \ | ||
case CAUGHT_OP_GREATER_THAN: \ | ||
return lhs > rhs; \ | ||
case CAUGHT_OP_LESS_THAN_EQ: \ | ||
return lhs <= rhs; \ | ||
case CAUGHT_OP_GREATER_THAN_EQ: \ | ||
return lhs >= rhs; \ | ||
default: \ | ||
return false; \ | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "formatters.h" | ||
|
||
#define _GNU_SOURCE | ||
#include <stdio.h> | ||
|
||
char *caught_internal_formatter_ptr(void *value) | ||
{ | ||
char *result; | ||
asprintf(&result, "%p", value); | ||
return result; | ||
} | ||
|
||
char *caught_internal_formatter_bool(bool value) | ||
{ | ||
return (value) ? strdup("true") : strdup("false"); | ||
} | ||
|
||
char *caught_internal_formatter_int(int value) | ||
{ | ||
char *result; | ||
asprintf(&result, "%i", value); | ||
return result; | ||
} | ||
|
||
char *caught_internal_formatter_char(char value) | ||
{ | ||
char *result; | ||
asprintf(&result, "%c", value); | ||
return result; | ||
} | ||
|
||
char *caught_internal_formatter_str(char *value) | ||
{ | ||
if (value == NULL) | ||
{ | ||
return NULL; | ||
} | ||
char *result; | ||
asprintf(&result, "\"%s\"", value); | ||
return result; | ||
} |
Oops, something went wrong.