Skip to content

Commit

Permalink
chore: switch to libtap for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
exbotanical committed Oct 3, 2024
1 parent d47e6c5 commit 09b0fe5
Show file tree
Hide file tree
Showing 10 changed files with 444 additions and 475 deletions.
2 changes: 1 addition & 1 deletion clib.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
"strdup": "*"
},
"development": {
"thlorenz/tap.c": "*"
"exbotanical/libtap": "*"
}
}
13 changes: 13 additions & 0 deletions deps/libtap/clib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "libtap",
"version": "0.0.5",
"author": "Matthew Zito",
"repo": "exbotanical/libtap",
"license": "MIT",
"description": "An implementation of TAP testing",
"keywords": ["test anything protocol", "testing", "unit test"],
"src": ["include/libtap.h", "src/tap.c"],
"development": {
"exbotanical/print-assert": "0.0.1"
}
}
138 changes: 138 additions & 0 deletions deps/libtap/libtap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#ifndef LIBTAP_H
#define LIBTAP_H

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/wait.h>
#include <unistd.h>

#ifdef __cplusplus
extern "C" {
#endif

// TODO: parser for gh actions, etc

static char*
__s_fmt__ (char* fmt, ...) {
va_list args, args_cp;
va_start(args, fmt);
va_copy(args_cp, args);

// Pass length of zero first to determine number of bytes needed
unsigned int n = vsnprintf(NULL, 0, fmt, args) + 1;
char* buf = (char*)malloc(n);
if (!buf) {
return NULL;
}

vsnprintf(buf, n, fmt, args_cp);

va_end(args);
va_end(args_cp);

return buf;
}

unsigned int __ok(
unsigned int ok,
const char* fn_name,
const char* file,
const unsigned int line,
char* msg
);

void __skip(unsigned int num_skips, char* msg);

int __write_shared_mem(int status);

void todo_start(const char* fmt, ...);

void todo_end(void);

void diag(const char* fmt, ...);

void plan(unsigned int num_tests);

unsigned int exit_status(void);

unsigned int bail_out(const char* fmt, ...);

#define ok(test, ...) \
__ok(test ? 1 : 0, __func__, __FILE__, __LINE__, __s_fmt__(__VA_ARGS__))

#define is(actual, expected, ...) \
__ok( \
!(actual == expected ? 0 \
: !actual ? -1 \
: !expected ? 1 \
: strcmp(actual, expected)), \
__func__, \
__FILE__, \
__LINE__, \
__s_fmt__(__VA_ARGS__) \
);

#define skip_start(cond, num_skips, ...) \
do { \
if (cond) { \
__skip(num_skips, __s_fmt__(__VA_ARGS__)); \
break; \
}

#define skip_end() \
} \
while (0)

#define skip(test, ...) __skip(1, __s_fmt__(__VA_ARGS__));

#define done_testing() return exit_status()

#define lives(...) _lives_or_dies(0, __VA_ARGS__)
#define dies(...) _lives_or_dies(1, __VA_ARGS__)

#define _lives_or_dies(wants_death, code, ...) \
do { \
/* set shared memory to 1 */ \
__write_shared_mem(1); \
\
int pid = fork(); \
switch (pid) { \
case -1: { \
perror("fork"); \
exit(EXIT_FAILURE); \
} \
case 0: { \
close(STDOUT_FILENO); \
close(STDERR_FILENO); \
/* execute test code, then set shared memory to zero */ \
code __write_shared_mem(0); \
exit(EXIT_SUCCESS); \
} \
} \
\
if (waitpid(pid, NULL, 0) < 0) { \
perror("waitpid"); \
exit(EXIT_FAILURE); \
} \
/* grab prev value (and reset) - if 0, code succeeded */ \
int test_died = __write_shared_mem(0); \
if (!test_died) { \
code \
} \
__ok( \
wants_death ? test_died : !test_died, \
__func__, \
__FILE__, \
__LINE__, \
__s_fmt__(__VA_ARGS__) \
); \
} while (0)

#ifdef __cplusplus
}
#endif

#endif /* LIBTAP_H */
Loading

0 comments on commit 09b0fe5

Please sign in to comment.