Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error handling #18

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions src/assertions.h → src/assertion-macros.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
#include <stdbool.h>

#include "state.h"
#include "config.h"
#include "evaluators.h"
#include "formatters.h"
#include "fork.h"

#ifndef CAUGHT_ASSERTIONS
#define CAUGHT_ASSERTIONS

typedef struct caught_internal_assertion_result
{
const char *file;
const int line;
const char *expression;
char *lhs;
char *rhs;
enum caught_operator operator;
bool pass;
} caught_internal_assertion_result;

bool caught_internal_handle_assertion_result(caught_internal_assertion_result assertion_result);
#include "assertion-result.h"
#include "formatters.h"
#include "evaluators.h"
#include "state.h"

// This is used by every expect define to handle taking lhs, op, rhs, & send them into their handlers.
// These handlers then determine how to display (format) the passed data, and whether the assertion passed (comparators).
Expand Down
60 changes: 60 additions & 0 deletions src/assertion-result.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <stddef.h>
#include <stdio.h>

#include "output.h"
#include "state.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];
}

// Processes the assertion result by updating the internal state & outputting result of assertion if needed.
// Finally, returns true if assertion failed and test should exit
bool caught_internal_handle_assertion_result(caught_internal_assertion_result assertion_result)
{
caught_internal_state.assertions += 1;
if (assertion_result.pass)
{
caught_internal_state.passed_assertions += 1;
}

int show_regardless_of_pass = 0;

#ifdef EXPLICIT_ASSERTION_PASS
show_regardless_of_pass = 1;
#endif

if (!assertion_result.pass || show_regardless_of_pass)
{
caught_output_assertion_result(assertion_result);
}

free(assertion_result.lhs);
free(assertion_result.rhs);

return !assertion_result.pass;
}
52 changes: 52 additions & 0 deletions src/assertion-result.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <stdbool.h>

#ifndef CAUGHT_ASSERTION_RESULT
#define CAUGHT_ASSERTION_RESULT

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);

typedef struct caught_internal_assertion_result
{
const char *file;
const int line;
const char *expression;
char *lhs;
char *rhs;
enum caught_operator operator;
bool pass;
} caught_internal_assertion_result;

bool caught_internal_handle_assertion_result(caught_internal_assertion_result assertion_result);

#endif
32 changes: 0 additions & 32 deletions src/assertions.c

This file was deleted.

2 changes: 1 addition & 1 deletion src/caught.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define CAUGHT_LIB

#define _GNU_SOURCE // needed to make compiler happy, since this is the entrypoint
#include "assertions.h"
#include "assertion-macros.h"
#include "mocks.h"
#include "tests.h"

Expand Down
36 changes: 4 additions & 32 deletions src/evaluators.c
Original file line number Diff line number Diff line change
@@ -1,35 +1,8 @@
#include "evaluators.h"
#include "output.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 (==, <=, >=, ...)
Expand Down Expand Up @@ -85,8 +58,8 @@ bool caught_internal_evaluator_str(char *lhs, enum caught_operator operator, cha
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);
caught_output_errorf("Cannot compare strings with %s, only == and != are supported!", caught_operator_to_str(operator));
return false;
}
}
bool caught_internal_evaluator_str_ptr(char **lhs, enum caught_operator operator, char ** rhs)
Expand All @@ -99,8 +72,7 @@ bool caught_internal_evaluator_exit_status(caught_internal_process_status lhs, e
{
if (operator!= CAUGHT_OP_EQUAL)
{
fprintf(stderr, "Cannot compare exit statuses with %s, only == is supported!", caught_operator_to_str(operator));
exit(1);
caught_output_errorf("Cannot compare exit statuses with %s, only == is supported!", caught_operator_to_str(operator));
}

return lhs.type == rhs.type && lhs.status == rhs.status;
Expand Down
34 changes: 1 addition & 33 deletions src/evaluators.h
Original file line number Diff line number Diff line change
@@ -1,43 +1,11 @@
#include <stdbool.h>
#include <string.h>
#include "fork.h"
#include "assertion-result.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_ptr_ptr(void **lhs, enum caught_operator operator, void ** rhs);

Expand Down
8 changes: 4 additions & 4 deletions src/fork.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <sys/types.h>
#include <sys/wait.h>

#include "output.h"

typedef struct caught_internal_process_status
{
int type; // 0 for exit status, 1 for signal status
Expand All @@ -31,16 +33,14 @@ caught_internal_process_status create_caught_internal_process_status(int type, i
pid_t caught_internal_pid = fork(); \
if (caught_internal_pid == -1) \
{ \
perror("Caught: failed to fork\n"); \
exit(EXIT_FAILURE); \
caught_output_perrorf("Failed to fork\n"); \
} \
if (caught_internal_pid == 0) \
{ \
caught_internal_cleanup_state(); \
child_execute_block \
\
perror("Caught: fork segment must call exit to prevent fork bombs\n"); \
exit(EXIT_FAILURE); \
caught_output_perrorf("Fork segment must call exit to prevent fork bombs\n"); \
} \
else \
{ \
Expand Down
4 changes: 2 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "caught.h"
#include "output.h"
#include "config.h"
#include "state.h"

int main()
{
Expand All @@ -23,8 +24,7 @@ int main()

if (caught_internal_state.original_stdout != -1)
{
perror("Caught: must restore stdout after mocking it, did you forget to call RESTORE_STDOUT()?");
exit(EXIT_FAILURE);
caught_output_errorf("Must restore stdout after mocking it, did you forget to call RESTORE_STDOUT()?");
}

int this_assertions = caught_internal_state.assertions - prev_assertions;
Expand Down
Loading