forked from cory/tildefriends
Add some test infrastructure.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3658 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
110
src/tests.c
110
src/tests.c
@ -1,20 +1,22 @@
|
||||
#include "tests.h"
|
||||
|
||||
#include "ssb.h"
|
||||
#include "ssb.tests.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static void _test_nop(const char* exe_path)
|
||||
static void _test_nop(const tf_test_options_t* options)
|
||||
{
|
||||
FILE* file = fopen("out/test.js", "w");
|
||||
fprintf(file, "print('hi');");
|
||||
fclose(file);
|
||||
|
||||
char command[256];
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path);
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", options->exe_path);
|
||||
printf("%s\n", command);
|
||||
int result = system(command);
|
||||
(void)result;
|
||||
@ -22,7 +24,7 @@ static void _test_nop(const char* exe_path)
|
||||
assert(WEXITSTATUS(result) == 0);
|
||||
}
|
||||
|
||||
static void _test_child(const char* exe_path)
|
||||
static void _test_child(const tf_test_options_t* options)
|
||||
{
|
||||
FILE* file = fopen("out/test.js", "w");
|
||||
fprintf(file,
|
||||
@ -43,7 +45,7 @@ static void _test_child(const char* exe_path)
|
||||
fclose(file);
|
||||
|
||||
char command[256];
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path);
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", options->exe_path);
|
||||
printf("%s\n", command);
|
||||
int result = system(command);
|
||||
(void)result;
|
||||
@ -54,7 +56,7 @@ static void _test_child(const char* exe_path)
|
||||
unlink("out/child.js");
|
||||
}
|
||||
|
||||
static void _test_promise(const char* exe_path)
|
||||
static void _test_promise(const tf_test_options_t* options)
|
||||
{
|
||||
FILE* file = fopen("out/test.js", "w");
|
||||
fprintf(file,
|
||||
@ -83,7 +85,7 @@ static void _test_promise(const char* exe_path)
|
||||
fclose(file);
|
||||
|
||||
char command[256];
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path);
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", options->exe_path);
|
||||
printf("%s\n", command);
|
||||
int result = system(command);
|
||||
(void)result;
|
||||
@ -94,7 +96,7 @@ static void _test_promise(const char* exe_path)
|
||||
unlink("out/child.js");
|
||||
}
|
||||
|
||||
static void _test_promise_remote_throw(const char* exe_path)
|
||||
static void _test_promise_remote_throw(const tf_test_options_t* options)
|
||||
{
|
||||
FILE* file = fopen("out/test.js", "w");
|
||||
fprintf(file,
|
||||
@ -127,7 +129,7 @@ static void _test_promise_remote_throw(const char* exe_path)
|
||||
fclose(file);
|
||||
|
||||
char command[256];
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path);
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", options->exe_path);
|
||||
printf("%s\n", command);
|
||||
int result = system(command);
|
||||
(void)result;
|
||||
@ -138,7 +140,7 @@ static void _test_promise_remote_throw(const char* exe_path)
|
||||
unlink("out/child.js");
|
||||
}
|
||||
|
||||
static void _test_promise_remote_reject(const char* exe_path)
|
||||
static void _test_promise_remote_reject(const tf_test_options_t* options)
|
||||
{
|
||||
FILE* file = fopen("out/test.js", "w");
|
||||
fprintf(file,
|
||||
@ -173,7 +175,7 @@ static void _test_promise_remote_reject(const char* exe_path)
|
||||
fclose(file);
|
||||
|
||||
char command[256];
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path);
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", options->exe_path);
|
||||
printf("%s\n", command);
|
||||
int result = system(command);
|
||||
(void)result;
|
||||
@ -184,7 +186,7 @@ static void _test_promise_remote_reject(const char* exe_path)
|
||||
unlink("out/child.js");
|
||||
}
|
||||
|
||||
static void _test_database(const char* exe_path)
|
||||
static void _test_database(const tf_test_options_t* options)
|
||||
{
|
||||
FILE* file = fopen("out/test.js", "w");
|
||||
fprintf(file,
|
||||
@ -219,7 +221,7 @@ static void _test_database(const char* exe_path)
|
||||
unlink("out/testdb.sqlite");
|
||||
|
||||
char command[256];
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=out/testdb.sqlite -s out/test.js", exe_path);
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=out/testdb.sqlite -s out/test.js", options->exe_path);
|
||||
printf("%s\n", command);
|
||||
int result = system(command);
|
||||
printf("returned %d\n", WEXITSTATUS(result));
|
||||
@ -230,7 +232,7 @@ static void _test_database(const char* exe_path)
|
||||
unlink("out/testdb.sqlite");
|
||||
}
|
||||
|
||||
static void _test_this(const char* exe_path)
|
||||
static void _test_this(const tf_test_options_t* options)
|
||||
{
|
||||
FILE* file = fopen("out/test.js", "w");
|
||||
fprintf(file,
|
||||
@ -240,7 +242,7 @@ static void _test_this(const char* exe_path)
|
||||
fclose(file);
|
||||
|
||||
char command[256];
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path);
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", options->exe_path);
|
||||
printf("%s\n", command);
|
||||
int result = system(command);
|
||||
printf("returned %d\n", WEXITSTATUS(result));
|
||||
@ -250,7 +252,7 @@ static void _test_this(const char* exe_path)
|
||||
unlink("out/test.js");
|
||||
}
|
||||
|
||||
static void _test_await(const char* exe_path)
|
||||
static void _test_await(const tf_test_options_t* options)
|
||||
{
|
||||
FILE* file = fopen("out/test.js", "w");
|
||||
fprintf(file,
|
||||
@ -273,7 +275,7 @@ static void _test_await(const char* exe_path)
|
||||
fclose(file);
|
||||
|
||||
char command[256];
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path);
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", options->exe_path);
|
||||
printf("%s\n", command);
|
||||
int result = system(command);
|
||||
printf("returned %d\n", WEXITSTATUS(result));
|
||||
@ -283,7 +285,7 @@ static void _test_await(const char* exe_path)
|
||||
unlink("out/test.js");
|
||||
}
|
||||
|
||||
static void _test_require(const char* exe_path)
|
||||
static void _test_require(const tf_test_options_t* options)
|
||||
{
|
||||
FILE* file = fopen("out/test.js", "w");
|
||||
fprintf(file,
|
||||
@ -312,7 +314,7 @@ static void _test_require(const char* exe_path)
|
||||
fclose(file);
|
||||
|
||||
char command[256];
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path);
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", options->exe_path);
|
||||
printf("%s\n", command);
|
||||
int result = system(command);
|
||||
printf("returned %d\n", WEXITSTATUS(result));
|
||||
@ -323,7 +325,7 @@ static void _test_require(const char* exe_path)
|
||||
unlink("out/required.js");
|
||||
}
|
||||
|
||||
static void _test_exit(const char* exe_path)
|
||||
static void _test_exit(const tf_test_options_t* options)
|
||||
{
|
||||
FILE* file = fopen("out/test.js", "w");
|
||||
fprintf(file, "require('blah');");
|
||||
@ -334,7 +336,7 @@ static void _test_exit(const char* exe_path)
|
||||
fclose(file);
|
||||
|
||||
char command[256];
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path);
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", options->exe_path);
|
||||
printf("%s\n", command);
|
||||
int result = system(command);
|
||||
printf("returned %d\n", WEXITSTATUS(result));
|
||||
@ -345,7 +347,7 @@ static void _test_exit(const char* exe_path)
|
||||
unlink("out/blah.js");
|
||||
}
|
||||
|
||||
static void _test_icu(const char* exe_path)
|
||||
static void _test_icu(const tf_test_options_t* options)
|
||||
{
|
||||
FILE* file = fopen("out/test.js", "w");
|
||||
fprintf(file,
|
||||
@ -354,7 +356,7 @@ static void _test_icu(const char* exe_path)
|
||||
fclose(file);
|
||||
|
||||
char command[256];
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path);
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", options->exe_path);
|
||||
printf("%s\n", command);
|
||||
int result = system(command);
|
||||
printf("returned %d\n", WEXITSTATUS(result));
|
||||
@ -364,7 +366,7 @@ static void _test_icu(const char* exe_path)
|
||||
unlink("out/test.js");
|
||||
}
|
||||
|
||||
static void _test_uint8array(const char* exe_path)
|
||||
static void _test_uint8array(const tf_test_options_t* options)
|
||||
{
|
||||
FILE* file = fopen("out/test.js", "w");
|
||||
fprintf(file,
|
||||
@ -404,7 +406,7 @@ static void _test_uint8array(const char* exe_path)
|
||||
fclose(file);
|
||||
|
||||
char command[256];
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path);
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", options->exe_path);
|
||||
printf("%s\n", command);
|
||||
int result = system(command);
|
||||
printf("returned %d\n", WEXITSTATUS(result));
|
||||
@ -415,7 +417,7 @@ static void _test_uint8array(const char* exe_path)
|
||||
unlink("out/child.js");
|
||||
}
|
||||
|
||||
static void _test_socket(const char* exe_path)
|
||||
static void _test_socket(const tf_test_options_t* options)
|
||||
{
|
||||
FILE* file = fopen("out/test.js", "w");
|
||||
fprintf(file,
|
||||
@ -468,7 +470,7 @@ static void _test_socket(const char* exe_path)
|
||||
fclose(file);
|
||||
|
||||
char command[256];
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path);
|
||||
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", options->exe_path);
|
||||
printf("%s\n", command);
|
||||
int result = system(command);
|
||||
printf("returned %d\n", WEXITSTATUS(result));
|
||||
@ -478,21 +480,45 @@ static void _test_socket(const char* exe_path)
|
||||
unlink("out/test.js");
|
||||
}
|
||||
|
||||
void tf_tests(const char* exe_path)
|
||||
static void _tf_test_run(const tf_test_options_t* options, const char* name, void (*test)(const tf_test_options_t* options))
|
||||
{
|
||||
tf_ssb_test();
|
||||
_test_nop(exe_path);
|
||||
_test_child(exe_path);
|
||||
_test_promise(exe_path);
|
||||
_test_promise_remote_throw(exe_path);
|
||||
_test_promise_remote_reject(exe_path);
|
||||
_test_database(exe_path);
|
||||
_test_this(exe_path);
|
||||
_test_await(exe_path);
|
||||
_test_require(exe_path);
|
||||
_test_exit(exe_path);
|
||||
_test_icu(exe_path);
|
||||
_test_uint8array(exe_path);
|
||||
_test_socket(exe_path);
|
||||
bool specified = false;
|
||||
if (options->tests) {
|
||||
char* dup = strdup(options->tests);
|
||||
char* state = NULL;
|
||||
const char* t = NULL;
|
||||
while ((t = strtok_r(t ? NULL : dup, ",", &state)) != NULL) {
|
||||
if (strcmp(t, name) == 0) {
|
||||
specified = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(dup);
|
||||
}
|
||||
|
||||
if (!options->tests || specified) {
|
||||
printf("Running test %s.", name);
|
||||
test(options);
|
||||
}
|
||||
}
|
||||
|
||||
void tf_tests(const tf_test_options_t* options)
|
||||
{
|
||||
_tf_test_run(options, "ssb", tf_ssb_test_ssb);
|
||||
_tf_test_run(options, "ssb_id", tf_ssb_test_id_conversion);
|
||||
_tf_test_run(options, "ssb_following", tf_ssb_test_following);
|
||||
_tf_test_run(options, "nop", _test_nop);
|
||||
_tf_test_run(options, "child", _test_child);
|
||||
_tf_test_run(options, "promise", _test_promise);
|
||||
_tf_test_run(options, "promise_remote_throw", _test_promise_remote_throw);
|
||||
_tf_test_run(options, "promise_remote_reject", _test_promise_remote_reject);
|
||||
_tf_test_run(options, "database", _test_database);
|
||||
_tf_test_run(options, "this", _test_this);
|
||||
_tf_test_run(options, "await", _test_await);
|
||||
_tf_test_run(options, "require", _test_require);
|
||||
_tf_test_run(options, "exit", _test_exit);
|
||||
_tf_test_run(options, "icu", _test_icu);
|
||||
_tf_test_run(options, "uint8array", _test_uint8array);
|
||||
_tf_test_run(options, "socket", _test_socket);
|
||||
printf("Tests completed.\n");
|
||||
}
|
||||
|
Reference in New Issue
Block a user